访问NiFi中对象和数组的第一个元素值

huangapple go评论92阅读模式
英文:

Access both object and array first element value in nifi

问题

I'm using executesql processor in apache nifi to extract data from a table.

Table Data

访问NiFi中对象和数组的第一个元素值

在nifi的executeSql处理器中,在执行SQL查询后,根据给定的条件和数据,我们可以获得一行或两行数据作为输出。

一行输出

访问NiFi中对象和数组的第一个元素值

两行输出

访问NiFi中对象和数组的第一个元素值

我找不到一种方法来访问第一行的值,不包括行数因素。

在执行SQL查询后,如果行数大于一,则我必须在EvaluateJsonPath处理器中使用$[0].id值来获取第一行的id列的值,如果SQL查询的输出只有一行,则我必须使用$.id值来获取第一行的id列的值。

在这里,如果输出有多行,它会给我们提供对象数组,只能使用$[0].id来访问,如果输出只有一行,它会给我们提供一个对象,只能使用$.id来访问。无论行数是1还是多于1,都无法使用$[0].id来处理两种情况。我应该使用哪个处理器或方法,以便无论行数是1还是多于1,我都可以访问第一行的数据。

英文:

I'm using executesql processor in apache nifi to extract data from a table.

Table Data

访问NiFi中对象和数组的第一个元素值

Here in executeSql Processor of nifi after executing the sql query we can get one row of data as output or both the rows based on the given conditions and data present.

One Row Output

访问NiFi中对象和数组的第一个元素值

Two Row Output

访问NiFi中对象和数组的第一个元素值

I'm not able to find a way to access the values of the first row excluding the number of rows factor.

After executing SQL Query if the number of rows is more than one then I've to use $[0].id value in EvaluateJsonPath processor to get the value of id column of first row and if the output of sql query is one row only then I've to use $.id value in EvaluateJsonPath processor to get the value of id column of first row.

Here if the output have multiple rows it gives us arrays of object which can be accessed only using $[0].id and if the output have only one row it gives us one object which can be accessed only using $.id. Both the scenario can't be handled using $[0].id.

Which processor or method should I use so that I can access the data of first row even if the number of rows is 1 only or more than 1.

答案1

得分: 1

One option would be adding a JoltTransformJSON processor just after the current mentioned processor with the following specification

[
  {
    "operation": "shift",
    "spec": {
      "0": { // $[0].id, eg. if there is an array of objects
        "id": ""
      },
      "id": "" // $.id, eg. if there is a single object
    }
  }
]
英文:

One option would be adding a JoltTransformJSON processor just after the current mentioned processor with the following speification

[
  {
    "operation": "shift",
    "spec": {
      "0": { // $[0].id, eg. if there is array of objects
        "id": ""
      },
      "id": "" // $.id, eg. if there is a single object
    }
  }
]

huangapple
  • 本文由 发表于 2023年5月10日 16:42:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76216483.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定