SimpleDBWrite.java Source code

Java tutorial

Introduction

Here is the source code for SimpleDBWrite.java

Source

/*
 * Copyright 2010-2013 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.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.ClasspathPropertiesFileCredentialsProvider;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.simpledb.AmazonSimpleDB;
import com.amazonaws.services.simpledb.AmazonSimpleDBClient;
import com.amazonaws.services.simpledb.model.BatchPutAttributesRequest;
import com.amazonaws.services.simpledb.model.CreateDomainRequest;
import com.amazonaws.services.simpledb.model.ReplaceableAttribute;
import com.amazonaws.services.simpledb.model.ReplaceableItem;

/**
 * This sample demonstrates how to make basic requests to Amazon SimpleDB using
 * the AWS SDK for Java.
 * <p>
 * <b>Prerequisites:</b> You must have a valid Amazon Web Services developer
 * account, and be signed up to use Amazon SimpleDB. For more information on
 * Amazon SimpleDB, see http://aws.amazon.com/simpledb.
 * <p>
 * <b>Important:</b> 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
 */
public class SimpleDBWrite {

    public static void run(String searchItem) throws Exception {
        /*
         * 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);

        try {
            // Create a domain
            String myDomain = "WanningStore";
            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, createData(searchItem)));

            //            // 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());
        }
    }

    /**
     * Creates an array of SimpleDB ReplaceableItems populated with sample data.
     *
     * @return An array of sample item data.
     * @throws IOException 
     */
    public static List<ReplaceableItem> createData(String searchItem) throws IOException {
        List<ReplaceableItem> Data = new ArrayList<ReplaceableItem>();

        PriceParser myParser = new PriceParser();
        ArrayList<resultItem> prodResultItems = myParser.Query2(searchItem, "1");
        for (resultItem e : prodResultItems) {
            Data.add(new ReplaceableItem(e.name).withAttributes(
                    new ReplaceableAttribute("Keyword", searchItem, true),
                    new ReplaceableAttribute("Price", e.price, true),
                    new ReplaceableAttribute("Site", "Walmart", true)));
        }
        return Data;
    }
}