数据湖探索 DLI
数据湖探索 DLI
- 最新动态
- 功能总览
- 服务公告
- 产品介绍
- 计费说明
- 快速入门
-
用户指南
- DLI作业开发流程
- 准备工作
- 创建弹性资源池和队列
- 创建数据库和表
- 数据迁移与数据传输
- 配置DLI访问其他云服务的委托权限
- 使用DLI提交SQL作业
- 使用DLI提交Flink作业
- 使用DLI提交Spark作业
- 使用Notebook实例提交DLI作业
- 使用CES监控DLI服务
- 使用CTS审计DLI服务
- 权限管理
- DLI常用管理操作
- 最佳实践
-
开发指南
- 使用客户端工具连接DLI
- SQL作业开发指南
- Flink作业开发指南
- Spark Jar作业开发指南
-
Spark SQL语法参考
- Spark SQL常用配置项说明
- Spark SQL语法概览
- Spark开源命令支持说明
- 数据库相关
- 表相关
- 数据相关
- 导出查询结果
- 跨源连接相关
- 视图相关
- 查看计划
- 数据权限相关
- 数据类型
- 自定义函数
-
内置函数
-
日期函数
- 日期函数概览
- add_months
- current_date
- current_timestamp
- date_add
- dateadd
- date_sub
- date_format
- datediff
- datediff1
- datepart
- datetrunc
- day/dayofmonth
- from_unixtime
- from_utc_timestamp
- getdate
- hour
- isdate
- last_day
- lastday
- minute
- month
- months_between
- next_day
- quarter
- second
- to_char
- to_date
- to_date1
- to_utc_timestamp
- trunc
- unix_timestamp
- weekday
- weekofyear
- year
-
字符串函数
- 字符串函数概览
- ascii
- concat
- concat_ws
- char_matchcount
- encode
- find_in_set
- get_json_object
- instr
- instr1
- initcap
- keyvalue
- length
- lengthb
- levenshtein
- locate
- lower/lcase
- lpad
- ltrim
- parse_url
- printf
- regexp_count
- regexp_extract
- replace
- regexp_replace
- regexp_replace1
- regexp_instr
- regexp_substr
- repeat
- reverse
- rpad
- rtrim
- soundex
- space
- substr/substring
- substring_index
- split_part
- translate
- trim
- upper/ucase
- 数学函数
- 聚合函数
- 分析窗口函数
- 其他函数
-
日期函数
- SELECT
-
标示符
- aggregate_func
- alias
- attr_expr
- attr_expr_list
- attrs_value_set_expr
- boolean_expression
- class_name
- col
- col_comment
- col_name
- col_name_list
- condition
- condition_list
- cte_name
- data_type
- db_comment
- db_name
- else_result_expression
- file_format
- file_path
- function_name
- groupby_expression
- having_condition
- hdfs_path
- input_expression
- input_format_classname
- jar_path
- join_condition
- non_equi_join_condition
- number
- num_buckets
- output_format_classname
- partition_col_name
- partition_col_value
- partition_specs
- property_name
- property_value
- regex_expression
- result_expression
- row_format
- select_statement
- separator
- serde_name
- sql_containing_cte_name
- sub_query
- table_comment
- table_name
- table_properties
- table_reference
- view_name
- view_properties
- when_expression
- where_condition
- window_function
- 运算符
-
Flink SQL语法参考
- Flink Opensource SQL1.15语法参考
- Flink Opensource SQL1.12语法参考
- Flink Opensource SQL1.10语法参考
-
HetuEngine SQL语法参考
-
HetuEngine SQL语法
- 使用前必读
- 数据类型
-
DDL 语法
- CREATE SCHEMA
- CREATE TABLE
- CREATE TABLE AS
- CREATE TABLE LIKE
- CREATE VIEW
- ALTER TABLE
- ALTER VIEW
- ALTER SCHEMA
- DROP SCHEMA
- DROP TABLE
- DROP VIEW
- TRUNCATE TABLE
- COMMENT
- VALUES
- SHOW语法使用概要
- SHOW SCHEMAS(DATABASES)
- SHOW TABLES
- SHOW TBLPROPERTIES TABLE|VIEW
- SHOW TABLE/PARTITION EXTENDED
- SHOW FUNCTIONS
- SHOW PARTITIONS
- SHOW COLUMNS
- SHOW CREATE TABLE
- SHOW VIEWS
- SHOW CREATE VIEW
- DML 语法
- DQL 语法
- 辅助命令语法
- 预留关键字
- SQL函数和操作符
- 数据类型隐式转换
- 附录
-
HetuEngine SQL语法
- Hudi SQL语法参考
- Delta SQL语法参考
-
API参考
- API使用前必读
- API概览
- 如何调用API
- API快速入门
- 权限相关API
- 全局变量相关API
- 资源标签相关API
- 增强型跨源连接相关API
- 跨源认证相关API
- 弹性资源池相关API
- 队列相关API(推荐)
- SQL作业相关API
- SQL模板相关API
- Flink作业相关API
- Flink作业模板相关API
- Flink作业管理相关API
- Spark作业相关API
- Spark作业模板相关API
- 权限策略和授权项
- 历史API
- 公共参数
- SDK参考
-
常见问题
- DLI产品咨询类
- DLI弹性资源池和队列类
-
DLI数据库和表类
- 为什么在DLI控制台中查询不到表?
- OBS表压缩率较高怎么办?
- 字符码不一致导致数据乱码怎么办?
- 删除表后再重新创建同名的表,需要对操作该表的用户和项目重新赋权吗?
- DLI分区内表导入的文件不包含分区列的数据,导致数据导入完成后查询表数据失败怎么办?
- 创建OBS外表,由于OBS文件中的某字段存在换行符导致表字段数据错误怎么办?
- join表时没有添加on条件,造成笛卡尔积查询,导致队列资源爆满,作业运行失败怎么办?
- 手动在OBS表的分区目录下添加了数据,但是无法查询到数据怎么办?
- 为什么insert overwrite覆盖分区表数据的时候,覆盖了全量数据?
- 跨源连接RDS表中create_date字段类型是datetime,为什么DLI中查出来的是时间戳呢?
- SQL作业执行完成后,修改表名导致datasize不正确怎么办?
- 从DLI导入数据到OBS,数据量不一致怎么办?
-
增强型跨源连接类
- 增强型跨源连接绑定队列失败怎么办?
- DLI增强型跨源连接DWS失败怎么办?
- 创建跨源成功但测试网络连通性失败怎么办?
- 怎样配置DLI队列与数据源的网络连通?
- 为什么DLI增强型跨源连接要创建对等连接?
- DLI创建跨源连接,绑定队列一直在创建中怎么办?
- 新建跨源连接,显示已激活,但使用时提示communication link failure错误怎么办?
- 跨源访问MRS HBase,连接超时,日志未打印错误怎么办?
- DLI跨源连接报错找不到子网怎么办?
- 跨源RDS表,执行insert overwrite提示Incorrect string value错误怎么办?
- 创建RDS跨源表提示空指针错误怎么办?
- 对跨源DWS表执行insert overwrite操作,报错:org.postgresql.util.PSQLException: ERROR: tuple concurrently updated
- 通过跨源表向CloudTable Hbase表导入数据,executor报错:RegionTooBusyException
- 通过DLI跨源写DWS表,非空字段出现空值异常怎么办?
- 更新跨源目的端源表后,未同时更新对应跨源表,导致insert作业失败怎么办?
- RDS表有自增主键时怎样在DLI插入数据?
-
SQL作业类
- SQL作业开发类
-
SQL作业运维类
- 用户导表到OBS报“path obs://xxx already exists”错误
- 对两个表进行join操作时,提示:SQL_ANALYSIS_ERROR: Reference 't.id' is ambiguous, could be: t.id, t.id.;
- 执行查询语句报错:The current account does not have permission to perform this operation,the current account was restricted. Restricted for no budget.
- 执行查询语句报错:There should be at least one partition pruning predicate on partitioned table XX.YYY
- LOAD数据到OBS外表报错:IllegalArgumentException: Buffer size too small. size
- SQL作业运行报错:DLI.0002 FileNotFoundException
- 用户通过CTAS创建hive表报schema解析异常错误
- 在DataArts Studio上运行DLI SQL脚本,执行结果报org.apache.hadoop.fs.obs.OBSIOException错误
- 使用CDM迁移数据到DLI,迁移作业日志上报UQUERY_CONNECTOR_0001:Invoke DLI service api failed错误
- SQL作业访问报错:File not Found
- SQL作业访问报错:DLI.0003: AccessControlException XXX
- SQL作业访问外表报错:DLI.0001: org.apache.hadoop.security.AccessControlException: verifyBucketExists on {{桶名}}: status [403]
- 执行SQL语句报错:The current account does not have permission to perform this operation,the current account was restricted. Restricted for no budget.
-
Flink作业类
- Flink作业咨询类
-
Flink SQL作业类
- 怎样将OBS表映射为DLI的分区表?
- Flink SQL作业Kafka分区数增加或减少,怎样不停止Flink作业实现动态感知?
- 在Flink SQL作业中创建表使用EL表达式,作业运行提示DLI.0005错误怎么办?
- Flink作业输出流写入数据到OBS,通过该OBS文件路径创建的DLI表查询无数据
- Flink SQL作业运行失败,日志中有connect to DIS failed java.lang.IllegalArgumentException: Access key cannot be null错误
- Flink SQL作业消费Kafka后sink到es集群,作业执行成功,但未写入数据
- Flink Opensource SQL如何解析复杂嵌套 JSON?
- Flink Opensource SQL从RDS数据库读取的时间和RDS数据库存储的时间为什么会不一致?
- Flink Opensource SQL Elasticsearch结果表failure-handler参数填写retry_rejected导致提交失败
- Kafka Sink配置发送失败重试机制
- 如何在一个Flink作业中将数据写入到不同的Elasticsearch集群中?
- 作业语义检验时提示DIS通道不存在怎么处理?
- Flink jobmanager日志一直报Timeout expired while fetching topic metadata怎么办?
- Flink Jar作业类
- Flink作业性能调优类
-
Spark作业相类
- Spark作业开发类
-
Spark作业运维类
- 运行Spark作业报java.lang.AbstractMethodError
- Spark作业访问OBS数据时报ResponseCode: 403和ResponseStatus: Forbidden错误
- 有访问OBS对应的桶的权限,但是Spark作业访问时报错 verifyBucketExists on XXXX: status [403]
- Spark作业运行大批量数据时上报作业运行超时异常错误
- 使用Spark作业访问sftp中的文件,作业运行失败,日志显示访问目录异常
- 执行作业的用户数据库和表权限不足导致作业运行失败
- 为什么Spark3.x的作业日志中打印找不到global_temp数据库
- 在使用Spark2.3.x访问元数据时,DataSource语法创建avro类型的OBS表创建失败
- DLI资源配额类
- DLI权限管理类
- DLI API类
- 视频帮助
-
更多文档
- 用户指南(阿布扎比区域)
- API参考(阿布扎比区域)
-
SQL语法参考(阿布扎比区域)
-
Spark SQL语法参考
- 批作业SQL常用配置项说明
- 批作业SQL语法概览
- Spark开源命令支持说明
- 数据库
- 创建OBS表
- 创建DLI表
- 删除表
- 查看表
- 修改表
- 分区表相关
- 导入数据
- 插入数据
- 清空数据
- 导出查询结果
- 表生命周期管理
- 跨源连接HBase表
- 跨源连接OpenTSDB表
- 跨源连接DWS表
- 跨源连接RDS表
- 跨源连接CSS表
- 跨源连接DCS表
- 跨源连接DDS表
- 跨源连接Oracle表
- 视图
- 查看计划
- 数据权限管理
- 数据类型
- 自定义函数
-
内置函数
-
日期函数
- 日期函数概览
- add_months
- current_date
- current_timestamp
- date_add
- dateadd
- date_sub
- date_format
- datediff
- datediff1
- datepart
- datetrunc
- day/dayofmonth
- from_unixtime
- from_utc_timestamp
- getdate
- hour
- isdate
- last_day
- lastday
- minute
- month
- months_between
- next_day
- quarter
- second
- to_char
- to_date
- to_date1
- to_utc_timestamp
- trunc
- unix_timestamp
- weekday
- weekofyear
- year
-
字符串函数
- 字符串函数概览
- ascii
- concat
- concat_ws
- char_matchcount
- encode
- find_in_set
- get_json_object
- instr
- instr1
- initcap
- keyvalue
- length
- lengthb
- levenshtein
- locate
- lower/lcase
- lpad
- ltrim
- parse_url
- printf
- regexp_count
- regexp_extract
- replace
- regexp_replace
- regexp_replace1
- regexp_instr
- regexp_substr
- repeat
- reverse
- rpad
- rtrim
- soundex
- space
- substr/substring
- substring_index
- split_part
- translate
- trim
- upper/ucase
- 数学函数
- 聚合函数
- 分析窗口函数
- 其他函数
-
日期函数
- SELECT基本语句
- 过滤SELECT
- 排序SELECT
- 分组SELECT
- 连接操作SELECT
- 子查询
- 别名SELECT
- 集合运算SELECT
- WITH...AS
- CASE...WHEN
- OVER子句
- Flink Opensource SQL1.12语法参考
- Flink Opensource SQL1.10语法参考
- 历史版本
-
标示符
- aggregate_func
- alias
- attr_expr
- attr_expr_list
- attrs_value_set_expr
- boolean_expression
- col
- col_comment
- col_name
- col_name_list
- condition
- condition_list
- cte_name
- data_type
- db_comment
- db_name
- else_result_expression
- file_format
- file_path
- function_name
- groupby_expression
- having_condition
- input_expression
- join_condition
- non_equi_join_condition
- number
- partition_col_name
- partition_col_value
- partition_specs
- property_name
- property_value
- regex_expression
- result_expression
- select_statement
- separator
- sql_containing_cte_name
- sub_query
- table_comment
- table_name
- table_properties
- table_reference
- when_expression
- where_condition
- window_function
- 运算符
-
Spark SQL语法参考
- 用户指南(巴黎区域)
- API参考 (巴黎区域)
-
SQL语法参考(巴黎区域)
-
Spark SQL语法参考
- 批作业SQL常用配置项说明
- 批作业SQL语法概览
- Spark开源命令支持说明
- 数据库
- 创建OBS表
- 创建DLI表
- 删除表
- 查看表
- 修改表
- 分区表相关
- 导入数据
- 插入数据
- 清空数据
- 导出查询结果
- 表生命周期管理
- 跨源连接HBase表
- 跨源连接OpenTSDB表
- 跨源连接DWS表
- 跨源连接RDS表
- 跨源连接CSS表
- 跨源连接DCS表
- 跨源连接DDS表
- 跨源连接Oracle表
- 视图
- 查看计划
- 数据权限管理
- 数据类型
- 自定义函数
-
内置函数
-
日期函数
- 日期函数概览
- add_months
- current_date
- current_timestamp
- date_add
- dateadd
- date_sub
- date_format
- datediff
- datediff1
- datepart
- datetrunc
- day/dayofmonth
- from_unixtime
- from_utc_timestamp
- getdate
- hour
- isdate
- last_day
- lastday
- minute
- month
- months_between
- next_day
- quarter
- second
- to_char
- to_date
- to_date1
- to_utc_timestamp
- trunc
- unix_timestamp
- weekday
- weekofyear
- year
-
字符串函数
- 字符串函数概览
- ascii
- concat
- concat_ws
- char_matchcount
- encode
- find_in_set
- get_json_object
- instr
- instr1
- initcap
- keyvalue
- length
- lengthb
- levenshtein
- locate
- lower/lcase
- lpad
- ltrim
- parse_url
- printf
- regexp_count
- regexp_extract
- replace
- regexp_replace
- regexp_replace1
- regexp_instr
- regexp_substr
- repeat
- reverse
- rpad
- rtrim
- soundex
- space
- substr/substring
- substring_index
- split_part
- translate
- trim
- upper/ucase
- 数学函数
- 聚合函数
- 分析窗口函数
- 其他函数
-
日期函数
- SELECT基本语句
- 过滤SELECT
- 排序SELECT
- 分组SELECT
- 连接操作SELECT
- 子查询
- 别名SELECT
- 集合运算SELECT
- WITH...AS
- CASE...WHEN
- OVER子句
- Flink Opensource SQL1.12语法参考
- Flink Opensource SQL1.10语法参考
- 历史版本
-
标示符
- aggregate_func
- alias
- attr_expr
- attr_expr_list
- attrs_value_set_expr
- boolean_expression
- col
- col_comment
- col_name
- col_name_list
- condition
- condition_list
- cte_name
- data_type
- db_comment
- db_name
- else_result_expression
- file_format
- file_path
- function_name
- groupby_expression
- having_condition
- input_expression
- join_condition
- non_equi_join_condition
- number
- partition_col_name
- partition_col_value
- partition_specs
- property_name
- property_value
- regex_expression
- result_expression
- select_statement
- separator
- sql_containing_cte_name
- sub_query
- table_comment
- table_name
- table_properties
- table_reference
- when_expression
- where_condition
- window_function
- 运算符
-
Spark SQL语法参考
- 用户指南(吉隆坡区域)
- API参考(吉隆坡区域)
-
SQL语法参考(吉隆坡区域)
-
Spark SQL语法参考
- 批作业SQL常用配置项说明
- 批作业SQL语法概览
- Spark开源命令支持说明
- 数据库
- 创建OBS表
- 创建DLI表
- 删除表
- 查看表
- 修改表
- 分区表相关
- 导入数据
- 插入数据
- 清空数据
- 导出查询结果
- 多版本备份恢复数据
- 表生命周期管理
- 跨源连接HBase表
- 跨源连接OpenTSDB表
- 跨源连接DWS表
- 跨源连接RDS表
- 跨源连接CSS表
- 跨源连接DCS表
- 跨源连接DDS表
- 跨源连接Oracle表
- 视图
- 查看计划
- 数据权限管理
- 数据类型
- 自定义函数
-
内置函数
-
日期函数
- 日期函数概览
- add_months
- current_date
- current_timestamp
- date_add
- dateadd
- date_sub
- date_format
- datediff
- datediff1
- datepart
- datetrunc
- day/dayofmonth
- from_unixtime
- from_utc_timestamp
- getdate
- hour
- isdate
- last_day
- lastday
- minute
- month
- months_between
- next_day
- quarter
- second
- to_char
- to_date
- to_date1
- to_utc_timestamp
- trunc
- unix_timestamp
- weekday
- weekofyear
- year
-
字符串函数
- 字符串函数概览
- ascii
- concat
- concat_ws
- char_matchcount
- encode
- find_in_set
- get_json_object
- instr
- instr1
- initcap
- keyvalue
- length
- lengthb
- levenshtein
- locate
- lower/lcase
- lpad
- ltrim
- parse_url
- printf
- regexp_count
- regexp_extract
- replace
- regexp_replace
- regexp_replace1
- regexp_instr
- regexp_substr
- repeat
- reverse
- rpad
- rtrim
- soundex
- space
- substr/substring
- substring_index
- split_part
- translate
- trim
- upper/ucase
- 数学函数
- 聚合函数
- 分析窗口函数
- 其他函数
-
日期函数
- SELECT基本语句
- 过滤SELECT
- 排序SELECT
- 分组SELECT
- 连接操作SELECT
- 子查询
- 别名SELECT
- 集合运算SELECT
- WITH...AS
- CASE...WHEN
- OVER子句
- Flink Opensource SQL1.12语法参考
- Flink Opensource SQL1.10语法参考
- 历史版本
-
标示符
- aggregate_func
- alias
- attr_expr
- attr_expr_list
- attrs_value_set_expr
- boolean_expression
- col
- col_comment
- col_name
- col_name_list
- condition
- condition_list
- cte_name
- data_type
- db_comment
- db_name
- else_result_expression
- file_format
- file_path
- function_name
- groupby_expression
- having_condition
- input_expression
- join_condition
- non_equi_join_condition
- number
- partition_col_name
- partition_col_value
- partition_specs
- property_name
- property_value
- regex_expression
- result_expression
- select_statement
- separator
- sql_containing_cte_name
- sub_query
- table_comment
- table_name
- table_properties
- table_reference
- when_expression
- where_condition
- window_function
- 运算符
-
Spark SQL语法参考
- 通用参考
链接复制成功!
使用Flink Jar连接开启SASL_SSL认证的Kafka
概述
本节操作介绍使用Flink Jar连接开启SASL_SSL认证的Kafka的操作方法。
如需使用Flink OpenSource SQL连接开启SASL_SSL认证的Kafka,请参考Flink SQL语法参考-Kafka源表。
环境准备
- 已在DLI控制台购买了通用队列。
- 已购买了Kafka实例,并开启了SASL_SSL认证。
- 已在DLI创建增强型跨源并绑定队列,确保DLI队列和Kafka连通。
操作须知
- 连接带SASL_SSL的Kafka,无论是消费者还是生产者,在对应的properties中都需要指定truststore文件的路径。
- 初始化consumer/producer都是在taskmanager里执行的,所以需要获取到taskmanager对应container下truststore文件的路径,在初始化前将其引入properties中才能生效。
- kafka source可以在open里引入。
图1 获取kafka source
- kafka sink可以在initializeState里引入。
图2 获取kafka sink
操作步骤
- 从Kafka实例的基本信息页面下载SSL证书,解压后将其中的clinet.jks文件上传到OBS。
图3 下载SSL证书
- 在DLI控制台,打开“数据管理 > 程序包管理”,单击“创建”,创建clinet.jks对应的程序包。
主要参数的填写说明:
- 包类型:File
- OBS路径:clinet.jks所在的OBS路径。
- 分组名称:自定义分组或选择已有的分组名称。
图4 创建clinet.jks
- 将示例代码打包,在DLI控制台,打开“数据管理 > 程序包管理”,单击“创建”,创建Flink Jar对应的程序包。代码样例请参考Kafaka ToKafakaExample.java样例、•SinkKafkaProducer.java样...、SourceKafkaConsumer.java...。
主要参数的填写说明:
- 包类型:JAR
- OBS路径:Flink Jar所在的OBS路径。
- 分组名称:自定义分组或选择已有的分组名称。
图5 创建Flink Jar对应的程序包 - 在DLI控制台,打开“数据管理 > 程序包管理”,单击“创建”,创建KafkaToKafka.properties对应的程序包。代码样例请参考•KafkaToKafkaExample.pro...。
主要参数的填写说明:
- 包类型:File
- OBS路径:KafkaToKafka.properties所在的OBS路径。
- 分组名称:自定义分组或选择已有的分组名称。
图6 创建KafkaToKafka.properties程序包 - 创建Flink Jar作业并运行。
创建Flink Jar作业,在应用程序、其他依赖文件选择步骤3导入Flink Jar,并指定主类。
主要参数的填写说明:
- 所属队列:选择Flink Jar作业运行的队列。
- 应用程序:自定义的程序包
- 主类:指定
- 类名:输入类名并确定类参数列表(参数间用空格分隔)。
- 其他依赖文件:自定义的依赖文件。选择2和4导入的jks和properties文件。
- Flink版本:1.10
图7 创建Flink Jar作业 - 结果校验。
作业处于运行中状态时,向kafka source.topic发送数据,验证kafka sink.topic能否收到数据。
图8 查看作业任务图9 查看kafka sink.topic
JAVA样例代码
- pom文件配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>Flink-demo</artifactId> <groupId>com.huaweicloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>flink-kafka-to-obs</artifactId> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!--Flink 版本--> <flink.version>1.12.2</flink.version> <!--JDK 版本--> <java.version>1.8</java.version> <!--Scala 2.11 版本--> <scala.binary.version>2.11</scala.binary.version> <slf4j.version>2.13.3</slf4j.version> <log4j.version>2.10.0</log4j.version> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <!-- flink --> <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-java</artifactId> <version>${flink.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-streaming-java_${scala.binary.version}</artifactId> <version>${flink.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-statebackend-rocksdb_2.11</artifactId> <version>${flink.version}</version> <scope>provided</scope> </dependency> <!-- kafka --> <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-connector-kafka_2.11</artifactId> <version>${flink.version}</version> </dependency> <!-- logging --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>${slf4j.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>${log4j.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>${log4j.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-jcl</artifactId> <version>${log4j.version}</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.3.0</version> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> <configuration> <archive> <manifest> <mainClass>com.huaweicloud.dli.FlinkKafkaToObsExample</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </plugins> <resources> <resource> <directory>../main/config</directory> <filtering>true</filtering> <includes> <include>**/*.*</include> </includes> </resource> </resources> </build> </project>
- KafkaToKafkaExample.java样例
userData为固定文件路径名,不支持修改或自定义其他路径名。
package kafka_to_kafka; import org.apache.flink.api.common.serialization.SimpleStringSchema; import org.apache.flink.api.java.utils.ParameterTool; import org.apache.flink.contrib.streaming.state.RocksDBStateBackend; import org.apache.flink.streaming.api.datastream.DataStream; import org.apache.flink.streaming.api.environment.CheckpointConfig; import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; import org.apache.kafka.clients.consumer.ConsumerConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Properties; public class KafkaToKafkaExample { private static final Logger LOG = LoggerFactory.getLogger(KafkaToKafkaExample.class); public static void main(String[] args) throws Exception { LOG.info("Start Kafka2Kafka Flink Streaming Source Java Demo."); String propertiesPath = KafkaToKafkaExample.class.getClassLoader() .getResource("userData/KafkaToKafka.properties").getPath(); ParameterTool params = ParameterTool.fromPropertiesFile(propertiesPath); LOG.info("Params: " + params.toString()); // Kafka连接地址 String bootstrapServers; // Kafka消费组 String kafkaGroup; // Kafka topic String sourceTopic; String sinkTopic; // 消费策略,只有当分区没有Checkpoint或者Checkpoint过期时,才会使用此配置的策略; // 如果存在有效的Checkpoint,则会从此Checkpoint开始继续消费 // 取值有: LATEST,从最新的数据开始消费,此策略会忽略通道中已有数据 // EARLIEST,从最初的数据开始消费,此策略会获取通道中所有的有效数据 String offsetPolicy; // SASL_SSL相关配置项。设置jaas账号和密码,username和password为创建Kafka实例过程中开启SASL_SSL时填入的用户名和密码, // 或者创建SASL_SSL用户时设置的用户名和密码。格式如下, // org.apache.kafka.common.security.plain.PlainLoginModule required // username="yourUsername" // password="yourPassword"; String saslJaasConfig; // Checkpoint输出路径,格式obs://bucket/path String checkpointPath; bootstrapServers = params.get("bootstrap.servers", "xxx:9093,xxx:9093,xxx:9093"); kafkaGroup = params.get("source.group", "test-group"); sourceTopic = params.get("source.topic", "test-source-topic"); sinkTopic = params.get("sink.topic", "test-sink-topic"); offsetPolicy = params.get("offset.policy", "earliest"); saslJaasConfig = params.get("sasl.jaas.config", "org.apache.kafka.common.security.plain.PlainLoginModule " + "required\nusername=\"yourUsername\"\npassword=\"yourPassword\";"); checkpointPath = params.get("checkpoint.path", "obs://bucket/path"); try { // 创建执行环境 StreamExecutionEnvonment streamEnv = StreamExecutionEnvironment.getExecutionEnvironment(); RocksDBStateBackend rocksDbBackend = new RocksDBStateBackend(checkpointPath, true); streamEnv.setStateBackend(rocksDbBackend); // 开启Flink CheckPoint配置,开启时若触发CheckPoint,会将Offset信息同步到Kafka streamEnv.enableCheckpointing(300000); // 设置两次checkpoint的最小间隔时间 streamEnv.getCheckpointConfig().setMinPauseBetweenCheckpoints(60000); // 设置checkpoint超时时间 streamEnv.getCheckpointConfig().setCheckpointTimeout(60000); // 设置checkpoint最大并发数 streamEnv.getCheckpointConfig().setMaxConcurrentCheckpoints(1); // 设置作业取消时保留checkpoint streamEnv.getCheckpointConfig().enableExternalizedCheckpoints( CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION); // Source: 连接kafka数据源 Properties sourceProperties = new Properties(); sourceProperties.setProperty("bootstrap.servers", bootstrapServers); sourceProperties.setProperty("group.id", kafkaGroup); sourceProperties.setProperty("sasl.jaas.config", saslJaasConfig); sourceProperties.setProperty("sasl.mechanism", "PLAIN"); sourceProperties.setProperty("security.protocol", "SASL_SSL"); sourceProperties.setProperty("ssl.truststore.password", "dms@kafka"); sourceProperties.setProperty("ssl.endpoint.identification.algorithm", ""); sourceProperties.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, offsetPolicy); // 创建kafka consumer SourceKafkaConsumer<String> kafkaConsumer = new SourceKafkaConsumer<>(sourceTopic, new SimpleStringSchema(), sourceProperties); /** * 从 Kafka brokers 中的 consumer 组(consumer 属性中的 group.id 设置)提交的偏移量中开始读取分区。 * 如果找不到分区的偏移量,那么将会使用配置中的 auto.offset.reset 设置。 * 详情 https://ci.apache.org/projects/flink/flink-docs-release-1.13/zh/docs/connectors/datastream/kafka/ */ kafkaConsumer.setStartFromGroupOffsets(); //将kafka 加入数据源 DataStream<String> stream = streamEnv.addSource(kafkaConsumer).name("kafka_source").setParallelism(1).disableChaining(); // Sink: 连接kafka数据汇 Properties sinkProperties = new Properties(); sinkProperties.setProperty("bootstrap.servers", bootstrapServers); sinkProperties.setProperty("sasl.jaas.config", saslJaasConfig); sinkProperties.setProperty("sasl.mechanism", "PLAIN"); sinkProperties.setProperty("security.protocol", "SASL_SSL"); sinkProperties.setProperty("ssl.truststore.password", "dms@kafka"); sinkProperties.setProperty("ssl.endpoint.identification.algorithm", ""); // 创建kafka producer SinkKafkaProducer<String> kafkaProducer = new SinkKafkaProducer<>(sinkTopic, new SimpleStringSchema(), sinkProperties); //将kafka 加入数据汇 stream.addSink(kafkaProducer).name("kafka_sink").setParallelism(1).disableChaining(); // stream.print(); streamEnv.execute(); } catch (Exception e) { LOG.error(e.getMessage(), e); } } }
- SinkKafkaProducer.java样例
userData为固定文件路径名,不支持修改或自定义其他路径名。
package kafka_to_kafka; import org.apache.flink.api.common.serialization.SerializationSchema; import org.apache.flink.runtime.state.FunctionInitializationContext; import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer; import java.util.Properties; public class SinkKafkaProducer<IN> extends FlinkKafkaProducer<IN>{ public SinkKafkaProducer(String topicId, SerializationSchema<IN> serializationSchema, Properties producerConfig) { super(topicId, serializationSchema, producerConfig); } @Override public void initializeState(FunctionInitializationContext context) throws Exception { String jksPath = SinkKafkaProducer.class.getClassLoader().getResource("userData/client.jks").getPath(); producerConfig.setProperty("ssl.truststore.location", jksPath); super.initializeState(context); } }
- SourceKafkaConsumer.java样例
userData为固定文件路径名,不支持修改或自定义其他路径名。
package kafka_to_kafka; import org.apache.flink.api.common.serialization.DeserializationSchema; import org.apache.flink.configuration.Configuration; import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer; import java.util.Properties; public class SourceKafkaConsumer<T> extends FlinkKafkaConsumer<T> { public SourceKafkaConsumer(String topic, DeserializationSchema<T> valueDeserializer, Properties props) { super(topic, valueDeserializer, props); } @Override public void open(Configuration configuration) throws Exception { String jksPath = SourceKafkaConsumer.class.getClassLoader().getResource("userData/client.jks").getPath(); super.properties.setProperty("ssl.truststore.location", jksPath); super.open(configuration); } }
- KafkaToKafkaExample.properties样例
bootstrap.servers=xxx:9093,xxx:9093,xxx:9093 checkpoint.path=obs://bucket/path source.group=swq-test-group source.topic=topic-swq sink.topic=topic-swq-out sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required\n\ username="xxxx"\n\ password="xxxx";
父主题: Flink作业开发指南