有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

amazon web服务通过Java代码将多个项目放入DynamoDB

我想使用SDK Amazon的batchWriteItem方法将很多项放入表中。 我从Kinesis那里找到了物品,里面有很多碎片。 我对一个项目使用了这种方法:

public static void addSingleRecord(Item thingRecord) {


    // Add an item
    try
    {

        DynamoDB dynamo = new DynamoDB(dynamoDB); 
        Table table = dynamo.getTable(dataTable);
        table.putItem(thingRecord);

    } catch (AmazonServiceException ase) {
        System.out.println("addThingsData request  "
                + "to AWS was rejected with an error response for some reason.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        System.out.println("addThingsData - Caught an AmazonClientException, which means the client encountered "
                + "a serious internal problem while trying to communicate with AWS, "
                + "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

public static void addThings(String thingDatum) {
    Item itemJ2;
    itemJ2 = Item.fromJSON(thingDatum);
    addSingleRecord(itemJ2);

}

该项从以下位置传递:

private void processSingleRecord(Record record) {
    // TODO Add your own record processing logic here


    String data = null;
    try {


        // For this app, we interpret the payload as UTF-8 chars.
        data = decoder.decode(record.getData()).toString();
        System.out.println("**processSingleRecord - data " + data);
        AmazonDynamoDBSample.addThings(data);

    } catch (NumberFormatException e) {
        LOG.info("Record does not match sample record format. Ignoring record with data; " + data);
    } catch (CharacterCodingException e) {
        LOG.error("Malformed data: " + data, e);
    }
}

现在,如果我想做很多记录,我将使用:

    public static void writeMultipleItemsBatchWrite(Item thingRecord) {
    try {                    


            dataTableWriteItems.addItemToPut(thingRecord);
        System.out.println("Making the request.");
        BatchWriteItemOutcome outcome = dynamo.batchWriteItem(dataTableWriteItems);
        do {

                // Check for unprocessed keys which could happen if you exceed provisioned throughput

                Map<String, List<WriteRequest>> unprocessedItems = outcome.getUnprocessedItems();

                if (outcome.getUnprocessedItems().size() == 0) {
                    System.out.println("No unprocessed items found");
                } else {
                    System.out.println("Retrieving the unprocessed items");
                    outcome = dynamo.batchWriteItemUnprocessed(unprocessedItems);
                }

            } while (outcome.getUnprocessedItems().size() > 0);

    }  catch (Exception e) {
        System.err.println("Failed to retrieve items: ");
        e.printStackTrace(System.err);
    }  

}

但我怎样才能把最后一批人送去呢?因为我只有当我有25件物品时才会发送,但最后发送的数量会更低


共 (1) 个答案

  1. # 1 楼答案

    您可以使用PutItem或UpdateItem,使用Lambda function中附加到Kinesis StreamDocument SDK中的Document SDK一次向DynamoDB表写入一个项。这样,您就可以在流记录出现在流中时对其做出反应,而不用担心是否还有更多记录要处理。在幕后,BatchWriteItem消耗的写容量单位与相应的PutItem调用相同。BatchWriteItem与耗时最长的批次中的PUT一样具有潜在性。因此,使用BatchWriteItem时,您可能会遇到比并行PutItem/UpdateItem调用更高的平均延迟