Support Questions
Find answers, ask questions, and share your expertise
Announcements
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.

ExecuteScript Failed to process session due to javax.script.ScriptException: IndexError: index out of range: 22 in <script> at line number 114

ExecuteScript Failed to process session due to javax.script.ScriptException: IndexError: index out of range: 22 in <script> at line number 114

New Contributor

ExecuteScript[id=c861ef26-2093-3948-b084-86944ec3345
6] Failed to process session due to javax.script.ScriptException: IndexError: index out of range: 22 in <script> at line number 114: org.apache.nifi.processor.exception.ProcessException: javax.script.Script
Exception: IndexError: index out of range: 22 in <script> at line number 114"

 

Script - 

import java.io
import datetime
from org.apache.commons.io import IOUtils
from java.nio.charset import StandardCharsets
from org.apache.nifi.processor.io import StreamCallback

'''
table_name = "veh_field_actn"
column_type = "int|str|str|int|str"
cassandra_pk = "col1|col2"

class OutputStream:
def __init__(self):
pass
def write(self, msg):
print(msg)

class StreamCallback:
def __init__(self):
pass
def process(self, inputStream, outputStream):
pass
'''

class PyStreamCallback(StreamCallback):
def __init__(self):
pass
def process(self, inputStream, outputStream):
input = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
#input = "col1|col2|status_cd|gg_src_sys_iud_cd|dw_mod_ts\nval11|val12||I|ts1\nval21|val22|O|U|ts1\nval31|val32|val33|D|ts1\nval41|val42|X|I|ts1"
#input = "col1|col2|status_cd|col4|col5\nval11|val12|O|I|ts1\nval21|val22|O|U|ts1\nval31|val32|val33|D|ts1\nval41|val42|X|I|ts1"

lines = input.split('\n')
if len(lines) > 1:
header = lines[0].split('|')
header[-1] = header[-1].strip('\n')
lines = lines[1:]

batch_start = 'BEGIN BATCH\n'
batch_end = 'APPLY BATCH;'
column_types = str(column_type).split('|')
tbl_name = str(table_name)

outputStream.write(batch_start.encode('utf-8'))

#Take a single line out of list of lines from flow file
for line in lines:
values = line.split('|')
columns = {}
flag_delete = 0

#Test values for NULL/empty and remove column headers associated with them
i = 0
while i < len(values):
value = values[i]
#replace unwanted new lines and quotes
value = value.replace('\n','').replace('\r','').replace('"','').replace("'","")

if (value and not value.isspace()):
name = str(header[i])
#test for column type based on attribute defined on the processor
if name != 'gg_src_sys_iud_cd' and name != 'dw_mod_ts':
if str(column_types[i]) == 'int':
columns[name] = value
else:
columns[name] = "'" + value + "'"

if tbl_name == 'veh_field_actn':
if (name == 'status_cd' and value != 'O') or \
(name == 'gg_src_sys_iud_cd' and value == 'D'):
flag_delete = 1
break
else:
if name == 'gg_src_sys_iud_cd' and value == 'D':
flag_delete = 1
break
i+=1

if columns.__len__() > 0:
stmt = ""

if flag_delete == 0:
column_names = ",".join(columns.keys())
column_values = ",".join(columns.values())
ts = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
#ts = "2017-09-12"
stmt = "insert into " + tbl_name + " (" + column_names + ",updated_timstm) " \
+ "values (" + column_values + ",'" + ts + "');\n"
outputStream.write(stmt.encode('utf-8'))
else:
pk_columns = str(cassandra_pk).split("|")
where_cond = ""
i = 0
while i < len(pk_columns):
if i > 0:
where_cond += " and "
where_cond += pk_columns[i] + "=" + columns[pk_columns[i]]
i += 1
stmt = "delete from " + tbl_name + " where " + where_cond + ";\n"
outputStream.write(stmt.encode('utf-8'))

#end flow file with APPLY_BATCH cSql
outputStream.write(batch_end.encode('utf-8'))

else:
global delete_flow_file
delete_flow_file = True


flowFileList = session.get(1)
if not flowFileList.isEmpty():
for flowFile in flowFileList:
delete_flow_file = False
flowFile = session.write(flowFile, PyStreamCallback())
if delete_flow_file == True:
session.remove(flowFile)
else:
session.transfer(flowFile, REL_SUCCESS)

'''
obj = PyStreamCallback()
obj.process(None, OutputStream())
'''

 

Flowfile - 

