Example usage for com.amazonaws.services.kinesis.model PutRecordRequest setSequenceNumberForOrdering

List of usage examples for com.amazonaws.services.kinesis.model PutRecordRequest setSequenceNumberForOrdering

Introduction

In this page you can find the example usage for com.amazonaws.services.kinesis.model PutRecordRequest setSequenceNumberForOrdering.

Prototype


public void setSequenceNumberForOrdering(String sequenceNumberForOrdering) 

Source Link

Document

Guarantees strictly increasing sequence numbers, for puts from the same client and to the same partition key.

Usage

From source file:com.alertlogic.aws.analytics.poc.RecordKinesisPutter.java

License:Open Source License

/**
 * Send a single record to Amazon Kinesis using PutRecord.
 *///from ww w .  jav a2s.co  m
private void sendRecord() {
    Record record = recordFactory.create();
    byte[] bytes;
    try {
        bytes = JSON.writeValueAsBytes(record);
    } catch (IOException e) {
        LOG.warn("Skipping record. Unable to serialize: '" + record + "'", e);
        return;
    }

    PutRecordRequest putRecord = new PutRecordRequest();
    putRecord.setStreamName(streamName);
    // We use the resource as the partition key so we can accurately calculate totals for a given resource
    putRecord.setPartitionKey(record.getField("resource"));
    putRecord.setData(ByteBuffer.wrap(bytes));
    // Order is not important for this application so we do not send a SequenceNumberForOrdering
    putRecord.setSequenceNumberForOrdering(null);

    try {
        kinesis.putRecord(putRecord);
    } catch (ProvisionedThroughputExceededException ex) {
        if (LOG.isDebugEnabled()) {
            LOG.debug(String.format("Thread %s's Throughput exceeded. Waiting 10ms",
                    Thread.currentThread().getName()));
        }
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    } catch (AmazonClientException ex) {
        LOG.warn("Error sending record to Amazon Kinesis.", ex);
    }
}

From source file:com.alertlogic.aws.kinesis.test1.producer.HttpReferrerKinesisPutter.java

License:Open Source License

/**
 * Send a single pair to Amazon Kinesis using PutRecord.
 *///from   w ww .  j  av a2s . c om
private void sendPair() {
    HttpReferrerPair pair = referrerFactory.create();
    byte[] bytes;
    try {
        bytes = JSON.writeValueAsBytes(pair);
    } catch (IOException e) {
        LOG.warn("Skipping pair. Unable to serialize: '" + pair + "'", e);
        return;
    }

    PutRecordRequest putRecord = new PutRecordRequest();
    putRecord.setStreamName(streamName);
    // We use the resource as the partition key so we can accurately calculate totals for a given resource
    putRecord.setPartitionKey(pair.getResource());
    putRecord.setData(ByteBuffer.wrap(bytes));
    // Order is not important for this application so we do not send a SequenceNumberForOrdering
    putRecord.setSequenceNumberForOrdering(null);

    try {
        kinesis.putRecord(putRecord);
    } catch (ProvisionedThroughputExceededException ex) {
        if (LOG.isDebugEnabled()) {
            LOG.debug(String.format("Thread %s's Throughput exceeded. Waiting 10ms",
                    Thread.currentThread().getName()));
        }
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    } catch (AmazonClientException ex) {
        LOG.warn("Error sending record to Amazon Kinesis.", ex);
    }
}

From source file:com.calamp.services.kinesis.events.writer.CalAmpEventWriter.java

License:Open Source License

/**
 * Uses the Kinesis client to send the event to the given stream.
 *
 * @param trade instance representing the stock trade
 * @param kinesisClient Amazon Kinesis client
 * @param streamName Name of stream /*from w w w.j ava  2 s.  c om*/
 */
public static void sendEvent(CalAmpEvent event, AmazonKinesis kinesisClient, String streamName) {
    byte[] bytes = event.toJsonAsBytes();
    // The bytes could be null if there is an issue with the JSON serialization by the Jackson JSON library.
    if (bytes == null) {
        LOG.warn("Could not get JSON bytes for stock trade");
        return;
    }

    LOG.info("Putting trade: " + event.toString());
    PutRecordRequest putRecord = new PutRecordRequest();
    putRecord.setStreamName(CalAmpParameters.unorderdStreamName);
    putRecord.setPartitionKey(String.valueOf(event.getMachineId()));
    putRecord.setData(ByteBuffer.wrap(bytes));

    //This is needed to guaranteed FIFO ordering per partitionKey
    if (prevSeqNum != null) {
        putRecord.setSequenceNumberForOrdering(prevSeqNum);
    }
    try {
        PutRecordResult res = kinesisClient.putRecord(putRecord);
        prevSeqNum = res.getSequenceNumber();
        Utils.lazyLog(putRecord, CalAmpParameters.writeLogName);
    } catch (AmazonClientException ex) {
        LOG.warn("Error sending record to Amazon Kinesis.", ex);
    }
}

From source file:com.informatica.surf.target.kinesis.KinesisTarget.java

License:Apache License

@Override
public void write(VDSEvent event) throws Exception {
    Map<String, String> headers = event.getEventInfo();
    PutRecordRequest req = new PutRecordRequest();
    req.setStreamName(_streamName);/*from  ww  w  . j  a  va  2 s. co  m*/
    req.setData(ByteBuffer.wrap(event.getBuffer().array(), 0, event.getBufferLen()));
    if (headers != null) {
        if (headers.containsKey(PARTITION_KEY)) {
            _logger.debug("Using partition key from header");
            req.setPartitionKey(headers.get(PARTITION_KEY));
        } else {
            _logger.debug("Using random partition key");
            req.setPartitionKey(String.valueOf(_random.nextLong()));
        }
        if (headers.containsKey(SEQUENCE_NUMBER)) {
            req.setSequenceNumberForOrdering(headers.get(SEQUENCE_NUMBER));
        }
    }
    if (_logger.isDebugEnabled()) {
        _logger.debug("Record content: {}", new String(event.getBuffer().array(), 0, event.getBufferLen()));
        _logger.debug("Record length: {}", event.getBufferLen());
    }
    _client.putRecordAsync(req, _callback);
}

From source file:io.radiowitness.kinesis.producer.PutRecordTask.java

License:Open Source License

@Override
public void run() {
    synchronized (txnLock) {
        if (!hasRun) {
            packed.flip();/*  ww  w  .  j  av  a  2  s  .c  om*/

            if (packed.remaining() < 1) {
                future.setException(new MessagePackingException("buffer is empty"));
            } else {
                PutRecordRequest request = new PutRecordRequest();
                request.setStreamName(config.getStreamName());
                request.setPartitionKey(partitionKey);
                request.setData(packed);

                if (sequenceNumber.isPresent()) {
                    request.setSequenceNumberForOrdering(sequenceNumber.get());
                }

                client.putRecordAsync(request, this);
                hasRun = true;
            }
        }
    }
}