如何使用 Oracle 12c 在 BLOB 字段中附加的 Json 文件中搜索?
How to search in Json file attached in BLOB field using Oracle 12c?
我想在 json 中搜索文本。 json 保存在 blob 字段中。我创建了以下 table
CREATE TABLE APPLICATION
(
applicationId VARCHAR2(36),
customerId VARCHAR2(36),
assignee VARCHAR2(36),
status VARCHAR2(36),
applicationDate TIMESTAMP,
closerDueDate TIMESTAMP,
closedDate TIMESTAMP,
application blob ,
CONSTRAINT application CHECK(application IS JSON FORMAT JSON)) LOB (application) STORE AS(STORAGE (NEXT 15M));
为了保留一行,我使用了 SQL Develop UI 添加所有字段值并将 json 文件附加到 blob 字段中。 blob 字段中附加的文件有 json 文本(巨大的 json)。
文件示例
{
"fields" : {
"Customerid" : "Organization"
}
}
//Huge Json
我想使用路径在 blob 字段内部进行搜索。喜欢:fields.Customerid:"Organization"
我正在使用跟随查询,但它没有得到任何行
select *
from application app
where dbms_lob.instr(app.application, utl_raw.CAST_TO_RAW('AutomationRuleSet'), 1, 1) > 0;
我知道里面存在价值 json 坚持
编辑 1
如果我尝试:
Select app.application
From APPLICATION app
Where JSON_QUERY(app.application, '$.fields.CustomerID') = 'AutomationRuleSet';
我得到了:
Error que empieza en la línea 1 del comando:
Select app.application From APPLICATION app Where JSON_QUERY(app.application, '$.fields.CustomerID') = 'AutomationRuleSet'
Error en la línea de comandos:2 Columna:6 Informe de error:
Error SQL: ORA-40499: no format defined for binary data type
有什么问题?
您可以找到 JSON BLOBS 包含键路径 json_exists()
:
的行
select * from application app
where json_exists(app.application, '$.fields.Customerid');
您可以使用 json_value()
:
搜索特定值
select * from application app
where json_value(app.application, '$.fields.Customerid') = 'AutomationRuleSet';
当您使用 BLOB 时,您可能需要指定 format json
,但在 12.2 和您的 table 定义中测试时我似乎不需要指定:
select * from application app
where json_exists(application format json, '$.fields.Customerid');
select * from application app
where json_value(application format json, '$.fields.Customerid') = 'AutomationRuleSet';
快速解决方案:
我找到了解决方案,基本上我必须将 BLOB 转换为 VARCHAR2
select UTL_RAW.CAST_TO_VARCHAR2( DBMS_LOB.SUBSTR( app.application, 4000, 1 )) AS aplication
from application app
Where JSON_VALUE(UTL_RAW.CAST_TO_VARCHAR2( DBMS_LOB.SUBSTR( app.application, 4000, 1 )), '$.fields.Customerid') = 'AutomationRuleSet';
其他解决方案:
使用 JSON_TABLE
并使用 json 字段创建 table
我想在 json 中搜索文本。 json 保存在 blob 字段中。我创建了以下 table
CREATE TABLE APPLICATION
(
applicationId VARCHAR2(36),
customerId VARCHAR2(36),
assignee VARCHAR2(36),
status VARCHAR2(36),
applicationDate TIMESTAMP,
closerDueDate TIMESTAMP,
closedDate TIMESTAMP,
application blob ,
CONSTRAINT application CHECK(application IS JSON FORMAT JSON)) LOB (application) STORE AS(STORAGE (NEXT 15M));
为了保留一行,我使用了 SQL Develop UI 添加所有字段值并将 json 文件附加到 blob 字段中。 blob 字段中附加的文件有 json 文本(巨大的 json)。
文件示例
{
"fields" : {
"Customerid" : "Organization"
}
}
//Huge Json
我想使用路径在 blob 字段内部进行搜索。喜欢:fields.Customerid:"Organization"
我正在使用跟随查询,但它没有得到任何行
select *
from application app
where dbms_lob.instr(app.application, utl_raw.CAST_TO_RAW('AutomationRuleSet'), 1, 1) > 0;
我知道里面存在价值 json 坚持
编辑 1
如果我尝试:
Select app.application
From APPLICATION app
Where JSON_QUERY(app.application, '$.fields.CustomerID') = 'AutomationRuleSet';
我得到了:
Error que empieza en la línea 1 del comando:
Select app.application From APPLICATION app Where JSON_QUERY(app.application, '$.fields.CustomerID') = 'AutomationRuleSet'
Error en la línea de comandos:2 Columna:6 Informe de error:
Error SQL: ORA-40499: no format defined for binary data type
有什么问题?
您可以找到 JSON BLOBS 包含键路径 json_exists()
:
select * from application app
where json_exists(app.application, '$.fields.Customerid');
您可以使用 json_value()
:
select * from application app
where json_value(app.application, '$.fields.Customerid') = 'AutomationRuleSet';
当您使用 BLOB 时,您可能需要指定 format json
,但在 12.2 和您的 table 定义中测试时我似乎不需要指定:
select * from application app
where json_exists(application format json, '$.fields.Customerid');
select * from application app
where json_value(application format json, '$.fields.Customerid') = 'AutomationRuleSet';
快速解决方案: 我找到了解决方案,基本上我必须将 BLOB 转换为 VARCHAR2
select UTL_RAW.CAST_TO_VARCHAR2( DBMS_LOB.SUBSTR( app.application, 4000, 1 )) AS aplication
from application app
Where JSON_VALUE(UTL_RAW.CAST_TO_VARCHAR2( DBMS_LOB.SUBSTR( app.application, 4000, 1 )), '$.fields.Customerid') = 'AutomationRuleSet';
其他解决方案:
使用 JSON_TABLE
并使用 json 字段创建 table