com.amazonaws.services.dynamodbv2.streamsadapter.model.RecordAdapter Java Examples

The following examples show how to use com.amazonaws.services.dynamodbv2.streamsadapter.model.RecordAdapter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StreamsRecordProcessor.java    From dynamo-cassandra-proxy with Apache License 2.0 5 votes vote down vote up
@Override
public void processRecords(ProcessRecordsInput processRecordsInput) {
    for (Record record : processRecordsInput.getRecords()) {
        String data = new String(record.getData().array(), Charset.forName("UTF-8"));
        System.out.println(data);
        if (record instanceof RecordAdapter) {
            com.amazonaws.services.dynamodbv2.model.Record streamRecord = ((RecordAdapter) record)
                    .getInternalObject();

            switch (streamRecord.getEventName()) {
                case "INSERT": case "MODIFY":
                    Map<String, AttributeValue> items = streamRecord.getDynamodb().getNewImage();
                    PutItemRequest putItemRequest = new PutItemRequest().withTableName(tableName).withItem(items);
                    dynamoDBClient.putItem(putItemRequest);
                    break;
                case "REMOVE":
                    Map<String, AttributeValue> keys = streamRecord.getDynamodb().getKeys();
                    DeleteItemRequest deleteItemRequest = new DeleteItemRequest().withTableName(tableName).withKey(keys);
                    dynamoDBClient.deleteItem(deleteItemRequest);
            }
        }
        checkpointCounter += 1;
        if (checkpointCounter % 10 == 0) {
            try {
                processRecordsInput.getCheckpointer().checkpoint();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
 
Example #2
Source File: StreamsRecordProcessor.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
@Override
public void processRecords(ProcessRecordsInput processRecordsInput) {
    for (Record record : processRecordsInput.getRecords()) {
        String data = new String(record.getData().array(), Charset.forName("UTF-8"));
        System.out.println(data);
        if (record instanceof RecordAdapter) {
            com.amazonaws.services.dynamodbv2.model.Record streamRecord = ((RecordAdapter) record)
                    .getInternalObject();

            switch (streamRecord.getEventName()) {
                case "INSERT":
                case "MODIFY":
                    StreamsAdapterDemoHelper.putItem(dynamoDBClient, tableName,
                                                     streamRecord.getDynamodb().getNewImage());
                    break;
                case "REMOVE":
                    StreamsAdapterDemoHelper.deleteItem(dynamoDBClient, tableName,
                                                        streamRecord.getDynamodb().getKeys().get("Id").getN());
            }
        }
        checkpointCounter += 1;
        if (checkpointCounter % 10 == 0) {
            try {
                processRecordsInput.getCheckpointer().checkpoint();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
}
 
Example #3
Source File: StreamsRecord.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public StreamsRecord(com.amazonaws.services.kinesis.model.Record record) {
    if (record instanceof RecordAdapter) {
        com.amazonaws.services.dynamodbv2.model.Record dynamoRecord = ((RecordAdapter) record).getInternalObject();
        this.key = Optional.of(dynamoRecord.getEventID());
        setProperty(EVENT_NAME, dynamoRecord.getEventName());
        setProperty(SEQUENCE_NUMBER, dynamoRecord.getDynamodb().getSequenceNumber());
    } else {
        this.key = Optional.of(record.getPartitionKey());
        setProperty(ARRIVAL_TIMESTAMP, record.getApproximateArrivalTimestamp().toString());
        setProperty(ENCRYPTION_TYPE, record.getEncryptionType());
        setProperty(PARTITION_KEY, record.getPartitionKey());
        setProperty(SEQUENCE_NUMBER, record.getSequenceNumber());
    }

    if (StringUtils.isBlank(record.getEncryptionType())) {
        String s = null;
        try {
            s = decoder.decode(record.getData()).toString();
        } catch (CharacterCodingException e) {
           // Ignore
        }
        this.value = (s != null) ? s.getBytes() : null;
    } else {
        // Who knows?
        this.value = null;
    }
}
 
Example #4
Source File: DynamoDBStreamsRecordTransformer.java    From dynamodb-cross-region-library with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Record toClass(final com.amazonaws.services.kinesis.model.Record record) throws IOException {
    if (record instanceof RecordAdapter) {
        return ((RecordAdapter) record).getInternalObject();
    } else {
        return MAPPER.readValue(new String(record.getData().array(), ENCODING), Record.class);
    }
}
 
Example #5
Source File: StreamsRecordProcessor.java    From aws-big-data-blog with Apache License 2.0 5 votes vote down vote up
private void processSingleRecord(Record record) throws Exception {
	// extract the data from the record as UTF-8 encoded data
	String data = new String(record.getData().array(), Charset.forName("UTF-8"));

	// confirm that the object is a DynamoDB stream record
	if (record instanceof RecordAdapter) {
		com.amazonaws.services.dynamodbv2.model.Record streamRecord = ((RecordAdapter) record).getInternalObject();
		String orderId;
		switch (streamRecord.getEventName()) {
		case "INSERT":
		case "MODIFY":
			orderId = streamRecord.getDynamodb().getNewImage().get("OrderId").getS();
			int orderQty = Integer.parseInt(streamRecord.getDynamodb().getNewImage().get("OrderQty").getN());
			long orderdate = Long.parseLong(streamRecord.getDynamodb().getNewImage().get("OrderDate").getN());
			String shipmethod = streamRecord.getDynamodb().getNewImage().get("ShipMethod").getS();
			String billaddress = streamRecord.getDynamodb().getNewImage().get("BillAddress").getS();
			String billcity = streamRecord.getDynamodb().getNewImage().get("BillCity").getS();
			int billpostalcode = Integer
					.parseInt(streamRecord.getDynamodb().getNewImage().get("BillPostalCode").getN());
			int unitprice = Integer.parseInt(streamRecord.getDynamodb().getNewImage().get("UnitPrice").getN());
			String productcategory = streamRecord.getDynamodb().getNewImage().get("ProductCategory").getS();

			// generate a new cache object value
			OrderData odata = new OrderData(orderId, orderdate, shipmethod, billaddress, billcity, billpostalcode,
					orderQty, unitprice, productcategory);

			cache.put(orderId, odata);
			break;
		case "REMOVE":
			orderId = streamRecord.getDynamodb().getOldImage().get("OrderId").getS();
			cache.remove(orderId);
		}
	} else {
		throw new Exception(
				String.format("Warning - record %s is not an instance of RecordAdapter", record.getPartitionKey()));
	}
}
 
Example #6
Source File: StreamsRecordProcessor.java    From aws-dynamodb-examples with Apache License 2.0 5 votes vote down vote up
@Override
public void processRecords(List<Record> records,
        IRecordProcessorCheckpointer checkpointer) {
    for(Record record : records) {
        String data = new String(record.getData().array(), Charset.forName("UTF-8"));
        System.out.println(data);
        if(record instanceof RecordAdapter) {
            com.amazonaws.services.dynamodbv2.model.Record streamRecord = ((RecordAdapter) record).getInternalObject();
            
            switch(streamRecord.getEventName()) { 
            case "INSERT" : case "MODIFY" :
                StreamsAdapterDemoHelper.putItem(dynamoDBClient, tableName, streamRecord.getDynamodb().getNewImage());
                break;
            case "REMOVE" :
                StreamsAdapterDemoHelper.deleteItem(dynamoDBClient, tableName, streamRecord.getDynamodb().getKeys().get("Id").getN());
            }
        }
        checkpointCounter += 1;
        if(checkpointCounter % 10 == 0) {
            try {
                checkpointer.checkpoint();
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }

}
 
Example #7
Source File: DynamoDBTableReplicator.java    From podyn with Apache License 2.0 4 votes vote down vote up
protected IRecordProcessor createStreamProcessor() {
	return new IRecordProcessor() {

		@Override
		public void initialize(InitializationInput initializationInput) {
		}

		public List<Record> extractDynamoStreamRecords(List<com.amazonaws.services.kinesis.model.Record> kinesisRecords) {
			List<Record> dynamoRecords = new ArrayList<>(kinesisRecords.size());

			for(com.amazonaws.services.kinesis.model.Record kinesisRecord : kinesisRecords) {
				if (kinesisRecord instanceof RecordAdapter) {
					Record dynamoRecord = ((RecordAdapter) kinesisRecord).getInternalObject();
					dynamoRecords.add(dynamoRecord);
				}
			}

			return dynamoRecords;
		}

		@Override
		public void processRecords(ProcessRecordsInput processRecordsInput) {
			List<Record> records = extractDynamoStreamRecords(processRecordsInput.getRecords());

			DynamoDBTableReplicator.this.processRecords(records);

			checkpoint(processRecordsInput.getCheckpointer());
		}

		@Override
		public void shutdown(ShutdownInput shutdownInput) {
			if (shutdownInput.getShutdownReason() == ShutdownReason.TERMINATE) {
				checkpoint(shutdownInput.getCheckpointer());
			}
		}

		void checkpoint(IRecordProcessorCheckpointer checkpointer) {
			try {
				checkpointer.checkpoint();
			} catch (KinesisClientLibDependencyException|InvalidStateException|ThrottlingException|ShutdownException e) {
				LOG.warn(e);
			}
		}
	};
}