rafiya
rafiya

Reputation: 97

AmazonDynamoDBClientBuilder.standard cannot be resolved to a type

I am trying to follow a basic AWS tutorial for interacting with DynamoDB in a java runtime environment on a AWS serverless setup. However, for some reason eclipse is throwing an error when I try to create a new AmazonDynamoDBClientBuilder

I double-checked and I see the proper dependencies documented in POM.xml, however I still keep getting the error "AmazonDynamoDBClientBuilder.standard cannot be resolved to a type"

The code:

package com.serverless.demo.function;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
import com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
import com.amazonaws.services.dynamodbv2.model.KeyType;
import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType;


public class HelloWorld implements RequestHandler<String, String> {
    @Override
    public String handleRequest(String input, Context context) {

        AmazonDynamoDB client = new AmazonDynamoDBClientBuilder.standard().build();  

    }
}

Upvotes: 6

Views: 8727

Answers (3)

dutoitns
dutoitns

Reputation: 2273

Can you post your maven.xml?

You should have the following dependency:

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-dynamodb</artifactId>
    <version>1.11.163</version>
</dependency>

Check for the latest version of the archive on mvnrepository [here].

Or alternatively a maven file structured as follows:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-bom</artifactId>
            <version>1.11.166</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-dynamodb</artifactId>
    </dependency>
</dependencies>

Please note:

This answer was given when there wasn't a 2nd version of the AWS SDK. In AWS SDK v2 your classes are prefixed with software.amazon.awssdk. They no longer reside in packages prefixed with com.amazonaws as in AWS SDK v1.

Using AWS SDK v2 you can setup your Maven BOM (bill of materials) as follows:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>bom</artifactId>
            <version>2.20.78</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

For the AWS documentation on setting up a Maven project check [here]. For the latest version of the AWS BOM check [here].

Upvotes: 5

Andrei
Andrei

Reputation: 829

Just having the following dependency is not enough

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-dynamodb</artifactId>
    <version>1.11.456</version>
</dependency>

you also need to add a classpath entry to the .classpath file (edit the path by replacing USERNAME and making other necessary edits)

<classpathentry kind="lib" path="C:/Users/USERNAME/.m2/repository/com/amazonaws/aws-java-sdk-dynamodb/1.11.456/aws-java-sdk-dynamodb-1.11.456.jar" sourcepath="C:/Users/USERNAME/.m2/repository/com/amazonaws/aws-java-sdk-dynamodb/1.11.456/aws-java-sdk-dynamodb-1.11.456-sources.jar">
    <attributes>
        <attribute name="javadoc_location" value="jar:file:/C:/Users/USERNAME/.m2/repository/com/amazonaws/aws-java-sdk-dynamodb/1.11.456/aws-java-sdk-dynamodb-1.11.456-javadoc.jar!/"/>
        <attribute name="maven.pomderived" value="true"/>
        <attribute name="org.eclipse.jst.component.nondependency" value=""/>
        <attribute name="maven.groupId" value="com.amazonaws"/>
        <attribute name="maven.artifactId" value="aws-java-sdk-dynamodb"/>
        <attribute name="maven.version" value="1.11.456"/>
        <attribute name="maven.scope" value="provided"/>
    </attributes>
</classpathentry>

You shold also check for the latest dependency here

Hope this helps!

Upvotes: 0

millems
millems

Reputation: 56

You'll need to not specify new, because the standard() method performs the instantiation for you:

AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();

Upvotes: 3

Related Questions