How to provide input to jolt when multiple objects are given

北慕城南 提交于 2020-05-17 07:04:13

问题


How to apply transformation to the json file having records in the following format (not array, just multiple objects). I want to provide a file with following input format and after applying transfornation.Want to get it saved in some folder. example:

Input Record Format

    {
     "name": "adam",
     "age": 12,
     "city": "australia"
   }
   {
     "name": "adam",
     "age": 12,
     "city": "australia"
   }
   {
     "name": "adam",
     "age": 12,
     "city": "australia"
   }
   {
     "name": "adam",
     "age": 12,
     "city": "australia"
   }
   {
     "name": "adam",
     "age": 12,
     "city": "australia"
   }
   {
     "name": "adam",
     "age": 12,
     "city": "australia"
   }
   .....

Desired Output Record Format

   {
     "fname": "adam",
     "age": 12,
     "city": "australia"
   }
   {
     "fname": "adam",
     "age": 12,
     "city": "australia"
   }
   {
     "fname": "adam",
     "age": 12,
     "city": "australia"
   }
   {
     "fname": "adam",
     "age": 12,
     "city": "australia"
   }
   {
     "fname": "adam",
     "age": 12,
     "city": "australia"
   }
   {
     "fname": "adam",
     "age": 12,
     "city": "australia"
   }
   // Just changing the name key to fname
   .....

回答1:


Since you tagged apache-nifi I assume you want to use one of the JOLT processors in Apache NiFi for this. In this case JoltTransformJSON won't work because it expects valid JSON as daggett said. However the JSON parser in JsonTreeReader is more lenient, so you can use JoltTransformRecord with a JsonTreeReader and the following spec:

[
  {
    "operation": "shift",
    "spec": {
      "name": "fname",
      "*": "&"
    }
  }
]

Although you can't "pretty print" the JSON without it being valid JSON, you can set the Output Grouping property on the JsonRecordSetWriter to One Line Per Object. That will give you input something like:

{"fname":"adam","age":12,"city":"australia"}
{"fname":"adam","age":12,"city":"australia"}
{"fname":"adam","age":12,"city":"australia"}
{"fname":"adam","age":12,"city":"australia"}
{"fname":"adam","age":12,"city":"australia"}
{"fname":"adam","age":12,"city":"australia"}


来源:https://stackoverflow.com/questions/61465241/how-to-provide-input-to-jolt-when-multiple-objects-are-given

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!