Search in sources :

Example 36 with DataPacket

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();
    }
}
Also used : Transaction(org.apache.nifi.remote.Transaction) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) DataPacket(org.apache.nifi.remote.protocol.DataPacket)

Aggregations

DataPacket (org.apache.nifi.remote.protocol.DataPacket)36 Test (org.junit.Test)21 StandardDataPacket (org.apache.nifi.remote.util.StandardDataPacket)20 Transaction (org.apache.nifi.remote.Transaction)13 InputStream (java.io.InputStream)10 ByteArrayInputStream (java.io.ByteArrayInputStream)8 IOException (java.io.IOException)8 SiteToSiteClient (org.apache.nifi.remote.client.SiteToSiteClient)8 SiteToSiteTestUtils.createDataPacket (org.apache.nifi.remote.protocol.SiteToSiteTestUtils.createDataPacket)8 ByteArrayInputStream (org.apache.nifi.stream.io.ByteArrayInputStream)8 ByteArrayOutputStream (org.apache.nifi.stream.io.ByteArrayOutputStream)8 HttpCommunicationsSession (org.apache.nifi.remote.io.http.HttpCommunicationsSession)7 Peer (org.apache.nifi.remote.Peer)5 Response (org.apache.nifi.remote.protocol.Response)5 DataInputStream (java.io.DataInputStream)4 DataOutputStream (java.io.DataOutputStream)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 ProvenanceEventRecord (org.apache.nifi.provenance.ProvenanceEventRecord)4 CommunicationsSession (org.apache.nifi.remote.protocol.CommunicationsSession)4