Paimon和Hive相集成

Flink版本1.17

Hive版本3.1.3

1、Paimon集成Hive

将paimon-hive-connector.jar复制到auxlib中,下载链接Index of /groups/snapshots/org/apache/https://repository.apache.org/snapshots/org/apache/paimon/

通过flink进入查看paimon

/opt/softwares/flink-1.17.0/bin/sql-client.sh -s yarn-session -i /opt/softwares/flink-1.17.0/conf/sql-client-init.sql

 sql-client-init.sql

CREATE CATALOG fs_catalog WITH (
	    'type' = 'paimon',
	    'warehouse' = 'hdfs://node154:8020/paimon/fs'
);

CREATE CATALOG hive_catalog WITH (
	    'type' = 'paimon',
	    'metastore' = 'hive',
	'uri' = 'thrift://node154:9083',
	'hive-conf-dir' = '/opt/softwares/hive/conf',
	    'warehouse' = 'hdfs://node154:8020/paimon/hive'
);


USE CATALOG hive_catalog;

SET 'sql-client.execution.result-mode' = 'tableau';

注意,加载配置文件进入flink之后,虽然说使用的是hive_catalog,但是使用的database是default的,需要使用test,否则找不到表欧。

 表ws_t;和名为test的database都是之前是在flink中操作paimon在hive_catalog 创建出来的,步骤看

paimon中批和流查看过去的快照的数据及变动的数据-CSDN博客文章浏览阅读258次,点赞10次,收藏4次。paimon中批和流查看过去的快照的数据及变动的数据 https://blog.csdn.net/yyf960126/article/details/147930584?spm=1001.2014.3001.5502

进入hive

hive中
use test;
SELECT * FROM ws_t;

补充知识点,hive中使用【test】database来创建hive表和paimon中使用使用hive_catalog中【test】的database创建出的paimon表存储位置不同,建表语句也能看出来。但是都能在hive中【test】的database查到。

hive查看test库中的表为

orders       paimon表
ws1          paimon表
ws_t         paimon表
test_hive    hive表
yyf          hive表

文件存储为如图:

---------------paimon表---------------------
CREATE TABLE `ws_t`(
  `id` int COMMENT 'from deserializer', 
  `ts` bigint COMMENT 'from deserializer', 
  `vc` int COMMENT 'from deserializer')
ROW FORMAT SERDE 
  'org.apache.paimon.hive.PaimonSerDe' 
STORED BY 
  'org.apache.paimon.hive.PaimonStorageHandler' 

LOCATION
  'hdfs://node154:8020/paimon/hive/test.db/ws_t'
TBLPROPERTIES (
  'transient_lastDdlTime'='1747128118')
-----------------hive表------------------
CREATE TABLE `yyf`(
  `a` int)
ROW FORMAT SERDE 
  'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' 
STORED AS INPUTFORMAT 
  'org.apache.hadoop.mapred.TextInputFormat' 
OUTPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
  'hdfs://node154:8020/user/hive/warehouse/test.db/yyf'
TBLPROPERTIES (
  'bucketing_version'='2', 
  'transient_lastDdlTime'='1747066788')

 在hive中创建paimon表

--使用hive_catalog的存储路径
SET hive.metastore.warehouse.dir=hdfs://node154:8020/paimon/hive;
--数据处理按照paimon来
CREATE TABLE test_h(
    a INT COMMENT 'The a field',
    b STRING COMMENT 'The b field'
)
STORED BY 'org.apache.paimon.hive.PaimonStorageHandler'

 

 通过创建hive外部表来使用现有的paimon表

字段随着paimon源表的修改而自动变动,paimon表的特性

CREATE EXTERNAL TABLE test.paimon_ex_ws_t
STORED BY 'org.apache.paimon.hive.PaimonStorageHandler'
LOCATION 'hdfs://node154:8020/paimon/hive/test.db/ws_t';

--或将路径写在表属性中:
CREATE EXTERNAL TABLE paimon_ex_ws_t
STORED BY 'org.apache.paimon.hive.PaimonStorageHandler'
TBLPROPERTIES (
 'paimon_location' ='hdfs://node154:8020/paimon/hive/test.db/ws_t'
);

### Flink CDC 集成 Paimon 示例代码与教程 #### 使用 Flink CDC 连接器读取 MySQL 数据并写入 Paimon 表 为了实现这一目标,可以按照如下方式配置编写代码: 1. **引入依赖** 在 `pom.xml` 文件中添加必要的 Maven 依赖项以支持 Flink CDC Paimon。 ```xml <dependencies> <!-- Flink dependencies --> <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-streaming-java_2.12</artifactId> <version>${flink.version}</version> </dependency> <!-- Flink CDC Connector for MySQL --> <dependency> <groupId>com.ververica</groupId> <artifactId>flink-connector-mysql-cdc</artifactId> <version>${cdc.connector.version}</version> </dependency> <!-- Apache Paimon Table Store --> <dependency> <groupId>org.apache.paimon</groupId> <artifactId>paimon-flink</artifactId> <version>${paimon.version}</version> </dependency> </dependencies> ``` 2. **创建表结构定义** 使用 SQL DDL 定义要同步到 Paimon 中的目标表。这里假设有一个名为 `orders` 的订单表。 ```sql CREATE TABLE orders ( order_id BIGINT NOT NULL, user_id BIGINT, product_name STRING, amount DOUBLE, PRIMARY KEY (order_id) NOT ENFORCED ) WITH ( 'connector' = 'paimon', 'warehouse.path' = 'file:///path/to/paimon/warehouse' ); ``` 3. **编写 Flink 应用程序** 下面是一个完整的 Java 实现例子,它展示了如何利用 Flink CDC 将来自 MySQL 的变更数据捕获并通过 Paimon 存储起来。 ```java import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; import com.alibaba.ververica.cdc.connectors.mysql.MySqlSource; import com.alibaba.ververica.cdc.debezium.DebeziumDeserializationSchema; import org.apache.flink.formats.json.JsonRowDataDeserializationSchema; import org.apache.flink.table.api.TableResult; import org.apache.flink.table.api.bridge.java.StreamTableEnvironment; public class MysqlToPaimonExample { public static void main(String[] args) throws Exception { final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); final StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env); MySqlSource<String> mySqlSource = MySqlSource.<String>builder() .hostname("localhost") .port(3306) .databaseList("test_db") // database name .tableList("test_db.orders") // table names, format is {database}.{table} .username("root") .password("your_password") .deserializer(new JsonRowDataDeserializationSchema()) // convert to JSON string .build(); tableEnv.executeSql(""" CREATE TABLE paimon_orders ( order_id BIGINT NOT NULL, user_id BIGINT, product_name STRING, amount DOUBLE, PRIMARY KEY(order_id) NOT ENFORCED ) WITH ( 'connector' = 'paimon', 'warehouse.path' = '/tmp/paimon_example' ) """); var cdcStream = env.fromSource(mySqlSource, WatermarkStrategy.noWatermarks(), "MySQL Source"); tableEnv.createTemporaryView("source_table", cdcStream.map(record -> record)); tableEnv.executeSql("INSERT INTO paimon_orders SELECT * FROM source_table").await(); } } ``` 此应用程序会持续监听 MySQL 数据库中的变化,并将这些更改实时地应用到指定路径下的 Paimon 表中[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值