Example usage for com.amazonaws.services.kinesis AmazonKinesis putRecord

List of usage examples for com.amazonaws.services.kinesis AmazonKinesis putRecord

Introduction

In this page you can find the example usage for com.amazonaws.services.kinesis AmazonKinesis putRecord.

Prototype

PutRecordResult putRecord(PutRecordRequest putRecordRequest);

Source Link

Document

Writes a single data record into an Amazon Kinesis data stream.

Usage

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  ww  w  .  j  av  a2 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.kinesis.main.KinesisLogger.java

License:Apache License

private static void sendOauthJson(OauthJSON json, AmazonKinesis kinesisClient, String streamName) {

    json = new OauthJSON();
    API currentAPI = API.getRandom();/*from   w w w. j  a  va2  s .co m*/
    json.setApi(currentAPI.name());
    json.setClientName(ClientName.getRandom().name());
    json.setServiceName(ServiceName.getRandom().name());
    json.setUserId(getRandomNumberInRange(10000, 100000));
    json.setTokenId(UUID.randomUUID().toString());
    json.setTickerSymbol("AMZN");
    switch (currentAPI) {
    case CreateTokenFromUserIdFailure:
        json.setError(CreateTokenFailureErrors.getRandom().name());
        break;
    case ValidateTokenFailure:
        json.setError(CreateTokenFailureErrors.getRandom().name());
        break;
    case RevokeTokenFailure:
        json.setError(CreateTokenFailureErrors.getRandom().name());
        break;
    default:
        break;
    }
    logger.info(json.toString());

    byte[] bytes = json.toJsonAsBytes();
    // The bytes could be null if there is an issue with the JSON serialization by the Jackson JSON library.
    if (bytes == null) {
        logger.info("Could not get JSON bytes for stock trade");
        return;
    }

    PutRecordRequest putRecord = new PutRecordRequest();
    putRecord.setStreamName(streamName);
    // We use the ticker symbol as the partition key, as explained in the tutorial.
    putRecord.setPartitionKey(json.getTickerSymbol());
    putRecord.setData(ByteBuffer.wrap(bytes));

    try {
        kinesisClient.putRecord(putRecord);
    } catch (AmazonClientException ex) {
        logger.warn("Error sending record to Amazon Kinesis.", ex);
    }
}

From source file:com.kinesis.writer.StockTradesWriter.java

License:Open Source License

/**
 * Uses the Kinesis client to send the stock trade to the given stream.
 *
 * @param trade instance representing the stock trade
 * @param kinesisClient Amazon Kinesis client
 * @param streamName Name of stream//from ww  w  .j av a  2s.  c o  m
 */
private static void sendStockTrade(StockTrade trade, AmazonKinesis kinesisClient, String streamName) {
    byte[] bytes = trade.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: " + trade.toString());
    PutRecordRequest putRecord = new PutRecordRequest();
    putRecord.setStreamName(streamName);
    // We use the ticker symbol as the partition key, as explained in the tutorial.
    putRecord.setPartitionKey(trade.getTickerSymbol());
    putRecord.setData(ByteBuffer.wrap(bytes));

    try {
        kinesisClient.putRecord(putRecord);
    } catch (AmazonClientException ex) {
        LOG.warn("Error sending record to Amazon Kinesis.", ex);
    }
}

From source file:producer.SensorReadingProducer.java

License:Open Source License

private void run(final int events, final OutputFormat format, final String streamName, final String region)
        throws Exception {
    AmazonKinesis kinesisClient = new AmazonKinesisClient(new DefaultAWSCredentialsProviderChain());
    kinesisClient.setRegion(Region.getRegion(Regions.fromName(region)));
    int count = 0;
    SensorReading r = null;/*from w  w w  .  j ava 2s  . c  o m*/
    do {
        r = nextSensorReading(format);

        try {
            PutRecordRequest req = new PutRecordRequest().withPartitionKey("" + rand.nextLong())
                    .withStreamName(streamName).withData(ByteBuffer.wrap(r.toString().getBytes()));
            kinesisClient.putRecord(req);
        } catch (ProvisionedThroughputExceededException e) {
            Thread.sleep(BACKOFF);
        }

        System.out.println(r);
        count++;
    } while (count < events);
}