java 如何在NiFi中使用Jolt将两个JSON数组合并为一个合并数组

jm2pwxwz  于 2023-09-29  发布在  Java
关注(0)|答案(2)|浏览(171)

我有一个要求,我需要将两个JSON数组合并为一个。目前JSON是这样的:

[
  { 
    "Id": "123",
    "test": [
      {
        "count": 5,
        "name": "highway"
      }
    ]
  },
  {
    "test": [
      {
        "count": 6,
        "name": "plateau"
      }
    ]
  }
]

现在我需要将整个json数组合并为一个数组。
比如:

{
  "Id": "123",
  "test": [
    {
      "count": 5,
      "name": "highway"
    },
    {
      "count": 6,
      "name": "plateau"
    }
  ]
}

我是Jolt的新手,因此不知道该尝试什么

h5qlskok

h5qlskok1#

如果使用Jackson和Lombok,则可以反序列化数据,然后减少数据。
你应该可以用Jolt做一些类似于Jackson的事情。底层的reduce是这里的主要业务逻辑。

import java.util.Arrays;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.Data;

public class MergeData {
    public static final String DATA = "[{\"test\":[{\"count\":5,\"name\":\"highway\"}]},{\"test\":[{\"count\":6,\"name\":\"plateau\"}]}]";
    private static final ObjectMapper MAPPER = new ObjectMapper();

    public static void main(String[] args) throws JsonMappingException, JsonProcessingException {
        TestModel[] testModels = MAPPER.readValue(DATA, TestModel[].class);
        System.out.println(Arrays.toString(testModels));

        TestModel reduced = reduceTestModels(testModels);
        System.out.println(reduced);
    }

    private static TestModel reduceTestModels(TestModel[] testModels) {
        TestObject[] testObjects = Arrays.stream(testModels)
                .map(TestModel::getTest)
                .flatMap(Arrays::stream)
                .toArray(TestObject[]::new);

        return new TestModel(testObjects);
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    private static class TestModel {
        private TestObject[] test;

        @Override
        public String toString() {
            return String.format("{ \"test\": %s }", Arrays.toString(test));
        }
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    private static class TestObject {
        private int count;
        private String name;

        @Override
        public String toString() {
            return String.format("{ \"count\": %d, \"name\": \"%s\" }", count, name);
        }
    }
}

输出量:

[
  { "test": [{ "count": 5, "name": "highway" }] },
  { "test": [{ "count": 6, "name": "plateau" }] }
]
{ "test": [{ "count": 5, "name": "highway" }, { "count": 6, "name": "plateau" }] }
t5fffqht

t5fffqht2#

您可以使用以下shift转换规范

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "&",
        "test": {
          "*": "&1[]" // &1 replicates the key "test" grabbing the literal's value 
            // after going up the tree one level
        }
      }
    }
  }
]

网站http://jolt-demo.appspot.com/上的***演示***是:

相关问题