Apache nifi Nifi将sql转换为json结构化

Apache nifi Nifi将sql转换为json结构化,apache-nifi,avro,Apache Nifi,Avro,我正试图找到一种从SQL中获取数据并将其格式化为特定json格式的方法,而我在nifi中很难做到这一点 表中的数据如下所示 { "location_id": "123456", "name": "My Organization", "address_1": "Address 1", "address_2": "Suite 123", "city": "My City", "state": "FL", "zip_code": "33333",

我正试图找到一种从SQL中获取数据并将其格式化为特定json格式的方法,而我在nifi中很难做到这一点

表中的数据如下所示

{
    "location_id": "123456",
    "name": "My Organization",
    "address_1": "Address 1",
    "address_2": "Suite 123",
    "city": "My City",
    "state": "FL",
    "zip_code": "33333",
    "description": "",
    "longitude": "-2222.132131321332113",
    "latitude": "111.21321321321321321",
    "type": "data type"
}
{
    "type": "FeatureCollection",
    "features": [
        {
            "geometry": {
                "type": "Point",
                "coordinates": [
                    $.longitude,
                    $.latitude
                ]
            },
            "type": "Feature",
            "properties": {
                "name": $.name,
                "phone": $.phone_number,
                "address1": $.address_1,
                "address2": $.address_2,
                "city": $.city,
                "state": $.state,
                "zip": $.zip_code,
                "type": $.type
            }
        }
    ]
}
我想把它转换成这样的格式

{
    "location_id": "123456",
    "name": "My Organization",
    "address_1": "Address 1",
    "address_2": "Suite 123",
    "city": "My City",
    "state": "FL",
    "zip_code": "33333",
    "description": "",
    "longitude": "-2222.132131321332113",
    "latitude": "111.21321321321321321",
    "type": "data type"
}
{
    "type": "FeatureCollection",
    "features": [
        {
            "geometry": {
                "type": "Point",
                "coordinates": [
                    $.longitude,
                    $.latitude
                ]
            },
            "type": "Feature",
            "properties": {
                "name": $.name,
                "phone": $.phone_number,
                "address1": $.address_1,
                "address2": $.address_2,
                "city": $.city,
                "state": $.state,
                "zip": $.zip_code,
                "type": $.type
            }
        }
    ]
}
这就是我到目前为止所做的,如果我以一种奇怪的方式做这件事,一定要让我知道

我在想我可以把所有这些都分割成单记录JSON格式,并用这种格式

{
            "geometry": {
                "type": "Point",
                "coordinates": [
                    $.longitude,
                    $.latitude
                ]
            },
            "type": "Feature",
            "properties": {
                "name": $.name,
                "phone": $.phone_number,
                "address1": $.address_1,
                "address2": $.address_2,
                "city": $.city,
                "state": $.state,
                "zip": $.zip_code,
                "type": $.type
            }
        }
然后将所有的记录合并在一起,并围绕这个

{
        "type": "FeatureCollection",
        "features": [
        ]
}

我觉得我这样做很奇怪,只是不知道如何完成哈哈。

尝试使用JsonRecordSetWriter而不是ExecuteSQL来执行ExecuteSQLRecord,这将允许您将行作为JSON对象输出,而无需转换为Avro或从Avro转换。如果没有太多的行(这会导致内存不足错误),可以使用JoltTransferMJSON使用以下链规范执行整个转换(不拆分行):

[
  {
    "operation": "shift",
    "spec": {
      "#FeatureCollection": "type",
      "*": {
        "#Feature": "features[&1].type",
        "name": "features[&1].properties.name",
        "address_1": "features[&1].properties.address_1",
        "address_2": "features[&1].properties.address_2",
        "city": "features[&1].properties.city",
        "state": "features[&1].properties.state",
        "zip_code": "features[&1].properties.zip",
        "type": "features[&1].properties.type",
        "longitude": "features[&1].geometry.coordinates.longitude",
        "latitude": "features[&1].geometry.coordinates.latitude"
      }
    }
  }
]

如果行太多,可以使用SplitJson将它们分割成较小的块,然后使用jolttransfermjson(使用上述规范),然后使用MergeRecord将它们合并回一个大数组。要将它们嵌套到
features
字段中,可以使用ReplaceText在外部JSON对象中“包装”数组,但这也可能导致内存不足错误。

检查nifi jolt处理器以转换JSON,这似乎不起作用。我试图从一开始就理解它是如何工作的,但我似乎只是将硬编码的值恢复为“type”:“FeatureCollection”而不是其他内容。您的输入是什么样子的?我使用了您上面提供的输入,并将其(和我的规范)放入,结果看起来像您想要的输出。编辑@mattyb这是我的照片这是为你做的吗?还是我做错了什么?对不起,我假设因为你们试图拆分/合并原始对象的数组,所以我基于这个假设编写了规范。如果你在你的原始对象周围放上数组大括号,那么这个规范就会起作用。哦,哇,这现在更有意义了,哈哈。我一直在摆弄它,试图弄清楚它是如何起作用的。这让我发疯。。。。