Java Code Examples for com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient#setEndpoint()

The following examples show how to use com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient#setEndpoint() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MoviesItemOps06.java    From aws-dynamodb-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args)  {

        AmazonDynamoDBClient client = new AmazonDynamoDBClient();
        client.setEndpoint("http://localhost:8000");
        DynamoDB dynamoDB = new DynamoDB(client);

        Table table = dynamoDB.getTable("Movies");
                
        // Conditional delete (will fail)
        
        DeleteItemSpec deleteItemSpec = new DeleteItemSpec()
            .withPrimaryKey(new PrimaryKey("year", 2015, "title", "The Big New Movie"))
            .withConditionExpression("info.rating <= :val")
            .withValueMap(new ValueMap()
                   .withNumber(":val", 5.0));
        
        System.out.println("Attempting a conditional delete...");
        try {
            table.deleteItem(deleteItemSpec);
            System.out.println("DeleteItem succeeded");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("DeleteItem failed");
        }

    }
 
Example 2
Source File: ImageIngester.java    From aws-dynamodb-mars-json-demo with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs a {@link ImageIngester} with the specified command line arguments and Amazon Web Services credentials
 * provider.
 *
 * @param args
 *            Command line arguments for retrieving the configuration
 * @param credentialsProvider
 *            Amazon Web Services credentials provider
 * @throws ExitException
 *             Error parsing configuration
 */
public ImageIngester(final String[] args, final AWSCredentialsProvider credentialsProvider) throws ExitException {
    // Parse command line arguments to locate configuration file
    final ImageIngesterCLI cli = new ImageIngesterCLI(args);
    config = cli.getConfig();
    // Validate the configuration file
    ConfigParser.validateConfig(config, REQUIRED_STRING_CONFIGURATIONS, REQUIRED_BOOLEAN_CONFIGURATIONS,
        REQUIRED_INTEGER_CONFIGURATIONS, REQUIRED_LONG_CONFIGURATIONS);
    // Parse configuration settings
    resourceTable = ConfigParser.parseString(config, CONFIG_RESOURCE_TABLE);
    imageTable = ConfigParser.parseString(config, CONFIG_IMAGE_TABLE);
    waitTime = ConfigParser.parseLong(config, CONFIG_WAIT_TIME, DEFAULT_WAIT_TIME);
    connectTimeout = ConfigParser.parseInteger(config, CONFIG_CONNECT_TIMEOUT, DEFAULT_CONNECT_TIMEOUT);
    final String endpoint = ConfigParser.parseString(config, CONFIG_ENDPOINT);
    final int numManifestThreads = ConfigParser.parseInteger(config, CONFIG_NUM_MANIFEST_THREADS, DEFAULT_THREADS);
    // Setup state
    dynamoDB = new AmazonDynamoDBClient(credentialsProvider);
    dynamoDB.setEndpoint(endpoint);
    manifestPool = Executors.newFixedThreadPool(numManifestThreads);
}
 
Example 3
Source File: DynamoDBService2.java    From Doradus with Apache License 2.0 6 votes vote down vote up
public DynamoDBService2(Tenant tenant) { 
    super(tenant);
    
    String accessKey = getParamString("ddb-access-key");
    String secretKey = getParamString("ddb-secret-key");
    String endpoint = getParamString("ddb-endpoint");
    m_readCapacityUnits = getParamInt("ddb-read-capacity-units", 1);
    m_writeCapacityUnits = getParamInt("ddb-write-capacity-units", 1);
    
    BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey); 
    m_client = new AmazonDynamoDBClient(awsCredentials);
    m_client.setEndpoint(endpoint);
    // try to connect to check the connection
    m_client.listTables();
    
    m_logger.info("Started DynamoDB service. Endpoint: {}, read/write capacity units for new namespaces: {}/{}",
                  new Object[] {endpoint, m_readCapacityUnits, m_writeCapacityUnits});
}
 
