List of usage examples for com.amazonaws.services.dynamodbv2.model GetItemRequest GetItemRequest
public GetItemRequest()
From source file:DynamoDB.java
License:Open Source License
public boolean getItem(Map<String, AttributeValue> item, String id, String message) { //GetItemResult result = dynamoDB.getItem(tableName, item); GetItemRequest getItemRequest = new GetItemRequest().withTableName(tableName).withKey(item); GetItemResult result = dynamoDB.getItem(getItemRequest); if (result.getItem() != null) { //addItem(id,message); System.out.println("item present"); return true; }//from ww w .jav a 2s . co m //System.out.println(result.getItem()); else { return false; } }
From source file:VideoServlet.java
private static String getItem(String keyVal) { Map<String, AttributeValue> key = new HashMap<String, AttributeValue>(); key.put("ID", new AttributeValue(keyVal)); GetItemRequest getItemRequest = new GetItemRequest().withTableName(TABLENAME).withKey(key); GetItemResult item = dynamoDBClient.getItem(getItemRequest); System.out.println("Get Result: " + item); return item.toString(); }
From source file:amazon.dynamodb.model.GetPointRequest.java
License:Open Source License
public GetPointRequest(GeoPunto geoPoint, AttributeValue rangeKeyValue) { getItemRequest = new GetItemRequest(); getItemRequest.setKey(new HashMap<String, AttributeValue>()); this.geoPoint = geoPoint; this.rangeKeyValue = rangeKeyValue; }
From source file:aws.example.dynamodb.GetItem.java
License:Open Source License
public static void main(String[] args) { final String USAGE = "\n" + "Usage:\n" + " GetItem <table> <name> [projection_expression]\n\n" + "Where:\n" + " table - the table to get an item from.\n" + " name - the item to get.\n\n" + "You can add an optional projection expression (a quote-delimited,\n" + "comma-separated list of attributes to retrieve) to limit the\n" + "fields returned from the table.\n\n" + "Example:\n" + " GetItem HelloTable World\n" + " GetItem SiteColors text \"default, bold\"\n"; if (args.length < 2) { System.out.println(USAGE); System.exit(1);// w w w .ja va 2 s.co m } String table_name = args[0]; String name = args[1]; String projection_expression = null; // if a projection expression was included, set it. if (args.length == 3) { projection_expression = args[2]; } System.out.format("Retrieving item \"%s\" from \"%s\"\n", name, table_name); HashMap<String, AttributeValue> key_to_get = new HashMap<String, AttributeValue>(); key_to_get.put("Name", new AttributeValue(name)); GetItemRequest request = null; if (projection_expression != null) { request = new GetItemRequest().withKey(key_to_get).withTableName(table_name) .withProjectionExpression(projection_expression); } else { request = new GetItemRequest().withKey(key_to_get).withTableName(table_name); } final AmazonDynamoDBClient ddb = new AmazonDynamoDBClient(); try { Map<String, AttributeValue> returned_item = ddb.getItem(request).getItem(); if (returned_item != null) { Set<String> keys = returned_item.keySet(); for (String key : keys) { System.out.format("%s: %s\n", key, returned_item.get(key).toString()); } } else { System.out.format("No item found with the key %s!\n", name); } } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); } }
From source file:com.amazon.janusgraph.diskstorage.dynamodb.AbstractDynamoDbStore.java
License:Open Source License
protected GetItemRequest createGetItemRequest() { return new GetItemRequest().withTableName(tableName).withConsistentRead(forceConsistentRead) .withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL); }
From source file:com.amediamanager.dao.DynamoDbUserDaoImpl.java
License:Apache License
@Override public User find(String email) throws DataSourceTableDoesNotExistException { try {/* ww w . j a v a2 s .c om*/ User user = null; // Create a request to find a User by email address GetItemRequest getItemRequest = new GetItemRequest() .withTableName(config.getProperty(ConfigurationSettings.ConfigProps.DDB_USERS_TABLE)) .addKeyEntry(HASH_KEY_NAME, new AttributeValue(email)); // Issue the request to find the User in DynamoDB GetItemResult getItemResult = dynamoClient.getItem(getItemRequest); // If an item was found if (getItemResult.getItem() != null) { // Marshal the Map<String, AttributeValue> structure returned in // the // GetItemResult to a User object user = getUserFromMap(getItemResult.getItem()); } return user; } catch (ResourceNotFoundException rnfe) { // The ResourceNotFoundException method is thrown by the getItem() // method // if the DynamoDB table doesn't exist. This exception is re-thrown // as a // custom, more specific DataSourceTableDoesNotExistException that // users // of this DAO understand. throw new DataSourceTableDoesNotExistException( config.getProperty(ConfigurationSettings.ConfigProps.DDB_USERS_TABLE)); } }
From source file:com.github.sdmcraft.slingdynamo.demo.App.java
License:Open Source License
/** * Main2.//w w w . j a v a 2 s . co m * * @param args the args */ public static void main2(String[] args) { init(); Map<String, AttributeValue> key = new HashMap<String, AttributeValue>(); key.put("name", new AttributeValue().withS("Airplane")); GetItemRequest getItemRequest = new GetItemRequest().withTableName("my-favorite-movies-table").withKey(key) .withAttributesToGet(Arrays.asList("name", "fans", "rating", "year")); GetItemResult result = dynamoDB.getItem(getItemRequest); // Check the response. System.out.println("Printing item after retrieving it...."); printItem(result.getItem()); }
From source file:com.grublr.geo.model.GetPointRequest.java
License:Open Source License
public GetPointRequest(GeoPoint geoPoint, AttributeValue rangeKeyValue) { getItemRequest = new GetItemRequest(); getItemRequest.setKey(new HashMap<String, AttributeValue>()); this.geoPoint = geoPoint; this.rangeKeyValue = rangeKeyValue; }
From source file:com.lvl6.mobsters.dynamo.setup.TransactionExamples.java
License:Open Source License
/** * This example demonstrates two transactions attempting to write to the same item. * Only one transaction will go through. *///from ww w .j a v a 2s . c o m public void conflictingTransactions() { print("\n*** conflictingTransactions() ***\n"); // Start transaction t1 Transaction t1 = txManager.newTransaction(); // Construct a primary key of an item that will overlap between two transactions. Map<String, AttributeValue> item1Key = new HashMap<String, AttributeValue>(); item1Key.put(EXAMPLE_TABLE_HASH_KEY, new AttributeValue("conflictingTransactions_Item1")); item1Key = Collections.unmodifiableMap(item1Key); // Add a new PutItem request to the transaction object (instead of on the AmazonDynamoDB client) // This will eventually get rolled back when t2 tries to work on the same item Map<String, AttributeValue> item1T1 = new HashMap<String, AttributeValue>(item1Key); item1T1.put("WhichTransaction?", new AttributeValue("t1")); print("T1 - Put item: " + item1T1); t1.putItem(new PutItemRequest().withTableName(EXAMPLE_TABLE_NAME).withItem(item1T1)); print("T1 - At this point Item1 is in the table, but is not yet committed"); Map<String, AttributeValue> item2T1 = new HashMap<String, AttributeValue>(); item2T1.put(EXAMPLE_TABLE_HASH_KEY, new AttributeValue("conflictingTransactions_Item2")); print("T1 - Put a second, non-overlapping item: " + item2T1); t1.putItem(new PutItemRequest().withTableName(EXAMPLE_TABLE_NAME).withItem(item2T1)); print("T1 - At this point Item2 is also in the table, but is not yet committed"); // Start a new transaction t2 Transaction t2 = txManager.newTransaction(); Map<String, AttributeValue> item1T2 = new HashMap<String, AttributeValue>(item1Key); item1T1.put("WhichTransaction?", new AttributeValue("t2 - I win!")); print("T2 - Put item: " + item1T2); t2.putItem(new PutItemRequest().withTableName(EXAMPLE_TABLE_NAME).withItem(item1T2)); print("T2 - At this point Item1 from t2 is in the table, but is not yet committed. t1 was rolled back."); // To prove that t1 will have been rolled back by this point, attempt to commit it. try { print("Attempting to commit t1 (this will fail)"); t1.commit(); throw new RuntimeException("T1 should have been rolled back. This is a bug."); } catch (TransactionRolledBackException e) { print("Transaction t1 was rolled back, as expected"); t1.delete(); // Delete it, no longer needed } // Now put a second item as a part of t2 Map<String, AttributeValue> item3T2 = new HashMap<String, AttributeValue>(); item3T2.put(EXAMPLE_TABLE_HASH_KEY, new AttributeValue("conflictingTransactions_Item3")); print("T2 - Put item: " + item3T2); t2.putItem(new PutItemRequest().withTableName(EXAMPLE_TABLE_NAME).withItem(item3T2)); print("T2 - At this point Item3 is in the table, but is not yet committed"); print("Committing and deleting t2"); t2.commit(); t2.delete(); // Now to verify, get the items Item1, Item2, and Item3. // More on read operations later. GetItemResult result = txManager.getItem( new GetItemRequest().withTableName(EXAMPLE_TABLE_NAME).withKey(item1Key), IsolationLevel.UNCOMMITTED); print("Notice that t2's write to Item1 won: " + result.getItem()); result = txManager.getItem(new GetItemRequest().withTableName(EXAMPLE_TABLE_NAME).withKey(item3T2), IsolationLevel.UNCOMMITTED); print("Notice that t2's write to Item3 also went through: " + result.getItem()); result = txManager.getItem(new GetItemRequest().withTableName(EXAMPLE_TABLE_NAME).withKey(item2T1), IsolationLevel.UNCOMMITTED); print("However, t1's write to Item2 did not go through (since Item2 is null): " + result.getItem()); }
From source file:com.lvl6.mobsters.dynamo.setup.TransactionExamples.java
License:Open Source License
/** * This example shows that reads can be performed in a transaction, and read locks can be upgraded to write locks. *//* w w w.j a v a 2s. c om*/ public void readThenWrite() { print("\n*** readThenWrite() ***\n"); Transaction t1 = txManager.newTransaction(); // Perform a GetItem request on the transaction print("Reading Item1"); Map<String, AttributeValue> key1 = new HashMap<String, AttributeValue>(); key1.put(EXAMPLE_TABLE_HASH_KEY, new AttributeValue("Item1")); Map<String, AttributeValue> item1 = t1 .getItem(new GetItemRequest().withKey(key1).withTableName(EXAMPLE_TABLE_NAME)).getItem(); print("Item1: " + item1); // Now call UpdateItem to add a new attribute. // Notice that the library supports ReturnValues in writes print("Updating Item1"); Map<String, AttributeValueUpdate> updates = new HashMap<String, AttributeValueUpdate>(); updates.put("Color", new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue(new AttributeValue("Green"))); item1 = t1.updateItem(new UpdateItemRequest().withKey(key1).withTableName(EXAMPLE_TABLE_NAME) .withAttributeUpdates(updates).withReturnValues(ReturnValue.ALL_NEW)).getAttributes(); print("Item1 is now: " + item1); t1.commit(); t1.delete(); }