dealer_bac_cd|offer_coupon_cd|offer_unique_id|dealer_nm|disclaimer_text|display_seq_nbr|general_catgy_cd|offer_desc|offer_end_dt|offer_primary1_val|offer_primary2_val|offer_secondary1_val|offer_secondary2_val|offer_start_dt|offer_sub_title_desc|offer_tagline1_val|offer_tagline2_val|offer_title_desc|service_group_cd|service_sub_type_cd|template_type|dw_mod_ts
XXXXXX|202|1d1bec63decc3646902368489a4dd517b5599fbef52b818f269b741a5deb3150b4428da2173651919300750a7XXXXX|XXXXX Chevrolet|"*Tire balancing, tax, and more than 6 quarts of oil extra. Excludes diesel engines. See owner's manual for specific oil grades recommended by vehicle model."|6|1||2020-12-31|$49||95*||2020-10-19|ACDelco dexos1 Full Synthetic 6 Quart Oil Change with Tire Rotation|||dexos1 6-Qt Oil Change Pkg|OEM-LOF&ROT|LOF/ROT/MPI/Standard|ONE_PRICE_TEMPLATE|2020-10-19 13:42:36
XXXXXX|4|1c80c129ce2c5fdaea2568eee9efb1189a0f3a015f790cf03d070d65748a2ae5189f8b155cf7a8a22be0d40f4XXXXX|XXXXX Chevrolet Inc.| |100|1|"Wiper blades, per pair. No cost installation with purchase. Plus tax, if applicable. Not valid with other offers. Present offer at time of service."|2020-12-18|$2||00 ||2020-10-19|| OFF||Wiper Blades|WIPERS|Replace Wiper Blades|ONE_PRICE_TEMPLATE|2020-10-19 13:42:36
XXXXXX|317|86cd41fa199db20148483a6f59661b2ca20578ced17ba4c4f8c67adaae0e73918ec444eda7d697df5c4a199c0XXXXX|XXXXX Chevrolet Inc.|"*Mail-in rebate available to retail customers with a U.S. mailing address only. Must purchase and install at a participating Chevrolet, Buick, GMC, or Cadillac dealer located in the United States or U.S. territories during the offer period. Transmissions exclude Saab, Chevrolet Medium-/Heavy-Duty Trucks, and GMC Medium-/Heavy-Duty Trucks. Rebate offer not valid with other offers. Limit one rebate per service performed per date and vehicle. Allow 6 to 8 weeks from promotion end date for delivery of Visa Prepaid Card. See mycertifiedservicerebates.com for complete details, eligible parts, and rebate form, which must be postmarked by 1/31/2021. Offer ends 12/31/2020."|100|1|Purchase and installation included.|2020-12-31|||||2020-10-19|"$200 Visa Prepaid Card* on an eligible GM Genuine Parts Engine, Transmission, or Transfer Case"|||$200 Transmission Rebate*|OEM-TRANSMISSION|Transmission Service|NO_PRICE_TEMPLATE|2020-10-19 13:42:36
XXXXXX|232|1fb905a0c7fcb5c0f2a0d1d1642deeffde14625629db26b03b61b3d345df7fb35f1a8b6870909ee88a870e243XXXXX|XXXXX Chevrolet Inc.|"*Installation and rotor inspection included. Turning or replacing rotors, all other services, and tax extra. Excludes high performance brake pads and some vehicles. **Please see your Dealer to learn more about the warranty part details and qualifications."|27|2|12 Months/Unlimited Mile Warranty**|2020-12-31|$169||95*||2020-10-19|Most ACDelco Advantage Front Brake Pads Installed|||ACDelco Advantage Brake Pads|OEM-BRAKE|Brake Pads Installed|ONE_PRICE_TEMPLATE|2020-10-19 13:42:36
XXXXXX|207|e854cfdb71636ab42a7bca5cd6a4474baaec81fe234111d1375298337eafd5e6e990ea022aaa752b0489e0d3eXXXXX|XXXXX Chevrolet Inc.|*Tax extra. Installation extra on some vehicles. **Please see your Dealer to learn more about exclusions and the warranty part details and qualifications.|46|1|30-month free-replacement limited warranty**|2020-12-31|$174||95*||2020-10-19|Most ACDelco Gold (Professional) Batteries Installed|||ACDelco Gold 30-Mo Battery|OEM-BATTERY|Battery Replacement|ONE_PRICE_TEMPLATE|2020-10-19 13:42:36
XXXXXX|202|589bb70bb978a40cb61d46eb3bcdb724b897cca77276f09a795756eec158153e7ac86d36fbe2587d26b602008XXXXX|XXXXX Chevrolet Inc.|"*Tire balancing, tax, and more than 6 quarts of oil extra. Excludes diesel engines. See owner's manual for specific oil grades recommended by vehicle model."|6|1||2020-12-31|$49||95*||2020-10-19|ACDelco dexos1 Full Synthetic 6 Quart Oil Change with Tire Rotation|||dexos1 6-Qt Oil Change Pkg|OEM-LOF&ROT|LOF/ROT/MPI/Standard|ONE_PRICE_TEMPLATE|2020-10-19 13:42:36
XXXXXX|122|de32dbb87da2768f5ceba774f1d65ecc50ff1770e6bfc551627063e9a0d9bc108b53efbd1f945533e27d47986XXXXX|XXXXX Auto Plaza| |100|3|"Bring your vehicle into our state of the art Body Shop/Collision Center and we will give you a COMPLIMENTARY QUOTE on your vehicle damage. Whether you did it or not, we want to fix it right the first time and get your car looking like brand new again. Coupon valid at vehicle check in."||FREE||||2020-10-19||||Body Shop | Collision Center|SPECIAL|Body Shop|ONE_PRICE_TEMPLATE|2020-10-19 13:42:36

Don't have an account?
Coming from Hortonworks? Activate your account here