【问题标题】:Spark SQL insertInto() failing for partition keySpark SQL insertInto() 分区键失败
【发布时间】:2016-03-24 03:03:53
【问题描述】:

我正在尝试从 S3 加载数据并对其进行转换,然后将其插入带有分区的配置单元表中。

首先我从 creation_date (bigint) 作为分区键开始,它运行良好,但是现在当我尝试使用 creation_month 分区键插入相同的数据时,它失败了。

这里是代码

var hiveCtx = new org.apache.spark.sql.hive.HiveContext(sc)
var df = hiveCtx.read.json("s3n://spark-feedstore/2016/1/*")
import org.apache.spark.storage.StorageLevel
import org.apache.spark.sql.SaveMode
hiveCtx.sql("SET hive.exec.dynamic.partition = true")
hiveCtx.sql("SET hive.exec.dynamic.partition.mode = nonstrict")

df.persist(StorageLevel.MEMORY_AND_DISK)
df.registerTempTable("posts")

第一个表的架构

[external_id,string,]
[tags,array<string>,]
[creation_date,bigint,]
[video_url,string,]
# Partition Information      
creation_date bigint

第二张表的架构

[external_id,string,]
[tags,array<string>,]
[creation_date,bigint,]
[video_url,string,]
[creation_month,date,]
# Partition Information      
creation_month bigint

使用插入到第一个表就可以了。

var udf = hiveCtx .sql("select externalId as external_id, first(sourceMap['tags']) as tags, first(sourceMap['creation_date']) as creation_date, 
first(sourceMap['video_url']) as video_url
from posts group by externalId")

udf.write.mode(SaveMode.Append).partitionBy("creation_date").insertInto("posts_1")

但是插入第二个表会出错。

var udf = hiveCtx .sql("select externalId as external_id, first(sourceMap['brand_hashtags']) as brand_hashtags, first(sourceMap['creation_date']) as creation_date, 

first(sourceMap['video_url']) as video_url, trunc(from_unixtime(first(sourceMap['creation_date']) / 1000), 'MONTH') as creation_month 从帖子组按 externalId")

 udf.write.mode(SaveMode.Append).partitionBy("creation_month").insertInto("posts_2")

错误:

org.apache.spark.sql.AnalysisException: cannot resolve 'cast(creation_date as array<string>)' due to data type mismatch: cannot cast LongType to ArrayType(StringType,true); 

我不确定当我们添加另一个字段 creation_month 时会发生什么变化。这两个表的架构的每个方面似乎都完全相同。

【问题讨论】:

    标签: apache-spark apache-spark-sql spark-dataframe


    【解决方案1】:

    我遇到了问题。 这是按列的顺序排列的。

    字段顺序是

    external_id, tag, video_url, creation_date
    

    但在选择查询中我有它

    external_id, creation_date, tag, video_url
    

    因此 Hive 试图将 creation_date 转换为数组

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-08
      • 1970-01-01
      • 2016-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-19
      • 2020-10-11
      相关资源
      最近更新 更多