gpt4 book ai didi

java - concat Jolt 转换可在演示站点上运行;在代码中不起作用

转载 作者:行者123 更新时间:2023-12-02 09:20:50 24 4
gpt4 key购买 nike

我想使用 Jolt 连接几个字段( com.bazaarvoice.jolt:jolt-core:0.1.1com.bazaarvoice.jolt:json-utils:0.1.1 )。下面是一个输入记录示例:

{
"ts": 1572873208.555711,
"uid": "CQXg712bv3ayjojRwd",
"orig_lat": 39.997,
"orig_long": -105.0974,
"resp_lat": 39.0481,
"resp_long": -77.4728
}

...这是 Jolt 变换:

[
{
"operation": "modify-default-beta",
"spec": {
"orig_location": "=concat(@(1,orig_lat),',',@(1,orig_long))",
"resp_location": "=concat(@(1,resp_lat),',',@(1,resp_long))"
}
}
]

...这是 Jolt Transform Demo site 转换的输出:

{
"ts" : 1.572873208555711E9,
"uid" : "CQXg712bv3ayjojRwd",
"orig_lat" : 39.997,
"orig_long" : -105.0974,
"resp_lat" : 39.0481,
"resp_long" : -77.4728,
"orig_location" : "39.997,-105.0974",
"resp_location" : "39.0481,-77.4728"
}

我尝试以编程方式执行此操作:

String input = "{\"ts\":1572873208.555711,\"uid\":\"CQXg712bv3ayjojRwd\",\"orig_lat\":39.997,\"orig_long\":-105.0974,\"resp_lat\":39.0481,\"resp_long\":-77.4728}";

String JOLT_SPEC_LIST = "[\n" +
" {\n" +
" \"operation\": \"modify-default-beta\",\n" +
" \"spec\": {\n" +
" \"orig_location\": \"=concat(@(1,orig_lat),',',@(1,orig_long))\",\n" +
" \"resp_location\": \"=concat(@(1,resp_lat),',',@(1,resp_long))\"\n" +
" }\n" +
" }\n" +
"]";

Chainr chainr = Chainr.fromSpec(JsonUtils.jsonToList(JOLT_SPEC_LIST));
Object transformed = chainr.transform(input);

transformed对象应该包含转换后的输出。由于某种原因,输出与输入相同。它不包含两个派生字段。

你能看出我做错了什么吗?

最佳答案

我错误地将字符串传递给转换方法。它应该是一个对象

我使用 Jackson 将字符串转换为对象:

ObjectMapper mapper = new ObjectMapper();
Object inputObject = mapper.readValue(input, Object.class);

...然后将该对象传递给 Chainr.transform() 方法。

关于java - concat Jolt 转换可在演示站点上运行;在代码中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58699645/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com