Advertisement
Guest User

Untitled

a guest
Jan 12th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.05 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?><template><description>This template can be used like a GenerateFlowFile processor, where the content and filename can be specified. It uses InvokeScriptedProcessor and Groovy to achieve this.</description><name>GenerateFlowFileWithContent</name><snippet><processors><id>8e949039-0779-458d-b825-32b09ef80b38</id><parentGroupId>d3d19b0c-5876-41a2-8b09-e5ed911dfa67</parentGroupId><position><x>-172.94315107850957</x><y>-64.62506581228018</y></position><config><bulletinLevel>WARN</bulletinLevel><comments></comments><concurrentlySchedulableTaskCount>1</concurrentlySchedulableTaskCount><defaultConcurrentTasks><entry><key>TIMER_DRIVEN</key><value>1</value></entry><entry><key>EVENT_DRIVEN</key><value>0</value></entry><entry><key>CRON_DRIVEN</key><value>1</value></entry></defaultConcurrentTasks><defaultSchedulingPeriod><entry><key>TIMER_DRIVEN</key><value>0 sec</value></entry><entry><key>CRON_DRIVEN</key><value>* * * * * ?</value></entry></defaultSchedulingPeriod><descriptors><entry><key>Script Engine</key><value><allowableValues><displayName>ECMAScript</displayName><value>ECMAScript</value></allowableValues><allowableValues><displayName>Groovy</displayName><value>Groovy</value></allowableValues><allowableValues><displayName>lua</displayName><value>lua</value></allowableValues><allowableValues><displayName>python</displayName><value>python</value></allowableValues><allowableValues><displayName>ruby</displayName><value>ruby</value></allowableValues><defaultValue>ECMAScript</defaultValue><description>The engine to execute scripts</description><displayName>Script Engine</displayName><dynamic>false</dynamic><name>Script Engine</name><required>true</required><sensitive>false</sensitive><supportsEl>false</supportsEl></value></entry><entry><key>Script File</key><value><description>Path to script file to execute. Only one of Script File or Script Body may be used</description><displayName>Script File</displayName><dynamic>false</dynamic><name>Script File</name><required>false</required><sensitive>false</sensitive><supportsEl>true</supportsEl></value></entry><entry><key>Script Body</key><value><description>Body of script to execute. Only one of Script File or Script Body may be used</description><displayName>Script Body</displayName><dynamic>false</dynamic><name>Script Body</name><required>false</required><sensitive>false</sensitive><supportsEl>false</supportsEl></value></entry><entry><key>Module Directory</key><value><description>Comma-separated list of paths to files and/or directories which contain modules required by the script.</description><displayName>Module Directory</displayName><dynamic>false</dynamic><name>Module Directory</name><required>false</required><sensitive>false</sensitive><supportsEl>false</supportsEl></value></entry><entry><key>File Content</key><value><description>The content for the generated flow file</description><displayName>File Content</displayName><dynamic>false</dynamic><name>File Content</name><required>false</required><sensitive>false</sensitive><supportsEl>true</supportsEl></value></entry><entry><key>Evaluate Expressions in Content</key><value><allowableValues><displayName>true</displayName><value>true</value></allowableValues><allowableValues><displayName>false</displayName><value>false</value></allowableValues><defaultValue>false</defaultValue><description>Whether to evaluate NiFi Expression Language constructs within the content</description><displayName>Evaluate Expressions in Content</displayName><dynamic>false</dynamic><name>Evaluate Expressions in Content</name><required>true</required><sensitive>false</sensitive><supportsEl>false</supportsEl></value></entry><entry><key>Filename</key><value><description>The name of the flow file to be stored in the filename attribute</description><displayName>Filename</displayName><dynamic>false</dynamic><name>Filename</name><required>false</required><sensitive>false</sensitive><supportsEl>true</supportsEl></value></entry></descriptors><lossTolerant>false</lossTolerant><penaltyDuration>30 sec</penaltyDuration><properties><entry><key>Script Engine</key><value>Groovy</value></entry><entry><key>Script File</key></entry><entry><key>Script Body</key><value>class GenerateFlowFileWithContent implements Processor {
  2.  
  3. def REL_SUCCESS = new Relationship.Builder()
  4. .name('success')
  5. .description('The flow file with the specified content and/or filename was successfully transferred')
  6. .build();
  7.  
  8. def CONTENT = new PropertyDescriptor.Builder()
  9. .name('File Content').description('The content for the generated flow file')
  10. .required(false).expressionLanguageSupported(true).addValidator(Validator.VALID).build()
  11.  
  12. def CONTENT_HAS_EL = new PropertyDescriptor.Builder()
  13. .name('Evaluate Expressions in Content').description('Whether to evaluate NiFi Expression Language constructs within the content')
  14. .required(true).allowableValues('true','false').defaultValue('false').build()
  15.  
  16. def FILENAME = new PropertyDescriptor.Builder()
  17. .name('Filename').description('The name of the flow file to be stored in the filename attribute')
  18. .required(false).expressionLanguageSupported(true).addValidator(StandardValidators.NON_EMPTY_VALIDATOR).build()
  19.  
  20. @Override
  21. void initialize(ProcessorInitializationContext context) { }
  22.  
  23. @Override
  24. Set<Relationship> getRelationships() { return [REL_SUCCESS] as Set }
  25.  
  26. @Override
  27. void onTrigger(ProcessContext context, ProcessSessionFactory sessionFactory) throws ProcessException {
  28. try {
  29. def session = sessionFactory.createSession()
  30. def flowFile = session.create()
  31.  
  32. def hasEL = context.getProperty(CONTENT_HAS_EL).asBoolean()
  33. def contentProp = context.getProperty(CONTENT)
  34. def content = (hasEL ? contentProp.evaluateAttributeExpressions().value : contentProp.value) ?: ''
  35. def filename = context.getProperty(FILENAME)?.evaluateAttributeExpressions()?.getValue()
  36.  
  37. flowFile = session.write(flowFile, { outStream ->
  38. outStream.write(content.getBytes("UTF-8"))
  39. } as OutputStreamCallback)
  40.  
  41. if(filename != null) { flowFile = session.putAttribute(flowFile, 'filename', filename) }
  42. // transfer
  43. session.transfer(flowFile, REL_SUCCESS)
  44. session.commit()
  45. } catch(e) {
  46. throw new ProcessException(e)
  47. }
  48. }
  49.  
  50. @Override
  51. Collection<ValidationResult> validate(ValidationContext context) { return null }
  52.  
  53. @Override
  54. PropertyDescriptor getPropertyDescriptor(String name) {
  55. switch(name) {
  56. case 'File Content': return CONTENT
  57. case 'Evaluate Expressions in Content': return CONTENT_HAS_EL
  58. case 'Filename': return FILENAME
  59. default: return null
  60. }
  61. }
  62.  
  63. @Override
  64. void onPropertyModified(PropertyDescriptor descriptor, String oldValue, String newValue) { }
  65.  
  66. @Override
  67. List<PropertyDescriptor> getPropertyDescriptors() { return [CONTENT, CONTENT_HAS_EL, FILENAME] as List }
  68.  
  69. @Override
  70. String getIdentifier() { return 'GenerateFlowFile-InvokeScriptedProcessor' }
  71.  
  72. }
  73.  
  74. processor = new GenerateFlowFileWithContent()</value></entry><entry><key>Module Directory</key></entry><entry><key>File Content</key></entry><entry><key>Evaluate Expressions in Content</key><value>false</value></entry><entry><key>Filename</key></entry></properties><runDurationMillis>0</runDurationMillis><schedulingPeriod>3 sec</schedulingPeriod><schedulingStrategy>TIMER_DRIVEN</schedulingStrategy><yieldDuration>1 sec</yieldDuration></config><name>GenerateFlowFileWithContent</name><relationships><autoTerminate>false</autoTerminate><description>The flow file with the specified content and/or filename was successfully transferred</description><name>success</name></relationships><state>STOPPED</state><style/><supportsEventDriven>false</supportsEventDriven><supportsParallelProcessing>true</supportsParallelProcessing><type>org.apache.nifi.processors.script.InvokeScriptedProcessor</type></processors></snippet><timestamp>02/24/2016 17:34:25 EST</timestamp></template>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement