首页 > 解决方案 > 为什么当我更改我喜欢的价格和时间时,我的 dynamoDb 喜欢更新而不添加新的?

问题描述

大家好,当我想像这样将数据插入到我的 dynamoDb 时出现错误

在我的代码中,我添加了符号。这里是我的代码

public class InsertData {

    static AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
            .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://dynamodb.us-east-2.amazonaws.com", "us-east-2"))
            .build();
    static DynamoDB dynamoDB = new DynamoDB(client);
    static String tableName = "PriceOneZero";
    public static void main(String[] args)throws IOException {
        // TODO Auto-generated method stub
        System.out.println("Call CreateItem");
        createItems();
}
    private static void createItems() {
        // TODO Auto-generated method stub
        Table table = dynamoDB.getTable(tableName);
        System.out.println("runFirstTime For Insert Data");
        try{
            String symbols= "USDJPY";
            String price = "100.89";
            String time = "2020-06-05T00:00:02.111";

            //Add Content 
            Item item = new Item().withPrimaryKey("Symbol", symbols).withString("Price", price).withString("Time", time);
            //save to DynamoDb
            table.putItem(item);
            System.out.println("Success For Insert Data");


        }catch (AmazonDynamoDBException e) {
            System.err.println("Create items failed.");
            System.err.println(e.getMessage());
           }
    }
}

当我在我的数据库中使用该代码时,只有数据

交易品种:USDJPY 价格:100.89 时间:2020-06-05T00:00:02.111

之后我尝试将我的代码更改为

公共类插入数据 {

    static AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
            .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://dynamodb.us-east-2.amazonaws.com", "us-east-2"))
            .build();
    static DynamoDB dynamoDB = new DynamoDB(client);
    static String tableName = "PriceOneZero";
    public static void main(String[] args)throws IOException {
        // TODO Auto-generated method stub
        System.out.println("Call CreateItem");
        createItems();
}
    private static void createItems() {
        // TODO Auto-generated method stub
        Table table = dynamoDB.getTable(tableName);
        System.out.println("runFirstTime For Insert Data");
        try{
            String symbols= "USDJPY";
            String price = "100.89";
            String time = "2020-06-05T00:00:02.112";

            //Add Content 
            Item item = new Item().withPrimaryKey("Symbol", symbols).withString("Price", price).withString("Time", time);
            //save to DynamoDb
            table.putItem(item);
            System.out.println("Success For Insert Data");


        }catch (AmazonDynamoDBException e) {
            System.err.println("Create items failed.");
            System.err.println(e.getMessage());
           }
    }
}

不同的时间是从 .111 变成 .112

在我的 dynamoDb 数据库中,我的数据只喜欢更新而不添加,所以我只有一个数据,比如 Symbol :USDJPY 价格:100.89 时间:2020-06-05T00:00:02.111

不喜欢这个 符号 价格时间 USDJPY 100.89 2020-06-05T00:00:02.111 USDJPY 100.89 2020-06-05T00:00:02.112

为什么这样?

我的问题为什么有任何错误?以及如何解决?

问候,

福阿德

标签: javaamazon-dynamodbamazon-dynamodb-streams

解决方案


推荐阅读