浙江省大数据Hudi配置

Hudi部署

完成解压安装及配置后使用maven对hudi进行构建(spark3.1,scala-2.12),使用spark-shell操作hudi(启动时若需要hudi-spark3.1-bundle_2.12-0.12.0.jar,该包已放置在/opt/software下,若不需要请忽略),将spark-shell启动命令复制并粘贴至对应报告中;使用spark-shell运行下面给到的案例,并将最终查询结果截图粘贴至对应报告中。

Scala 复制代码
 import org.apache.hudi.QuickstartUtils._
 import scala.collection.JavaConversions._
 import org.apache.spark.sql.SaveMode.
 import org.apache.hudi.DataSourceReadOptions._
 import org.apache.hudi.DataSourceWriteOptions._
 import org.apache.hudi.config.HoodieWriteConfig._
 import org.apache.hudi.common.model.HoodieRecord
 ​
 val tableName = "hudi_trips_cow"
 val basePath = "file:///tmp/hudi_trips_cow"
 val dataGen = new DataGenerator
 ​
 val inserts = convertToStringList(dataGen.generateInserts(10))
 val df = spark.read.json(spark.sparkContext.parallelize(inserts, 2))
 df.write.format("hudi").
   options(getQuickstartWriteConfigs).
   option(PRECOMBINE_FIELD_OPT_KEY, "ts").
   option(RECORDKEY_FIELD_OPT_KEY, "uuid").
   option(PARTITIONPATH_FIELD_OPT_KEY, "partitionpath").
   option(TABLE_NAME, tableName).
   mode(Overwrite).
   save(basePath)
 ​
 val tripsSnapshotDF = spark.read.format("hudi").load(basePath + "/*/*/*/*")
 tripsSnapshotDF.createOrReplaceTempView("hudi_trips_snapshot")
 spark.sql("select fare, begin_lon, begin_lat, ts from  hudi_trips_snapshot where fare > 20.0").show()

操作步骤:

1 将编译完成与spark集成的jar包,集成后使用spark-shell操作hudi,启动spark-shell。

bash 复制代码
 spark-shell \
   --jars /opt/software/hudi-spark3.1-bundle_2.12-0.12.0.jar \
   --conf 'spark.serializer=org.apache.spark.serializer.KryoSerializer' \
   --conf 'spark.sql.extensions=org.apache.spark.sql.hudi.HoodieSparkSessionExtension'

2 在spark-shell使用:paste编译多条语句按下ctrl+D执行多行语句

复制代码
 :paste
Scala 复制代码
 // 导入依赖包
 import org.apache.hudi.QuickstartUtils._
 import scala.collection.JavaConversions._
 import org.apache.spark.sql.SaveMode._
 import org.apache.hudi.DataSourceReadOptions._
 import org.apache.hudi.DataSourceWriteOptions._
 import org.apache.hudi.config.HoodieWriteConfig._
 import org.apache.hudi.common.model.HoodieRecord
 ​
 // 定义Hudi映射到的文件目录,以及存储表的名称
 val tableName = "hudi_trips_cow"
 val basePath = "file:///tmp/hudi_trips_cow"
 ​
 // 创建数据生成器实例
 val dataGen = new DataGenerator
 ​
 // 使用Hudi的数据生成器生成10条JSON数据
 val inserts = convertToStringList(dataGen.generateInserts(10))
复制代码
 ctrl+D           //执行多行语句

3 将10条JSON数据加载到DataFrame中,并写入hudi,实现一个简单的ETL处理

复制代码
 :paste
Scala 复制代码
 // 读取json数据到DataFrame中
 val df = spark.read.json(spark.sparkContext.parallelize(inserts, 2))
 ​
 // 将DataFrame写入数据湖
 df.write.format("hudi").
   options(getQuickstartWriteConfigs).
   option(PRECOMBINE_FIELD_OPT_KEY, "ts").
   option(RECORDKEY_FIELD_OPT_KEY, "uuid").
   option(PARTITIONPATH_FIELD_OPT_KEY, "partitionpath").
   option(TABLE_NAME, tableName).
   mode(Overwrite).
   save(basePath)
复制代码
 ctrl+D           //执行多行语句

4 将10条JSON数据加载到DataFrame中,并写入hudi,实现一个简单的ETL处理

复制代码
 :paste
Scala 复制代码
 // 从数据湖中读取数据到DataFrame中
 val tripsSnapshotDF = spark.read.format("hudi").load(basePath + "/*/*/*/*")
 ​
 // 注册临时表
 tripsSnapshotDF.createOrReplaceTempView("hudi_trips_snapshot")
 ​
 // 执行SQL查询,并显示
 spark.sql("select fare, begin_lon, begin_lat, ts from  hudi_trips_snapshot where fare > 20.0").show()
复制代码
 ctrl+D           //执行多行语句
相关推荐
阿里云大数据AI技术21 小时前
StarRocks x Fluss x Paimon湖流一体方案:构建秒级响应、湖流一体的实时数据引擎
大数据·人工智能
Databend21 小时前
Agent 轨迹分析与归因的数据工程实践
大数据·数据库·agent
喵个咪1 天前
Go Wind UBA 拆解系列 - 架构总览:三服务、数据流与契约优先
大数据·后端·go
喵个咪1 天前
Go Wind UBA 拆解系列 - 多租户与安全:两套隔离机制的边界
大数据·后端·go
喵个咪1 天前
Go Wind UBA 拆解系列 - OLAP 与 SQL 硬核:25 个分析模型怎么落地
大数据·后端·go
喵个咪1 天前
Go Wind UBA 拆解系列 - SDK 与采集层:从浏览器到 Kafka
大数据·后端·go
QCC产品中心1 天前
MiniMax Agent 接入实测:企业查询、股权穿透与 UBO 识别(附 Prompt 模板)
大数据·mcp·金融/非金融
SelectDB2 天前
Apache Doris Python UDF:让 SQL 直接调用 Python 生态,支撑 Agent 时代复杂业务逻辑
大数据·数据库·python
ApacheSeaTunnel2 天前
当多表数据涌入,Apache SeaTunnel 如何巧妙化解主键冲突?
大数据·开源·数据集成·seatunnel·技术分享·数据同步
大大大大晴天5 天前
Hudi Metadata Table 与 Hive Sync (HMS)怎么选?
大数据