简体   繁体   English

如何通过两个键使用Jolt聚合JSON数据?

[英]How do I aggregate JSON data using Jolt by two keys?

If I have an input of a list of JSON objects. 如果我输入的是JSON对象列表。 How do I go about nesting the data in Java by the date, and then the category in and also sorting it by date in descending order? 如何按日期嵌套Java中的数据,然后按类别嵌套,并按日期降序对数据进行排序?

Input: 输入:

    { "data":[{
      "date": "2015-02-26",
      "buyer": "Ryan",
      "category": "clothes",
      "quantity":"10.0"
    },
    {
      "date": "2015-02-18",
      "buyer": "Lisa",
      "category": "food",
      "quantity": "2.0"
    },    
    {
      "date": "2015-02-18",
      "buyer": "Brian",
      "category": "food",
      "quantity": "11.0",
    },
    {
      "date": "2015-02-26",
      "buyer": "Jim",
      "category": "clothes",
      "quantity": "20.0",
    },
    {
      "date": "2015-02-26",
      "buyer": "Tom",
      "category": "food",
      "quantity": "40.0",
    },
    {
      "date": "2015-02-18",
      "buyer": "Alyssa",
      "category": "clothes",
      "quantity": "13.0",
    }]
}

You can see in my below output, that I am trying to group the data by the date first, and then within the date I want to group the objects by the category. 您可以在下面的输出中看到,我正在尝试首先按日期对数据进行分组,然后在要按类别对对象进行分组的日期之内。

Desired Output: 所需输出:

{
    "2015-02-26”:{
                    “clothes”:[{
                                "date": "2015-02-26",
                                "buyer": "Ryan",
                                "category": "clothes",
                                "quantity":"10.0"
                                },
                                {
                                    "date": "2015-02-26",
                                    "buyer": "Jim",
                                    "category": "clothes",
                                    "quantity": "20.0",
                                }],
                    "food":[{
                                  "date": "2015-02-26",
                                  "buyer": "Tom",
                                  "category": "food",
                                  "quantity": "40.0",
                            }]
                }
     "2015-02-18":{
                    “clothes”:[{
                                  "date": "2015-02-18",
                                  "buyer": "Alyssa",
                                  "category": "clothes",
                                  "quantity": "13.0",
                                }],
                    "food":[{
                          "date": "2015-02-18",
                          "buyer": "Lisa",
                          "category": "food",
                          "quantity": "2.0"
                        },
                        {
                          "date": "2015-02-18",
                          "buyer": "Brian",
                          "category": "food",
                          "quantity": "11.0",
                        }]
                } 
}

Surprising easy. 令人惊讶的容易。

Spec 规格

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        // Ex. send the first data item to 
        //    2015-02-26.clothes[] 
        //    where we want clothes to always be an array 
        //    even if it only got one value assigned to it.
        "*": "@(0,date).@(0,category)[]"
      }
    }
  }
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM