Skip to content

Commit

Permalink
Added dynamodb
Browse files Browse the repository at this point in the history
  • Loading branch information
RajeshNutalapati committed Oct 15, 2015
1 parent 6fa678e commit b5da880
Show file tree
Hide file tree
Showing 10 changed files with 1,033 additions and 0 deletions.
11 changes: 11 additions & 0 deletions nosql/cassandra/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>nosql</groupId>
<artifactId>gettingstarted</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com</groupId>
<artifactId>cassandra</artifactId>
<version>0.0.1-CASSANDRA-SNAPSHOT</version>
</project>
79 changes: 79 additions & 0 deletions nosql/dynamo/dynamocli.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
aws dynamodb create-table \
--table-name stock_eod \
--attribute-definitions \
AttributeName=stockTicker,AttributeType=S AttributeName=tradeDate,AttributeType=S \
--key-schema AttributeName=stockTicker,KeyType=HASH AttributeName=tradeDate,KeyType=RANGE \
--provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 \
--region us-east-1

aws dynamodb put-item \
--table-name stock_eod \
--item '{
"stockTicker": {"S": "A"},
"tradeDate": {"S": "01-Jan-2009"},
"lowPrice": {"N": "35"},
"openPrice": {"N": "35"},
"highPrice": {"N": "35"},
"closePrice": {"N": "35"},
"volume": {"N": "0"} }' \
--return-consumed-capacity TOTAL \
--region us-east-1

aws dynamodb put-item \
--table-name stock_eod \
--item '{
"stockTicker": {"S": "A"},
"tradeDate": {"S": "02-Jan-2009"},
"lowPrice": {"N": "35"},
"openPrice": {"N": "35"},
"highPrice": {"N": "35"},
"closePrice": {"N": "35"},
"volume": {"N": "0"} }' \
--return-consumed-capacity TOTAL \
--region us-east-1

aws dynamodb query --table-name stock_eod --key-conditions file:///Users/usdgadiraj/code/dynamoquery.json --region us-east-1

aws dynamodb query --table-name stock_eod --region us-east-1 --key-conditions '{
"stockTicker": {
"AttributeValueList": [
{
"S": "A"
}
],
"ComparisonOperator": "EQ"
},
"tradeDate": {
"AttributeValueList": [
{
"S": "01-Jan-2009"
}
],
"ComparisonOperator": "EQ"
}
}'

aws dynamodb scan --table-name stock_eod --region us-east-1

aws dynamodb scan --table-name stock_eod --region us-east-1 --limit 10 --endpoint-url http://dynamo.itversity.com:8000

aws dynamodb scan --table-name stock_eod --region us-east-1 --select volume --limit 10 --endpoint-url http://dynamo.itversity.com:8000

aws dynamodb scan --table-name stock_eod --region us-east-1 --select COUNT --endpoint-url http://dynamo.itversity.com:8000

aws dynamodb scan \
--table-name stock_eod \
--region us-east-1 \
--select SPECIFIC_ATTRIBUTES \
--attributes-to-get "v" \
--limit 10 \
--endpoint-url http://dynamo.itversity.com:8000

aws dynamodb scan \
--table-name stock_eod \
--region us-east-1 \
--select SPECIFIC_ATTRIBUTES \
--projection-expression "tradeDate,stockTicker,v" \
--limit 10 \
--endpoint-url http://dynamo.itversity.com:8000

69 changes: 69 additions & 0 deletions nosql/dynamo/dynamolocalcli.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
aws dynamodb create-table \
--table-name stock_eod \
--attribute-definitions \
AttributeName=stockTicker,AttributeType=S AttributeName=tradeDate,AttributeType=S \
--key-schema AttributeName=stockTicker,KeyType=HASH AttributeName=tradeDate,KeyType=RANGE \
--provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 \
--region us-east-1 \
--endpoint-url http://dynamo.itversity.com:8000

