You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Databricks中处理JSON列数据的两类验证需求求助

问题描述

在Databricks中有如下表格:

datejson_col
10/05/2025{estudents = [{id_student = 12341, score = 5.23}]}
01/05/2025{estudents = [{id_student = 4325, score = 10}]}
21/02/2025{estudents = [{id_student = 4674, score = 6}]}
11/03/2025{estudents = [{id_student = 6764, score = 3.5}]}

需要完成两个验证任务:

  • 检查是否存在非整数类型的分数
  • 列出id_student字段长度超过4位数字的学生
解决方案

1. 检查非整数分数

先解析JSON列提取学生数据,通过对比score与其整数转换后的值,筛选出非整数分数的记录:

SELECT 
  date,
  json_col,
  student.id_student,
  student.score
FROM 
  your_table_name,
  LATERAL VIEW json_tuple(json_col, 'estudents') jt AS students_arr,
  LATERAL VIEW explode(from_json(students_arr, 'array<struct<id_student:string, score:double>>')) exploded AS student
WHERE 
  student.score != CAST(student.score AS INTEGER);

如果查询返回结果,说明存在非整数分数;无返回结果则所有分数均为整数。

2. 筛选id_student长度超4位的学生

同样解析JSON列后,通过字符串长度判断筛选目标记录:

SELECT 
  date,
  json_col,
  student.id_student,
  student.score
FROM 
  your_table_name,
  LATERAL VIEW json_tuple(json_col, 'estudents') jt AS students_arr,
  LATERAL VIEW explode(from_json(students_arr, 'array<struct<id_student:string, score:double>>')) exploded AS student
WHERE 
  LENGTH(student.id_student) > 4;

注意事项

  • 替换your_table_name为实际表名
  • 若id_student存储为数值类型,可将其转为字符串后判断长度:LENGTH(CAST(student.id_student AS STRING)) > 4
  • from_json的Schema可根据实际数据类型调整,确保解析准确

内容的提问来源于stack exchange,提问作者Julio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 23:50:09