Example usage for com.amazonaws.services.dynamodbv2.model PutItemRequest PutItemRequest

List of usage examples for com.amazonaws.services.dynamodbv2.model PutItemRequest PutItemRequest

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.model PutItemRequest PutItemRequest.

Prototype

public PutItemRequest(String tableName, java.util.Map<String, AttributeValue> item) 

Source Link

Document

Constructs a new PutItemRequest object.

Usage

From source file:com.app.dynamoDb.DynamoUserAuthority.java

License:Open Source License

private static void insert(String UserID, String FileID, String UserName) {
    Map<String, AttributeValue> item = new HashMap<String, AttributeValue>();
    item.put("UserID", new AttributeValue(UserID));
    item.put("FileURL", new AttributeValue(FileID));
    item.put("SharedUsers", new AttributeValue(UserName));

    PutItemRequest putItemRequest = new PutItemRequest("UserAuthority", item);
    PutItemResult putItemResult = dynamoDB.putItem(putItemRequest);

}

From source file:com.aws.sampleImage.url.LambdaFunctionImageURLCrawlerHandler.java

private static void insertScrapedImageURLsInDynamoDB(String scrapedImageUrl, String synsetCode,
        String keyword) {/* www . j  a v  a  2 s .c o m*/
    String tableName = "image_urls_b";

    DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(tableName);
    TableDescription tableDescription = dynamoDB.describeTable(describeTableRequest).getTable();
    //System.out.println("For table name --> " + tableName + ", Table Description for : " + tableDescription);

    //Check if record with corresponding scraped image url exists
    //If it exists, don't do anything, else insert.
    //TODO - Do a DB check.

    // Add an item
    Map<String, AttributeValue> item = newItem(scrapedImageUrl, synsetCode, keyword);
    PutItemRequest putItemRequest = new PutItemRequest(tableName, item);
    PutItemResult putItemResult = dynamoDB.putItem(putItemRequest);
    //System.out.println("Result: " + putItemResult);
    //System.out.println("$$$$$$ scrapedImageUrl -> " + scrapedImageUrl + " synsetCode --> " + synsetCode + " keyword --> " + keyword + " has been insrted succesfully!");

}

From source file:com.climate.oada.dao.impl.DynamodbDAO.java

License:Open Source License

@Override
public boolean insert(LandUnit lu) {
    boolean retval = false;
    Map<String, AttributeValue> item = new HashMap<String, AttributeValue>();
    item.put(LandUnit.ID_ATTR_NAME, new AttributeValue(Long.toString(lu.getUnitId())));
    item.put(LandUnit.USER_ID_ATTR_NAME, new AttributeValue(Long.toString(lu.getUserId())));
    item.put(LandUnit.NAME_ATTR_NAME, new AttributeValue(lu.getName()));
    item.put(LandUnit.FARM_NAME_ATTR_NAME, new AttributeValue(lu.getFarmName()));
    item.put(LandUnit.CLIENT_NAME_ATTR_NAME, new AttributeValue(lu.getClientName()));
    item.put(LandUnit.ACRES_ATTR_NAME, new AttributeValue(Float.toString(lu.getAcres())));
    item.put(LandUnit.SOURCE_ATTR_NAME, new AttributeValue(lu.getSource()));
    item.put(LandUnit.OTHER_PROPS_ATTR_NAME, new AttributeValue(lu.getOtherProps()));
    item.put(LandUnit.GEOM_ATTR_NAME, new AttributeValue(lu.getWktBoundary()));
    PutItemRequest putItemRequest = new PutItemRequest(LANDUNIT_DYNAMO_DB_TABLE_NAME, item);
    try {/*  w  w w.j  av a  2 s  .  c o  m*/
        PutItemResult putItemResult = dynamoDB.putItem(putItemRequest);
        LOG.debug("DDB Insert Result: " + putItemResult);
        retval = true;
    } catch (Exception e) {
        LOG.error("DDB Insert failed " + e.getMessage());
    }
    return retval;
}

From source file:com.cloudkon.remote.worker.Dynamodb.java

License:Open Source License

public synchronized PutItemResult addItemInTable(String taskId, String workerName) {
    Map<String, AttributeValue> item = newItem(taskId, workerName);
    PutItemRequest putItemRequest = new PutItemRequest(tableName, item);
    PutItemResult putItemResult = dynamoDB.putItem(putItemRequest);
    return putItemResult;
}

From source file:com.erudika.para.persistence.AWSDynamoDAO.java

License:Apache License

