List of usage examples for com.amazonaws.services.simpledb AmazonSimpleDB putAttributes
PutAttributesResult putAttributes(PutAttributesRequest putAttributesRequest);
The PutAttributes operation creates or replaces attributes in an item.
From source file:SimpleDBExample.java
License:Open Source License
public static void main(String[] args) throws Exception { /*/*from w ww . j a v a 2 s .co m*/ * This credentials provider implementation loads your AWS credentials * from a properties file at the root of your classpath. * * Important: Be sure to fill in your AWS access credentials in the * AwsCredentials.properties file before you try to run this * sample. * http://aws.amazon.com/security-credentials */ AmazonSimpleDB sdb = new AmazonSimpleDBClient(new ClasspathPropertiesFileCredentialsProvider()); Region usWest2 = Region.getRegion(Regions.US_WEST_2); sdb.setRegion(usWest2); System.out.println("==========================================="); System.out.println("Getting Started with Amazon SimpleDB"); System.out.println("===========================================\n"); try { // Create a domain String myDomain = "MyStore"; System.out.println("Creating domain called " + myDomain + ".\n"); sdb.createDomain(new CreateDomainRequest(myDomain)); // List domains System.out.println("Listing all domains in your account:\n"); for (String domainName : sdb.listDomains().getDomainNames()) { System.out.println(" " + domainName); } System.out.println(); // Put data into a domain System.out.println("Putting data into " + myDomain + " domain.\n"); sdb.batchPutAttributes(new BatchPutAttributesRequest(myDomain, createSampleData())); // Select data from a domain // Notice the use of backticks around the domain name in our select expression. String selectExpression = "select * from `" + myDomain + "` where Category = 'Clothes'"; System.out.println("Selecting: " + selectExpression + "\n"); SelectRequest selectRequest = new SelectRequest(selectExpression); for (Item item : sdb.select(selectRequest).getItems()) { System.out.println(" Item"); System.out.println(" Name: " + item.getName()); for (Attribute attribute : item.getAttributes()) { System.out.println(" Attribute"); System.out.println(" Name: " + attribute.getName()); System.out.println(" Value: " + attribute.getValue()); } } System.out.println(); // Delete values from an attribute System.out.println("Deleting Blue attributes in Item_O3.\n"); Attribute deleteValueAttribute = new Attribute("Color", "Blue"); sdb.deleteAttributes( new DeleteAttributesRequest(myDomain, "Item_03").withAttributes(deleteValueAttribute)); // Delete an attribute and all of its values System.out.println("Deleting attribute Year in Item_O3.\n"); sdb.deleteAttributes(new DeleteAttributesRequest(myDomain, "Item_03") .withAttributes(new Attribute().withName("Year"))); // Replace an attribute System.out.println("Replacing Size of Item_03 with Medium.\n"); List<ReplaceableAttribute> replaceableAttributes = new ArrayList<ReplaceableAttribute>(); replaceableAttributes.add(new ReplaceableAttribute("Size", "Medium", true)); sdb.putAttributes(new PutAttributesRequest(myDomain, "Item_03", replaceableAttributes)); // Delete an item and all of its attributes System.out.println("Deleting Item_03.\n"); sdb.deleteAttributes(new DeleteAttributesRequest(myDomain, "Item_03")); // Delete a domain System.out.println("Deleting " + myDomain + " domain.\n"); sdb.deleteDomain(new DeleteDomainRequest(myDomain)); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which means your request made it " + "to Amazon SimpleDB, 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 SimpleDB, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
From source file:SimpleDBPerformanceSample.java
License:Apache License
public static void main(String[] args) throws Exception { AmazonSimpleDB sdb = new AmazonSimpleDBClient( new PropertiesCredentials(SimpleDBSample.class.getResourceAsStream("AwsCredentials.properties"))); // ?[WGh|Cgw/*from w ww . j a v a2 s. co m*/ sdb.setEndpoint(args[0]); int roopCount = 100; long insertTimeSum = 0; long selectTimeSum = 0; long updateTimeSum = 0; long deleteTimeSum = 0; long startTime; long finishedTime; try { // h?C?? String myDomain = "MyStore"; sdb.createDomain(new CreateDomainRequest(myDomain)); for (int i = 0; i < roopCount; i++) { // ?f?[^ sdb.batchPutAttributes(new BatchPutAttributesRequest(myDomain, createSampleData())); // f?[^1?}(PutAttributes API) ReplaceableItem rItem = new ReplaceableItem("Item_06").withAttributes( new ReplaceableAttribute("Category", "Car Parts", true), new ReplaceableAttribute("Subcategory", "Exhaust", true), new ReplaceableAttribute("Name", "O2 Sensor", true), new ReplaceableAttribute("Make", "Audi", true), new ReplaceableAttribute("Model", "TT Coupe", true), new ReplaceableAttribute("Year", "2009", true), new ReplaceableAttribute("Year", "2010", true), new ReplaceableAttribute("Year", "2011", true)); PutAttributesRequest putAttributesRequest = new PutAttributesRequest(myDomain, rItem.getName(), rItem.getAttributes()); startTime = System.currentTimeMillis(); sdb.putAttributes(putAttributesRequest); finishedTime = System.currentTimeMillis(); insertTimeSum += finishedTime - startTime; // f?[^?iSelect API) String selectExpression = "select * from `" + myDomain + "` where Category = 'Clothes'"; SelectRequest selectRequest = new SelectRequest(selectExpression); startTime = System.currentTimeMillis(); sdb.select(selectRequest); finishedTime = System.currentTimeMillis(); selectTimeSum += finishedTime - startTime; // f?[^?X?V(PutAttributes API) List<ReplaceableAttribute> replaceableAttributes = new ArrayList<ReplaceableAttribute>(); replaceableAttributes.add(new ReplaceableAttribute("Size", "Medium", true)); startTime = System.currentTimeMillis(); sdb.putAttributes(new PutAttributesRequest(myDomain, "Item_03", replaceableAttributes)); finishedTime = System.currentTimeMillis(); updateTimeSum += finishedTime - startTime; // f?[^??(DeleteAttributes API) startTime = System.currentTimeMillis(); sdb.deleteAttributes(new DeleteAttributesRequest(myDomain, "Item_03")); finishedTime = System.currentTimeMillis(); deleteTimeSum += finishedTime - startTime; // f?[^?? sdb.batchDeleteAttributes(new BatchDeleteAttributesRequest(myDomain, deleteSampleData())); } // h?C?? sdb.deleteDomain(new DeleteDomainRequest(myDomain)); System.out.println("insert:AVG:" + (float) insertTimeSum / roopCount); System.out.println("select:AVG:" + (float) selectTimeSum / roopCount); System.out.println("update:AVG:" + (float) updateTimeSum / roopCount); System.out.println("delete:AVG:" + (float) deleteTimeSum / roopCount); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which means your request made it " + "to Amazon SimpleDB, 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 SimpleDB, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
From source file:SimpleDBSample.java
License:Open Source License
public static void main(String[] args) throws Exception { /*/*from www. j a v a 2s .c om*/ * Important: Be sure to fill in your AWS access credentials in the * AwsCredentials.properties file before you try to run this * sample. * http://aws.amazon.com/security-credentials */ AmazonSimpleDB sdb = new AmazonSimpleDBClient( new PropertiesCredentials(SimpleDBSample.class.getResourceAsStream("AwsCredentials.properties"))); System.out.println("==========================================="); System.out.println("Getting Started with Amazon SimpleDB"); System.out.println("===========================================\n"); try { // Create a domain String myDomain = "MyStore"; System.out.println("Creating domain called " + myDomain + ".\n"); sdb.createDomain(new CreateDomainRequest(myDomain)); // List domains System.out.println("Listing all domains in your account:\n"); for (String domainName : sdb.listDomains().getDomainNames()) { System.out.println(" " + domainName); } System.out.println(); // Put data into a domain System.out.println("Putting data into " + myDomain + " domain.\n"); sdb.batchPutAttributes(new BatchPutAttributesRequest(myDomain, createSampleData())); // Select data from a domain // Notice the use of backticks around the domain name in our select expression. String selectExpression = "select * from `" + myDomain + "` where Category = 'Clothes'"; System.out.println("Selecting: " + selectExpression + "\n"); SelectRequest selectRequest = new SelectRequest(selectExpression); for (Item item : sdb.select(selectRequest).getItems()) { System.out.println(" Item"); System.out.println(" Name: " + item.getName()); for (Attribute attribute : item.getAttributes()) { System.out.println(" Attribute"); System.out.println(" Name: " + attribute.getName()); System.out.println(" Value: " + attribute.getValue()); } } System.out.println(); // Delete values from an attribute System.out.println("Deleting Blue attributes in Item_O3.\n"); Attribute deleteValueAttribute = new Attribute("Color", "Blue"); sdb.deleteAttributes( new DeleteAttributesRequest(myDomain, "Item_03").withAttributes(deleteValueAttribute)); // Delete an attribute and all of its values System.out.println("Deleting attribute Year in Item_O3.\n"); sdb.deleteAttributes(new DeleteAttributesRequest(myDomain, "Item_03") .withAttributes(new Attribute().withName("Year"))); // Replace an attribute System.out.println("Replacing Size of Item_03 with Medium.\n"); List<ReplaceableAttribute> replaceableAttributes = new ArrayList<ReplaceableAttribute>(); replaceableAttributes.add(new ReplaceableAttribute("Size", "Medium", true)); sdb.putAttributes(new PutAttributesRequest(myDomain, "Item_03", replaceableAttributes)); // Delete an item and all of its attributes System.out.println("Deleting Item_03.\n"); sdb.deleteAttributes(new DeleteAttributesRequest(myDomain, "Item_03")); // Delete a domain System.out.println("Deleting " + myDomain + " domain.\n"); sdb.deleteDomain(new DeleteDomainRequest(myDomain)); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which means your request made it " + "to Amazon SimpleDB, 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 SimpleDB, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
From source file:aws.sample.SimpleDBSample.java
License:Open Source License
public static void main(String[] args) throws Exception { /*/*from w w w. j av a 2s .c o m*/ * Important: Be sure to fill in your AWS access credentials in the AwsCredentials.properties file before you try to run this sample. http://aws.amazon.com/security-credentials */ AmazonSimpleDB sdb = new AmazonSimpleDBClient( new PropertiesCredentials(SimpleDBSample.class.getResourceAsStream("AwsCredentials.properties"))); System.out.println("==========================================="); System.out.println("Getting Started with Amazon SimpleDB"); System.out.println("===========================================\n"); try { // Create a domain String myDomain = "MyStore"; System.out.println("Creating domain called " + myDomain + ".\n"); sdb.createDomain(new CreateDomainRequest(myDomain)); // List domains System.out.println("Listing all domains in your account:\n"); for (String domainName : sdb.listDomains().getDomainNames()) { System.out.println(" " + domainName); } System.out.println(); // Put data into a domain System.out.println("Putting data into " + myDomain + " domain.\n"); sdb.batchPutAttributes(new BatchPutAttributesRequest(myDomain, createSampleData())); // Select data from a domain // Notice the use of backticks around the domain name in our select expression. String selectExpression = "select * from `" + myDomain + "` where Category = 'Clothes'"; System.out.println("Selecting: " + selectExpression + "\n"); SelectRequest selectRequest = new SelectRequest(selectExpression); for (Item item : sdb.select(selectRequest).getItems()) { System.out.println(" Item"); System.out.println(" Name: " + item.getName()); for (Attribute attribute : item.getAttributes()) { System.out.println(" Attribute"); System.out.println(" Name: " + attribute.getName()); System.out.println(" Value: " + attribute.getValue()); } } System.out.println(); // Delete values from an attribute System.out.println("Deleting Blue attributes in Item_O3.\n"); Attribute deleteValueAttribute = new Attribute("Color", "Blue"); sdb.deleteAttributes( new DeleteAttributesRequest(myDomain, "Item_03").withAttributes(deleteValueAttribute)); // Delete an attribute and all of its values System.out.println("Deleting attribute Year in Item_O3.\n"); sdb.deleteAttributes(new DeleteAttributesRequest(myDomain, "Item_03") .withAttributes(new Attribute().withName("Year"))); // Replace an attribute System.out.println("Replacing Size of Item_03 with Medium.\n"); List<ReplaceableAttribute> replaceableAttributes = new ArrayList<ReplaceableAttribute>(); replaceableAttributes.add(new ReplaceableAttribute("Size", "Medium", true)); sdb.putAttributes(new PutAttributesRequest(myDomain, "Item_03", replaceableAttributes)); // Delete an item and all of its attributes System.out.println("Deleting Item_03.\n"); sdb.deleteAttributes(new DeleteAttributesRequest(myDomain, "Item_03")); // Delete a domain System.out.println("Deleting " + myDomain + " domain.\n"); sdb.deleteDomain(new DeleteDomainRequest(myDomain)); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which means your request made it " + "to Amazon SimpleDB, 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 SimpleDB, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
From source file:bench.auth.SimpleDBSample.java
License:Open Source License
public static void main(String[] args) throws Exception { /*// w ww . j a v a2 s . c om * Important: Be sure to fill in your AWS access credentials in the * AwsCredentials.properties file before you try to run this sample. * http://aws.amazon.com/security-credentials */ String home = System.getProperty("user.home"); String props = home + "/.amazon/AwsCredentials.properties"; System.out.println("props = " + props); URL url = new File(props).toURI().toURL(); System.out.println("url = " + url); final AWSCredentials credentials = new PropertiesCredentials(url.openStream()); final AmazonSimpleDB sdb = new AmazonSimpleDBClient(credentials); System.out.println("==========================================="); System.out.println("Getting Started with Amazon SimpleDB"); System.out.println("===========================================\n"); try { // Create a domain String domain = "domain-tester"; System.out.println("Creating domain called " + domain + ".\n"); sdb.createDomain(new CreateDomainRequest(domain)); // List domains System.out.println("Listing all domains in your account:\n"); for (String domainName : sdb.listDomains().getDomainNames()) { System.out.println(" " + domainName); } System.out.println(); // Put data into a domain System.out.println("Putting data into " + domain + " domain.\n"); sdb.batchPutAttributes( // new BatchPutAttributesRequest(domain, createSampleData())); // Select data from a domain // Notice the use of backticks around the domain name in our select // expression. String selectExpression = // "select * from `" + domain + "` where Category = 'Clothes'"; System.out.println("Selecting: " + selectExpression + "\n"); SelectRequest selectRequest = new SelectRequest(selectExpression); for (Item item : sdb.select(selectRequest).getItems()) { System.out.println(" Item"); System.out.println(" Name: " + item.getName()); for (Attribute attribute : item.getAttributes()) { System.out.println(" Attribute"); System.out.println(" Name: " + attribute.getName()); System.out.println(" Value: " + attribute.getValue()); } } System.out.println(); // Delete values from an attribute System.out.println("Deleting Blue attributes in Item_O3.\n"); Attribute deleteValueAttribute = new Attribute("Color", "Blue"); sdb.deleteAttributes( new DeleteAttributesRequest(domain, "Item_03").withAttributes(deleteValueAttribute)); // Delete an attribute and all of its values System.out.println("Deleting attribute Year in Item_O3.\n"); sdb.deleteAttributes(new DeleteAttributesRequest(domain, "Item_03") .withAttributes(new Attribute().withName("Year"))); // Replace an attribute System.out.println("Replacing Size of Item_03 with Medium.\n"); List<ReplaceableAttribute> replaceableAttributes = new ArrayList<ReplaceableAttribute>(); replaceableAttributes.add(new ReplaceableAttribute("Size", "Medium", true)); sdb.putAttributes(new PutAttributesRequest(domain, "Item_03", replaceableAttributes)); // Delete an item and all of its attributes System.out.println("Deleting Item_03.\n"); sdb.deleteAttributes(new DeleteAttributesRequest(domain, "Item_03")); // Delete a domain // System.out.println("Deleting " + myDomain + " domain.\n"); // sdb.deleteDomain(new DeleteDomainRequest(myDomain)); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which means your request made it " + "to Amazon SimpleDB, 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 SimpleDB, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
From source file:com.aipo.aws.simpledb.SimpleDB.java
License:Open Source License
private static Integer counterJob(String domain) { AmazonSimpleDB client = getClient(); GetAttributesRequest getAttributesRequest = new GetAttributesRequest(); getAttributesRequest.setDomainName(DEFAULT_COUNTER_DOMAIN); getAttributesRequest.setItemName(domain); getAttributesRequest.setConsistentRead(true); Integer count = null;//from ww w. j av a 2s. co m try { GetAttributesResult attributes = client.getAttributes(getAttributesRequest); List<Attribute> list = attributes.getAttributes(); for (Attribute item : list) { if ("c".equals(item.getName())) { try { count = Integer.valueOf(item.getValue()); } catch (Throwable ignore) { } } } } catch (Throwable t) { t.printStackTrace(); } if (count == null) { CreateDomainRequest createDomainRequest = new CreateDomainRequest(DEFAULT_COUNTER_DOMAIN); client.createDomain(createDomainRequest); count = 0; } int next = count + 1; PutAttributesRequest putAttributesRequest = new PutAttributesRequest(); putAttributesRequest.setDomainName(DEFAULT_COUNTER_DOMAIN); putAttributesRequest.setItemName(domain); List<ReplaceableAttribute> attr = new ArrayList<ReplaceableAttribute>(); attr.add(new ReplaceableAttribute("c", String.valueOf(next), true)); putAttributesRequest.setAttributes(attr); UpdateCondition updateCondition = new UpdateCondition(); if (next == 1) { updateCondition.setExists(false); updateCondition.setName("c"); } else { updateCondition.setExists(true); updateCondition.setName("c"); updateCondition.setValue(String.valueOf(count)); } putAttributesRequest.setExpected(updateCondition); client.putAttributes(putAttributesRequest); return next; }
From source file:squash.booking.lambdas.core.OptimisticPersister.java
License:Apache License
@Override public int put(String itemName, Optional<Integer> version, ReplaceableAttribute attribute) throws Exception { if (!initialised) { throw new IllegalStateException("The optimistic persister has not been initialised"); }// w w w .ja v a 2 s .c o m logger.log("About to add attrbutes to simpledb item: " + itemName); AmazonSimpleDB client = getSimpleDBClient(); // Check the put will not take us over the maximum number of attributes: // N.B. if (replace == true) then this check could be over-eager, but not // worth refining it, since this effectively just alters the limit by one. ImmutablePair<Optional<Integer>, Set<Attribute>> versionedAttributes = get(itemName); if (versionedAttributes.left.isPresent()) { logger.log("Retrieved versioned attributes(Count: " + versionedAttributes.right.size() + ") have version number: " + versionedAttributes.left.get()); } else { // There should be no attributes in this case. logger.log("Retrieved versioned attributes(Count: " + versionedAttributes.right.size() + ") have no version number"); } Boolean tooManyAttributes = versionedAttributes.right.size() >= maxNumberOfAttributes; if (tooManyAttributes && !attribute.getValue().startsWith("Inactive")) { // We allow puts to inactivate attributes even when on the limit - // otherwise we could never delete when we're on the limit. logger.log("Cannot create attribute - the maximum number of attributes already exists (" + maxNumberOfAttributes + ") so throwing a 'Database put failed - too many attributes' exception"); throw new Exception("Database put failed - too many attributes"); } // Do a conditional put - so we don't overwrite someone else's attributes UpdateCondition updateCondition = new UpdateCondition(); updateCondition.setName(versionAttributeName); ReplaceableAttribute versionAttribute = new ReplaceableAttribute(); versionAttribute.setName(versionAttributeName); versionAttribute.setReplace(true); // Update will proceed unless the version number has changed if (version.isPresent()) { // A version number attribute exists - so it should be unchanged updateCondition.setValue(Integer.toString(version.get())); // Bump up our version number attribute versionAttribute.setValue(Integer.toString(version.get() + 1)); } else { // A version number attribute did not exist - so it still should not updateCondition.setExists(false); // Set initial value for our version number attribute versionAttribute.setValue("0"); } List<ReplaceableAttribute> replaceableAttributes = new ArrayList<>(); replaceableAttributes.add(versionAttribute); // Add the new attribute replaceableAttributes.add(attribute); PutAttributesRequest simpleDBPutRequest = new PutAttributesRequest(simpleDbDomainName, itemName, replaceableAttributes, updateCondition); try { client.putAttributes(simpleDBPutRequest); } catch (AmazonServiceException ase) { if (ase.getErrorCode().contains("ConditionalCheckFailed")) { // Someone else has mutated an attribute since we read them. This is // likely to be rare, and a retry should almost always succeed. However, // we leave it to clients of this class to retry the call if they wish, // as the new mutation may mean they no longer want to do the put. logger.log("Caught AmazonServiceException for ConditionalCheckFailed whilst creating" + " attribute(s) so throwing as 'Database put failed' instead"); throw new Exception("Database put failed - conditional check failed"); } throw ase; } logger.log("Created attribute(s) in simpledb"); return Integer.parseInt(versionAttribute.getValue()); }