incompatible types: com.amazonaws.waiters.WaiterParameters 不是一个功能接口

huangapple go评论79阅读模式
英文:

incompatible types: com.amazonaws.waiters.WaiterParameters is not a functional interface

问题

当我创建DynamoDB表时,我尝试使用等待器等待AWS服务完成其操作。

CreateTableRequest createRequest = new CreateTableRequest()
            .withTableName(tableName);
    
CreateTableResult createResult = dynamoDbClient.createTable(createRequest);
dynamoDbClient.waiters().tableExists().run(() -> new DescribeTableRequest(tableName));
return dynamoDB.getTable(tableName);

但我收到以下错误信息:

incompatible types: com.amazonaws.waiters.WaiterParameters is not a functional interface

我是Java的新手,什么是函数式接口?我如何解决这个问题?

英文:

When I create a dynamodb table, I try to use waiters to wait AWS service to complete its action.

CreateTableRequest createRequest = new CreateTableRequest()
            .withTableName(tableName);
    
CreateTableResult createResult = dynamoDbClient.createTable(createRequest);
dynamoDbClient.waiters().tableExists().run(() -> new DescribeTableRequest(tableName));
return dynamoDB.getTable(tableName);

But I get:

incompatible types: com.amazonaws.waiters.WaiterParameters is not a functional interface

I'm new to Java, what's a functional interface? How can I get around this problem?

答案1

得分: 1

After dividing into 2 lines I was able to see the problem, new WaiterParameters<> is missing.

将其分成2行后,我能够看到问题,缺少新的 WaiterParameters<>。

Waiter<DescribeTableRequest> waiter = dynamoDbClient.waiters().tableExists();
waiter.run(new WaiterParameters<>(new DescribeTableRequest(tableName)));

英文:

After dividing into 2 lines I was able to see the problem, new WaiterParameters<> is missing.

Waiter&lt;DescribeTableRequest&gt; waiter = dynamoDbClient.waiters().tableExists();
waiter.run(new WaiterParameters&lt;&gt;(new DescribeTableRequest(tableName)));

答案2

得分: 0

使用AWS SDK for Java V2,而不是不再建议使用的AWS SDK for Java V1,详见此AWS页面

您可以轻松使用Amazon Dynamo V2 API使用等待器。请参考此代码示例。

//snippet-sourcedescription:[CreateTable.java演示如何使用等待器创建Amazon DynamoDB表。]
//snippet-keyword:[SDK for Java v2]
//snippet-service:[Amazon DynamoDB]

/*
   版权所有Amazon.com, Inc.或其附属公司。保留所有权利。
   SPDX许可证标识:Apache-2.0
*/

package com.example.dynamodb;

// snippet-start:[dynamodb.java2.create_table.import]
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.core.waiters.WaiterResponse;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition;
import software.amazon.awssdk.services.dynamodb.model.CreateTableRequest;
import software.amazon.awssdk.services.dynamodb.model.CreateTableResponse;
import software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest;
import software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse;
import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;
import software.amazon.awssdk.services.dynamodb.model.KeySchemaElement;
import software.amazon.awssdk.services.dynamodb.model.KeyType;
import software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput;
import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType;
import software.amazon.awssdk.services.dynamodb.waiters.DynamoDbWaiter;
// snippet-end:[dynamodb.java2.create_table.import]

/**
 * 在运行此Java V2代码示例之前,请设置您的开发环境,包括凭据。
 *
 * 有关更多信息,请参阅以下文档主题:
 *
 * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
 */
public class CreateTable {

    public static void main(String[] args) {

        final String usage = "\n" +
            "Usage:\n" +
            "    <tableName> <key>\n\n" +
            "Where:\n" +
            "    tableName - 要创建的Amazon DynamoDB表(例如,Music3)。\n\n" +
            "    key - Amazon DynamoDB表的主键(例如,Artist)。\n" ;

       if (args.length != 2) {
           System.out.println(usage);
           System.exit(1);
       }

       String tableName = args[0];
       String key = args[1];
       System.out.println("创建Amazon DynamoDB表 " + tableName + ",使用简单主键: " + key );

       ProfileCredentialsProvider credentialsProvider = ProfileCredentialsProvider.create();
       Region region = Region.US_EAST_1;
       DynamoDbClient ddb = DynamoDbClient.builder()
           .credentialsProvider(credentialsProvider)
           .region(region)
           .build();

       String result = createTable(ddb, tableName, key);
       System.out.println("新表格为 " + result);
       ddb.close();
    }

