forked from sucitw/python-script-in-NiFi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite_content.py
56 lines (48 loc) · 1.6 KB
/
write_content.py
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
###############################################################################
# Sample to write content a flowfile from json format
# modified from "https://github.com/BatchIQ/nifi-scripting-samples"
#
# Assumed input json format:
# {
# "tagname": "first",
# "value": 12345,
# "message": "Foo"
# "timesatmp": 1514541007050
# }
# output
# {
# "tagname": "first",
# "value": 152399025,
# "message": "Hello World"
# "timesatmp": 1514541007050
# }
# }
###############################################################################
import json
import sys
import traceback
from datetime import datetime, timedelta, timezone
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)
# Transform content
output_obj = input_obj
# Write output content
outputStream.write(StringUtil.(output_obj['values']))
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)