本文整理汇总了Java中org.apache.nifi.processor.io.OutputStreamCallback类的典型用法代码示例。如果您正苦于以下问题:Java OutputStreamCallback类的具体用法?Java OutputStreamCallback怎么用?Java OutputStreamCallback使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
OutputStreamCallback类属于org.apache.nifi.processor.io包,在下文中一共展示了OutputStreamCallback类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: rendezvousWithJms
import org.apache.nifi.processor.io.OutputStreamCallback; //导入依赖的package包/类
/**
* Will construct a {@link FlowFile} containing the body of the consumed JMS
* message (if {@link GetResponse} returned by {@link JMSConsumer} is not
* null) and JMS properties that came with message which are added to a
* {@link FlowFile} as attributes, transferring {@link FlowFile} to
* 'success' {@link Relationship}.
*/
@Override
protected void rendezvousWithJms(final ProcessContext context, final ProcessSession processSession) throws ProcessException {
final String destinationName = context.getProperty(DESTINATION).evaluateAttributeExpressions().getValue();
this.targetResource.consume(destinationName, new ConsumerCallback(){
@Override
public void accept(final JMSResponse response) {
if (response != null){
FlowFile flowFile = processSession.create();
flowFile = processSession.write(flowFile, new OutputStreamCallback() {
@Override
public void process(final OutputStream out) throws IOException {
out.write(response.getMessageBody());
}
});
Map<String, Object> jmsHeaders = response.getMessageHeaders();
Map<String, Object> jmsProperties = Collections.<String, Object>unmodifiableMap(response.getMessageProperties());
flowFile = ConsumeJMS.this.updateFlowFileAttributesWithJMSAttributes(jmsHeaders, flowFile, processSession);
flowFile = ConsumeJMS.this.updateFlowFileAttributesWithJMSAttributes(jmsProperties, flowFile, processSession);
flowFile = processSession.putAttribute(flowFile, JMS_SOURCE_DESTINATION_NAME, destinationName);
processSession.getProvenanceReporter().receive(flowFile, destinationName);
processSession.transfer(flowFile, REL_SUCCESS);
processSession.commit();
} else {
context.yield();
}
}
});
}
示例2: extractFaces
import org.apache.nifi.processor.io.OutputStreamCallback; //导入依赖的package包/类
private static void extractFaces(ProcessSession session, FlowFile flowFile, AtomicReference<List<Rect>> value,
byte[] temporaryImageInMemory, List<FlowFile> splits) {
// send out the original colour image, not the grayscale one used to cascade.
Mat inputImage = Imgcodecs.imdecode(new MatOfByte(temporaryImageInMemory), Imgcodecs.IMREAD_UNCHANGED);
for (Rect face : value.get()) {
final Mat faceFile = inputImage.submat(face);
FlowFile faceFlowFile = session.create(flowFile);
faceFlowFile = session.write(faceFlowFile, new OutputStreamCallback() {
@Override
public void process(OutputStream out) throws IOException {
MatOfByte buf = new MatOfByte();
Imgcodecs.imencode(".png", faceFile, buf);
out.write(buf.toArray());
}
});
// add the face location attributes
Map<String, String> attributes = new HashMap<String, String>();
attributes.put("face.x", String.valueOf(face.x));
attributes.put("face.y", String.valueOf(face.y));
attributes.put("face.w", String.valueOf(face.width));
attributes.put("face.h", String.valueOf(face.height));
faceFlowFile = session.putAllAttributes(faceFlowFile, attributes);
splits.add(faceFlowFile);
}
}
示例3: onTrigger
import org.apache.nifi.processor.io.OutputStreamCallback; //导入依赖的package包/类
@Override
public void onTrigger(final ProcessContext context,
final ProcessSession session) throws ProcessException {
MatOfByte image = new MatOfByte();
MatOfByte bytemat = new MatOfByte();
camera.read(image);
Imgcodecs.imencode(".png", image, bytemat);
byte[] bytes = bytemat.toArray();
InputStream in = new ByteArrayInputStream(bytes);
try {
final BufferedImage img = ImageIO.read(in);
FlowFile flowFile = session.create();
flowFile = session.write(flowFile, new OutputStreamCallback() {
@Override
public void process(final OutputStream out) throws IOException {
ImageIO.write(img, "PNG", out);
}
});
session.getProvenanceReporter().create(flowFile);
session.transfer(flowFile, REL_SUCCESS);
} catch (IOException e) {
}
}
示例4: onTrigger
import org.apache.nifi.processor.io.OutputStreamCallback; //导入依赖的package包/类
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
final String data;
data = generateData(context);
Map<PropertyDescriptor, String> processorProperties = context.getProperties();
Map<String, String> generatedAttributes = new HashMap<String, String>();
for (final Map.Entry<PropertyDescriptor, String> entry : processorProperties.entrySet()) {
PropertyDescriptor property = entry.getKey();
if (property.isDynamic() && property.isExpressionLanguageSupported()) {
String dynamicValue = context.getProperty(property).evaluateAttributeExpressions().getValue();
generatedAttributes.put(property.getName(), dynamicValue);
}
}
FlowFile flowFile = session.create();
if (data.length() > 0) {
flowFile = session.write(flowFile, new OutputStreamCallback() {
@Override
public void process(final OutputStream out) throws IOException {
out.write(data.getBytes());
}
});
}
flowFile = session.putAllAttributes(flowFile, generatedAttributes);
session.getProvenanceReporter().create(flowFile);
session.transfer(flowFile, SUCCESS);
}
示例5: onTrigger
import org.apache.nifi.processor.io.OutputStreamCallback; //导入依赖的package包/类
@Override
public void onTrigger(ProcessContext context, final ProcessSession session) throws ProcessException {
getLogger().info("onTrigger rabbitMQMessageQueue size: " + rabbitMQMessageQueue.size());
final RabbitMQMessage rabbitMQMessage = rabbitMQMessageQueue.poll();
if (rabbitMQMessage == null) {
return;
}
final String rabbitQueue = context.getProperty(RABBITMQ_QUEUE).getValue();
final long start = System.nanoTime();
FlowFile flowFile = session.create();
try {
flowFile = session.write(flowFile,
new OutputStreamCallback() {
@Override
public void process(final OutputStream out) throws IOException {
out.write(rabbitMQMessage.getBody());
}
});
if (flowFile.getSize() == 0L) {
session.remove(flowFile);
} else {
final long millis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
session.getProvenanceReporter().receive(flowFile, "rabbitmq://" + rabbitQueue, "Received RabbitMQ RabbitMQMessage", millis);
getLogger().info("Successfully received {} ({}) from RabbitMQ in {} millis", new Object[]{flowFile, flowFile.getSize(), millis});
session.transfer(flowFile, SUCCESS);
}
} catch (Exception e) {
session.remove(flowFile);
throw e;
}
}
示例6: onTrigger
import org.apache.nifi.processor.io.OutputStreamCallback; //导入依赖的package包/类
@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
final AtomicReference<String[]> value = new AtomicReference<String[]>();
final AtomicReference<String> result =new AtomicReference<String>();
HashMap<String, String> map = new HashMap<String, String>();
FlowFile flowfile = session.get();
openFile(context.getProperty(EXPERIMENT_LOG_FILE).getValue());
expLogStream.println(String.format("%s,%s,%s,%s", flowfile.getAttribute(ExperimentAttributes.FLOWFILE_ID.key()),
SenMLParser.class, (new Timestamp(System.currentTimeMillis())).getTime(), "ENTRY"));
session.read(flowfile, new InputStreamCallback() {
@Override
public void process(InputStream in) {
try{
String json = IOUtils.toString(in, "UTF-8");
String[] obsType = json.split(System.getProperty("line.separator"));
value.set(obsType);
}catch(Exception ex){
ex.printStackTrace();
getLogger().error("Failed to read json string.");
getLogger().error(ex.toString());
}
}
});
String partResult = "";
for(String msg:value.get()) {
Map<String, String> parsedMap = getResult(msg.split(",",2)[1]);
partResult += parsedMap.toString()+"\n";
}
partResult.trim();
result.set(partResult);
// To write the results back out to flow file
flowfile = session.write(flowfile, new OutputStreamCallback() {
public void process(OutputStream out) throws IOException {
out.write(result.toString().getBytes());
//out.write(value.get().getBytes());
}
});
expLogStream.println(String.format("%s,%s,%s,%s", flowfile.getAttribute(ExperimentAttributes.FLOWFILE_ID.key()),
ETLGetFile.class, (new Timestamp(System.currentTimeMillis())).getTime(), "EXIT"));
expLogStream.flush();
session.transfer(flowfile,REL_SUCCESS);
}
示例7: onTrigger
import org.apache.nifi.processor.io.OutputStreamCallback; //导入依赖的package包/类
@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
final ComponentLog logger = getLogger();
StringBuilder stringBuilder = new StringBuilder();
// Submit to getValue
final OPCUAService opcUAService = context.getProperty(OPCUA_SERVICE)
.asControllerService(OPCUAService.class);
if(opcUAService.updateSession()){
logger.debug("Session current");
}else {
logger.debug("Session update failed");
}
// Set the starting node and parse the node tree
if ( starting_node == null) {
logger.debug("Parse the root node " + new ExpandedNodeId(Identifiers.RootFolder));
stringBuilder.append(opcUAService.getNameSpace(print_indentation, max_recursiveDepth, new ExpandedNodeId(Identifiers.RootFolder)));
} else {
logger.debug("Parse the result list for node " + new ExpandedNodeId(NodeId.parseNodeId(starting_node)));
stringBuilder.append(opcUAService.getNameSpace(print_indentation, max_recursiveDepth, new ExpandedNodeId(NodeId.parseNodeId(starting_node))));
}
// Write the results back out to a flow file
FlowFile flowFile = session.create();
if ( flowFile == null ) {
logger.error("Flowfile is null");
}
flowFile = session.write(flowFile, new OutputStreamCallback() {
public void process(OutputStream out) throws IOException {
out.write(stringBuilder.toString().getBytes());
}
});
// Transfer data to flow file
session.transfer(flowFile, SUCCESS);
}
示例8: onTrigger
import org.apache.nifi.processor.io.OutputStreamCallback; //导入依赖的package包/类
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
final ComponentLog logger = getLogger();
// Initialize response variable
final AtomicReference<String> requestedTagname = new AtomicReference<>();
// get FlowFile
FlowFile flowFile = session.get();
if ( flowFile == null ) {
return;
}
// Read tag name from flow file content
session.read(flowFile, new InputStreamCallback() {
@Override
public void process(InputStream in) throws IOException {
try{
String tagname = new BufferedReader(new InputStreamReader(in))
.lines().collect(Collectors.joining("\n"));
requestedTagname.set(tagname);
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
// Submit to getValue
final OPCUAService opcUAService = context.getProperty(OPCUA_SERVICE)
.asControllerService(OPCUAService.class);
if(opcUAService.updateSession()){
logger.debug("Session current");
}else {
logger.debug("Session update failed");
}
// Write the results back out to flow file
flowFile = session.write(flowFile, new OutputStreamCallback() {
@Override
public void process(OutputStream out) throws IOException {
out.write(opcUAService.getValue(requestedTagname.get()));
}
});
session.transfer(flowFile, SUCCESS);
}
示例9: onTrigger
import org.apache.nifi.processor.io.OutputStreamCallback; //导入依赖的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);
}
}
示例10: onTrigger
import org.apache.nifi.processor.io.OutputStreamCallback; //导入依赖的package包/类
@Override
public void onTrigger(final ProcessContext context,
final ProcessSession session) throws ProcessException {
final Event event = eventQueue.poll();
if (event != null) {
switch (event.getEventType()) {
case STOPPED_BY_ERROR:
getLogger()
.error("Received error {}: {} due to {}. Will not attempt to reconnect",
new Object[] { event.getEventType(),
event.getMessage(),
event.getUnderlyingException() });
break;
case CONNECTION_ERROR:
case HTTP_ERROR:
getLogger()
.error("Received error {}: {}. Will attempt to reconnect",
new Object[] { event.getEventType(),
event.getMessage() });
client.reconnect();
break;
default:
break;
}
}
final String tweet = messageQueue.poll();
if (tweet == null) {
context.yield();
return;
}
FlowFile flowFile = session.create();
flowFile = session.write(flowFile, new OutputStreamCallback() {
@Override
public void process(final OutputStream out) throws IOException {
out.write(tweet.getBytes(StandardCharsets.UTF_8));
}
});
final Map<String, String> attributes = new HashMap<>();
attributes.put(CoreAttributes.MIME_TYPE.key(), "application/json");
attributes.put(CoreAttributes.FILENAME.key(),
flowFile.getAttribute(CoreAttributes.FILENAME.key()) + ".json");
flowFile = session.putAllAttributes(flowFile, attributes);
session.transfer(flowFile, REL_SUCCESS);
session.getProvenanceReporter().receive(
flowFile,
Constants.STREAM_HOST
+ client.getEndpoint().getURI().toString());
}
示例11: onTrigger
import org.apache.nifi.processor.io.OutputStreamCallback; //导入依赖的package包/类
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
FlowFile flowfile = session.get();
final String regex = context.getProperty(REGEX).getValue();
final Boolean hasHeader = Boolean.parseBoolean(context.getProperty(HAS_HEADER).getValue());
// final AtomicReference<String> partValue = new AtomicReference<String>();
final RecordLayoutValidator fp = new RecordLayoutValidator();
fp.setExpectedRecordFormatRegEx(regex);
fp.setHasHeader(hasHeader);
if (flowfile == null) {
return;
}
session.read(flowfile, new InputStreamCallback() {
@Override
public void process(InputStream in) throws IOException {
try {
fp.setInputStream(in);
fp.validate();
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
boolean valid = fp.isValid();
if (valid) {
flowfile = session.putAttribute(flowfile, "record.count", Long.toString(fp.getRecordCount()));
session.transfer(flowfile, SUCCESS);
} else {
session.transfer(flowfile, FAILURE);
// To write the results back out ot flow file
flowfile = session.write(flowfile, new OutputStreamCallback() {
@Override
public void process(OutputStream out) throws IOException {
Map<Long, String> errorMap = fp.getErrors();
Set<Long> keys = errorMap.keySet();
for (Long key: keys) {
String badLine = errorMap.get(key);
StringBuilder sb = new StringBuilder();
sb.append(key);
sb.append("\t");
sb.append(badLine);
out.write(sb.toString().getBytes());
}
}
});
session.transfer(flowfile, ERRORS);
}
}
示例12: onTrigger
import org.apache.nifi.processor.io.OutputStreamCallback; //导入依赖的package包/类
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
final ProcessorLog logger = getLogger();
final Message<JsonObject> message = messageQueue.poll();
if (message == null) {
context.yield();
return;
}
FlowFile flowFile = session.create();
try {
final StopWatch stopWatch = new StopWatch(true);
flowFile = session.write(flowFile, new OutputStreamCallback() {
@Override
public void process(final OutputStream out) throws IOException {
out.write(message.body().encode().getBytes(StandardCharsets.UTF_8));
}
});
if (flowFile.getSize() == 0L) {
session.remove(flowFile);
} else {
final Map<String, String> attributes = new HashMap<>();
if (headerPattern != null) {
MultiMap headers = message.headers();
headers.names()
.stream()
.filter(headerName -> headerPattern.matcher(headerName).matches())
.forEach(headerName -> attributes.put(headerName, headers.get(headerName)));
// attributes.putAll(
// headers.entries()
// .stream()
// .filter(entry -> headerPattern.matcher(entry.getKey()).matches())
// .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))
// );
}
attributes.put(CoreAttributes.MIME_TYPE.key(), "application/json");
attributes.put(CoreAttributes.FILENAME.key(), flowFile.getAttribute(CoreAttributes.FILENAME.key()) + ".json");
attributes.put("eventbus.address", message.address());
flowFile = session.putAllAttributes(flowFile, attributes);
session.getProvenanceReporter().receive(flowFile, message.address(), "received eventBus message", stopWatch.getElapsed(TimeUnit.MILLISECONDS));
getLogger().info("Successfully received {} ({}) from EventBus in {} millis", new Object[]{flowFile, flowFile.getSize(), stopWatch.getElapsed(TimeUnit.MILLISECONDS)});
session.transfer(flowFile, REL_SUCCESS);
}
} catch (Exception e) {
session.remove(flowFile);
throw e;
}
}
示例13: onTrigger
import org.apache.nifi.processor.io.OutputStreamCallback; //导入依赖的package包/类
@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
final long start = System.nanoTime();
try (Jedis jedis = jedisPool.getResource()) {
String desiredKey = context.getProperty(TOPIC).getValue();
int requiredEnrichments = context.getProperty(ENRICHMENTS).asInteger();
int numMessages = 0;
Set<String> possibleResults = jedis.zrangeByScore(desiredKey, requiredEnrichments, requiredEnrichments);
for (String flowKey : possibleResults) {
String contentKey = "content:" + flowKey;
String attributesKey = "attrib:" + flowKey;
String enrichmentKey = "enrich:" + flowKey;
final byte[] content = jedis.get(contentKey.getBytes());
Map<String, String> attributes = jedis.hgetAll(attributesKey);
Map<String, String> enrichment = jedis.hgetAll(enrichmentKey);
if (requiredEnrichments != enrichment.size()) {
continue;
}
numMessages += 1;
FlowFile ff = session.create();
ff = session.write(ff, new OutputStreamCallback() {
@Override
public void process(final OutputStream out) throws IOException {
out.write(content);
}
});
ff = session.putAllAttributes(ff, attributes);
ff = session.putAllAttributes(ff, enrichment);
jedis.zrem(desiredKey, flowKey);
jedis.del(contentKey, attributesKey, enrichmentKey);
final long millis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
session.getProvenanceReporter().receive(ff, "redis://" + desiredKey, "Received " + numMessages + " Kafka messages", millis);
getLogger().info("Successfully received {} from Redis with {} messages in {} millis", new Object[]{ff, numMessages, millis});
session.transfer(ff, REL_SUCCESS);
}
context.yield();
}
}