大数据hudi之集成Hive:Hive同步

Flink 同步Hive

1)使用方式
Flink hive sync 现在支持两种 hive sync mode, 分别是 hms 和 jdbc 模式。 其中 hms 只需要配置 metastore uris;而 jdbc 模式需要同时配置 jdbc 属性 和 metastore uris,具体配置模版如下:

## hms mode 配置

CREATE TABLE t1(
  uuid VARCHAR(20),
  name VARCHAR(10),
  age INT,
  ts TIMESTAMP(3),
  `partition` VARCHAR(20)
)
PARTITIONED BY (`partition`)
with(
  'connector'='hudi',
  'path' = 'hdfs://xxx.xxx.xxx.xxx:9000/t1',
  'table.type'='COPY_ON_WRITE',        -- MERGE_ON_READ方式在没生成 parquet 文件前,hive不会有输出
  'hive_sync.enable'='true',           -- required,开启hive同步功能
  'hive_sync.table'='${hive_table}',              -- required, hive 新建的表名
  'hive_sync.db'='${hive_db}',             -- required, hive 新建的数据库名
  'hive_sync.mode' = 'hms',            -- required, 将hive sync mode设置为hms, 默认jdbc
  'hive_sync.metastore.uris' = 'thrift://ip:9083' -- required, metastore的端口
);
2)案例实操
CREATE TABLE t10(
  id int,
  num int,
  ts int,
  primary key (id) not enforced
)
PARTITIONED BY (num)
with(
  'connector'='hudi',
  'path' = 'hdfs://hadoop1:8020/tmp/hudi_flink/t10',
  'table.type'='COPY_ON_WRITE', 
  'hive_sync.enable'='true', 
  'hive_sync.table'='h10', 
  'hive_sync.db'='default', 
  'hive_sync.mode' = 'hms',
  'hive_sync.metastore.uris' = 'thrift://hadoop1:9083'
);


insert into t10 values(1,1,1); 

Spark 同步Hive

参数:https://hudi.apache.org/docs/basic_configurations#Write-Options
1)使用方式
以Spark shell为例:

  option("hoodie.datasource.hive_sync.enable","true").                         //设置数据集注册并同步到hive
  option("hoodie.datasource.hive_sync.mode","hms").                         //使用hms
  option("hoodie.datasource.hive_sync.metastore.uris", "thrift://ip:9083"). //hivemetastore地址
  option("hoodie.datasource.hive_sync.username","").                          //登入hiveserver2的用户
  option("hoodie.datasource.hive_sync.password","").                      //登入hiveserver2的密码
  option("hoodie.datasource.hive_sync.database", "").                   //设置hudi与hive同步的数据库
  option("hoodie.datasource.hive_sync.table", "").                        //设置hudi与hive同步的表名
  option("hoodie.datasource.hive_sync.partition_fields", "").               //hive表同步的分区列
  option("hoodie.datasource.hive_sync.partition_extractor_class", "org.apache.hudi.hive.MultiPartKeysValueExtractor"). // 分区提取器 按/ 提取分区

2)案例实操

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._

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))
        .withColumn("a",split(col("partitionpath"),"\\/")(0))
        .withColumn("b",split(col("partitionpath"),"\\/")(1))
        .withColumn("c",split(col("partitionpath"),"\\/")(2))
df.write.format("hudi").
  options(getQuickstartWriteConfigs).
  option(PRECOMBINE_FIELD_OPT_KEY, "ts").
  option(RECORDKEY_FIELD_OPT_KEY, "uuid").
  option("hoodie.table.name", tableName). 
  option("hoodie.datasource.hive_sync.enable","true").
  option("hoodie.datasource.hive_sync.mode","hms").
  option("hoodie.datasource.hive_sync.metastore.uris", "thrift://hadoop1:9083").
  option("hoodie.datasource.hive_sync.database", "default").
  option("hoodie.datasource.hive_sync.table", "spark_hudi").
  option("hoodie.datasource.hive_sync.partition_fields", "a,b,c").
  option("hoodie.datasource.hive_sync.partition_extractor_class", "org.apache.hudi.hive.MultiPartKeysValueExtractor").
  mode(Overwrite).
  save(basePath)
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hudi是一种用于存储大数据的开源工具,它可以高效地存储和管理大规模的数据集。如果你想将Hudi数据同步Hive中,可以按照以下步骤进行操作: 1. 在Hudi中配置Hive Metastore URI和数据库名称。 2. 将Hudi表注册到Hive中,以便Hive可以访问它们。 3. 使用HiveQL语句查询Hudi表。 具体来讲,可以按照以下步骤进行操作: 1. 配置Hive Metastore URI和数据库名称 在Hudi中,可以使用SparkSession对象来配置Hive Metastore URI和数据库名称,例如: ``` val spark = SparkSession.builder() .appName("HudiSyncToHiveExample") .config("hive.metastore.uris", "thrift://localhost:9083") .config("spark.sql.catalogImplementation", "hive") .enableHiveSupport() .getOrCreate() spark.sql("use hudi_db") ``` 这里我们将Hive Metastore URI设置为“thrift://localhost:9083”,将数据库名称设置为“hudi_db”。 2. 注册Hudi表到Hive中 在Hudi中,可以使用HoodieSparkUtils类中的registerHudi表方法来将Hudi表注册到Hive中,例如: ``` HoodieSparkUtils.registerHudiTable(spark, "hdfs://localhost:9000/hudi/my_table", "my_table") ``` 这里我们将Hudi表的路径设置为“hdfs://localhost:9000/hudi/my_table”,将表名设置为“my_table”。 3. 使用HiveQL查询Hudi表 在Hudi表注册到Hive之后,就可以使用HiveQL语句来查询Hudi表了,例如: ``` spark.sql("SELECT * FROM my_table").show() ``` 这里我们使用SELECT语句来查询“my_table”表中的所有数据,并使用show()方法将查询结果展示出来。 总之,以上就是将Hudi数据同步Hive中的步骤,你可以根据自己的需求进行调整和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值