【发布时间】: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