    // snippet-start:[dynamodb.java2.create_table.main]
    public static String createTable(DynamoDbClient ddb, String tableName, String key) {
        DynamoDbWaiter dbWaiter = ddb.waiter();
        CreateTableRequest request = CreateTableRequest.builder()
            .attributeDefinitions(AttributeDefinition.builder()
                .attributeName(key)
                .attributeType(ScalarAttributeType.S)
                .build())
            .keySchema(KeySchemaElement.builder()
                .attributeName(key)
                .keyType(KeyType.HASH)
                .build())
            .provisionedThroughput(ProvisionedThroughput.builder()
                .readCapacityUnits(new Long(10))
                .writeCapacityUnits(new Long(10))
                .build())
            .tableName(tableName)
            .build();

        String newTable = "";
        try {
            CreateTableResponse response = ddb.createTable(request);
            DescribeTableRequest tableRequest = DescribeTableRequest.builder()
                .tableName(tableName)
                .build();

            // 等待Amazon DynamoDB表创建完成。
            WaiterResponse<DescribeTableResponse> waiterResponse = dbWaiter.waitUntilTableExists(tableRequest);
            waiterResponse.matched().response().ifPresent(System.out::println);
            newTable = response.tableDescription().tableName();
            return newTable;

        } catch (DynamoDbException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
       return "";
    }
    // snippet-end:[dynamodb.java2.create_table.main]
}

要了解有关等待器和AWS SDK for Java V2的更多信息,请参阅:

在AWS SDK for Java 2.x中使用等待器

英文:

Use AWS SDK for Java V2, not the AWS SDK for Java V1 - which is no longer recommended to use as specified on this AWS page.

You can use Waiters easily using the Amazon Dynamo V2 API. See this code example.

//snippet-sourcedescription:[CreateTable.java demonstrates how to create an Amazon DynamoDB table by using a waiter.]
//snippet-keyword:[SDK for Java v2]
//snippet-service:[Amazon DynamoDB]
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package com.example.dynamodb;
// snippet-start:[dynamodb.java2.create_table.import]
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.core.waiters.WaiterResponse;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition;
import software.amazon.awssdk.services.dynamodb.model.CreateTableRequest;
import software.amazon.awssdk.services.dynamodb.model.CreateTableResponse;
import software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest;
import software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse;
import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;
import software.amazon.awssdk.services.dynamodb.model.KeySchemaElement;
import software.amazon.awssdk.services.dynamodb.model.KeyType;
import software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput;
import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType;
import software.amazon.awssdk.services.dynamodb.waiters.DynamoDbWaiter;
// snippet-end:[dynamodb.java2.create_table.import]
/**
* Before running this Java V2 code example, set up your development environment, including your credentials.
*
* For more information, see the following documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
*/
public class CreateTable {
public static void main(String[] args) {
final String usage = &quot;\n&quot; +
&quot;Usage:\n&quot; +
&quot;    &lt;tableName&gt; &lt;key&gt;\n\n&quot; +
&quot;Where:\n&quot; +
&quot;    tableName - The Amazon DynamoDB table to create (for example, Music3).\n\n&quot; +
&quot;    key - The key for the Amazon DynamoDB table (for example, Artist).\n&quot; ;
if (args.length != 2) {
System.out.println(usage);
System.exit(1);
}
String tableName = args[0];
String key = args[1];
System.out.println(&quot;Creating an Amazon DynamoDB table &quot;+tableName +&quot; with a simple primary key: &quot; +key );
ProfileCredentialsProvider credentialsProvider = ProfileCredentialsProvider.create();
Region region = Region.US_EAST_1;
DynamoDbClient ddb = DynamoDbClient.builder()
.credentialsProvider(credentialsProvider)
.region(region)
.build();
String result = createTable(ddb, tableName, key);
System.out.println(&quot;New table is &quot;+result);
ddb.close();
}
// snippet-start:[dynamodb.java2.create_table.main]
public static String createTable(DynamoDbClient ddb, String tableName, String key) {
DynamoDbWaiter dbWaiter = ddb.waiter();
CreateTableRequest request = CreateTableRequest.builder()
.attributeDefinitions(AttributeDefinition.builder()
.attributeName(key)
.attributeType(ScalarAttributeType.S)
.build())
.keySchema(KeySchemaElement.builder()
.attributeName(key)
.keyType(KeyType.HASH)
.build())
.provisionedThroughput(ProvisionedThroughput.builder()
.readCapacityUnits(new Long(10))
.writeCapacityUnits(new Long(10))
.build())
.tableName(tableName)
.build();
String newTable =&quot;&quot;;
try {
CreateTableResponse response = ddb.createTable(request);
DescribeTableRequest tableRequest = DescribeTableRequest.builder()
.tableName(tableName)
.build();
// Wait until the Amazon DynamoDB table is created.
WaiterResponse&lt;DescribeTableResponse&gt; waiterResponse = dbWaiter.waitUntilTableExists(tableRequest);
waiterResponse.matched().response().ifPresent(System.out::println);
newTable = response.tableDescription().tableName();
return newTable;
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
return &quot;&quot;;
}
// snippet-end:[dynamodb.java2.create_table.main]
}

To read more about waiter and the AWS SDK for Java V2, see:

Using waiters in the AWS SDK for Java 2.x

huangapple
  • 本文由 发表于 2023年7月6日 21:30:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629377.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定