已有数据
topic_name | json |
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")