Example 4
Source File: MoviesScan.java    From aws-dynamodb-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {

        AmazonDynamoDBClient client = new AmazonDynamoDBClient();
        client.setEndpoint("http://localhost:8000");
        DynamoDB dynamoDB = new DynamoDB(client);
   
        Table table = dynamoDB.getTable("Movies");
        
        ScanSpec scanSpec = new ScanSpec()
            .withProjectionExpression("#yr, title, info.rating")
            .withFilterExpression("#yr between :start_yr and :end_yr")
            .withNameMap(new NameMap().with("#yr",  "year"))
            .withValueMap(new ValueMap().withNumber(":start_yr", 1950).withNumber(":end_yr", 1959));
        
        ItemCollection<ScanOutcome> items = table.scan(scanSpec);
        
        Iterator<Item> iter = items.iterator();
        while (iter.hasNext()) {
            Item item = iter.next();
            System.out.println(item.toString());
        }
    }
 
Example 5
Source File: MoviesCreateTable.java    From aws-dynamodb-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

        AmazonDynamoDBClient client = new AmazonDynamoDBClient();
        
        client.setEndpoint("http://localhost:8000");
        DynamoDB dynamoDB = new DynamoDB(client);
        
        String tableName = "Movies";
        Table table = dynamoDB.createTable(tableName,
                Arrays.asList(
                        new KeySchemaElement("year", KeyType.HASH),
                        new KeySchemaElement("title", KeyType.RANGE)), 
                Arrays.asList(
                        new AttributeDefinition("year", ScalarAttributeType.N),
                        new AttributeDefinition("title", ScalarAttributeType.S)), 
                new ProvisionedThroughput(10L, 10L));

        try {
            TableUtils.waitUntilActive(client, tableName);
            System.out.println("Table status: " + table.getDescription().getTableStatus());
        } catch (AmazonClientException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
 
Example 6
Source File: DeviceAuthentication.java    From reinvent2013-mobile-photo-share with Apache License 2.0 5 votes vote down vote up
/**
 * Looks up table name and creates one if it does not exist
 */
public DeviceAuthentication() {
    ddb = new AmazonDynamoDBClient(new BasicAWSCredentials(Configuration.AWS_ACCESS_KEY_ID,
            Configuration.AWS_SECRET_KEY));
    ddb.setEndpoint(Configuration.DYNAMODB_ENDPOINT);

    try {
        if (!doesTableExist(DEVICE_TABLE)) {
            createDeviceTable();
        }
    } catch (DataAccessException e) {
        throw new RuntimeException("Failed to create device table.", e);
    }
}
 
Example 7
Source File: MoviesItemOps03.java    From aws-dynamodb-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args)  {

        AmazonDynamoDBClient client = new AmazonDynamoDBClient();
        client.setEndpoint("http://localhost:8000");
        DynamoDB dynamoDB = new DynamoDB(client);

        Table table = dynamoDB.getTable("Movies");
        
        int year = 2015;
        String title = "The Big New Movie";

        UpdateItemSpec updateItemSpec = new UpdateItemSpec()
            .withPrimaryKey("year", year, "title", title)
            .withUpdateExpression("set info.rating = :r, info.plot=:p, info.actors=:a")
            .withValueMap(new ValueMap()
                .withNumber(":r", 5.5)
                .withString(":p", "Everything happens all at once.")
                .withList(":a", Arrays.asList("Larry","Moe","Curly")));

        System.out.println("Updating the item...");
        try {
            table.updateItem(updateItemSpec);
            System.out.println("UpdateItem succeeded: " + table.getItem("year", year, "title", title).toJSONPretty());
        } catch (Exception e) {
            System.out.println("UpdateItem failed");
            e.printStackTrace();
        }
                

    }
 
Example 8
Source File: MoviesItemOps04.java    From aws-dynamodb-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args)  {

        AmazonDynamoDBClient client = new AmazonDynamoDBClient();
        client.setEndpoint("http://localhost:8000");
        DynamoDB dynamoDB = new DynamoDB(client);

        Table table = dynamoDB.getTable("Movies");
        
        int year = 2015;
        String title = "The Big New Movie";
        
        UpdateItemSpec updateItemSpec = new UpdateItemSpec()
            .withPrimaryKey("year", year, "title", title)
            .withUpdateExpression("set info.rating = info.rating + :val")
            .withValueMap(new ValueMap()
                .withNumber(":val", 1));

        System.out.println("Incrementing an atomic counter...");
        try {
            table.updateItem(updateItemSpec);
            System.out.println("UpdateItem succeeded: " + table.getItem("year", year, "title", title).toJSONPretty());
        } catch (Exception e) {
            System.out.println("UpdateItem failed");
            e.printStackTrace();
        }

    }
 
Example 9
Source File: MoviesDeleteTable.java    From aws-dynamodb-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args)  {

        AmazonDynamoDBClient client = new AmazonDynamoDBClient();
        client.setEndpoint("http://localhost:8000");
        DynamoDB dynamoDB = new DynamoDB(client);
       
        Table table = dynamoDB.getTable("Movies");
        table.delete();
        System.out.println("Table is being deleted");
        
    }
 
Example 10
Source File: MoviesItemOps05.java    From aws-dynamodb-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args)  {

        AmazonDynamoDBClient client = new AmazonDynamoDBClient();
        client.setEndpoint("http://localhost:8000");
        DynamoDB dynamoDB = new DynamoDB(client);

        Table table = dynamoDB.getTable("Movies");
        
        int year = 2015;
        String title = "The Big New Movie";

        // Conditional update (will fail)

        UpdateItemSpec updateItemSpec = new UpdateItemSpec()
            .withPrimaryKey(new PrimaryKey("year", 2015, "title",  "The Big New Movie"))
            .withUpdateExpression("remove info.actors[0]")
            .withConditionExpression("size(info.actors) > :num")
            .withValueMap(new ValueMap().withNumber(":num", 3));

        System.out.println("Attempting a conditional update...");
        try {
            table.updateItem(updateItemSpec);
            System.out.println("UpdateItem succeeded: " + table.getItem("year", year, "title", title).toJSONPretty());
        } catch (ConditionalCheckFailedException e) {
            e.printStackTrace();
            System.out.println("UpdateItem failed");
        }

    }
 
Example 11
Source File: MoviesItemOps02.java    From aws-dynamodb-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args)  {

        AmazonDynamoDBClient client = new AmazonDynamoDBClient();
        client.setEndpoint("http://localhost:8000");
        DynamoDB dynamoDB = new DynamoDB(client);

        Table table = dynamoDB.getTable("Movies");

        int year = 2015;
        String title = "The Big New Movie";

        final Map<String, Object> infoMap = new HashMap<String, Object>();
        infoMap.put("plot",  "Nothing happens at all.");
        infoMap.put("rating",  0.0);
        Item item = new Item()
            .withPrimaryKey(new PrimaryKey("year", year, "title", title))
            .withMap("info", infoMap);
        
        // Attempt a conditional write.  We expect this to fail.

        PutItemSpec putItemSpec = new PutItemSpec()
            .withItem(item)
            .withConditionExpression("attribute_not_exists(#yr) and attribute_not_exists(title)")
            .withNameMap(new NameMap()
                .with("#yr", "year"));
        
        System.out.println("Attempting a conditional write...");
        try {
            table.putItem(putItemSpec);
            System.out.println("PutItem succeeded: " + table.getItem("year", year, "title", title).toJSONPretty());
        } catch (ConditionalCheckFailedException e) {
            e.printStackTrace(System.err);
            System.out.println("PutItem failed");
        }
    }
 
Example 12
Source File: UserAuthentication.java    From reinvent2013-mobile-photo-share with Apache License 2.0 5 votes vote down vote up
/**
 * Looks up table name and creates one if it does not exist
 */
