mirror of
https://github.com/OpenSPG/openspg.git
synced 2025-06-27 03:20:10 +00:00
feat(reasoner): add udf to get json str info (#165)
This commit is contained in:
parent
508f3e0a81
commit
497da2e1b8
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2023 OpenSPG Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License
|
||||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
||||
* or implied.
|
||||
*/
|
||||
|
||||
package com.antgroup.openspg.reasoner.udf.builtin.udf;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson.JSONPath;
|
||||
import com.antgroup.openspg.reasoner.udf.model.UdfDefine;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class JsonStringGet {
|
||||
|
||||
/**
|
||||
* @param plainJson
|
||||
* @param jsonPath
|
||||
* @return
|
||||
*/
|
||||
@UdfDefine(name = "json_get", compatibleName = "UDF_JsonGet")
|
||||
public Object jsonStrGet(String plainJson, String jsonPath) {
|
||||
try {
|
||||
JSONObject jsonObject = JSON.parseObject(plainJson);
|
||||
if (jsonObject != null) {
|
||||
Object result = JSONPath.eval(jsonObject, jsonPath);
|
||||
if (result != null) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return "";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
@UdfDefine(name = "get_rdf_property")
|
||||
public Object getRdfProperty(Object properties, String propKey) {
|
||||
if (properties instanceof Map) {
|
||||
Map<String, Object> objectMap = (Map<String, Object>) properties;
|
||||
List<String> jsonStrList = new ArrayList<>();
|
||||
for (String key : objectMap.keySet()) {
|
||||
if (!key.contains("basicInfo")) {
|
||||
continue;
|
||||
}
|
||||
return jsonStrGet(objectMap.get(key).toString(), "$." + propKey);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -13,6 +13,7 @@
|
||||
|
||||
package com.antgroup.openspg.reasoner.udf.test;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.antgroup.openspg.reasoner.common.types.KTArray;
|
||||
import com.antgroup.openspg.reasoner.common.types.KTBoolean$;
|
||||
import com.antgroup.openspg.reasoner.common.types.KTDouble$;
|
||||
@ -53,6 +54,36 @@ public class UdfTest {
|
||||
DateUtils.timeZone = TimeZone.getTimeZone("Asia/Shanghai");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonGet() {
|
||||
UdfMng mng = UdfMngFactory.getUdfMng();
|
||||
String params = "{'v':'123'}";
|
||||
IUdfMeta udfMeta =
|
||||
mng.getUdfMeta("json_get", Lists.newArrayList(KTString$.MODULE$, KTString$.MODULE$));
|
||||
Object rst = udfMeta.invoke(params, "$.v");
|
||||
Assert.assertEquals(rst, "123");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRdfProperty() {
|
||||
UdfMng mng = UdfMngFactory.getUdfMng();
|
||||
String params =
|
||||
"{\n"
|
||||
+ " \"B\": {\n"
|
||||
+ " \"extInfo\": \"gen_pic_gamble_hitsp_flag=1\",\n"
|
||||
+ " \"lbs\": \"{'v':'123'}\"\n"
|
||||
+ " }\n"
|
||||
+ "}";
|
||||
;
|
||||
Map<String, Object> paramsMap = JSONObject.parseObject(params, Map.class);
|
||||
paramsMap.put("basicInfo", "{'v':'123'}");
|
||||
IUdfMeta udfMeta =
|
||||
mng.getUdfMeta(
|
||||
"get_rdf_property", Lists.newArrayList(KTObject$.MODULE$, KTString$.MODULE$));
|
||||
Object rst = udfMeta.invoke(paramsMap, "v");
|
||||
Assert.assertEquals(rst, "123");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInStr() {
|
||||
UdfMng mng = UdfMngFactory.getUdfMng();
|
||||
|
Loading…
x
Reference in New Issue
Block a user