如何使用 Spark SQL 将数据写入 Apache Iceberg 表?

问题描述 投票:0回答:3

我正在尝试熟悉 Apache Iceberg,但在理解如何使用 Spark SQL 将一些外部数据写入表中时遇到一些困难。

  • 我有一个文件 one.csv,位于目录 /data
  • 我的 Iceberg 目录配置为指向此目录,/warehouse
  • 我想将这个 one.csv 写入 Apache Iceberg 表(最好使用 Spark SQL)

是否可以使用 Spark SQL 读取外部数据?然后将其写入冰山表?我必须使用 scala 或 python 来执行此操作吗?我已经阅读了很多 Iceberg 和 Spark 3.0.1 文档,但也许我遗漏了一些东西。

代码更新

这是一些我希望能有所帮助的代码

spark.conf.set("spark.sql.catalog.spark_catalog", "org.apache.iceberg.spark.SparkSessionCatalog")
spark.conf.set("spark.sql.catalog.spark_catalog.type", "hive")
spark.conf.set("spark.sql.catalog.local", "org.apache.iceberg.spark.SparkCatalog")
spark.conf.set("spark.sql.catalog.local.type", "hadoop")
spark.conf.set("spark.sql.catalog.local.warehouse", "data/warehouse")

我需要使用的数据位于目录中 /one/one.csv

如何使用 Spark 将其放入 Iceberg 表中?这一切都可以纯粹使用 SparkSQL 来完成吗?

spark.sql(
"""
CREATE or REPLACE TABLE local.db.one
USING iceberg
AS SELECT * FROM `/one/one.csv`
"""
)

那么目标是我可以直接使用这个冰山表,例如:

select * from local.db.one

这将为我提供 /one/one.csv 文件中的所有内容。

apache-spark apache-spark-sql iceberg
3个回答
3
投票

要使用 SparkSQL,请将文件读入数据帧,然后将其注册为临时视图。现在可以在 SQL 中将此临时视图称为:

var df = spark.read.format("csv").load("/data/one.csv")
df.createOrReplaceTempView("tempview");

spark.sql("CREATE or REPLACE TABLE local.db.one USING iceberg AS SELECT * FROM tempview");

回答你的另一个问题,Scala 或 Python 不是必需的;上面的例子是用Java编写的。


0
投票
val sparkConf = new SparkConf()
sparkConf.set("spark.sql.extensions", "org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions")
sparkConf.set("spark.sql.catalog.spark_catalog", "org.apache.iceberg.spark.SparkSessionCatalog")
sparkConf.set("spark.sql.catalog.spark_catalog.type", "hive")
sparkConf.set("spark.sql.catalog.hive_catalog", "org.apache.iceberg.spark.SparkCatalog")
sparkConf.set("spark.sql.catalog.hive_catalog.type", "hadoop")
sparkConf.set("spark.sql.catalog.hive_catalog.warehouse", "hdfs://host:port/user/hive/warehouse")
sparkConf.set("hive.metastore.uris", "thrift://host:19083")
sparkConf.set("spark.sql.catalog.hive_prod", " org.apache.iceberg.spark.SparkCatalog")
sparkConf.set("spark.sql.catalog.hive_prod.type", "hive")
sparkConf.set("spark.sql.catalog.hive_prod.uri", "thrift://host:19083")
sparkConf.set("hive.metastore.warehouse.dir", "hdfs://host:port/user/hive/warehouse")
val spark: SparkSession = SparkSession.builder()
  .enableHiveSupport()
  .config(sparkConf)
  .master("yarn")
  .appName("kafkaTableTest")
  .getOrCreate()

spark.sql(
  """
    |
    |create table if not exists hive_catalog.icebergdb.kafkatest1(
    |    company_id int,
    |    event string,
    |    event_time timestamp,
    |    position_id int,
    |    user_id int
    |)using iceberg
    |PARTITIONED BY (days(event_time))
    |""".stripMargin)

import spark.implicits._



val df: DataFrame = spark.readStream
  .format("kafka")
  .option("kafka.bootstrap.servers", "kafka_server")
  .option("subscribe", "topic")
  .option("startingOffsets", "latest")
  .load()
//.selectExpr("cast (value as string)")

val value: DataFrame = df.selectExpr("CAST(value AS STRING)")
  .as[String]
  .map(data => {
    val json_str: JSONObject = JSON.parseObject(data)
    val company_id: Integer = json_str.getInteger("company_id")
    val event: String = json_str.getString("event")
    val event_time: String = json_str.getString("event_time")
    val position_id: Integer = json_str.getInteger("position_id")
    val user_id: Integer = json_str.getInteger("user_id")
    (company_id, event, event_time, position_id, user_id)
  })
  .toDF("company_id", "event", "event_time", "position_id", "user_id")



value.createOrReplaceTempView("table")

spark.sql(
  """
    |select
    | company_id,
    | event,
    | to_timestamp(event_time,'yyyy-MM-dd HH:mm:ss') as event_time,
    | position_id,
    | user_id
    |from table
    |""".stripMargin)
  .writeStream
  .format("iceberg")
  .outputMode("append")
  .trigger(Trigger.ProcessingTime(1, TimeUnit.MINUTES))
  .option("path","hive_catalog.icebergdb.kafkatest1") // tablePath: catalog.db.tableName
  .option("checkpointLocation","hdfspath")
  .start()
  .awaitTermination()

此示例是从 Kafka 读取数据并将数据写入 Iceberg 表


0
投票

在此代码中 upsert 将如何处理 假设同一事件伴随着某些列的更新而发生。如何应对口是心非?

© www.soinside.com 2019 - 2024. All rights reserved.