use of org.apache.nifi.remote.protocol.DataPacket in project flink by apache.
the class NiFiSource method run.
@Override
public void run(SourceContext<NiFiDataPacket> ctx) throws Exception {
while (isRunning) {
final Transaction transaction = client.createTransaction(TransferDirection.RECEIVE);
if (transaction == null) {
LOG.warn("A transaction could not be created, waiting and will try again...");
try {
Thread.sleep(waitTimeMs);
} catch (InterruptedException ignored) {
}
continue;
}
DataPacket dataPacket = transaction.receive();
if (dataPacket == null) {
transaction.confirm();
transaction.complete();
LOG.debug("No data available to pull, waiting and will try again...");
try {
Thread.sleep(waitTimeMs);
} catch (InterruptedException ignored) {
}
continue;
}
final List<NiFiDataPacket> niFiDataPackets = new ArrayList<>();
do {
// Read the data into a byte array and wrap it along with the attributes
// into a NiFiDataPacket.
final InputStream inStream = dataPacket.getData();
final byte[] data = new byte[(int) dataPacket.getSize()];
StreamUtils.fillBuffer(inStream, data);
final Map<String, String> attributes = dataPacket.getAttributes();
niFiDataPackets.add(new StandardNiFiDataPacket(data, attributes));
dataPacket = transaction.receive();
} while (dataPacket != null);
// Confirm transaction to verify the data
transaction.confirm();
for (NiFiDataPacket dp : niFiDataPackets) {
ctx.collect(dp);
}
transaction.complete();
}
}
Aggregations