Nifi RecordReader和RecordWriter序列化错误。 IllegalTypeConversionException;无法转换类的值;因为不支持该类型

时间:2019-03-04 10:42:23

标签: avro apache-nifi jolt

我正在尝试使用Apache Nifi中的JoltTransformRecord将json转换为json。当我尝试在https://jolt-demo.appspot.com/中转换json时,得到的是正确的结果。这很好。

但是,当我尝试使用JoltTransformRecord转换json时,它将引发异常。错误是; “无法转换类[Ljava.lang.Object;的值;因为不支持该类型”” 。但是我不明白为什么会出现此错误。我确实检查了输入和输出模式,但是没有找到任何东西。他们看起来很正确。

在下面,给出了我的输入和输出json示例,颠簸规范,输入和输出模式。另外,为此,我正在使用 JsonTreeReader JsonRecordSetWriter

---如何解决此问题? ---

JoltTransformRecord输入json 的示例(在此示例中,数组内部只有一个json对象。但实际上,数组内部有很多json对象。)

[ {
  "uuid" : "MFMS1-MC5",
  "componentId" : "path1",
  "Samples" : {
    "PathFeedrate" : [ {
      "dataItemId" : "pf",
      "timestamp" : "2019-03-01T21:48:27.940558Z",
      "sequence" : "68104",
      "value" : "425.5333",
      "name" : "Fact",
      "subType" : "ACTUAL"
    }, {
      "dataItemId" : "pf",
      "timestamp" : "2019-03-01T21:48:30.244219Z",
      "sequence" : "68117",
      "value" : "0",
      "name" : "Fact",
      "subType" : "ACTUAL"
    } ]
  },
  "Events" : {
    "SequenceNumber" : [ {
      "dataItemId" : "seq",
      "timestamp" : "2019-03-01T21:48:27.940558Z",
      "sequence" : "68105",
      "value" : "0",
      "name" : "sequenceNum"
    } ],
    "Unit" : [ {
      "dataItemId" : "unit",
      "timestamp" : "2019-03-01T21:48:27.940558Z",
      "sequence" : "68106",
      "value" : "13",
      "name" : "unitNum"
    } ]
  }
}]

示例我想要的输出Json ;

{
  "DataItems" : [ {
    "uuid" : "MFMS1-MC5",
    "componentId" : "path1",
    "eventType" : "Samples",
    "type" : "PathFeedrate",
    "dataItemId" : "pf",
    "timestamp" : "2019-03-01T21:48:27.940558Z",
    "sequence" : "68104",
    "value" : "425.5333",
    "name" : "Fact",
    "subType" : "ACTUAL"
  }, {
    "uuid" : "MFMS1-MC5",
    "componentId" : "path1",
    "eventType" : "Samples",
    "type" : "PathFeedrate",
    "dataItemId" : "pf",
    "timestamp" : "2019-03-01T21:48:30.244219Z",
    "sequence" : "68117",
    "value" : "0",
    "name" : "Fact",
    "subType" : "ACTUAL"
  }, {
    "uuid" : "MFMS1-MC5",
    "componentId" : "path1",
    "eventType" : "Events",
    "type" : "SequenceNumber",
    "dataItemId" : "seq",
    "timestamp" : "2019-03-01T21:48:27.940558Z",
    "sequence" : "68105",
    "value" : "0",
    "name" : "sequenceNum"
  }, {
    "uuid" : "MFMS1-MC5",
    "componentId" : "path1",
    "eventType" : "Events",
    "type" : "Unit",
    "dataItemId" : "unit",
    "timestamp" : "2019-03-01T21:48:27.940558Z",
    "sequence" : "68106",
    "value" : "13",
    "name" : "unitNum"
  } ]
}

我的震动规格

[
  {
    "operation": "shift",
    "spec": {
      "Samples": {
        "*": {
          "*": {
            "@(3,uuid)": "Items.&2[#2].uuid",
            "@(3,componentId)": "Items.&2[#2].componentId",
            "$2": "Items.&2[#2].eventType",
            "$1": "Items.&2[#2].type",
            "*": "Items.&2[#2].&"
          }
        }
      },
      "Events": {
        "*": {
          "*": {
            "@(3,uuid)": "Items.&2[#2].uuid",
            "@(3,componentId)": "Items.&2[#2].componentId",
            "$2": "Items.&2[#2].eventType",
            "$1": "Items.&2[#2].type",
            "*": "Items.&2[#2].&"
          }
        }
      },
      "Condition": {
        "*": {
          "*": {
            "@(3,uuid)": "Items.&2[#2].uuid",
            "@(3,componentId)": "Items.&2[#2].componentId",
            "$2": "Items.&2[#2].eventType",
            "$1": "Items.&2[#2].value",
            "*": "Items.&2[#2].&"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "Items": {
        "*": {
          "*": "DataItems[]"
        }
      }
    }
  }
]

此规范正常工作。因为我已经在Jolt transform演示中尝试过了。

我正在使用 JsonTreeReader JoltTransformRecord 中读取json。这是我的输入模式;

{
    "name": "Items",
    "namespace": "Items",
    "type": "record",
    "fields": [
        {
            "name": "uuid",
            "type": "string"
        },
        {
            "name": "componentId",
            "type": "string"
        },
        {
            "name": "Samples",
            "type": ["null", {
                "type": "map",
                "values": {
                    "type": "array",
                    "items": {
                        "name": "SamplesDataItem",
                        "type": "record",
                        "fields": [
                            {
                                "name": "dataItemId",
                                "type": "string"
                            },
                            {
                                "name": "timestamp",
                                "type": "string"
                            },
                            {
                                "name": "sequence",
                                "type": "string"
                            },
                            {
                                "name": "value",
                                "type": "string"
                            },
                            {
                                "name": "name",
                                "type": ["null", "string"]
                            },
                            {
                                "name": "subType",
                                "type": ["null", "string"]
                            },
                            {
                                "name": "sampleRate",
                                "type": ["null", "string"]
                            },
                            {
                                "name": "statistic",
                                "type": ["null", "string"]
                            },
                            {
                                "name": "duration",
                                "type": ["null", "string"]
                            },
                            {
                                "name": "sampleCount",
                                "type": ["null", "string"]
                            },
                            {
                                "name": "compositionId",
                                "type": ["null", "string"]
                            },
                            {
                                "name": "resetTriggered",
                                "type": ["null", "string"]
                            }
                        ]
                    }
                }
            }]
        },
        {
            "name": "Events",
            "type": ["null", {
                "type": "map",
                "values": {
                    "type": "array",
                    "items": {
                        "name": "EventsDataItem",
                        "type": "record",
                        "fields": [
                            {
                                "name": "dataItemId",
                                "type": "string"
                            },
                            {
                                "name": "timestamp",
                                "type": "string"
                            },
                            {
                                "name": "sequence",
                                "type": "string"
                            },
                            {
                                "name": "value",
                                "type": "string"
                            },
                            {
                                "name": "name",
                                "type": ["null", "string"]
                            },
                            {
                                "name": "subType",
                                "type": ["null", "string"]
                            },
                            {
                                "name": "compositionId",
                                "type": ["null", "string"]
                            },
                            {
                                "name": "resetTriggered",
                                "type": ["null", "string"]
                            }
                        ]
                    }
                }
            }]
        },
        {
            "name": "Condition",
            "type": ["null", {
                "type": "map",
                "values": {
                    "type": "array",
                    "items": {
                        "name": "ConditionDataItem",
                        "type": "record",
                        "fields": [
                            {
                                "name": "dataItemId",
                                "type": "string"
                            },
                            {
                                "name": "timestamp",
                                "type": "string"
                            },
                            {
                                "name": "type",
                                "type": "string"
                            },
                            {
                                "name": "sequence",
                                "type": "string"
                            },
                            {
                                "name": "name",
                                "type": ["null", "string"]
                            },
                            {
                                "name": "subType",
                                "type": ["null", "string"]
                            },
                            {
                                "name": "nativeCode",
                                "type": ["null", "string"]
                            },
                            {
                                "name": "nativeSeverity",
                                "type": ["null", "string"]
                            },
                            {
                                "name": "qualifier",
                                "type": ["null", "string"]
                            },
                            {
                                "name": "statistic",
                                "type": ["null", "string"]
                            },
                            {
                                "name": "compositionId",
                                "type": ["null", "string"]
                            }
                        ]
                    }
                }
            }]
        }
    ]
}

我正在使用 JsonRecordSetWriter JoltTransformRecord 中写入转换后的结果。这是我的输出架构;

{
    "name": "Items",
    "type": "record",
    "namespace": "Items",
    "fields": [
        {
            "name": "DataItems",
            "type": {
                "type": "array",
                "items": {
                    "name": "DataItems",
                    "type": "record",
                    "fields": [
                        {
                            "name": "uuid",
                            "type": "string"
                        },
                        {
                            "name": "componentId",
                            "type": "string"
                        },
                        {
                            "name": "eventType",
                            "type": "string"
                        },
                        {
                            "name": "type",
                            "type": "string"
                        },
                        {
                            "name": "dataItemId",
                            "type": "string"
                        },
                        {
                            "name": "timestamp",
                            "type": "string"
                        },
                        {
                            "name": "value",
                            "type": "string"
                        },
                        {
                            "name": "name",
                            "type": ["null", "string"],
                            "default": null
                        },
                        {
                            "name": "subType",
                            "type": ["null", "string"],
                            "default": null
                        }
                    ]
                }
            }
        }
    ]
} 

1 个答案:

答案 0 :(得分:2)

这确实是记录处理实用程序中的错误,我已经写了NIFI-6105来介绍此修复程序。好收获!

作为一种解决方法,由于将JSON作为输入和输出,因此可以使用JoltTransformJson代替JoltTransformRecord。另外,如果您知道地图中的键(例如PathFeedrate),则可以更改架构以将其视为记录而不是地图,这可能会带您解决错误。