- Java锁的逻辑(结合对象头和ObjectMonitor)
- 还在用饼状图?来瞧瞧这些炫酷的百分比可视化新图形(附代码实现)⛵
- 自动注册实体类到EntityFrameworkCore上下文,并适配ABP及ABPVNext
- 基于Sklearn机器学习代码实战
使用Flink读取Kafka数据并实时写入Hive表.
。
EMR环境:Hadoop 3.3.3, Hive 3.1.3, Flink 1.16.0 。
根据官网描述:
https://nightlies.apache.org/flink/flink-docs-release-1.16/docs/connectors/table/hive/overview/ 。
当前Flink 1.16.0 支持Hive 3.1.3版本,如果是开发,则需要加入依赖有:
<dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-connector-hive_2.12</artifactId> <version>1.16.0</version> <scope>provided</scope> </dependency> // Hive dependencies <dependency> <groupId>org.apache.hive</groupId> <artifactId>hive-exec</artifactId> <version>3.1.3</version> </dependency>
。
在读写hive表时,预先条件是注册hive catalog:
// set hive dialect tableEnv.getConfig().setSqlDialect(SqlDialect.HIVE) // set hive catalog tableEnv.executeSql("CREATE CATALOG myhive WITH (" + "'type' = 'hive'," + "'default-database' = 'default'," + "'hive-conf-dir' = 'hiveconf'" + ")" ) tableEnv.executeSql( "use catalog myhive")
。
然后创建hive表:
// hive table tableEnv.executeSql("CREATE TABLE IF NOT EXISTS hive_table (" + "id string," + "`value` float," + "hashdata string," + "num integer," + "token string," + "info string," + "ts timestamp " + ") " + "PARTITIONED BY (dt string, hr string) STORED AS ORC TBLPROPERTIES (" + // "'path'='hive-output'," + "'partition.time-extractor.timestamp-pattern'='$dt $hr:00:00'," + "'sink.partition-commit.policy.kind'='metastore,success-file'," + "'sink.partition-commit.trigger'='partition-time'," + "'sink.partition-commit.delay'='0 s'" + " )")
。
。
参考官方文档:
https://nightlies.apache.org/flink/flink-docs-release-1.16/docs/connectors/datastream/kafka/ 。
。
添加对应依赖:
<!-- https: // mvnrepository.com/artifact/org.apache.flink/flink-connector-kafka --> <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-connector-kafka</artifactId> <version>${flink.version}</version> </dependency>
。
flinksql参考代码:
package com.tang.hive import org.apache.flink.api.java.utils.ParameterTool import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment import org.apache.flink.table.api.SqlDialect import org.apache.flink.table.api.bridge.scala.StreamTableEnvironment object Kafka2Hive { /** * * create hive table * @param tbl_env * @param drop * @param hiveConfDir * @param database * @return */ def buildHiveTable(tbl_env: StreamTableEnvironment, drop: Boolean, hiveConfDir: String, database: String, tableName: String, dbLocation: String) = { // set hive dialect tbl_env.getConfig().setSqlDialect(SqlDialect.HIVE) // set hive catalog tbl_env.executeSql("CREATE CATALOG myhive WITH (" + "'type' = 'hive'," + "'default-database' = '"+ database + "'," + "'hive-conf-dir' = '" + hiveConfDir + "'" + ")" ) tbl_env.executeSql( "use catalog myhive" ) // whether drop hive table first if (drop) { // drop first tbl_env.executeSql("drop table if exists" + tableName) } val sql = "CREATE TABLE IF NOT EXISTS " + tableName + "(" + "id string," + "`value` float," + "hashdata string," + "num integer," + "token string," + "info string," + "ts timestamp " + ") " + "PARTITIONED BY (dt string, hr string) STORED AS ORC " + "LOCATION '" + dbLocation + "/" + tableName +"' TBLPROPERTIES (" + "'partition.time-extractor.timestamp-pattern'='$dt $hr:00:00'," + "'sink.partition-commit.policy.kind'='metastore,success-file'," + "'sink.partition-commit.trigger'='partition-time'," + "'sink.partition-commit.watermark-time-zone'='Asia/Shanghai'," + "'sink.partition-commit.delay'='0 s'," + "'auto-compaction'='true'" + " )" // hive table tbl_env.executeSql(sql) } /** * * create kafka table * @param tbl_env * @param drop * @param bootstrapServers * @param topic * @param groupId * @return */ def buildKafkaTable(tbl_env: StreamTableEnvironment, drop: Boolean, bootstrapServers: String, topic: String, groupId: String, tableName: String) = { // set to default dialect tbl_env.getConfig.setSqlDialect(SqlDialect.DEFAULT) if (drop) { tbl_env.executeSql( "drop table if exists " + tableName) } // kafka table tbl_env.executeSql("CREATE TABLE IF NOT EXISTS "+ tableName + " (" + "id string," + "`value` float," + "hashdata string," + "num integer," + "token string," + "info string," + "created_timestamp bigint," + "ts AS TO_TIMESTAMP( FROM_UNIXTIME(created_timestamp) ), " + "WATERMARK FOR ts AS ts - INTERVAL '5' SECOND "+ " )" + "with (" + " 'connector' = 'kafka'," + " 'topic' = '" + topic + "'," + " 'properties.bootstrap.servers' = '" + bootstrapServers +"'," + " 'properties.group.id' = '" + groupId + "'," + " 'scan.startup.mode' = 'latest-offset'," + " 'format' = 'json'," + " 'json.fail-on-missing-field' = 'false'," + " 'json.ignore-parse-errors' = 'true'" + ")" ) } def main(args: Array[String]): Unit = { val senv = StreamExecutionEnvironment.getExecutionEnvironment val tableEnv = StreamTableEnvironment.create(senv) // set checkpoint // senv.enableCheckpointing(60000); // senv.getCheckpointConfig.setCheckpointStorage("file: // flink-hive-chk"); // get parameter val tool: ParameterTool = ParameterTool.fromArgs(args) val hiveConfDir = tool.get("hive.conf.dir", "src/main/resources" ) val database = tool.get("database", "default" ) val hiveTableName = tool.get("hive.table.name", "hive_tbl" ) val kafkaTableName = tool.get("kafka.table.name", "kafka_tbl" ) val bootstrapServers = tool.get("bootstrap.servers", "b-2.cdc.62vm9h.c4.kafka.ap-northeast-1.amazonaws.com:9092,b-1.cdc.62vm9h.c4.kafka.ap-northeast-1.amazonaws.com:9092,b-3.cdc.62vm9h.c4.kafka.ap-northeast-1.amazonaws.com:9092" ) val groupId = tool.get("group.id", "flinkConsumer" ) val reset = tool.getBoolean("tables.reset", false ) val topic = tool.get("kafka.topic", "cider" ) val hiveDBLocation = tool.get("hive.db.location", "s3://tang-emr-tokyo/flink/kafka2hive/" ) buildHiveTable(tableEnv, reset, hiveConfDir, database, hiveTableName, hiveDBLocation) buildKafkaTable(tableEnv, reset, bootstrapServers, topic, groupId, kafkaTableName) // select from kafka table and write to hive table tableEnv.executeSql("insert into " + hiveTableName + " select id, `value`, hashdata, num, token, info, ts, DATE_FORMAT(ts, 'yyyy-MM-dd'), DATE_FORMAT(ts, 'HH') from " + kafkaTableName) } }
。
Kafka写入数据格式:
{"id": "35f1c5a8-ec19-4dc3-afa5-84ef6bc18bd8", "value": 1327.12, "hashdata": "0822c055f097f26f85a581da2c937895c896200795015e5f9e458889", "num": 3, "token": "800879e1ef9a356cece14e49fb6949c1b8c1862107468dc682d406893944f2b6", "info": "valentine", "created_timestamp": 1690165700
}
。
Hive表的部分配置:
"'sink.partition-commit.policy.kind'='metastore,success-file'," =》在分区完成写入后,如何通知下游“分区数据已经可读”。目前支持metastore和success- file "'sink.partition-commit.trigger'='partition-time'," =》什么时候触发partition commit。Partition-time表示在watermark超过了“分区时间”+ “delay”的时间后,commit partition "'sink.partition-commit.delay'='0 s'" = 》延迟这个时间后再commit分区 'sink.partition-commit.watermark-time-zone'='Asia/Shanghai' = 》时区必须与数据时间戳一致 "'auto-compaction'='true'" = 》开启文件合并,在落盘前先合并 通过checkponit来决定落盘频率 senv.enableCheckpointing( 60000 );
在这个配置下,每1分钟会做一次checkpoint,即将文件写入s3。同时,还会触发自动合并的动作,最终每1分钟生成1个orc文件.
。
参考flink官网:
。
。
需要移除flink-table-planner-loader-1.16.0.jar,并移入flink-table-planner_2.12-1.16.0:
cd /usr/lib/flink/ lib sudo mv flink -table-planner-loader-1.16.0.jar ../ sudo wget https: // repo1.maven.org/maven2/org/apache/flink/flink-table-planner_2.12/1.16.0/flink-table-planner_2.12-1.16.0.jar sudo chown flink:flink flink -table-planner_2.12-1.16.0 .jar sudo chmod +x flink-table-planner_2.12-1.16.0 .jar 然后主节点运行: sudo cp /usr/lib/hive/lib/antlr-runtime-3.5.2.jar /usr/lib/flink/ lib sudo cp /usr/lib/hive/lib/hive-exec-3.1.3*.jar /lib/flink/ lib sudo cp /usr/lib/hive/lib/libfb303-0.9.3.jar /lib/flink/ lib sudo cp /usr/lib/flink/opt/flink-connector-hive_2.12-1.16.0.jar /lib/flink/ lib sudo chmod 755 /usr/lib/flink/lib/antlr-runtime-3.5.2 .jar sudo chmod 755 /usr/lib/flink/lib/hive-exec-3.1.3* .jar sudo chmod 755 /usr/lib/flink/lib/libfb303-0.9.3 .jar sudo chmod 755 /usr/lib/flink/lib/flink-connector-hive_2.12-1.16.0 .jar
。
上传hive配置文件到hdfs:
hdfs dfs -mkdir /user/hadoop/hiveconf/ hdfs dfs -put /etc/hive/conf/hive-site.xml /user/hadoop/hiveconf/hive- site.xml
。
Emr主节点提交job:
flink run- application \ -t yarn- application \ - c com.tang.hive.Kafka2Hive \ -p 8 \ -D state.backend= rocksdb \ -D state.checkpoint-storage= filesystem \ -D state.checkpoints.dir=s3: // tang-emr-tokyo/flink/kafka2hive/checkpoints \ -D execution.checkpointing.interval=60000 \ -D state.checkpoints.num-retained=5 \ -D execution.checkpointing.mode= EXACTLY_ONCE \ -D execution.checkpointing.externalized-checkpoint-retention= RETAIN_ON_CANCELLATION \ -D state.backend.incremental= true \ -D execution.checkpointing.max-concurrent-checkpoints=1 \ -D rest.flamegraph.enabled= true \ flink - tutorial.jar \ --hive.conf.dir hdfs: // /user/hadoop/hiveconf \ --reset true
。
从写入基于s3的hive表来看,基本是1分钟2个文件(因为超出了默认rolling配置的128MB文件大小,所以会额外再写1个文件)。同时,未compaction的文件对下游不可见:
。
。
从hive表来看,写入数据后默认在hive元数据内注册了新分区.
。
S3路径:
。
。
Hive分区:
。
。
从hive查询结果来看,下游能查询到的数据为最近1分钟之前的数据:
select current_timestamp, ts from hive_tbl order by ts desc limit 10
;
》
2023-07-27 09:25:24.193 2023-07-27 09:24:24
2023-07-27 09:25:24.193 2023-07-27 09:24:24
2023-07-27 09:25:24.193 2023-07-27 09:24:24
2023-07-27 09:25:24.193 2023-07-27 09:24:24
。
最后此篇关于Flink-读Kafka写Hive表的文章就讲到这里了,如果你想了解更多关于Flink-读Kafka写Hive表的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我在 Windows 机器上启动 Kafka-Server 时出现以下错误。我已经从以下链接下载了 Scala 2.11 - kafka_2.11-2.1.0.tgz:https://kafka.ap
关于Apache-Kafka messaging queue . 我已经从 Kafka 下载页面下载了 Apache Kafka。我已将其提取到 /opt/apache/installed/kafka
假设我有 Kafka 主题 cars。 我还有一个消费者组 cars-consumers 订阅了 cars 主题。 cars-consumers 消费者组当前位于偏移量 89。 当我现在删除 cars
我想知道什么最适合我:Kafka 流或 Kafka 消费者 api 或 Kafka 连接? 我想从主题中读取数据,然后进行一些处理并写入数据库。所以我编写了消费者,但我觉得我可以编写 Kafka 流应
我曾研究过一些 Kafka 流应用程序和 Kafka 消费者应用程序。最后,Kafka流不过是消费来自Kafka的实时事件的消费者。因此,我无法弄清楚何时使用 Kafka 流或为什么我们应该使用
Kafka Acknowledgement 和 Kafka 消费者 commitSync() 有什么区别 两者都用于手动偏移管理,并希望两者同步工作。 请协助 最佳答案 使用 spring-kafka
如何在 Kafka 代理上代理 Apache Kafka 生产者请求,并重定向到单独的 Kafka 集群? 在我的特定情况下,无法更新写入此集群的客户端。这意味着,执行以下操作是不可行的: 更新客户端
我需要在 Kafka 10 中命名我的消费者,就像我在 Kafka 8 中所做的一样,因为我有脚本可以嗅出并进一步使用这些信息。 显然,consumer.id 的默认命名已更改(并且现在还单独显示了
1.概述 我们会看到zk的数据中有一个节点/log_dir_event_notification/,这是一个序列号持久节点 这个节点在kafka中承担的作用是: 当某个Broker上的LogDir出现
我正在使用以下命令: bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test.topic --property
我很难理解 Java Spring Boot 中的一些 Kafka 概念。我想针对在服务器上运行的真实 Kafka 代理测试消费者,该服务器有一些生产者已将数据写入/已经将数据写入各种主题。我想与服务
我的场景是我使用了很多共享前缀的 Kafka 主题(例如 house.door, house.room ) 并使用 Kafka 流正则表达式主题模式 API 使用所有主题。 一切看起来都不错,我得到了
有没有办法以编程方式获取kafka集群的版本?例如,使用AdminClient应用程序接口(interface)。 我想在消费者/生产者应用程序中识别 kafka 集群的版本。 最佳答案 目前无法检索
每当我尝试重新启动 kafka 时,它都会出现以下错误。一旦我删除/tmp/kafka-logs 它就会得到解决,但它也会删除我的主题。 有办法解决吗? ERROR Error while
我是 Apache Kafka 的新用户,我仍在了解内部结构。 在我的用例中,我需要从 Kafka Producer 客户端动态增加主题的分区数。 我发现了其他类似的 questions关于增加分区大
正如 Kafka 文档所示,一种方法是通过 kafka.tools.MirrorMaker 来实现这一点。但是,我需要将一个主题(比如 测试 带 1 个分区)(其内容和元数据)从生产环境复制到没有连接
我已经在集群中配置了 3 个 kafka,我正在尝试与 spring-kafka 一起使用。 但是在我杀死 kafka 领导者之后,我无法将其他消息发送到队列中。 我将 spring.kafka.bo
我的 kafka sink 连接器从多个主题(配置了 10 个任务)读取,并处理来自所有主题的 300 条记录。根据每个记录中保存的信息,连接器可以执行某些操作。 以下是触发器记录中键值对的示例: "
我有以下 kafka 流代码 public class KafkaStreamHandler implements Processor{ private ProcessorConte
当 kafka-streams 应用程序正在运行并且 Kafka 突然关闭时,应用程序进入“等待”模式,发送警告日志的消费者和生产者线程无法连接,当 Kafka 回来时,一切都应该(理论上)去恢复正常
我是一名优秀的程序员,十分优秀!