本文整理汇总了Java中org.apache.nifi.processor.ProcessSession.putAttribute方法的典型用法代码示例。如果您正苦于以下问题:Java ProcessSession.putAttribute方法的具体用法?Java ProcessSession.putAttribute怎么用?Java ProcessSession.putAttribute使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.nifi.processor.ProcessSession
的用法示例。
在下文中一共展示了ProcessSession.putAttribute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: singleAttributeTest
import org.apache.nifi.processor.ProcessSession; //导入方法依赖的package包/类
@Test
public void singleAttributeTest() {
TestRunner runner = TestRunners.newTestRunner(Md5HashAttributes.class);
runner.setProperty("hash", "${attribute1}");
ProcessSession session = runner.getProcessSessionFactory().createSession();
FlowFile ff = session.create();
ff = session.putAttribute(ff, "attribute1", "pbs.twimg.com/media/ClvFsHiUgAE4fT6.jpg");
runner.enqueue(ff);
runner.run();
// All results were processed with out failure
runner.assertQueueEmpty();
runner.assertTransferCount(Md5HashAttributes.REL_SUCCESS, 1);
// If you need to read or do additional tests on results you can access the content
List<MockFlowFile> results = runner.getFlowFilesForRelationship(Md5HashAttributes.REL_SUCCESS);
assertTrue("1 match", results.size() == 1);
MockFlowFile result = results.get(0);
result.assertAttributeEquals("hash", "e2c26a2479f497562a615a42ecb1ef7e");
}
示例2: testQuery
import org.apache.nifi.processor.ProcessSession; //导入方法依赖的package包/类
@Test
public void testQuery() throws Exception {
final TestRunner runner = TestRunners.newTestRunner(new QueryMongo());
addMongoService(runner);
runner.setProperty(MongoProps.DATABASE, MONGO_DATABASE_NAME);
runner.setProperty(MongoProps.COLLECTION, "insert_test");
runner.setProperty(MongoProps.QUERY, "{\"criteria\": \"${test_attribute}\"}");
ProcessSession session = runner.getProcessSessionFactory().createSession();
FlowFile ff = session.create();
ff = session.putAttribute(ff, "test_attribute", "12345");
runner.enqueue(ff);
runner.run();
runner.assertTransferCount(AbstractMongoProcessor.REL_FAILURE, 0);
runner.assertTransferCount(AbstractMongoProcessor.REL_SUCCESS, 1);
MockFlowFile out = runner.getFlowFilesForRelationship(AbstractMongoProcessor.REL_SUCCESS).get(0);
BasicDBObject actual = (BasicDBObject) JSON.parse(new String(out.toByteArray(), StandardCharsets.UTF_8));
assertEquals("[ \"12345\" , \"23456\" , \"34567\"]", actual.getString("criteria"));
}
示例3: testNoResults
import org.apache.nifi.processor.ProcessSession; //导入方法依赖的package包/类
@Test
public void testNoResults() throws Exception {
final TestRunner runner = TestRunners.newTestRunner(new QueryMongo());
addMongoService(runner);
runner.setProperty(MongoProps.DATABASE, MONGO_DATABASE_NAME);
runner.setProperty(MongoProps.COLLECTION, "insert_test");
runner.setProperty(MongoProps.QUERY, "{\"criteria\": \"${test_attribute}\"}");
ProcessSession session = runner.getProcessSessionFactory().createSession();
FlowFile ff = session.create();
ff = session.putAttribute(ff, "test_attribute", "98765");
runner.enqueue(ff);
runner.run();
runner.assertTransferCount(AbstractMongoProcessor.REL_FAILURE, 0);
runner.assertTransferCount(AbstractMongoProcessor.REL_SUCCESS, 0);
}
示例4: testAttributesInTemplate
import org.apache.nifi.processor.ProcessSession; //导入方法依赖的package包/类
@Test
public void testAttributesInTemplate() throws Exception {
final TestRunner testRunner = TestRunners.newTestRunner(new PutFileFromTemplate());
testRunner.setProperty(PutFileFromTemplate.TEMPLATE_PROPERTY, "hello_{{ attributes.attr }}");
ProcessSession session = testRunner.getProcessSessionFactory().createSession();
FlowFile ff = session.create();
ff = session.putAttribute(ff, "attr", "test");
testRunner.enqueue(ff);
testRunner.run();
MockFlowFile successFlowFile = testRunner.getFlowFilesForRelationship(PutFileFromTemplate.SUCCESS_RELATIONSHIP).get(0);
successFlowFile.assertAttributeExists(PutFileFromTemplate.DEFAULT_OUTPUT_PATH_SAVED_IN_ATTRIBUTE);
String outputFilePath = successFlowFile.getAttribute(PutFileFromTemplate.DEFAULT_OUTPUT_PATH_SAVED_IN_ATTRIBUTE);
String renderedTemplate = FileUtils.readFileToString(new File(outputFilePath));
assertEquals(renderedTemplate, "hello_test");
testRunner.assertTransferCount(PutFileFromTemplate.SUCCESS_RELATIONSHIP, 1);
testRunner.assertTransferCount(PutFileFromTemplate.FAILURE_RELATIONSHIP, 0);
testRunner.assertTransferCount(PutFileFromTemplate.JSON_PARSING_FAILURE_RELATIONSHIP, 0);
}
示例5: onTrigger
import org.apache.nifi.processor.ProcessSession; //导入方法依赖的package包/类
@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
// TODO Auto-generated method stub
FlowFile flowfile = session.get();
flowfile = session.putAttribute(flowfile, "Directory", "/home/sivaprakash/Siva/Edgent/NifiTestout");
session.transfer(flowfile,REL_SUCCESS);
}
示例6: onTrigger
import org.apache.nifi.processor.ProcessSession; //导入方法依赖的package包/类
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
FlowFile flowFile = session.get();
if (flowFile == null) {
flowFile = session.create();
}
try {
flowFile.getAttributes();
flowFile = session.putAttribute(flowFile, "mime.type", "application/json");
flowFile = session.write(flowFile, new StreamCallback() {
@Override
public void process(InputStream inputStream, OutputStream outputStream) throws IOException {
Tika tika = new Tika();
String text = "";
try {
text = tika.parseToString(inputStream);
} catch (TikaException e) {
getLogger().error("Apache Tika failed to parse input " + e.getLocalizedMessage());
e.printStackTrace();
}
// TODO: wrap in JSON???
outputStream.write(text.getBytes());
}
});
session.transfer(flowFile, REL_SUCCESS);
session.commit();
} catch (final Throwable t) {
getLogger().error("Unable to process ExtractTextProcessor file " + t.getLocalizedMessage());
getLogger().error("{} failed to process due to {}; rolling back session", new Object[] { this, t });
throw t;
}
}
示例7: onTrigger
import org.apache.nifi.processor.ProcessSession; //导入方法依赖的package包/类
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
FlowFile flowFile = session.get();
if ( flowFile == null ) {
flowFile = session.create();
}
try {
flowFile.getAttributes();
service = new SentimentService();
String sentence = flowFile.getAttribute(ATTRIBUTE_INPUT_NAME);
String sentence2 = context.getProperty(ATTRIBUTE_INPUT_NAME).evaluateAttributeExpressions(flowFile).getValue();
if ( sentence == null) {
sentence = sentence2;
}
if ( sentence == null) {
return;
}
String value = service.getSentiment( sentence );
if ( value == null) {
return;
}
flowFile = session.putAttribute(flowFile, "mime.type", "application/json");
flowFile = session.putAttribute(flowFile, ATTRIBUTE_OUTPUT_NAME, value);
session.transfer(flowFile, REL_SUCCESS);
session.commit();
} catch (final Throwable t) {
getLogger().error("Unable to process Sentiment Processor file " + t.getLocalizedMessage()) ;
getLogger().error("{} failed to process due to {}; rolling back session", new Object[]{this, t});
throw t;
}
}
示例8: onTrigger
import org.apache.nifi.processor.ProcessSession; //导入方法依赖的package包/类
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
FlowFile flowFile = session.get();
if (flowFile == null) {
return;
}
final byte[] buffer = new byte[(int) flowFile.getSize()];
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(buffer)) {
session.read(flowFile, in -> StreamUtils.fillBuffer(in, buffer));
try (ZipInputStream zipInputStream = new ZipInputStream(byteArrayInputStream)) {
// Check the first entry.
ZipEntry zipEntry = zipInputStream.getNextEntry();
String name = "";
if (zipEntry != null) {
name = zipEntry.getName();
}
flowFile = session.putAttribute(flowFile, ATTR, name);
session.getProvenanceReporter().modifyAttributes(flowFile);
session.transfer(flowFile, REL_SUCCESS);
}
} catch (Exception e) {
getLogger().error("Unable to update flowFile {} due to {}", new Object[]{flowFile, e.getMessage()}, e);
session.transfer(flowFile, REL_FAILURE);
}
}
示例9: multipleAttributeTest
import org.apache.nifi.processor.ProcessSession; //导入方法依赖的package包/类
@Test
public void multipleAttributeTest() {
TestRunner runner = TestRunners.newTestRunner(Md5HashAttributes.class);
runner.setProperty("hash", "${attribute1}");
runner.setProperty("hash2", "${attribute2}");
ProcessSession session = runner.getProcessSessionFactory().createSession();
FlowFile ff = session.create();
ff = session.putAttribute(ff, "attribute1", "pbs.twimg.com/media/ClvFsHiUgAE4fT6.jpg");
ff = session.putAttribute(ff, "attribute2", "");
runner.enqueue(ff);
runner.run();
// All results were processed with out failure
runner.assertQueueEmpty();
runner.assertTransferCount(Md5HashAttributes.REL_SUCCESS, 1);
// If you need to read or do additional tests on results you can access the content
List<MockFlowFile> results = runner.getFlowFilesForRelationship(Md5HashAttributes.REL_SUCCESS);
assertTrue("1 match", results.size() == 1);
MockFlowFile result = results.get(0);
result.assertAttributeEquals("hash", "e2c26a2479f497562a615a42ecb1ef7e");
result.assertAttributeEquals("hash2", "d41d8cd98f00b204e9800998ecf8427e");
}
示例10: 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<List<Map.Entry<File, Boolean>>> value = new AtomicReference<>();
final File tempDir = new File(context.getProperty(TEMP_DIR).getValue());
System.getProperties().setProperty("jna.library.path", context.getProperty(JNI_PATH).getValue());
FlowFile flowfile = session.get();
session.read(flowfile, in -> {
try {
value.set(convert(in, tempDir));
}
catch(Exception e) {
log.error("Unable to convert: " + e.getMessage(), e);
}
});
if(value.get() != null) {
for(Map.Entry<File, Boolean> kv : value.get()) {
final File convertedFile = kv.getKey();
try {
final int pageNumber = getPageNumber(convertedFile.getName());
if(kv.getValue()) {
FlowFile ff = session.clone(flowfile);
ff = session.putAttribute(ff, "pageNumber", "" + pageNumber);
ff = session.write(ff, out -> IOUtils.copy(new BufferedInputStream(new FileInputStream(convertedFile)), out));
session.transfer(ff, SUCCESS);
}
}
finally {
if(convertedFile != null && convertedFile.exists()) {
convertedFile.delete();
}
}
}
}
session.transfer(flowfile, RAW);
}
示例11: onTrigger
import org.apache.nifi.processor.ProcessSession; //导入方法依赖的package包/类
@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
// TODO Auto-generated method stub
resultMap = new HashMap<String,String>();
System.out.println("Started Nifi");
FlowFile flowfile = session.get();
String jsonDir = flowfile.getAttribute("sourceDir");
String batchID = flowfile.getAttribute("batchID");
// why are they being removed?
//if(batchMap.containsKey(batchID)) {
// session.remove(flowfile);
// return;
//}
batchMap.put(batchID, 1);
DirectProvider dp = new DirectProvider();
Topology topology = dp.newTopology("Edgent Application");
String objectClass = context.getProperty(OBJECT_CLASS).getValue();
getLogger().info("Created Edgent Topology: "+topology.getName());
TempSource source = new TempSource(jsonDir +"/out");
TStream<Pair<String,String>> sourceStream = topology.source(source);
TStream<Pair<String,String>> filteredStream = sourceStream.filter(new Predicate<Pair<String,String>>() {
@Override
public boolean test(Pair<String,String> map) {
boolean isValid = false;
String jsonString = map.getRight();
JsonArray jsonArray = (new Gson()).fromJson(jsonString, JsonArray.class);
for(int i=0;i<jsonArray.size();i++)
{
JsonObject jsonObject = jsonArray.get(i).getAsJsonObject();
if(jsonObject.get("label").getAsString().equals(objectClass)) {
isValid = true;
break;
}
}
return isValid;
}
});
filteredStream.sink(tuple -> update(tuple));
dp.submit(topology);
System.out.println("Submitted Topology");
// Why are we sleeping here?
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(resultMap.size());
// To write the results back out ot flow file
session.remove(flowfile);
//for(Map.Entry<String, String> entry:resultMap.entrySet())
if(resultMap.size()>0){
FlowFile flowFile = session.create();
flowFile = session.putAttribute(flowFile, "batchID", batchID);
// should imageID be a NULL string?
flowFile = session.putAttribute(flowFile, "imageID", "");
flowFile = session.putAttribute(flowFile, "fromEdgent", "1");
session.transfer(flowFile,REL_SUCCESS);
}
}
示例12: onTrigger
import org.apache.nifi.processor.ProcessSession; //导入方法依赖的package包/类
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
FlowFile flowFile = session.get();
if (flowFile == null) {
flowFile = session.create();
}
try {
flowFile.getAttributes();
String imagepath = flowFile.getAttribute(ATTRIBUTE_INPUT_NAME);
String imagepath2 = context.getProperty(ATTRIBUTE_INPUT_NAME).evaluateAttributeExpressions(flowFile)
.getValue();
if (imagepath == null) {
imagepath = imagepath2;
}
if (imagepath == null) {
return;
}
String modelDir = flowFile.getAttribute(ATTRIBUTE_INPUT_NAME2);
String modelDir2 = context.getProperty(ATTRIBUTE_INPUT_NAME2).evaluateAttributeExpressions(flowFile)
.getValue();
if (modelDir == null) {
modelDir = modelDir2;
}
if (modelDir == null) {
modelDir = "/models";
}
service = new DL4JService();
String value = service.getInception(imagepath, modelDir);
if (value == null) {
return;
}
flowFile = session.putAttribute(flowFile, "mime.type", "application/json");
flowFile = session.putAttribute(flowFile, ATTRIBUTE_OUTPUT_NAME, value);
session.transfer(flowFile, REL_SUCCESS);
session.commit();
} catch (final Throwable t) {
getLogger().error("Unable to process DL4J Processor file " + t.getLocalizedMessage());
getLogger().error("{} failed to process due to {}; rolling back session", new Object[] { this, t });
throw t;
}
}
示例13: onTrigger
import org.apache.nifi.processor.ProcessSession; //导入方法依赖的package包/类
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
FlowFile flowFile = session.get();
if ( flowFile == null ) {
flowFile = session.create();
}
try {
flowFile.getAttributes();
service = new OpenNLPService();
String sentence = flowFile.getAttribute(ATTRIBUTE_INPUT_NAME);
String sentence2 = context.getProperty(ATTRIBUTE_INPUT_NAME).evaluateAttributeExpressions(flowFile).getValue();
if ( sentence == null) {
sentence = sentence2;
}
if ( sentence == null) {
return;
}
String value = service.getPeople(context.getProperty(EXTRA_RESOURCE).evaluateAttributeExpressions(flowFile).getValue(), sentence);
if ( value == null) {
return;
}
flowFile = session.putAttribute(flowFile, "mime.type", "application/json");
flowFile = session.putAttribute(flowFile, ATTRIBUTE_OUTPUT_NAME, value);
try {
String locations = service.getLocations(context.getProperty(EXTRA_RESOURCE).evaluateAttributeExpressions(flowFile).getValue(), sentence);
flowFile = session.putAttribute(flowFile, ATTRIBUTE_OUTPUT_LOCATION_NAME, locations);
} catch (Exception e) {
e.printStackTrace();
}
session.transfer(flowFile, REL_SUCCESS);
session.commit();
} catch (final Throwable t) {
getLogger().error("Unable to process NLP Processor file " + t.getLocalizedMessage()) ;
getLogger().error("{} failed to process due to {}; rolling back session", new Object[]{this, t});
throw t;
}
}
示例14: onTrigger
import org.apache.nifi.processor.ProcessSession; //导入方法依赖的package包/类
@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
FlowFile flowFile = session.get();
if (flowFile == null) {
return;
}
ComponentLog logger = this.getLogger();
// Evaluate expression language and create BSON Documents
Document query = (queryProperty.isSet()) ? Document.parse(queryProperty.evaluateAttributeExpressions(flowFile).getValue()) : null;
Document projection = (projectionProperty.isSet()) ? Document.parse(projectionProperty.evaluateAttributeExpressions(flowFile).getValue()) : null;
Document sort = (sortProperty.isSet()) ? Document.parse(sortProperty.evaluateAttributeExpressions(flowFile).getValue()) : null;
try {
FindIterable<Document> it = (query != null) ? collection.find(query) : collection.find();
// Apply projection if needed
if (projection != null) {
it.projection(projection);
}
// Apply sort if needed
if (sort != null) {
it.sort(sort);
}
// Apply limit if set
if (limit != null) {
it.limit(limit.intValue());
}
// Iterate and create flowfile for each result
final MongoCursor<Document> cursor = it.iterator();
try {
while (cursor.hasNext()) {
// Create new flowfile with all parent attributes
FlowFile ff = session.clone(flowFile);
ff = session.write(ff, new OutputStreamCallback() {
@Override
public void process(OutputStream outputStream) throws IOException {
IOUtils.write(cursor.next().toJson(), outputStream);
}
});
session.transfer(ff, REL_SUCCESS);
}
} finally {
cursor.close();
session.remove(flowFile);
}
} catch (Exception e) {
logger.error("Failed to execute query {} due to {}.", new Object[]{query, e}, e);
flowFile = session.putAttribute(flowFile, "mongo.exception", e.getMessage());
session.transfer(flowFile, REL_FAILURE);
}
}
示例15: onTrigger
import org.apache.nifi.processor.ProcessSession; //导入方法依赖的package包/类
@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
final List<FlowFile> flowFiles = session.get(batchSize);
if (flowFiles == null) {
return;
}
ComponentLog logger = this.getLogger();
final String source = context.getProperty(INSERT_COMMAND_SOURCE).getValue();
List<InsertOneModel<Document>> documentsToInsert = new ArrayList<>(flowFiles.size());
/*
* Collect FlowFiles that are marked for bulk insertion. Matches same
* index as documentsToInsert
*/
List<FlowFile> flowFilesAttemptedInsert = new ArrayList<>();
logger.debug("Attempting to batch insert {} FlowFiles", new Object[]{flowFiles.size()});
for (FlowFile flowFile : flowFiles) {
final String payload;
try {
switch (source) {
case "content":
final String[] result = new String[1];
session.read(flowFile, (in) -> result[0] = IOUtils.toString(in));
payload = result[0];
break;
case "attribute":
String command = context.getProperty(INSERT_COMMAND_ATTRIBUTE).evaluateAttributeExpressions(flowFile).getValue();
payload = flowFile.getAttribute(command);
break;
default:
throw new Exception("Invalid source choice: " + source);
}
BasicDBObject parse = (BasicDBObject) JSON.parse(payload);
Document documentToInsert = new Document(parse.toMap());
logger.debug("Creating InsertOneModel with Document {}", new Object[]{documentToInsert});
InsertOneModel<Document> iom = new InsertOneModel<>(documentToInsert);
documentsToInsert.add(iom);
} catch (Exception e) {
/*
* If any FlowFiles error on translation to a Mongo Object, they were not added to
* the documentsToInsert, so route to failure immediately
*/
logger.error("Encountered exception while processing FlowFile for Mongo Storage. Routing to failure and continuing.", e);
FlowFile failureFlowFile = session.putAttribute(flowFile, "mongo.exception", e.getMessage());
session.transfer(failureFlowFile, REL_FAILURE);
continue;
}
// add to the ordered list so we can determine which fail on bulk
// write
flowFilesAttemptedInsert.add(flowFile);
}
/*
* Perform the bulk insert if any documents are there to insert
*/
if (!documentsToInsert.isEmpty()) {
logger.debug("Attempting to bulk insert {} documents", new Object[]{documentsToInsert.size()});
Map<Integer, BulkWriteError> writeErrors = executeBulkInsert(documentsToInsert);
/*
* Route FlowFiles to the proper relationship based on the returned
* errors
*/
logger.debug("Evaluating FlowFile routing against {} Write Errors for {} FlowFiles", new Object[]{writeErrors.size(), flowFilesAttemptedInsert.size()});
transferFlowFiles(session, flowFilesAttemptedInsert, writeErrors);
}
}