城乡建设网站职业查询,中国最顶尖的服装设计公司,镇江特色,本地电商平台开发在将 hive 数仓数据写入 MySQL 时候#xff0c;有时我们需将数据转为 json 字符串#xff0c;然后再存入 MySQL。但 hive 数仓中的 null 类型遇到 json 函数之后会变为 ‘null’ 字符串#xff0c;这时我们只需在使用 json 函数之前对值进行判断即可#xff0c;当值为 null…在将 hive 数仓数据写入 MySQL 时候有时我们需将数据转为 json 字符串然后再存入 MySQL。但 hive 数仓中的 null 类型遇到 json 函数之后会变为 ‘null’ 字符串这时我们只需在使用 json 函数之前对值进行判断即可当值为 null 时直接返回 null, 当值非null 时则使用 json 函数
1 正常情况
在 pyspark 中执行如下代码
history_loc_df spark.sql(SELECTuser_id,null as active_points,20230405 as ymdFROM tmp.tmp_user
export_data_mysql(mysql_result_df)将 history_loc_df 数据存入 MySQLnull 数据会为空如下所示
2 null 类型变为 ‘null’ 字符串
使用 to_json 函数之后null 类型会变为 ‘null’ 字符串
def to_json(info):return json.dumps(info)# udf 注册: 转为 json
spark.udf.register(to_json, to_json, StringType())history_loc_df spark.sql(SELECTuser_id,to_json(null) as active_points,20230405 as ymdFROM tmp.tmp_userexport_data_mysql(mysql_result_df)这时将 history_loc_df 数据存入 MySQLnull 数据会变为字符串如下所示
3 在 to_json 之前判断是否为空
若想使用 to_json 函数当遇到 null 值返回 null 类型遇到其它值则转为 json 字符串
只需要在转为 json 字符串之前对值进行判断即可
def to_json(info):return json.dumps(info)# udf 注册: 转为 json
spark.udf.register(to_json, to_json, StringType())history_loc_df spark.sql(SELECTuser_id,if(active_points is null, null, to_json(null)) as active_points,20230405 as ymdFROM tmp.tmp_user
export_data_mysql(mysql_result_df)