public UserAuthentication() {
    ddb = new AmazonDynamoDBClient(new BasicAWSCredentials(Configuration.AWS_ACCESS_KEY_ID,
            Configuration.AWS_SECRET_KEY));
    ddb.setEndpoint(Configuration.DYNAMODB_ENDPOINT);

    try {
        if (!doesTableExist(USER_TABLE)) {
            createIdentityTable();
        }
    } catch (DataAccessException e) {
        throw new RuntimeException("Failed to create device table.", e);
    }
}
 
Example 13
Source File: DeviceAuthentication.java    From reinvent2013-mobile-photo-share with Apache License 2.0 5 votes vote down vote up
/**
 * Looks up table name and creates one if it doesnot exist
 */
public DeviceAuthentication() {
    ddb = new AmazonDynamoDBClient(new BasicAWSCredentials(Configuration.AWS_ACCESS_KEY_ID,
            Configuration.AWS_SECRET_KEY));
    ddb.setEndpoint(Configuration.DYNAMODB_ENDPOINT);

    try {
        if (!doesTableExist(DEVICE_TABLE)) {
            createDeviceTable();
        }
    } catch (DataAccessException e) {
        throw new RuntimeException("Failed to create device table.", e);
    }
}
 
Example 14
Source File: DynamoDocumentStoreTemplateIntegrationTest.java    From Cheddar with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void createTables() throws Exception {
    amazonDynamoDbClient = new AmazonDynamoDBClient();
    amazonDynamoDbClient.setEndpoint(AwsIntegration.getDynamoDbEndpoint());
    dataGenerator = new DynamoDbDataGenerator(amazonDynamoDbClient);

    dataGenerator.createStubItemTable();
    dataGenerator.createStubItemWithRangeTable();
    dataGenerator.createStubItemWithCompoundGlobalSecondaryIndexTable();
    dataGenerator.createStubItemWithHashAndRangePrimaryKeyAndCompoundGlobalSecondaryIndexTable();
}
 
Example 15
Source File: DynamoDbTemplateIntegrationTest.java    From Cheddar with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void createTables() throws Exception {
    amazonDynamoDbClient = new AmazonDynamoDBClient();
    amazonDynamoDbClient.setEndpoint(AwsIntegration.getDynamoDbEndpoint());
    dataGenerator = new DynamoDbDataGenerator(amazonDynamoDbClient);

    dataGenerator.createStubItemTable();
    dataGenerator.createStubItemWithRangeTable();
    dataGenerator.createStubItemWithCompoundGlobalSecondaryIndexTable();
    dataGenerator.createStubItemWithHashAndRangePrimaryKeyAndCompoundGlobalSecondaryIndexTable();
}
 
Example 16
Source File: StreamsRecordProcessorFactory.java    From aws-dynamodb-examples with Apache License 2.0 4 votes vote down vote up
@Override
public IRecordProcessor createProcessor() {
    AmazonDynamoDBClient dynamoDBClient = new AmazonDynamoDBClient(dynamoDBCredentials, new ClientConfiguration());
    dynamoDBClient.setEndpoint(dynamoDBEndpoint);
    return new StreamsRecordProcessor(dynamoDBClient, tableName);
}
 
Example 17
Source File: StreamsAdapterDemo.java    From aws-dynamodb-examples with Apache License 2.0 4 votes vote down vote up
/**
 * @param args
 */
