mysql通过创建存储过程遍历json数组

本文介绍如何在MySQL中通过创建存储过程来解析JSON数组,提取特定字段,包括topic以及JSON对象fields内的name、type和doc。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

已有数据

topic_namejson
oradb.core4-fcr.user.ebay-users[{"doc": "Rheos header - This is mandatory for every schema in Rheos", "name": "rheosHeader", "type": {"name": "RheosHeader", "type": "record", "fields": [{"name": "eventCreateTimestamp", "type": "long"}, {"name": "eventSentTimestamp", "type": "long"}, {"name": "schemaId", "type": "int"}, {"name": "eventId", "type": ["null", {"type": "string", "avro.java.string": "String"}]}, {"name": "producerId", "type": {"type": "string", "avro.java.string": "String"}}]}}, {"doc": "Raw text to be queued in Rheos, interpretation is solely application's responsiblity.", "name": "payload", "type": "string"}]
tora.in.rtdf.transaction.new[{"doc": "Rheos header - This is mandatory for every schema in Rheos", "name": "rheosHeader", "type": {"name": "RheosHeader", "type": "record", "fields": [{"name": "eventCreateTimestamp", "type": "long"}, {"name": "eventSentTimestamp", "type": "long"}, {"name": "schemaId", "type": "int"}, {"name": "eventId", "type": ["null", {"type": "string", "avro.java.string": "String"}]}, {"name": "producerId", "type": {"type": "string", "avro.java.string": "String"}}]}}, {"doc": "derived from segment", "name": "segment", "type": ["null", {"type": "string", "avro.java.string": "String"}]}, {"doc": "yyyy-MM-dd HH:mm:ss", "name": "runDate", "type": ["null", {"type": "string", "avro.java.string": "String"}]}, {"doc": "user Id", "name": "userId", "type": ["null", {"type": "string", "avro.java.string": "String"}]}, {"doc": "epoch", "name": "eventTimeStamp", "type": ["null", "long"]}, {"doc": "Site ID", "name": "siteId", "type": ["null", "int"], "default": null}, {"doc": "", "name": "dispatchId", "type": ["null", {"type": "string", "avro.java.string": "String"}]}, {"doc": "", "name": "msgData", "type": ["null", {"type": "string", "avro.java.string": "String"}]}, {"doc": "", "name": "email", "type": ["null", {"type": "string", "avro.java.string": "String"}]}, {"doc": "", "name": "templateId", "type": ["null", {"type": "string", "avro.java.string": "String"}]}, {"doc": "", "name": "templateVersion", "type": ["null", {"type": "string", "avro.java.string": "String"}]}, {"doc": "", "name": "campaignId", "type": ["null", {"type": "string", "avro.java.string": "String"}]}, {"doc": "", "name": "campaignCode", "type": ["null", {"type": "string", "avro.java.string": "String"}]}, {"doc": "", "name": "cellCode", "type": ["null", {"type": "string", "avro.java.string": "String"}]}, {"doc": "", "name": "rLogId", "type": ["null", {"type": "string", "avro.java.string": "String"}]}]

要求取出四个字段,topic以及json字符串里的fields里面的name,type,doc

delimiter //
DROP PROCEDURE IF EXISTS proc_json_array;
CREATE PROCEDURE proc_json_array(i_topic_name varchar(100))
BEGIN
declare i int default 0;
declare v_length int(11);-- 接收数组的长度
declare v_name varchar(100);-- 接收数组的角标的变量
declare v_type varchar(100);
declare v_doc varchar(100);
select json_length(json_context) into v_length from topic_test where topic_name=i_topic_name;
DROP temporary TABLE IF EXISTS tmp_t;
create temporary table if not exists tmp_t(-- 创建临时表保存查询的结果集中的需要的数据
name text,
type text,
docs text
);
while i<v_length
do
set v_name=concat("'$[",i,"].name'");
set v_type=concat("'$[",i,"].type'");
set v_doc=concat("'$[",i,"].doc'");

set @sql=concat('insert into tmp_t(name,type,docs) select json_context->',v_name ,',json_context->',v_type ,',json_context->',v_doc ,' from topic_test where topic_name=','"misc.crossdcs.ReACT.Group.ReACT"');
prepare s from @sql;
execute s;
set i=i+1;
end while;
select 'misc.crossdcs.ReACT.Group.ReACT',name,type,'null',0,docs from tmp_t;

END
//

call proc_json_array("tora.in.rtdf.transaction.new")




 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值