我想使用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对象应该包含转换后的输出。由于某些原因,输出与输入相同。它不包含两个派生字段。

你能看到我做错了吗?

最佳答案

我错误地将字符串传递给了transform方法。应该是Object

我使用杰克逊将字符串转换为对象:

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


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

07-24 09:52