gpt4 book ai didi

hive - 不支持 SparkSQL 表创建 CLUSTER BY

转载 作者:行者123 更新时间:2023-12-03 17:38:38 26 4
gpt4 key购买 nike

根据 Spark 文档 https://spark.apache.org/docs/2.1.0/sql-programming-guide.html#supported-hive-features支持 hive 语句 CLUSTER BY。但是当我尝试使用 beeline 中的以下查询创建表时

CREATE TABLE set_bucketing_test (key INT, value STRING) CLUSTERED BY (key) INTO 10 BUCKETS;

我收到以下错误
Error: org.apache.spark.sql.catalyst.parser.ParseException:
Operation not allowed: CREATE TABLE ... CLUSTERED BY(line 1, pos 0)

不知道我在做什么错误。有什么帮助吗?

最佳答案

您可以在 spark-sql 中按功能利用集群进行表创建、表连接等,它充当 hive 以避免在 spark2.1+ 中进行数据交换和排序

https://issues.apache.org/jira/browse/SPARK-15453

目前 hive 无法识别此功能,因为元数据在 spark 和 hive 之间不兼容,这就是为什么即使在 hive 端识别此表也不能使用相同的语法,它将所有列视为 array
以下示例可能会给您一些想法:

准备源
val df = (0 until 80000).map(i => (i, i.toString, i.toString)).toDF("item_id", "country", "state").coalesce(1)
从源创建两个桶表

您会看到“这与 Hive 不兼容”。通过向右滚动

df.write.bucketBy(100, "country", "state").sortBy("country", "state").saveAsTable("kofeng.lstg_bucket_test")

17/03/13 15:12:01 WARN HiveExternalCatalog: Persisting bucketed data source table `kofeng`.`lstg_bucket_test` into Hive metastore in Spark SQL specific format, which is NOT compatible with Hive.

df.write.bucketBy(100, "country", "state").sortBy("country", "state").saveAsTable("kofeng.lstg_bucket_test2")

加入他们并解释

由于音量小,请先禁用广播加入。
import org.apache.spark.sql.SparkSession
val spark = SparkSession.builder().appName("Spark SQL basic example").config("spark.sql.autoBroadcastJoinThreshold", "0").getOrCreate()

方案在SPARK 2.1.0中避免交换和排序,在SPARK2.0中避免交换,仅过滤和扫描证明数据局部性利用率。
 val query = """
|SELECT *
|FROM
| kofeng.lstg_bucket_test a
|JOIN
| kofeng.lstg_bucket_test2 b
|ON a.country=b.country AND
| a.state=b.state
""".stripMargin
val joinDF = sql(query)


scala> joinDF.queryExecution.executedPlan
res10: org.apache.spark.sql.execution.SparkPlan =
*SortMergeJoin [country#71, state#72], [country#74, state#75], Inner
:- *Project [item_id#70, country#71, state#72]
: +- *Filter (isnotnull(country#71) && isnotnull(state#72))
: +- *FileScan parquet kofeng.lstg_bucket_test[item_id#70,country#71,state#72] Batched: true, Format: Parquet, Location: InMemoryFileIndex[hdfs://ares-lvs-nn-ha/user/hive/warehouse/kofeng.db/lstg_bucket_test], PartitionFilters: [], PushedFilters: [IsNotNull(country), IsNotNull(state)], ReadSchema: struct<item_id:int,country:int,state:string>
+- *Project [item_id#73, country#74, state#75]
+- *Filter (isnotnull(country#74) && isnotnull(state#75))
+- *FileScan parquet kofeng.lstg_bucket_test2[item_id#73,country#74,state#75] Batched: true, Format: Parquet...

关于hive - 不支持 SparkSQL 表创建 CLUSTER BY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41941918/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com