- 最新动态
- 功能总览
- 服务公告
- 产品介绍
- 计费说明
- 快速入门
-
用户指南
- 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语法参考
- 通用参考
链接复制成功!
scala样例代码
开发说明
- 前提条件
在DLI管理控制台上已完成创建跨源连接并绑定队列。具体操作请参考《数据湖探索用户指南》。
说明:
认证用的password硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全。
- 构造依赖信息,创建SparkSession
- 导入依赖
1 2 3 4 5
<dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-sql_2.11</artifactId> <version>2.3.2</version> </dependency>
import相关依赖包1 2 3
import java.util.Properties import org.apache.spark.sql.{Row,SparkSession} import org.apache.spark.sql.SaveMode
- 创建会话。
1
val sparkSession = SparkSession.builder().getOrCreate()
- 导入依赖
- 通过SQL API 访问
- 创建DLI跨源访问 rds的关联表,填写连接参数。
1 2 3 4 5 6 7
sparkSession.sql( "CREATE TABLE IF NOT EXISTS dli_to_rds USING JDBC OPTIONS ( 'url'='jdbc:mysql://to-rds-1174404209-cA37siB6.datasource.com:3306', //根据实际url修改 'dbtable'='test.customer', 'user'='root', //根据实际user修改 'password'='######', //根据实际password修改 'driver'='com.mysql.jdbc.Driver')")
表1 创建表参数 参数
说明
url
RDS的连接地址,需要先创建跨源连接,管理控制台操作请参考《数据湖探索用户指南》。
创建增强型跨源连接后,使用RDS提供的"内网域名"或者内网地址和数据库端口访问,MySQL格式为"协议头://内网IP:内网端口",PostGre格式为"协议头://内网IP:内网端口/数据库名"。
例如:"jdbc:mysql://192.168.0.193:3306"或者"jdbc:postgresql://192.168.0.193:3306/postgres",获取方式请参考“图 RDS集群信息”。
dbtable
访问MySQL集群填写"数据库名.表名",访问PostGre集群填写"模式名.表名"。
说明:
如果数据库和表不存在,请先创建数据库和表,否则系统会报错并且运行失败。
user
RDS数据库用户名。
password
RDS数据库用户名对应密码。
driver
jdbc驱动类名,访问MySQL集群请填写:"com.mysql.jdbc.Driver",访问PostGre集群请填写:"org.postgresql.Driver"。
partitionColumn
读取数据时,用于设置并发使用的数值型字段。
说明:
- “partitionColumn”,“lowerBound”,“upperBound”,“numPartitions”4个参数必须同时设置,不支持仅设置其中一部分
- 为了提升并发读取的性能,建议使用自增列。
lowerBound
partitionColumn设置的字段数据最小值,该值包含在返回结果中。
upperBound
partitionColumn设置的字段数据最大值,该值不包含在返回结果中。
numPartitions
读取数据时并发数。
说明:
实际读取数据时,会根据lowerBound与upperBound,平均分配给每个task获取其中一部分的数据。例如:
'partitionColumn'='id',
'lowerBound'='0',
'upperBound'='100',
'numPartitions'='2'
DLI中会起2个并发task,一个task执行id>=0 and id < 50,另一个task执行id >=50 and id < 100。
fetchsize
读取数据时,每一批次获取数据的记录数,默认值1000。设置越大性能越好,但占用内存越多,该值设置过大会有内存溢出的风险。
batchsize
写入数据时,每一批次写入数据的记录数,默认值1000。设置越大性能越好,但占用内存越多,该值设置过大会有内存溢出的风险。
truncate
执行overwrite时是否不删除原表,直接执行清空表操作,取值范围:
- true
- false
默认为'false',即在执行overwrite操作时,先将原表删除再重新建表。
isolationLevel
事务隔离级别,取值范围:
- NONE
- READ_UNCOMMITTED
- READ_COMMITTED
- REPEATABLE_READ
- SERIALIZABLE
默认值为'READ_UNCOMMITTED'。
图1 RDS集群信息 - 插入数据
1
sparkSession.sql("insert into dli_to_rds values(1, 'John',24),(2, 'Bob',32)")
- 查询数据
1 2
val dataFrame = sparkSession.sql("select * from dli_to_rds") dataFrame.show()
插入数据前:
插入数据后:
- 删除关联表
1
sparkSession.sql("drop table dli_to_rds")
- 创建DLI跨源访问 rds的关联表,填写连接参数。
- 通过DataFrame API 访问
- 连接参数配置
1 2 3 4
val url = "jdbc:mysql://to-rds-1174405057-EA1Kgo8H.datasource.com:3306" val username = "root" val password = "######" val dbtable = "test.customer"
- 创建DataFrame,添加数据,并重命名字段。
1 2 3 4
var dataFrame_1 = sparkSession.createDataFrame(List((8, "Jack_1", 18))) val df = dataFrame_1.withColumnRenamed("_1", "id") .withColumnRenamed("_2", "name") .withColumnRenamed("_3", "age")
- 导入数据到RDS。
1 2 3 4 5 6 7 8
df.write.format("jdbc") .option("url", url) .option("dbtable", dbtable) .option("user", username) .option("password", password) .option("driver", "com.mysql.jdbc.Driver") .mode(SaveMode.Append) .save()
说明:
SaveMode 有四种保存类型:
- ErrorIfExis:如果已经存在数据,则抛出异常。
- Overwrite:如果已经存在数据,则覆盖原数据。
- Append:如果已经存在数据,则追加保存。
- Ignore:如果已经存在数据,则不做操作。这类似于SQL中的“如果不存在则创建表”。
- 读取RDS上的数据。
- 方式一:read.format()方法
1 2 3 4 5 6 7
val jdbcDF = sparkSession.read.format("jdbc") .option("url", url) .option("dbtable", dbtable) .option("user", username) .option("password", password) .option("driver", "org.postgresql.Driver") .load()
- 方式二:read.jdbc()方法
1 2 3 4
val properties = new Properties() properties.put("user", username) properties.put("password", password) val jdbcDF2 = sparkSession.read.jdbc(url, dbtable, properties)
插入数据前:
插入数据后:
使用上述read.format()或者read.jdbc()方法读取到的dataFrame注册为临时表,就可使用sql语句进行数据查询了。
1 2
jdbcDF.registerTempTable("customer_test") sparkSession.sql("select * from customer_test where id = 1").show()
查询结果:
- 方式一:read.format()方法
- 连接参数配置
- DataFrame相关操作
createDataFrame() 方法创建的数据和read.format() 方法及read.jdbc() 方法查询的数据都为DataFrame对象,可以直接进行查询单条记录等操作(在“步骤4”中,提到将DataFrame数据注册为临时表)。
- where
where 方法中可传入包含and 和 or 的条件筛选表达式,返回过滤后的DataFrame对象,示例如下:
1
jdbcDF.where("id = 1 or age <=10").show()
- filter
filter同where的使用方式一致,传入条件筛选表达式,返回过滤后的结果 。示例如下:
1
jdbcDF.filter("id = 1 or age <=10").show()
- select
传入待查询的字段,返回指定字段的DataFrame对象,并且可多个字段查询,示例如下:
- 示例1:
1
jdbcDF.select("id").show()
- 示例2:
1
jdbcDF.select("id", "name").show()
- 示例3:
1
jdbcDF.select("id","name").where("id<4").show()
- 示例1:
- selectExpr
对字段进行特殊处理。例如,可使用selectExpr修改字段名。示例如下:
将name字段取名name_test,age数据加1。
1
jdbcDF.selectExpr("id", "name as name_test", "age+1").show()
- col
获取指定字段。不同于select,col每次只能获取一个字段,返回类型为Column类型,示例如下:
1
val idCol = jdbcDF.col("id")
- drop
删除指定字段。传入要删除的字段,返回不包含此字段的DataFrame对象,并且每次只能删除一个字段,示例如下:
1
jdbcDF.drop("id").show()
- where
- 提交Spark作业
- 将写好的代码生成jar包,上传至OBS桶中。
- 在Spark作业编辑器中选择对应的Module模块并执行Spark作业。
说明:
- 如果选择spark版本为2.3.2(即将下线)或2.4.5提交作业时,需要指定Module模块,名称为:sys.datasource.rds。
- 如果选择Spark版本为3.1.1及以上版本时,无需选择Module模块, 需在 'Spark参数(--conf)' 配置
spark.driver.extraClassPath=/usr/share/extension/dli/spark-jar/datasource/rds/*
spark.executor.extraClassPath=/usr/share/extension/dli/spark-jar/datasource/rds/*
- 通过控制台提交作业请参考《数据湖探索用户指南》中的“选择依赖资源参数说明”。
- 通过API提交作业请参考《数据湖探索API参考》>《创建批处理作业》中“表2-请求参数说明”关于“modules”参数的说明。
完整示例代码
- Maven依赖
1 2 3 4 5
<dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-sql_2.11</artifactId> <version>2.3.2</version> </dependency>
- 通过SQL API访问
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
import java.util.Properties import org.apache.spark.sql.SparkSession object Test_SQL_RDS { def main(args: Array[String]): Unit = { // Create a SparkSession session. val sparkSession = SparkSession.builder().getOrCreate() // Create a data table for DLI-associated RDS sparkSession.sql("CREATE TABLE IF NOT EXISTS dli_to_rds USING JDBC OPTIONS ( 'url'='jdbc:mysql://to-rds-1174404209-cA37siB6.datasource.com:3306, 'dbtable'='test.customer', 'user'='root', 'password'='######', 'driver'='com.mysql.jdbc.Driver')") //*****************************SQL model*********************************** //Insert data into the DLI data table sparkSession.sql("insert into dli_to_rds values(1,'John',24),(2,'Bob',32)") //Read data from DLI data table val dataFrame = sparkSession.sql("select * from dli_to_rds") dataFrame.show() //drop table sparkSession.sql("drop table dli_to_rds") sparkSession.close() } }
- 通过DataFrame API访问
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
import java.util.Properties import org.apache.spark.sql.SparkSession import org.apache.spark.sql.SaveMode object Test_SQL_RDS { def main(args: Array[String]): Unit = { // Create a SparkSession session. val sparkSession = SparkSession.builder().getOrCreate() //*****************************DataFrame model*********************************** // Set the connection configuration parameters. Contains url, username, password, dbtable. val url = "jdbc:mysql://to-rds-1174404209-cA37siB6.datasource.com:3306" val username = "root" val password = "######" val dbtable = "test.customer" // Create a DataFrame and initialize the DataFrame data. var dataFrame_1 = sparkSession.createDataFrame(List((1, "Jack", 18))) // Rename the fields set by the createDataFrame() method. val df = dataFrame_1.withColumnRenamed("_1", "id") .withColumnRenamed("_2", "name") .withColumnRenamed("_3", "age") // Write data to the rds_table_1 table df.write.format("jdbc") .option("url", url) .option("dbtable", dbtable) .option("user", username) .option("password", password) .option("driver", "com.mysql.jdbc.Driver") .mode(SaveMode.Append) .save() // DataFrame object for data manipulation //Filter users with id=1 var newDF = df.filter("id!=1") newDF.show() // Filter the id column data var newDF_1 = df.drop("id") newDF_1.show() // Read the data of the customer table in the RDS database // Way one:Read data from RDS using read.format() val jdbcDF = sparkSession.read.format("jdbc") .option("url", url) .option("dbtable", dbtable) .option("user", username) .option("password", password) .option("driver", "com.mysql.jdbc.Driver") .load() // Way two:Read data from RDS using read.jdbc() val properties = new Properties() properties.put("user", username) properties.put("password", password) val jdbcDF2 = sparkSession.read.jdbc(url, dbtable, properties) /** * Register the dateFrame read by read.format() or read.jdbc() as a temporary table, and query the data * using the sql statement. */ jdbcDF.registerTempTable("customer_test") val result = sparkSession.sql("select * from customer_test where id = 1") result.show() sparkSession.close() } }
- DataFrame相关操作
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
// The where() method uses " and" and "or" for condition filters, returning filtered DataFrame objects jdbcDF.where("id = 1 or age <=10").show() // The filter() method is used in the same way as the where() method. jdbcDF.filter("id = 1 or age <=10").show() // The select() method passes multiple arguments and returns the DataFrame object of the specified field. jdbcDF.select("id").show() jdbcDF.select("id", "name").show() jdbcDF.select("id","name").where("id<4").show() /** * The selectExpr() method implements special handling of fields, such as renaming, increasing or * decreasing data values. */ jdbcDF.selectExpr("id", "name as name_test", "age+1").show() // The col() method gets a specified field each time, and the return type is a Column type. val idCol = jdbcDF.col("id") /** * The drop() method returns a DataFrame object that does not contain deleted fields, and only one field * can be deleted at a time. */ jdbcDF.drop("id").show()