Spark结构化的流Writestream到Hive ORC分区的外部表

时间:2018-08-11 22:29:49

标签: apache-spark hive spark-structured-streaming orc hive-partitions

我正在尝试使用Spark结构化流-writeStream API写入外部分区的Hive表。

CREATE EXTERNAL TABLE `XX`(
`a` string,
`b` string,
`b` string,
`happened` timestamp,
`processed` timestamp,
`d` string,
`e` string,
`f` string )
 PARTITIONED BY (
`year` int, `month` int, `day` int)      
 CLUSTERED BY (d)
INTO 6 BUCKETS
STORED AS ORC 
TBLPROPERTIES (
'orc.compress'='ZLIB',
'orc.compression.strategy'='SPEED',
'orc.create.index'='true',
'orc.encoding.strategy'='SPEED');

和Spark代码中

val hiveOrcWriter:   DataStreamWriter[Row] = event_stream
  .writeStream
  .outputMode("append")
  .format("orc")
  .partitionBy("year","month","day")
  //.option("compression", "zlib")
  .option("path", _table_loc)
  .option("checkpointLocation", _table_checkpoint)

我看到在非分区表上,记录被插入到Hive中。但是,在使用分区表时,spark作业不会失败或引发异常,但是记录不会插入到Hive表中。

处理过类似问题的任何人都应给予赞赏。

修改

仅发现.orc文件确实已以正确的分区目录结构写入HDFS: /_table_loc/_table_name/year/month/day/part-0000-0123123.c000.snappy.orc

但是

select * from 'XX' limit 1; (or where year=2018)

不返回任何行。

表'XX'的InputFormatOutputFormatorg.apache.hadoop.hive.ql.io.orc.OrcInputFormat和 分别org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat

1 个答案:

答案 0 :(得分:1)

结构化流媒体中未提供此功能。在正常处理中,您将使用dataset.write.saveAsTable(table_name),但该方法不可用。

在HDFS中处理并保存数据后,您可以手动更新分区(或使用按计划执行此操作的脚本):

如果您使用Hive

MSCK REPAIR TABLE table_name

如果使用Impala

ALTER TABLE table_name RECOVER PARTITIONS
相关问题