public static void main(String[] args) throws Exception {
    System.out.println("Starting demo...");

    String srcTable = tablePrefix + "-src";
    String destTable = tablePrefix + "-dest";
    streamsCredentials = new ProfileCredentialsProvider();
    dynamoDBCredentials = new ProfileCredentialsProvider();
    recordProcessorFactory = new StreamsRecordProcessorFactory(dynamoDBCredentials, dynamodbEndpoint, serviceName, destTable);


    /* ===== REQUIRED =====
     * Users will have to explicitly instantiate and configure the adapter, then pass it to
     * the KCL worker.
     */
    adapterClient = new AmazonDynamoDBStreamsAdapterClient(streamsCredentials, new ClientConfiguration());
    adapterClient.setEndpoint(streamsEndpoint);

    dynamoDBClient = new AmazonDynamoDBClient(dynamoDBCredentials, new ClientConfiguration());
    dynamoDBClient.setEndpoint(dynamodbEndpoint);

    cloudWatchClient = new AmazonCloudWatchClient(dynamoDBCredentials, new ClientConfiguration());

    setUpTables();

    workerConfig = new KinesisClientLibConfiguration("streams-adapter-demo",
            streamArn, streamsCredentials, "streams-demo-worker")
        .withMaxRecords(1)
        .withInitialPositionInStream(InitialPositionInStream.TRIM_HORIZON);

    System.out.println("Creating worker for stream: " + streamArn);
    worker = new Worker(recordProcessorFactory, workerConfig, adapterClient, dynamoDBClient, cloudWatchClient);
    System.out.println("Starting worker...");
    Thread t = new Thread(worker);
    t.start();

    Thread.sleep(25000);
    worker.shutdown();
    t.join();

    if(StreamsAdapterDemoHelper.scanTable(dynamoDBClient, srcTable).getItems().equals(StreamsAdapterDemoHelper.scanTable(dynamoDBClient, destTable).getItems())) {
        System.out.println("Scan result is equal.");
    } else {
        System.out.println("Tables are different!");
    }

    System.out.println("Done.");
    cleanupAndExit(0);
}
 
Example 18
Source File: AmazonDynamoDBStreamstoIgnite.java    From aws-big-data-blog with Apache License 2.0 4 votes vote down vote up
public void run() throws Exception {
	adapterClient = new AmazonDynamoDBStreamsAdapterClient(new ClientConfiguration());
	adapterClient.setEndpoint(streamsEndpoint);
	dynamoDBClient = new AmazonDynamoDBClient(new ClientConfiguration());
	dynamoDBClient.setEndpoint(dynamodbEndpoint);

	cloudWatchClient = new AmazonCloudWatchClient(dynamoDBCredentials, new ClientConfiguration());

	TcpDiscoverySpi spi = new TcpDiscoverySpi();
	TcpDiscoveryVmIpFinder ipFinder = new TcpDiscoveryVmIpFinder();
	List<String> hostList = Arrays.asList(Properties.getString("hostList").split(","));
	ipFinder.setAddresses(hostList);
	spi.setIpFinder(ipFinder);
	IgniteConfiguration cfg = new IgniteConfiguration();
	cfg.setDiscoverySpi(spi);
	cfg.setClientMode(true);
	cfg.setPeerClassLoadingEnabled(true);

	@SuppressWarnings("unused")
	Ignite ignite = Ignition.start(cfg);
	cache = Ignition.ignite().cache(Properties.getString("cacheName"));
	LOG.info(">>> cache acquired");

	recordProcessorFactory = new StreamsRecordProcessorFactory(cache);
	workerConfig = new KinesisClientLibConfiguration(Properties.getString("applicationName"), streamArn,
			streamsCredentials, "ddbstreamsworker")
					.withMaxRecords(Integer.parseInt(Properties.getString("maxRecords")))
					.withInitialPositionInStream(
							InitialPositionInStream.valueOf(Properties.getString("initialPositionInStream")));

	LOG.info("Creating worker for stream: " + streamArn);
	worker = new Worker(recordProcessorFactory, workerConfig, adapterClient, dynamoDBClient, cloudWatchClient);
	LOG.info("Starting worker...");

	int exitCode = 0;
	try {
		worker.run();
	} catch (Throwable t) {
		LOG.error("Caught throwable while processing data.");
		t.printStackTrace();
		exitCode = 1;
	}
	System.exit(exitCode);
}
 
Example 19
Source File: BaseAdmin.java    From reinvent2013-mobile-photo-share with Apache License 2.0 4 votes vote down vote up
public BaseAdmin(String awsAccessKeyID, String awsSecretKey) {
    ddb = new AmazonDynamoDBClient(new BasicAWSCredentials(awsAccessKeyID, awsSecretKey));
    ddb.setEndpoint("http://dynamodb.us-east-1.amazonaws.com");
}
 
