【问题标题】:What is the alternative for AmazonDynamoDBClient that got deprecated?已弃用的 AmazonDynamoDBClient 的替代方案是什么?
【发布时间】:2017-01-25 11:12:29
【问题描述】:

有谁知道什么取代了 AmazonDynamoDBClient? 在文档中找不到任何内容

包 - com.amazonaws.services.dynamodbv2

    AmazonDynamoDBClient amazonDynamoDBClient = new AmazonDynamoDBClient();

【问题讨论】:

    标签: amazon-dynamodb


    【解决方案1】:

    根据API doc,应使用构建器类(例如AmazonDynamoDBClientBuilder)来创建实例。

    使用构建器类的示例代码:-

    我已经为本地 DynamoDB 创建了客户端。

    DynamoDB dynamoDB = new DynamoDB(AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(new EndpointConfiguration("http://localhost:8000", "us-east-1")).build());
    
    Table table = dynamoDB.getTable("Movies");
    

    使用 DynamoDB 表类进行扫描:-

    private static void findProductsForPriceLessThanZero() {
    
            Table table = dynamoDB.getTable(tableName);
    
            Map<String, Object> expressionAttributeValues = new HashMap<String, Object>();
            expressionAttributeValues.put(":pr", 100);
    
            ItemCollection<ScanOutcome> items = table.scan(
                "Price < :pr", //FilterExpression
                "Id, Title, ProductCategory, Price", //ProjectionExpression
                null, //ExpressionAttributeNames - not used in this example 
                expressionAttributeValues);
    
            System.out.println("Scan of " + tableName + " for items with a price less than 100.");
            Iterator<Item> iterator = items.iterator();
            while (iterator.hasNext()) {
                System.out.println(iterator.next().toJSONPretty());
            }    
        }
    

    【讨论】:

    • 请参阅此链接以获取 Dynamodb 端点 docs.aws.amazon.com/general/latest/gr/rande.html#ddb_region ... 如果您不使用本地 DynamoDB,则可能不需要端点,因为它将根据区域自动派生。或者,您可以使用上述链接中给出的端点。
    • 谢谢。只是想知道这是否可以帮助我一次性获取表中的所有记录?
    • 可以,可以递归扫描表,得到表中的所有项目。 docs.aws.amazon.com/amazondynamodb/latest/developerguide/…
    • 我可以使用 - ScanRequest scanRequest = new ScanRequest().withTableName(tableName); ScanResult 结果 = amazonDynamoDBClient.scan(scanRequest); //然后使用result.getItems()
    • 是的,但扫描方法在 AmazonDynamoDBClient 中可用 - 上面的答案使用 DynamoDB
    【解决方案2】:

    我正在使用 spring-boot,我使用 Dynamo 的方式是注入一个 AWSCredentialsProvider 并以这种方式使用我环境中的变量:

        @Bean
        public AmazonDynamoDB amazonDynamoDB(AWSCredentialsProvider awsCredentialsProvider) {
            AmazonDynamoDB amazonDynamoDB
                    = AmazonDynamoDBClientBuilder.standard()
                    .withCredentials(awsCredentialsProvider).build();
            return amazonDynamoDB;
        }
    
        @Bean
        public AWSCredentialsProvider awsCredentialsProvider() {
            return new EnvironmentVariableCredentialsProvider();
        }
    

    完整示例可在此处获得:https://github.com/ioet/bpm-skills-api

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-21
      • 1970-01-01
      • 1970-01-01
      • 2022-06-11
      • 1970-01-01
      • 2016-08-16
      相关资源
      最近更新 更多