本文整理汇总了Java中org.apache.nifi.processor.ProcessSession类的典型用法代码示例。如果您正苦于以下问题:Java ProcessSession类的具体用法?Java ProcessSession怎么用?Java ProcessSession使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ProcessSession类属于org.apache.nifi.processor包,在下文中一共展示了ProcessSession类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onTrigger
import org.apache.nifi.processor.ProcessSession; //导入依赖的package包/类
@Override
public void onTrigger(ProcessContext context, ProcessSession session) {
FlowFile original = session.get();
FlowFile flowFile;
if (original == null) {
flowFile = session.create();
session.getProvenanceReporter().create(flowFile);
} else {
flowFile = session.clone(original);
session.transfer(original, REL_ORIGINAL);
}
final String updatedContent = StringFormatter.format(context.getProperty(CONTENT_FIELD).getValue(), flowFile.getAttributes());
this.getLogger().debug("Created content: {}", new Object[]{updatedContent});
flowFile = session.write(flowFile, outputStream -> outputStream.write(updatedContent.getBytes(StandardCharsets.UTF_8)));
session.getProvenanceReporter().modifyContent(flowFile);
session.transfer(flowFile, REL_SUCCESS);
}
示例2: onTrigger
import org.apache.nifi.processor.ProcessSession; //导入依赖的package包/类
@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
final ProcessorLog log = this.getLogger();
final AtomicReference<String> value = new AtomicReference<>();
final Map<String, String> tessProperties = toProperties(context.getProperty(TESS_PROPERTIES).getValue());
final File tessDataDir = new File(context.getProperty(TESS_DATA).getValue());
System.getProperties().setProperty("jna.library.path", context.getProperty(JNI_PATH).getValue());
FlowFile flowfile = session.get();
if (null != flowfile) {
session.read(flowfile, in -> {
try {
value.set(TesseractUtil.INSTANCE.ocr(in, tessDataDir, tessProperties));
} catch (Exception e) {
log.error("Unable to ocr: " + e.getMessage(), e);
}
});
flowfile = session.write(flowfile, out -> {
out.write(value.get().getBytes());
out.flush();
});
session.transfer(flowfile, SUCCESS);
} else {
log.warn("NULL flow file");
}
}
示例3: rendezvousWithJms
import org.apache.nifi.processor.ProcessSession; //导入依赖的package包/类
/**
* Will construct JMS {@link Message} by extracting its body from the
* incoming {@link FlowFile}. {@link FlowFile} attributes that represent
* standard JMS headers will be extracted from the {@link FlowFile} and set
* as JMS headers on the newly constructed message. For the list of
* available message headers please see {@link JmsHeaders}. <br>
* <br>
* Upon success the incoming {@link FlowFile} is transferred to the'success'
* {@link Relationship} and upon failure FlowFile is penalized and
* transferred to the 'failure' {@link Relationship}
*
*/
@Override
protected void rendezvousWithJms(ProcessContext context, ProcessSession processSession) throws ProcessException {
FlowFile flowFile = processSession.get();
if (flowFile != null) {
try {
String destinationName = context.getProperty(DESTINATION).evaluateAttributeExpressions(flowFile).getValue();
this.targetResource.publish(destinationName, this.extractMessageBody(flowFile, processSession), flowFile.getAttributes());
processSession.transfer(flowFile, REL_SUCCESS);
processSession.getProvenanceReporter().send(flowFile, context.getProperty(DESTINATION).evaluateAttributeExpressions().getValue());
} catch (Exception e) {
processSession.transfer(flowFile, REL_FAILURE);
this.getLogger().error("Failed while sending message to JMS via " + this.targetResource, e);
context.yield();
}
}
}
示例4: createPut
import org.apache.nifi.processor.ProcessSession; //导入依赖的package包/类
@Override
protected PutFlowFile createPut(final ProcessSession session, final ProcessContext context, final FlowFile flowFile) {
final String tableName = context.getProperty(TABLE_NAME).evaluateAttributeExpressions(flowFile).getValue();
final String row = context.getProperty(ROW_ID).evaluateAttributeExpressions(flowFile).getValue();
final String columnFamily = context.getProperty(COLUMN_FAMILY).evaluateAttributeExpressions(flowFile).getValue();
final String columnQualifier = context.getProperty(COLUMN_QUALIFIER).evaluateAttributeExpressions(flowFile).getValue();
final String columnVisibility = context.getProperty(COLUMN_VISIBILITY).evaluateAttributeExpressions(flowFile).getValue();
final byte[] buffer = new byte[(int) flowFile.getSize()];
session.read(flowFile, new InputStreamCallback() {
@Override
public void process(final InputStream in) throws IOException {
StreamUtils.fillBuffer(in, buffer);
}
});
final Collection<PutMutation> mutations = Collections.singletonList(new PutMutation(columnFamily, columnQualifier, columnVisibility, buffer.toString()));
return new PutFlowFile(tableName, row, mutations, flowFile);
}
示例5: onTrigger
import org.apache.nifi.processor.ProcessSession; //导入依赖的package包/类
@Override
public void onTrigger(ProcessContext context, ProcessSession session) {
DesiredCapabilities DesireCaps = new DesiredCapabilities();
DesireCaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, context.getProperty(DRIVER_LOCATION).getValue());
driver = new PhantomJSDriver(DesireCaps);
FlowFile flowFile = session.create();
try {
driver.get(url);
(new WebDriverWait(driver, timeout)).until(
ExpectedConditions.visibilityOfElementLocated(getExpectedCondition(selectorType, selector))
);
final byte[] page = formatToXHtml(driver.getPageSource(), StandardCharsets.UTF_8);
flowFile = session.write(flowFile, outputStream -> outputStream.write(page));
session.transfer(flowFile, REL_SUCCESS);
} catch (Exception e) {
flowFile = session.write(flowFile, outputStream -> outputStream.write(e.getMessage().getBytes()));
session.transfer(flowFile, REL_FAILURE);
} finally {
driver.quit();
}
session.getProvenanceReporter().create(flowFile);
}
示例6: sendStatsIfPresent
import org.apache.nifi.processor.ProcessSession; //导入依赖的package包/类
/**
* Send a flowfile with the stats as attributes: IF the report time is exceeded AND there are
* stats to send
*/
private void sendStatsIfPresent(ProcessSession session, long currentTimestamp) {
if (currentTimestamp < lastReportTime + reportingIntervalMillis) {
return;
}
for (Map.Entry<String, Optional<Map<String, String>>> statsMap : latestStats.entrySet()) {
String statsMapKey = statsMap.getKey();
Optional<Map<String, String>> result = buildStatAttributes(currentTimestamp, momentsMap.get(statsMapKey));
if (result.isPresent()) {
Map<String, String> attrs = new HashMap<>(result.get());
attrs.put("AbstractStatsProcessor.correlationKey", statsMapKey);
String uuid = UUID.randomUUID().toString();
attrs.put(CoreAttributes.UUID.key(), uuid);
attrs.put(CoreAttributes.FILENAME.key(), statsMapKey + "_" + System.currentTimeMillis() + "_" + uuid);
FlowFile statsFlowFile = session.create();
statsFlowFile = session.putAllAttributes(statsFlowFile, attrs);
session.getProvenanceReporter().create(statsFlowFile);
session.transfer(statsFlowFile, REL_STATS);
}
}
lastReportTime = currentTimestamp;
momentsMap.values().forEach(MomentAggregator::reset);
}
示例7: onTrigger
import org.apache.nifi.processor.ProcessSession; //导入依赖的package包/类
@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
List<FlowFile> incoming = session.get(batchSize);
if (incoming.isEmpty()) {
return;
}
/*
Each relationship can have multiple connections.
context.getAvailableRelationships().contains(PASS_THROUGH) will return true if all
PASS_THROUGH connections are available (not back pressured). So if a PASS_THROUGH
relationship has 2 connections with back pressure of 10 and 20 and only the 10's
connection is back pressured, contains(PASS_THROUGH) will return false
*/
final boolean isPassthroughClear = context.getAvailableRelationships().contains(PASS_THROUGH);
session.transfer(incoming, isPassthroughClear ? PASS_THROUGH : BACK_PRESSURED);
}
示例8: payloadWithSubstitutions
import org.apache.nifi.processor.ProcessSession; //导入依赖的package包/类
@Test
public void payloadWithSubstitutions() {
String payload = "{\"a\": {{from}}, \"pi\": {{pi}}, \"boolean\": {{boolean}}}";
final TestRunner runner = TestRunners.newTestRunner(new CreateContent());
runner.setProperty(CreateContent.CONTENT_FIELD, payload);
ProcessSession session = runner.getProcessSessionFactory().createSession();
FlowFile ff = session.create();
Map<String, String> props = new HashMap<>();
props.put("from", "\"[email protected]\"");
props.put("pi", "3.14");
props.put("boolean", "true");
ff = session.putAllAttributes(ff, props);
runner.enqueue(ff);
runner.run();
runner.assertTransferCount(CreateContent.REL_FAILURE, 0);
runner.assertTransferCount(CreateContent.REL_SUCCESS, 1);
runner.assertTransferCount(CreateContent.REL_ORIGINAL, 1);
String expected = "{\"a\": \"[email protected]\", \"pi\": 3.14, \"boolean\": true}";
final MockFlowFile out = runner.getFlowFilesForRelationship(CreateContent.REL_SUCCESS).get(0);
assertEquals(expected, new String(out.toByteArray()));
}
示例9: payloadWithMissingAttributes
import org.apache.nifi.processor.ProcessSession; //导入依赖的package包/类
@Test
public void payloadWithMissingAttributes() {
String payload = "{\"a\": {{from}}, \"pi\": {{pi}}, \"boolean\": {{boolean}}}";
final TestRunner runner = TestRunners.newTestRunner(new CreateContent());
runner.setProperty(CreateContent.CONTENT_FIELD, payload);
ProcessSession session = runner.getProcessSessionFactory().createSession();
FlowFile ff = session.create();
Map<String, String> props = new HashMap<>();
props.put("from", "\"[email protected]\"");
props.put("pi", "3.14");
ff = session.putAllAttributes(ff, props);
runner.enqueue(ff);
runner.run();
runner.assertTransferCount(CreateContent.REL_FAILURE, 0);
runner.assertTransferCount(CreateContent.REL_SUCCESS, 1);
runner.assertTransferCount(CreateContent.REL_ORIGINAL, 1);
String expected = "{\"a\": \"[email protected]\", \"pi\": 3.14, \"boolean\": null}";
MockFlowFile out = runner.getFlowFilesForRelationship(CreateContent.REL_SUCCESS).get(0);
String actual = new String(out.toByteArray());
assertEquals(expected, actual);
}
示例10: payloadWithTokenAttributes
import org.apache.nifi.processor.ProcessSession; //导入依赖的package包/类
@Test
public void payloadWithTokenAttributes() {
String payload = "{\"a\": {{from}}, \"pi\": {{pi}}, \"text\": {{text}}}";
final TestRunner runner = TestRunners.newTestRunner(new CreateContent());
runner.setProperty(CreateContent.CONTENT_FIELD, payload);
ProcessSession session = runner.getProcessSessionFactory().createSession();
FlowFile ff = session.create();
Map<String, String> props = new HashMap<>();
props.put("from", "\"[email protected]\"");
props.put("pi", "3.14");
props.put("text", "\"here is some {{text}}\"");
ff = session.putAllAttributes(ff, props);
runner.enqueue(ff);
runner.run();
runner.assertTransferCount(CreateContent.REL_FAILURE, 0);
runner.assertTransferCount(CreateContent.REL_SUCCESS, 1);
runner.assertTransferCount(CreateContent.REL_ORIGINAL, 1);
String expected = "{\"a\": \"[email protected]\", \"pi\": 3.14, \"text\": \"here is some {{text}}\"}";
MockFlowFile out = runner.getFlowFilesForRelationship(CreateContent.REL_SUCCESS).get(0);
String actual = new String(out.toByteArray());
assertEquals(expected, actual);
}
示例11: payloadWithSpecialCharacters
import org.apache.nifi.processor.ProcessSession; //导入依赖的package包/类
@Test
public void payloadWithSpecialCharacters() {
String payload = "{\"a\": {{from}}, \"pi\": {{pi}}, \"text\": {{text}}}";
final TestRunner runner = TestRunners.newTestRunner(new CreateContent());
runner.setProperty(CreateContent.CONTENT_FIELD, payload);
ProcessSession session = runner.getProcessSessionFactory().createSession();
FlowFile ff = session.create();
Map<String, String> props = new HashMap<>();
props.put("from", "\"[email protected]\"");
props.put("pi", "3.14");
props.put("text", "\"here is some &$^()}{{}[email protected]#\"");
ff = session.putAllAttributes(ff, props);
runner.enqueue(ff);
runner.run();
runner.assertTransferCount(CreateContent.REL_FAILURE, 0);
runner.assertTransferCount(CreateContent.REL_SUCCESS, 1);
runner.assertTransferCount(CreateContent.REL_ORIGINAL, 1);
String expected = "{\"a\": \"[email protected]\", \"pi\": 3.14, \"text\": \"here is some &$^()}{{}[email protected]#\"}";
MockFlowFile out = runner.getFlowFilesForRelationship(CreateContent.REL_SUCCESS).get(0);
String actual = new String(out.toByteArray());
assertEquals(expected, actual);
}
示例12: payloadWithDoubleQuotes
import org.apache.nifi.processor.ProcessSession; //导入依赖的package包/类
@Test
public void payloadWithDoubleQuotes() {
String payload = "{\"a\": {{from}}, \"pi\": {{pi}}, \"text\": {{text}}}";
final TestRunner runner = TestRunners.newTestRunner(new CreateContent());
runner.setProperty(CreateContent.CONTENT_FIELD, payload);
ProcessSession session = runner.getProcessSessionFactory().createSession();
FlowFile ff = session.create();
Map<String, String> props = new HashMap<>();
props.put("from", "\"[email protected]\"");
props.put("pi", "3.14");
props.put("text", "\"here is some \"text\"\"");
ff = session.putAllAttributes(ff, props);
runner.enqueue(ff);
runner.run();
runner.assertTransferCount(CreateContent.REL_FAILURE, 0);
runner.assertTransferCount(CreateContent.REL_SUCCESS, 1);
runner.assertTransferCount(CreateContent.REL_ORIGINAL, 1);
String expected = "{\"a\": \"[email protected]\", \"pi\": 3.14, \"text\": \"here is some \"text\"\"}";
MockFlowFile out = runner.getFlowFilesForRelationship(CreateContent.REL_SUCCESS).get(0);
String actual = new String(out.toByteArray());
assertEquals(expected, actual);
}
示例13: payloadWithSpacesWithinDelimiters
import org.apache.nifi.processor.ProcessSession; //导入依赖的package包/类
@Test
public void payloadWithSpacesWithinDelimiters() {
String payload = "{\"a\": {{ from}}, \"pi\": {{pi }}, \"text\": {{ text }}}";
final TestRunner runner = TestRunners.newTestRunner(new CreateContent());
runner.setProperty(CreateContent.CONTENT_FIELD, payload);
ProcessSession session = runner.getProcessSessionFactory().createSession();
FlowFile ff = session.create();
Map<String, String> props = new HashMap<>();
props.put("from", "\"[email protected]\"");
props.put("pi", "3.14");
props.put("text", "\"here is some text\"");
ff = session.putAllAttributes(ff, props);
runner.enqueue(ff);
runner.run();
runner.assertTransferCount(CreateContent.REL_FAILURE, 0);
runner.assertTransferCount(CreateContent.REL_SUCCESS, 1);
runner.assertTransferCount(CreateContent.REL_ORIGINAL, 1);
String expected = "{\"a\": \"[email protected]\", \"pi\": 3.14, \"text\": \"here is some text\"}";
MockFlowFile out = runner.getFlowFilesForRelationship(CreateContent.REL_SUCCESS).get(0);
String actual = new String(out.toByteArray());
assertEquals(expected, actual);
}
示例14: payloadWithSpanishCharacters
import org.apache.nifi.processor.ProcessSession; //导入依赖的package包/类
@Test
public void payloadWithSpanishCharacters() {
String payload = "{\"a\": {{from}}, \"pi\": {{pi}}, \"text\": {{text}}}";
final TestRunner runner = TestRunners.newTestRunner(new CreateContent());
runner.setProperty(CreateContent.CONTENT_FIELD, payload);
ProcessSession session = runner.getProcessSessionFactory().createSession();
FlowFile ff = session.create();
Map<String, String> props = new HashMap<>();
props.put("from", "\"[email protected]\"");
props.put("pi", "3.14");
props.put("text", "\"Mañana se me va un amigo capas el mejor que tuve y que voy a tener, te re quiero turrasdf Pasa que cada cosa que pienso/hago quiero contarle a mi él. Quiero gelatina y la bastarda no se hace mas -.-\"");
ff = session.putAllAttributes(ff, props);
runner.enqueue(ff);
runner.run();
runner.assertTransferCount(CreateContent.REL_FAILURE, 0);
runner.assertTransferCount(CreateContent.REL_SUCCESS, 1);
runner.assertTransferCount(CreateContent.REL_ORIGINAL, 1);
String expected = "{\"a\": \"[email protected]\", \"pi\": 3.14, \"text\": \"Mañana se me va un amigo capas el mejor que tuve y que voy a tener, te re quiero turrasdf Pasa que cada cosa que pienso/hago quiero contarle a mi él. Quiero gelatina y la bastarda no se hace mas -.-\"}";
MockFlowFile out = runner.getFlowFilesForRelationship(CreateContent.REL_SUCCESS).get(0);
String actual = new String(out.toByteArray());
assertEquals(expected, actual);
}
示例15: payloadWithSubstitutions
import org.apache.nifi.processor.ProcessSession; //导入依赖的package包/类
@Test
public void payloadWithSubstitutions() {
String payload = "{\"a\": \"{{from}}\", \"pi\": \"{{pi:float}}\", \"boolean\": \"{{boolean:bool}}\", \"long\": \"{{long:int}}\"}";
final TestRunner runner = TestRunners.newTestRunner(new CreateJsonContent());
runner.setProperty(CreateJsonContent.CONTENT_FIELD, payload);
ProcessSession session = runner.getProcessSessionFactory().createSession();
FlowFile ff = session.create();
Map<String, String> props = new HashMap<>();
props.put("from", "[email protected]");
props.put("pi", "3.14");
props.put("long", "12345678901234");
props.put("boolean", "true");
ff = session.putAllAttributes(ff, props);
runner.enqueue(ff);
runner.run();
runner.assertTransferCount(CreateJsonContent.REL_FAILURE, 0);
runner.assertTransferCount(CreateJsonContent.REL_SUCCESS, 1);
runner.assertTransferCount(CreateJsonContent.REL_ORIGINAL, 1);
String expected = "{\"a\":\"[email protected]\",\"pi\":3.14,\"boolean\":true,\"long\":12345678901234}";
final MockFlowFile out = runner.getFlowFilesForRelationship(CreateJsonContent.REL_SUCCESS).get(0);
assertEquals(expected, new String(out.toByteArray()));
}