使用ApacheNIFI进行数据操作的JSON转换

使用ApacheNIFI进行数据操作的JSON转换,json,apache-nifi,jolt,hortonworks-dataflow,Json,Apache Nifi,Jolt,Hortonworks Dataflow,我想对下面的示例JSON进行一些基本转换,我想将timeStamp标记的值更改为date格式,并想使用NiFi将一个新的标记添加到我预期的JSON输出中,该标记的值为current_timeStamp 示例JSON: 预期的JSON: 能否请您帮助了解Apache NiFi/HDF中要遵循的详细步骤。未实现数据转换 查看官方文件: 股票转换 股票转换为: shift : copy data from the input tree and put it the output tree

我想对下面的示例JSON进行一些基本转换,我想将timeStamp标记的值更改为date格式,并想使用NiFi将一个新的标记添加到我预期的JSON输出中,该标记的值为current_timeStamp

示例JSON:

预期的JSON:


能否请您帮助了解Apache NiFi/HDF中要遵循的详细步骤。

未实现数据转换

查看官方文件:

股票转换

股票转换为:

shift       : copy data from the input tree and put it the output tree
default     : apply default values to the tree
remove      : remove data from the tree
sort        : sort the Map key values alphabetically ( for debugging and human readability )
cardinality : "fix" the cardinality of input data.  Eg, the "urls" element is usually a List, 
                    but if there is only one, then it is a String
目前,所有的股票转换只影响数据的结构

要进行数据操作,需要编写Java代码

如果编写Java数据操作代码来实现转换接口,则可以将代码插入转换链中

因此,为了完成您的任务,我看到了两个主要变体:

V1:

使用以下处理器的顺序:

EvaluateJsonPath->UpdateAttributes->AttributesToJSON

在EvaluateJsonPath中,使用诸如$.name、$.timeStamp、

在UpdateAttributes中,转换时间戳格式并定义新属性:

attribute  |   value/expression
-----------------------------------------------------------
timeStamp  |   timeStamp:format('yyyy-MM-dd HH:mm:ss.SSS')
created_ts |   now():format('yyyy-MM-dd HH:mm:ss.SSS')
在AttributesToJSON中,定义要作为json对象存储到文件内容中的属性列表

V2:将ExecuteScript处理器与以下代码一起使用:

import groovy.json.JsonSlurper
import groovy.json.JsonBuilder

def ff = session.get()
if(!ff)return
ff = session.write(ff, {rawIn, rawOut->
    // transform streams into reader and writer
    rawIn.withReader("UTF-8"){reader->
        rawOut.withWriter("UTF-8"){writer->
            //parse reader into Map
            def json = new JsonSlurper().parse(reader)
            //change/set values
            json.timeStamp = new Date(json.timeStamp as Long).format('yyyy-MM-dd HH:mm:ss.SSS')
            json.created_ts = new Date().format('yyyy-MM-dd HH:mm:ss.SSS')
            //write changed object to writer
            new JsonBuilder(json).writeTo(writer)
        }
    }
} as StreamCallback)
session.transfer(ff, REL_SUCCESS)

未实现数据转换

查看官方文件:

股票转换

股票转换为:

shift       : copy data from the input tree and put it the output tree
default     : apply default values to the tree
remove      : remove data from the tree
sort        : sort the Map key values alphabetically ( for debugging and human readability )
cardinality : "fix" the cardinality of input data.  Eg, the "urls" element is usually a List, 
                    but if there is only one, then it is a String
目前,所有的股票转换只影响数据的结构

要进行数据操作,需要编写Java代码

如果编写Java数据操作代码来实现转换接口,则可以将代码插入转换链中

因此,为了完成您的任务,我看到了两个主要变体:

V1:

使用以下处理器的顺序:

EvaluateJsonPath->UpdateAttributes->AttributesToJSON

在EvaluateJsonPath中,使用诸如$.name、$.timeStamp、

在UpdateAttributes中,转换时间戳格式并定义新属性:

attribute  |   value/expression
-----------------------------------------------------------
timeStamp  |   timeStamp:format('yyyy-MM-dd HH:mm:ss.SSS')
created_ts |   now():format('yyyy-MM-dd HH:mm:ss.SSS')
在AttributesToJSON中,定义要作为json对象存储到文件内容中的属性列表

V2:将ExecuteScript处理器与以下代码一起使用:

import groovy.json.JsonSlurper
import groovy.json.JsonBuilder

def ff = session.get()
if(!ff)return
ff = session.write(ff, {rawIn, rawOut->
    // transform streams into reader and writer
    rawIn.withReader("UTF-8"){reader->
        rawOut.withWriter("UTF-8"){writer->
            //parse reader into Map
            def json = new JsonSlurper().parse(reader)
            //change/set values
            json.timeStamp = new Date(json.timeStamp as Long).format('yyyy-MM-dd HH:mm:ss.SSS')
            json.created_ts = new Date().format('yyyy-MM-dd HH:mm:ss.SSS')
            //write changed object to writer
            new JsonBuilder(json).writeTo(writer)
        }
    }
} as StreamCallback)
session.transfer(ff, REL_SUCCESS)

我可以添加使用jolt创建的新字段,但仍在努力将时间戳1477307252000转换为时间戳2016-11-08 14:46:13.674格式。我可以添加使用jolt创建的新字段,但仍在努力将时间戳1477307252000转换为时间戳2016-11-08 14:46:13.674格式。我们可以使用其他nifi处理器进行数据转换吗?@nilesh1212,修改了响应谢谢你的帮助,我正在试用v1,但它看起来EvaluateJsonPath根本没有改变我的json。我把场地摆正了attributes@nilesh1212,EvaluateJsonPath只是将json属性提取到flowfile属性,使用UpdateAttributes,您可以更改flow file属性,AttributesToJSON将从flow file attributes为您构建新的json消息当我在EvaluateJsonPath上执行数据出处时,UpdateAttribute和AttributeToJson我可以看到预期的值,但我的最后一个流是PutFile,PutFile没有写入修改后的json。流程如下->`ConsumerKafka->SplitJson将json拆分为一行,因为我正在获取json数组{data[{name:..},{name:..}]->EvaluateJsonPath->UpdateAttributes->AttributesToJson->PutFile`我们可以使用其他nifi处理器进行数据转换吗?@nilesh1212,修改了响应谢谢您的帮助,我正在试用v1,但EvaluateJsonPath似乎根本没有更改我的json。我已经设置了正确的字段attributes@nilesh1212,EvaluateJsonPath just将json属性提取到flowfile属性,使用UpdateAttributes,您可以更改flow file属性,AttributesToJSON将从flow file attributes为您构建一条新的json消息当我在EvaluateJsonPath、UpdateAttribute和AttributeToJson上执行数据出处时,我可以看到预期的值,但我的最后一个流是PutFile、PutFile没有写入修改后的json。流程如下->`ConsumerKafka->SplitJson将json拆分为一行,因为我正在获取json数组{data[{name:..},{name:..}]->EvaluateJsonPath->UpdateAttributes->AttributesToJson->PutFile`