-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathnested_json_transform.py
More file actions
65 lines (57 loc) · 1.73 KB
/
nested_json_transform.py
File metadata and controls
65 lines (57 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
###############################################################################
# Sample to transforming a flowfile with json format
# modified from "https://github.com/BatchIQ/nifi-scripting-samples"
#
# Assumed input json format:
# {
# "timestamp": 1514541007050,
# "values":[
# {
# "name": "first",
# "value": 12345,
# "message": "Foo"
# "timesatmp": 1514541007050
# },
# {
# "name": "second",
# "value": 12345,
# "message": "qoo"
# "timestamp": 1514541004656
# },
# ]
# }
#
# output
# {
# "tagname": "first",
# "value": 152399025,
# "message": "Hello World"
# "timestamp": 1514541007050
# }
# }
###############################################################################
import json
import sys
import traceback
from java.nio.charset import StandardCharsets
from org.apache.commons.io import IOUtils
from org.apache.nifi.processor.io import StreamCallback
from org.python.core.util import StringUtil
class TransformCallback(StreamCallback):
def __init__(self):
pass
def process(self, inputStream, outputStream):
try:
# Read input FlowFile content
input_text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
input_obj = json.loads(input_text)
output_text = input_obj['name']
outputStream.write(bytearray(output_text.encode('utf-8')))
except:
traceback.print_exc(file=sys.stdout)
raise
flowFile = session.get()
if flowFile != None:
flowFile = session.write(flowFile, TransformCallback())
# Finish by transferring the FlowFile to an output relationship
session.transfer(flowFile, REL_SUCCESS)