本文整理汇总了Java中org.apache.nifi.processor.io.InputStreamCallback类的典型用法代码示例。如果您正苦于以下问题:Java InputStreamCallback类的具体用法?Java InputStreamCallback怎么用?Java InputStreamCallback使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
InputStreamCallback类属于org.apache.nifi.processor.io包,在下文中一共展示了InputStreamCallback类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createPut
import org.apache.nifi.processor.io.InputStreamCallback; //导入依赖的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);
}
示例2: validateAndParseJSONContent
import org.apache.nifi.processor.io.InputStreamCallback; //导入依赖的package包/类
Map<String, Object> validateAndParseJSONContent(ProcessSession processSession, FlowFile flowFile) throws JsonParseException {
final Map<String, Object> jsonContent = Maps.newHashMap();
try {
processSession.read(flowFile, new InputStreamCallback() {
@Override
public void process(InputStream in) throws IOException {
JsonParser jp = jsonFactory.createParser(in);
jsonContent.putAll(jp.readValueAs(Map.class));
}
});
} catch (ProcessException e) {
if (e.getCause() instanceof JsonParseException) {
throw (JsonParseException)e.getCause();
} else {
throw e;
}
}
return jsonContent;
}
示例3: extractMessageBody
import org.apache.nifi.processor.io.InputStreamCallback; //导入依赖的package包/类
/**
* Extracts contents of the {@link FlowFile} as byte array.
*/
private byte[] extractMessageBody(FlowFile flowFile, ProcessSession session) {
final byte[] messageContent = new byte[(int) flowFile.getSize()];
session.read(flowFile, new InputStreamCallback() {
@Override
public void process(final InputStream in) throws IOException {
StreamUtils.fillBuffer(in, messageContent, true);
}
});
return messageContent;
}
示例4: onTrigger
import org.apache.nifi.processor.io.InputStreamCallback; //导入依赖的package包/类
@Override
public void onTrigger(final ProcessContext context, final ProcessSession processSession) {
List<FlowFile> flowFiles = processSession.get(batchSize);
if (flowFiles.isEmpty()) {
return;
}
Session jschSession = null;
Channel channel = null;
try {
jschSession = openSession(context);
final String remotePath = context.getProperty(REMOTE_PATH).evaluateAttributeExpressions().getValue();
channel = openExecChannel(context, jschSession, "scp -r -d -t " + remotePath);
InputStream channelIn = channel.getInputStream();
OutputStream channelOut = channel.getOutputStream();
channel.connect();
waitForAck(channelIn);
ListIterator<FlowFile> fileIt = flowFiles.listIterator();
while (fileIt.hasNext()) {
final FlowFile flowFile = fileIt.next();
// conditionally reject files that are zero bytes or less
if (context.getProperty(REJECT_ZERO_BYTE).asBoolean() && flowFile.getSize() == 0) {
logger.warn("Rejecting {} because it is zero bytes", new Object[]{flowFile});
processSession.transfer(processSession.penalize(flowFile), REL_REJECT);
fileIt.remove();
continue;
}
final String filename = flowFile.getAttribute(CoreAttributes.FILENAME.key());
final String permissions = context.getProperty(PERMISSIONS).evaluateAttributeExpressions(flowFile).getValue();
// destination path + filename
// final String fullPath = buildFullPath(context, flowFile, filename);
processSession.read(flowFile, new InputStreamCallback() {
@Override
public void process(final InputStream flowFileIn) throws IOException {
// send "C0644 filesize filename", where filename should not include '/'
StringBuilder command = new StringBuilder("C").append(permissions).append(' ');
command.append(flowFile.getSize()).append(' ');
command.append(filename).append('\n');
channelOut.write(command.toString().getBytes(StandardCharsets.UTF_8));
channelOut.flush();
waitForAck(channelIn);
IOUtils.copy(flowFileIn, channelOut);
channelOut.flush();
sendAck(channelOut);
waitForAck(channelIn);
}
});
processSession.transfer(flowFile, REL_SUCCESS);
processSession.getProvenanceReporter().send(flowFile, remotePath);
fileIt.remove();
if (logger.isDebugEnabled()) {
logger.debug("Sent {} to remote host", new Object[]{flowFile});
}
}
} catch (JSchException | IOException ex) {
context.yield();
logger.error("Unable to create session to remote host due to {}", new Object[]{ex}, ex);
processSession.transfer(flowFiles, REL_FAILURE);
} finally {
if (channel != null) {
channel.disconnect();
}
if (jschSession != null) {
jschSession.disconnect();
}
}
}
示例5: onTrigger
import org.apache.nifi.processor.io.InputStreamCallback; //导入依赖的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 regexGroupSupport = Boolean.parseBoolean(context.getProperty(REGEX_GROUP_SUPPORT).getValue());
final Integer occurrence = Integer.parseInt(context.getProperty(OCCURRENCE).getValue());
final AtomicReference<String> partValue = new AtomicReference<String>();
final FilePartByRegEx fp = new FilePartByRegEx();
fp.setOccurrence(occurrence);
fp.setRegexGroupSupport(regexGroupSupport);
fp.setRegex(regex);
if (flowfile == null) {
return;
}
session.read(flowfile, new InputStreamCallback() {
@Override
public void process(InputStream in) throws IOException {
try {
fp.setInputStream(in);
partValue.set(fp.getValue());
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
flowfile = session.putAttribute(flowfile, "file.part.value", partValue.get());
session.transfer(flowfile, SUCCESS);
}
示例6: onTrigger
import org.apache.nifi.processor.io.InputStreamCallback; //导入依赖的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.InputStreamCallback; //导入依赖的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);
}
示例8: onTrigger
import org.apache.nifi.processor.io.InputStreamCallback; //导入依赖的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();
// read all bytes of the flowfile (tensor requires whole image)
String modelDir = flowFile.getAttribute(MODEL_DIR_NAME);
if (modelDir == null) {
modelDir = context.getProperty(MODEL_DIR_NAME).evaluateAttributeExpressions(flowFile).getValue();
}
if (modelDir == null) {
modelDir = "/models";
}
final String model = modelDir;
try {
final HashMap<String, String> attributes = new HashMap<String, String>();
session.read(flowFile, new InputStreamCallback() {
@Override
public void process(InputStream input) throws IOException {
byte[] byteArray = IOUtils.toByteArray(input);
getLogger().info(
String.format("read %d bytes from incoming file", new Object[] { byteArray.length }));
List<Entry<Float, String>> results = service.getInception(byteArray, model);
getLogger().debug(String.format("Found %d results", new Object[] { results.size() }));
for (int i = 0; i < results.size(); i++) {
Object[] key = new Object[] { ATTRIBUTE_OUTPUT_NAME, i };
Entry<Float, String> entry = results.get(i);
attributes.put(String.format("%s.%d.label", key), entry.getValue());
attributes.put(String.format("%s.%d.probability", key), entry.getKey().toString());
}
}
});
if (attributes.size() == 0) {
session.transfer(flowFile, REL_FAILURE);
} else {
flowFile = session.putAllAttributes(flowFile, attributes);
session.transfer(flowFile, REL_SUCCESS);
}
} catch (Exception e) {
throw new ProcessException(e);
}
session.commit();
} catch (
final Throwable t) {
getLogger().error("Unable to process TensorFlow Processor file " + t.getLocalizedMessage());
throw new ProcessException(t);
}
}
示例9: onTrigger
import org.apache.nifi.processor.io.InputStreamCallback; //导入依赖的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);
}
}
示例10: onTrigger
import org.apache.nifi.processor.io.InputStreamCallback; //导入依赖的package包/类
@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
FlowFile flowFile = session.get();
if ( flowFile == null ) {
return;
}
final String outboundAddress = context.getProperty(OUTBOUND_ADDRESS).evaluateAttributeExpressions(flowFile).getValue();
try {
final StopWatch stopWatch = new StopWatch(true);
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, false);
}
});
// TODO detect flowFile MIME_TYPE and convert buffer into JSON, String or buffer
JsonObject message = new JsonObject(new String(buffer, StandardCharsets.UTF_8));
if(attributePattern != null) {
DeliveryOptions options = new DeliveryOptions();
for(String attributeName : flowFile.getAttributes().keySet()) {
if (attributePattern != null && attributePattern.matcher(attributeName).matches()) {
options.addHeader(attributeName, flowFile.getAttribute(attributeName));
}
}
put(outboundAddress, message, options);
} else {
put(outboundAddress, message);
}
final Map<String, String> attributes = new HashMap<>();
attributes.put("eventbus.address", outboundAddress);
flowFile = session.putAllAttributes(flowFile, attributes);
session.getProvenanceReporter().receive(flowFile, outboundAddress, "sent message to eventBus", stopWatch.getElapsed(TimeUnit.MILLISECONDS));
getLogger().info("Successfully sent {} ({}) to EventBuss in {} millis", new Object[]{flowFile, flowFile.getSize(), stopWatch.getElapsed(TimeUnit.MILLISECONDS)});
session.transfer(flowFile, REL_SUCCESS);
} catch (ProcessException pe) {
getLogger().error("Failed to send {} to EventBuss due to {}; routing to failure", new Object[] {flowFile, pe.toString()}, pe);
session.transfer(flowFile, REL_FAILURE);
}
}
示例11: onTrigger
import org.apache.nifi.processor.io.InputStreamCallback; //导入依赖的package包/类
/**
*
*/
@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
final ProcessorLog log = this.getLogger();
long executionCompletionTimeout = Long.parseLong(context.getProperty(EXECUTION_COMPLETION_TIMEOUT).getValue());
FlowFile flowFile = session.get();
if (flowFile != null){
try {
String configurationName = flowFile.getAttribute("filename");
Assert.isTrue(configurationName.endsWith(".cfg"), "Received invalid configuration file '" +
configurationName + "'. DStream configuration file must end with '.cfg'.");
log.info("Recieved configuration '" + configurationName + "'");
AtomicReference<String> outputPathRef = new AtomicReference<String>();
session.read(flowFile, new InputStreamCallback() {
@Override
public void process(InputStream confFileInputStream) throws IOException {
outputPathRef.set(installConfiguration(configurationName, confFileInputStream));
}
});
String executionName = configurationName.split("\\.")[0];
DStream<?> dstream = this.getDStream(executionName);
if (dstream != null){
log.info("Executing DStream for '" + executionName + "'");
this.postProcessResults(this.executeDStream(dstream, executionName, executionCompletionTimeout));
FlowFile resultFlowFile = session.create();
resultFlowFile = session.putAttribute(resultFlowFile, CoreAttributes.FILENAME.key(), outputPathRef.get());
session.getProvenanceReporter().receive(resultFlowFile, outputPathRef.get());
session.transfer(resultFlowFile, OUTPUT);
} else {
log.warn("Failed to locate DStream for execution '" + executionName + "'"
+ ". Nothing was executed. Possible reasons: " + this.getClass().getSimpleName()
+ " may not have provided a DStream for '" + executionName + "'");
}
}
catch (Exception e) {
throw new IllegalStateException("Failed DStream execution with unexpected exception ", e);
}
finally {
session.remove(flowFile);
session.commit();
}
}
}