Skip to content

Commit

Permalink
Merge pull request brianfrankcooper#592 from yuyantingzero/dynamo-db
Browse files Browse the repository at this point in the history
Dynamodb API update
  • Loading branch information
stfeng2 committed Jan 23, 2016
2 parents cbe330f + 44ce7f5 commit 3c5519f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 33 deletions.
2 changes: 1 addition & 1 deletion dynamodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ LICENSE file.
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.3.14</version>
<version>1.10.48</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
Expand Down
59 changes: 27 additions & 32 deletions dynamodb/src/main/java/com/yahoo/ycsb/db/DynamoDBClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,16 @@
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.dynamodb.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodb.model.AttributeValue;
import com.amazonaws.services.dynamodb.model.AttributeValueUpdate;
import com.amazonaws.services.dynamodb.model.DeleteItemRequest;
import com.amazonaws.services.dynamodb.model.DeleteItemResult;
import com.amazonaws.services.dynamodb.model.GetItemRequest;
import com.amazonaws.services.dynamodb.model.GetItemResult;
import com.amazonaws.services.dynamodb.model.Key;
import com.amazonaws.services.dynamodb.model.PutItemRequest;
import com.amazonaws.services.dynamodb.model.PutItemResult;
import com.amazonaws.services.dynamodb.model.ScanRequest;
import com.amazonaws.services.dynamodb.model.ScanResult;
import com.amazonaws.services.dynamodb.model.UpdateItemRequest;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate;
import com.amazonaws.services.dynamodbv2.model.DeleteItemRequest;
import com.amazonaws.services.dynamodbv2.model.GetItemRequest;
import com.amazonaws.services.dynamodbv2.model.GetItemResult;
import com.amazonaws.services.dynamodbv2.model.PutItemRequest;
import com.amazonaws.services.dynamodbv2.model.ScanRequest;
import com.amazonaws.services.dynamodbv2.model.ScanResult;
import com.amazonaws.services.dynamodbv2.model.UpdateItemRequest;
import com.yahoo.ycsb.ByteIterator;
import com.yahoo.ycsb.DB;
import com.yahoo.ycsb.DBException;
Expand All @@ -51,7 +48,7 @@
import java.util.Vector;

/**
* DynamoDB v1.3.14 client for YCSB
* DynamoDB v1.10.48 client for YCSB
*/

public class DynamoDBClient extends DB {
Expand Down Expand Up @@ -94,18 +91,18 @@ private enum PrimaryKeyType {
@Override
public void init() throws DBException {
// initialize DynamoDb driver & table.
String debug = getProperties().getProperty("dynamodb.debug",null);
String debug = getProperties().getProperty("dynamodb.debug", null);

if (null != debug && "true".equalsIgnoreCase(debug)) {
logger.setLevel(Level.DEBUG);
}

String endpoint = getProperties().getProperty("dynamodb.endpoint",null);
String credentialsFile = getProperties().getProperty("dynamodb.awsCredentialsFile",null);
String primaryKey = getProperties().getProperty("dynamodb.primaryKey",null);
String endpoint = getProperties().getProperty("dynamodb.endpoint", null);
String credentialsFile = getProperties().getProperty("dynamodb.awsCredentialsFile", null);
String primaryKey = getProperties().getProperty("dynamodb.primaryKey", null);
String primaryKeyTypeString = getProperties().getProperty("dynamodb.primaryKeyType", null);
String consistentReads = getProperties().getProperty("dynamodb.consistentReads",null);
String connectMax = getProperties().getProperty("dynamodb.connectMax",null);
String consistentReads = getProperties().getProperty("dynamodb.consistentReads", null);
String connectMax = getProperties().getProperty("dynamodb.connectMax", null);

if (null != connectMax) {
this.maxConnects = Integer.parseInt(connectMax);
Expand Down Expand Up @@ -154,7 +151,7 @@ public void init() throws DBException {
AWSCredentials credentials = new PropertiesCredentials(new File(credentialsFile));
ClientConfiguration cconfig = new ClientConfiguration();
cconfig.setMaxConnections(maxConnects);
dynamoDB = new AmazonDynamoDBClient(credentials,cconfig);
dynamoDB = new AmazonDynamoDBClient(credentials, cconfig);
dynamoDB.setEndpoint(this.endpoint);
primaryKeyName = primaryKey;
logger.info("dynamodb connection created with " + this.endpoint);
Expand Down Expand Up @@ -184,8 +181,7 @@ public Status read(String table, String key, Set<String> fields,
return CLIENT_ERROR;
}

if (null != res.getItem())
{
if (null != res.getItem()) {
result.putAll(extractResult(res.getItem()));
logger.debug("Result: " + res.toString());
}
Expand Down Expand Up @@ -221,7 +217,7 @@ public Status scan(String table, String startkey, int recordcount,

int count = 1; // startKey is done, rest to go.

Key startKey = createPrimaryKey(startkey);
Map<String, AttributeValue> startKey = createPrimaryKey(startkey);
ScanRequest req = new ScanRequest(table);
req.setAttributesToGet(fields);
while (count < recordcount) {
Expand Down Expand Up @@ -278,7 +274,7 @@ public Status update(String table, String key, HashMap<String, ByteIterator> val
}

@Override
public Status insert(String table, String key,HashMap<String, ByteIterator> values) {
public Status insert(String table, String key, HashMap<String, ByteIterator> values) {
logger.debug("insertkey: " + primaryKeyName + "-" + key + " from table: " + table);
Map<String, AttributeValue> attributes = createAttributes(values);
// adding primary key
Expand Down Expand Up @@ -337,20 +333,19 @@ private HashMap<String, ByteIterator> extractResult(Map<String, AttributeValue>
HashMap<String, ByteIterator> rItems = new HashMap<String, ByteIterator>(item.size());

for (Entry<String, AttributeValue> attr : item.entrySet()) {
logger.debug(String.format("Result- key: %s, value: %s", attr.getKey(), attr.getValue()) );
logger.debug(String.format("Result- key: %s, value: %s", attr.getKey(), attr.getValue()));
rItems.put(attr.getKey(), new StringByteIterator(attr.getValue().getS()));
}
return rItems;
}

private Key createPrimaryKey(String key) {
Key k;
private Map<String, AttributeValue> createPrimaryKey(String key) {
Map<String, AttributeValue> k = new HashMap<String, AttributeValue>();
if (primaryKeyType == PrimaryKeyType.HASH) {
k = new Key().withHashKeyElement(new AttributeValue().withS(key));
k.put(primaryKeyName, new AttributeValue().withS(key));
} else if (primaryKeyType == PrimaryKeyType.HASH_AND_RANGE) {
k = new Key()
.withHashKeyElement(new AttributeValue().withS(hashKeyValue))
.withRangeKeyElement(new AttributeValue().withS(key));
k.put(hashKeyName, new AttributeValue().withS(hashKeyValue));
k.put(primaryKeyName, new AttributeValue().withS(key));
} else {
throw new RuntimeException("Assertion Error: impossible primary key"
+ " type");
Expand Down

0 comments on commit 3c5519f

Please sign in to comment.