private String createRow(String key, String appid, Map<String, AttributeValue> row) {
    if (StringUtils.isBlank(key) || StringUtils.isBlank(appid) || row == null || row.isEmpty()) {
        return null;
    }//from   w  ww  .  java2 s  .  c  o m
    try {
        key = getKeyForAppid(key, appid);
        setRowKey(key, row);
        PutItemRequest putItemRequest = new PutItemRequest(getTableNameForAppid(appid), row);
        client().putItem(putItemRequest);
    } catch (Exception e) {
        logger.error("Could not write row to DB - appid={}, key={}", appid, key, e);
    }
    return key;
}

From source file:com.github.sdmcraft.slingdynamo.demo.App.java

License:Open Source License

/**
 * Main1.//from w w w .  j a va  2  s . c  o  m
 *
 * @param args the args
 * @throws Exception the exception
 */
public static void main1(String[] args) throws Exception {
    init();

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

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

From source file:com.hussi.aws.dynamoDB.AmazonDynamoDBSample.java

License:Open Source License

public static void main(String[] args) throws Exception {
    init();//from  w  w w .  j  a va2s.co  m

    try {
        String tableName = "hussi_first_dynamo_table";

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

        // Create table if it does not exist yet
        TableUtils.createTableIfNotExists(dynamoDB, createTableRequest);
        // wait for the table to move into ACTIVE state
        TableUtils.waitUntilActive(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());
    }
}

From source file:com.mycompany.rproject.runnableClass.java

private static void putItem(Map<String, AttributeValue> item) {

    try {// w w w.  ja v  a  2  s .c  o m

        PutItemRequest putItemRequest = new PutItemRequest(TABLENAME, item);
        PutItemResult putItemResult = dynamoDBClient.putItem(putItemRequest);
        System.out.println("putresult " + putItemResult);
    } catch (Exception e) {
        // TODO: handle exception
    }
}

From source file:com.stockcloud.deletestock.java

License:Open Source License

public static void main(String[] args) throws Exception {
    init();//w  w w  . j a  v  a2  s.com
    String csvString;
    URL url = null;
    URLConnection urlConn = null;
    InputStreamReader inStream = null;
    BufferedReader buff = null;
    // static AmazonDynamoDBClient dynamoDB;
    // String jb=symbol.substring(0, 3)+"+"+symbol.substring(3, 7);
    // System.out.println(jb);
    // String[] newsymbol=symbol.split(",");
    int i = 0;
    String symbol[] = { "ABT", "ABBV", "ACE", "ACN", "ACT", "ADBE", "ADT", "AET", "AFL", "ARG", "AA", "ALL",
            "ALTR", "AMZN", "AAPL", "BLL", "BAC", "BMS", "BBY", "BLK", "BSX", "CA", "COF", "CCL", "CAT", "CERN",
            "CVX", "CSCO", "COH", "KO", "COST", "DVN", "DTV", "DG", "DOV", "DOW", "DUK", "EBAY", "EOG", "EMR",
            "EFX", "EQR", "EXPE", "ESRX", "FB", "FE", "FLIR", "FMC", "F", "FOSL", "GME", "GPS", "GD", "GE",
            "GIS", "GS", "GOOG", "HOG", "HRS", "HCP", "HAS", "HES", "HD", "HST", "HUM", "ITW", "INTC", "ICE",
            "IBM", "INTU", "IVZ", "JOY", "JNPR", "KEY", "KMI", "KLAC", "KSS", "KR", "LB", "LRCX", "LM", "LEN",
            "LNC", "LOW", "MAC", "MMM", "M", "MAR", "MA", "MCD", "MRK", "MET", "MU", "MSFT", "MNST", "MS",
            "MOS", "MUR", "MYL" };
    // String
    // Num[]={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","3","2","3","4","5","6","7","8","9","10","1","2","3","4","5","6","7","8","9","10","1","2","3","4","5","6","7","8","9","10","1","2","3","4","5","6","7","8","9","10","1","2","3","4","5","6","7","8","9","10","1","2","3","4","5","6","7","8","9","10",}
    // loop to get the current data of every stocks(100)
    while (i < symbol.length) {
        url = new URL("http://quote.yahoo.com/d/quotes.csv?s=" + symbol[i] + "&f=osl1d1t1c1ohgv&e=.csv");
        urlConn = url.openConnection();
        inStream = new InputStreamReader(urlConn.getInputStream());
        buff = new BufferedReader(inStream);
        // get the quote as a csv string
        csvString = buff.readLine();
        // while(csvString!=null)

        // {
        // parse the csv string
        StringTokenizer tokenizer = new StringTokenizer(csvString, ",");
        String open = tokenizer.nextToken();
        String ticker = tokenizer.nextToken();
        String price = tokenizer.nextToken();
        String tradeDate = tokenizer.nextToken();
        String tradeTime = tokenizer.nextToken();

        System.out.println(
                "Symbol: " + ticker + " Price: " + price + " Date: " + tradeDate + " Time: " + tradeTime);

        // table
        try {
            String tableName = symbol[i] + "current";

            // 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("Time").withKeyType(KeyType.HASH))
                        .withAttributeDefinitions(new AttributeDefinition().withAttributeName("Time")
                                .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.waitForTableToBecomeActive(dynamoDB, tableName);
            }
            Map<String, AttributeValue> item = newItem(ticker, price, tradeDate, tradeTime, open);
            PutItemRequest putItemRequest = new PutItemRequest(tableName, item);
            PutItemResult putItemResult = dynamoDB.putItem(putItemRequest);
            System.out.println("Result: " + putItemResult);

        }

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

        i++;
    } // whileover

    inStream.close();
    buff.close();

}

From source file:com.stockcloud.updatestock.java

License:Open Source License

public void update(String stockName) throws Exception {
    init();//from www  . j  a v  a2 s. c o m
    String csvString;
    URL url = null;
    URLConnection urlConn = null;
    InputStreamReader inStream = null;
    BufferedReader buff = null;
    // static AmazonDynamoDBClient dynamoDB;
    // String jb=symbol.substring(0, 3)+"+"+symbol.substring(3, 7);
    // System.out.println(jb);
    // String[] newsymbol=symbol.split(",");
    int i = 0;
    String symbol = stockName;
    // String symbol[] = { "ABT", "ABBV", "ACE", "ACN", "ACT", "ADBE",
    // "ADT",
    // "AET", "AFL", "ARG", "AA", "ALL", "ALTR", "AMZN", "AAPL",
    // "BLL", "BAC", "BMS", "BBY", "BLK", "BSX", "CA", "COF", "CCL",
    // "CAT", "CERN", "CVX", "CSCO", "COH", "KO", "COST", "DVN",
    // "DTV", "DG", "DOV", "DOW", "DUK", "EBAY", "EOG", "EMR", "EFX",
    // "EQR", "EXPE", "ESRX", "FB", "FE", "FLIR", "FMC", "F", "FOSL",
    // "GME", "GPS", "GD", "GE", "GIS", "GS", "GOOG", "HOG", "HRS",
    // "HCP", "HAS", "HES", "HD", "HST", "HUM", "ITW", "INTC", "ICE",
    // "IBM", "INTU", "IVZ", "JOY", "JNPR", "KEY", "KMI", "KLAC",
    // "KSS", "KR", "LB", "LRCX", "LM", "LEN", "LNC", "LOW", "MAC",
    // "MMM", "M", "MAR", "MA", "MCD", "MRK", "MET", "MU", "MSFT",
    // "MNST", "MS", "MOS", "MUR", "MYL" };

    // String
    // Num[]={"1","2","3","4","5","6","7","8","9","10","11","12","13","14",
    // "15","16","17","18","19","20","21","22","23","24","25","26","27","28",
    // "29","30","3","2","3","4","5","6","7","8","9","10","1","2","3","4","5",
    // "6","7","8","9","10","1","2","3","4","5","6","7","8","9","10","1","2",
    // "3","4","5","6","7","8","9","10","1","2","3","4","5","6","7","8","9",
    // "10","1","2","3","4","5","6","7","8","9","10",}

    // loop to get the current data of every stocks(100)

    url = new URL("http://quote.yahoo.com/d/quotes.csv?s=" + symbol + "&f=osl1d1t1c1ohgv&e=.csv");
    urlConn = url.openConnection();
    inStream = new InputStreamReader(urlConn.getInputStream());
    buff = new BufferedReader(inStream);
    // get the quote as a csv string
    csvString = buff.readLine();
    // while(csvString!=null)

    // {
    // parse the csv string
    StringTokenizer tokenizer = new StringTokenizer(csvString, ",");
    String open = tokenizer.nextToken();
    String ticker = tokenizer.nextToken();
    String price = tokenizer.nextToken();
    String tradeDate = tokenizer.nextToken();
    String tradeTime = tokenizer.nextToken();

    System.out
            .println("Symbol: " + ticker + " Price: " + price + " Date: " + tradeDate + " Time: " + tradeTime);

    // table
    try {
        String tableName = symbol + "current";

        // 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("Time").withKeyType(KeyType.HASH))
                    .withAttributeDefinitions(new AttributeDefinition().withAttributeName("Time")
                            .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.waitForTableToBecomeActive(dynamoDB, tableName);
        }
        Map<String, AttributeValue> item = newItem(ticker, price, tradeDate, tradeTime, open);
        PutItemRequest putItemRequest = new PutItemRequest(tableName, item);
        PutItemResult putItemResult = dynamoDB.putItem(putItemRequest);
        System.out.println("Result: " + putItemResult);

    }

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

    inStream.close();
    buff.close();

}