Example 20
Source File: AWSDatabaseHolder.java    From billow with Apache License 2.0 4 votes vote down vote up
public AWSDatabaseHolder(Config config) {
    maxAgeInMs = config.getDuration("maxAge", TimeUnit.MILLISECONDS);

    final DefaultAWSCredentialsProviderChain awsCredentialsProviderChain = new DefaultAWSCredentialsProviderChain();

    final ClientConfiguration clientConfig = new ClientConfiguration();
    clientConfig.setRetryPolicy(new RetryPolicy(null, null, config.getInt("maxErrorRetry"), true));
    clientConfig.setSocketTimeout(config.getInt("socketTimeout") * 1000);

    final AmazonEC2 bootstrapEC2Client = AmazonEC2ClientBuilder.standard().withCredentials(awsCredentialsProviderChain).build();

    ec2Clients = Maps.newHashMap();
    rdsClients = Maps.newHashMap();
    sqsClients = Maps.newHashMap();
    dynamoDBClients = Maps.newHashMap();
    elasticacheClients = Maps.newHashMap();
    elasticsearchClients = Maps.newHashMap();

    final List<Region> ec2Regions = bootstrapEC2Client.describeRegions().getRegions();
    for (Region region : ec2Regions) {
        final String regionName = region.getRegionName();
        final String endpoint = region.getEndpoint();
        log.debug("Adding ec2 region {}", region);

        if (config.getBoolean("ec2Enabled")) {
            final AmazonEC2Client ec2Client = new AmazonEC2Client(awsCredentialsProviderChain, clientConfig);
            ec2Client.setEndpoint(endpoint);
            ec2Clients.put(regionName, ec2Client);
        }

        if (config.getBoolean("rdsEnabled")) {
            final AmazonRDSClient rdsClient = new AmazonRDSClient(awsCredentialsProviderChain, clientConfig);
            rdsClient.setEndpoint(endpoint.replaceFirst("ec2\\.", "rds."));
            rdsClients.put(regionName, rdsClient);
        }

        if (config.getBoolean("dynamodbEnabled")) {
            final AmazonDynamoDBClient dynamoDBClient =
                new AmazonDynamoDBClient(awsCredentialsProviderChain, clientConfig);
            dynamoDBClient.setEndpoint(endpoint.replaceFirst("ec2\\.", "dynamodb."));
            dynamoDBClients.put(regionName, dynamoDBClient);
        }

        if (config.getBoolean("sqsEnabled")) {
            final AmazonSQSClient sqsClient = new AmazonSQSClient(awsCredentialsProviderChain, clientConfig);
            sqsClient.setEndpoint(endpoint.replaceFirst("ec2\\.", "sqs."));
            sqsClients.put(regionName, sqsClient);
        }

        if (config.getBoolean("elasticacheEnabled")) {
            final AmazonElastiCacheClient elastiCacheClient = new AmazonElastiCacheClient
                (awsCredentialsProviderChain, clientConfig);
            elastiCacheClient.setEndpoint(endpoint.replaceFirst("ec2\\.", "elasticache."));
            elasticacheClients.put(regionName, elastiCacheClient);
        }

        if (config.getBoolean("elasticsearchEnabled")) {
            final AWSElasticsearchClient elasticsearchClient = new AWSElasticsearchClient
                (awsCredentialsProviderChain, clientConfig);
            elasticsearchClient.setEndpoint(endpoint.replaceFirst("ec2\\.", "es."));
            elasticsearchClients.put(regionName, elasticsearchClient);
        }
    }

    this.iamClient = AmazonIdentityManagementClientBuilder.standard()
        .withCredentials(awsCredentialsProviderChain)
        .withClientConfiguration(clientConfig)
        .build();

    if (config.hasPath("accountNumber")) {
        this.awsAccountNumber = config.getString("accountNumber");
    } else {
        this.awsAccountNumber = null;
    }

    if (config.hasPath("arnPartition")) {
        this.awsARNPartition = config.getString("arnPartition");
    } else {
        this.awsARNPartition = "aws";
    }

    rebuild();
}