aws dynamodb put-item \
--table-name stock_eod \
--item '{
"stockTicker": {"S": "A"},
"tradeDate": {"S": "01-Jan-2009"},
"lowPrice": {"N": "35"},
"openPrice": {"N": "35"},
"highPrice": {"N": "35"},
"closePrice": {"N": "35"},
"volume": {"N": "0"} }' \
--return-consumed-capacity TOTAL \
--region us-east-1 \
--endpoint-url http://dynamo.itversity.com:8000


aws dynamodb put-item \
--table-name stock_eod \
--item '{
"stockTicker": {"S": "A"},
"tradeDate": {"S": "02-Jan-2009"},
"lowPrice": {"N": "35"},
"openPrice": {"N": "35"},
"highPrice": {"N": "35"},
"closePrice": {"N": "35"},
"volume": {"N": "0"} }' \
--return-consumed-capacity TOTAL \
--region us-east-1 \
--endpoint-url http://dynamo.itversity.com:8000


aws dynamodb query \
--table-name stock_eod \
--key-conditions file:///Users/usdgadiraj/code/dynamoquery.json \
--region us-east-1 \
--endpoint-url http://dynamo.itversity.com:8000

aws dynamodb query --table-name stock_eod --region us-east-1 --key-conditions '{
"stockTicker": {
"AttributeValueList": [
{
"S": "A"
}
],
"ComparisonOperator": "EQ"
},
"tradeDate": {
"AttributeValueList": [
{
"S": "01-Jan-2009"
}
],
"ComparisonOperator": "EQ"
}
}'

aws dynamodb scan \
--table-name stock_eod \
--region us-east-1 \
--endpoint-url http://dynamo.itversity.com:8000

18 changes: 18 additions & 0 deletions nosql/dynamo/dynamoquery.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"stockTicker": {
"AttributeValueList": [
{
"S": "A"
}
],
"ComparisonOperator": "EQ"
},
"tradeDate": {
"AttributeValueList": [
{
"S": "01-Jan-2009"
}
],
"ComparisonOperator": "EQ"
}
}
171 changes: 171 additions & 0 deletions nosql/dynamo/src/AmazonDynamoDBSample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/*
* Copyright 2012-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
import java.util.HashMap;
import java.util.Map;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.ComparisonOperator;
import com.amazonaws.services.dynamodbv2.model.Condition;
import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
import com.amazonaws.services.dynamodbv2.model.DescribeTableRequest;
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.PutItemRequest;
import com.amazonaws.services.dynamodbv2.model.PutItemResult;
import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType;
import com.amazonaws.services.dynamodbv2.model.ScanRequest;
import com.amazonaws.services.dynamodbv2.model.ScanResult;
import com.amazonaws.services.dynamodbv2.model.TableDescription;
import com.amazonaws.services.dynamodbv2.util.Tables;

/**
* This sample demonstrates how to perform a few simple operations with the
* Amazon DynamoDB service.
*/
public class AmazonDynamoDBSample {

/*
* Before running the code:
* Fill in your AWS access credentials in the provided credentials
* file template, and be sure to move the file to the default location
* (/Users/usdgadiraj/.aws/credentials) where the sample code will load the
* credentials from.
* https://console.aws.amazon.com/iam/home?#security_credential
*
* WARNING:
* To avoid accidental leakage of your credentials, DO NOT keep
* the credentials file in your source directory.
*/

static AmazonDynamoDBClient dynamoDB;

/**
* The only information needed to create a client are security credentials
* consisting of the AWS Access Key ID and Secret Access Key. All other
* configuration, such as the service endpoints, are performed
* automatically. Client parameters, such as proxies, can be specified in an
* optional ClientConfiguration object when constructing a client.
*
* @see com.amazonaws.auth.BasicAWSCredentials
* @see com.amazonaws.auth.ProfilesConfigFile
* @see com.amazonaws.ClientConfiguration
*/
private static void init() throws Exception {
/*
* The ProfileCredentialsProvider will return your [default]
* credential profile by reading from the credentials file located at
* (/Users/usdgadiraj/.aws/credentials).
*/
AWSCredentials credentials = null;
try {
credentials = new ProfileCredentialsProvider("default").getCredentials();
} catch (Exception e) {
throw new AmazonClientException(
"Cannot load the credentials from the credential profiles file. " +
"Please make sure that your credentials file is at the correct " +
"location (/Users/usdgadiraj/.aws/credentials), and is in valid format.",
e);
}
dynamoDB = new AmazonDynamoDBClient(credentials);
Region usWest2 = Region.getRegion(Regions.US_WEST_2);
dynamoDB.setRegion(usWest2);
}

public static void main(String[] args) throws Exception {
init();

try {
String tableName = "my-favorite-movies-table";

// Create table if it does not exist yet
if (Tables.doesTableExist(dynamoDB, tableName)) {
System.out.println("Table " + tableName + " is already ACTIVE");
} else {
// Create a table with a primary hash key named 'name', which holds a string
CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName)
.withKeySchema(new KeySchemaElement().withAttributeName("name").withKeyType(KeyType.HASH))
.withAttributeDefinitions(new AttributeDefinition().withAttributeName("name").withAttributeType(ScalarAttributeType.S))
.withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(1L).withWriteCapacityUnits(1L));
TableDescription createdTableDescription = dynamoDB.createTable(createTableRequest).getTableDescription();
System.out.println("Created Table: " + createdTableDescription);

// Wait for it to become active
System.out.println("Waiting for " + tableName + " to become ACTIVE...");
Tables.awaitTableToBecomeActive(dynamoDB, tableName);
}

// Describe our new table
DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(tableName);
TableDescription tableDescription = dynamoDB.describeTable(describeTableRequest).getTable();
System.out.println("Table Description: " + tableDescription);

// Add an item
Map<String, AttributeValue> item = newItem("Bill & Ted's Excellent Adventure", 1989, "****", "James", "Sara");
PutItemRequest putItemRequest = new PutItemRequest(tableName, item);
PutItemResult putItemResult = dynamoDB.putItem(putItemRequest);
System.out.println("Result: " + putItemResult);

// Add another item
item = newItem("Airplane", 1980, "*****", "James", "Billy Bob");
putItemRequest = new PutItemRequest(tableName, item);
putItemResult = dynamoDB.putItem(putItemRequest);
System.out.println("Result: " + putItemResult);

// Scan items for movies with a year attribute greater than 1985
HashMap<String, Condition> scanFilter = new HashMap<String, Condition>();
Condition condition = new Condition()
.withComparisonOperator(ComparisonOperator.GT.toString())
.withAttributeValueList(new AttributeValue().withN("1985"));
scanFilter.put("year", condition);
ScanRequest scanRequest = new ScanRequest(tableName).withScanFilter(scanFilter);
ScanResult scanResult = dynamoDB.scan(scanRequest);
System.out.println("Result: " + scanResult);

} catch (AmazonServiceException ase) {
System.out.println("Caught an AmazonServiceException, which means your request made it "
+ "to AWS, but was rejected with an error response for some reason.");
System.out.println("Error Message: " + ase.getMessage());
System.out.println("HTTP Status Code: " + ase.getStatusCode());
System.out.println("AWS Error Code: " + ase.getErrorCode());
System.out.println("Error Type: " + ase.getErrorType());
System.out.println("Request ID: " + ase.getRequestId());
} catch (AmazonClientException ace) {
System.out.println("Caught an AmazonClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with AWS, "
+ "such as not being able to access the network.");
System.out.println("Error Message: " + ace.getMessage());
}
}

private static Map<String, AttributeValue> newItem(String name, int year, String rating, String... fans) {
Map<String, AttributeValue> item = new HashMap<String, AttributeValue>();
item.put("name", new AttributeValue(name));
item.put("year", new AttributeValue().withN(Integer.toString(year)));
item.put("rating", new AttributeValue(rating));
item.put("fans", new AttributeValue().withSS(fans));

return item;
}

}
Loading

0 comments on commit b5da880

Please sign in to comment.