Example usage for com.amazonaws.services.dynamodbv2.document Table putItem

List of usage examples for com.amazonaws.services.dynamodbv2.document Table putItem

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.document Table putItem.

Prototype

@Override
    public PutItemOutcome putItem(PutItemSpec spec) 

Source Link

Usage

From source file:com.eho.dynamodb.DynamoDBConnection.java

public static String upload_resource(BaseResource resource,
        String primary_key /* if no primary key in case of post, send null*/ ) throws Exception {
    String id = add_primary_as_extension(resource, primary_key);
    String resource_string = DynamoDBConnection.fCtx.newJsonParser().setPrettyPrint(true)
            .encodeResourceToString(resource);
    ;//from w  ww .j av  a 2 s . c  o  m
    DynamoDB dynamoDB = new DynamoDB(dynamoDBClient);
    Table table = dynamoDB.getTable(PATIENT_TABLE);

    //lets retreive based on the key. if key invalid (not assigned yet) nullis returned.
    Item retreived_item = table.getItem(PRIMARY_KEY, id);
    if (retreived_item == null)//if null instantiate it
    {
        retreived_item = new Item();
        retreived_item.withPrimaryKey(PRIMARY_KEY, id);
        retreived_item.withInt("version", -1);
    }

    Integer new_version = retreived_item.getInt("version") + 1;
    retreived_item.withInt("version", new_version);

    Item item_to_upload = retreived_item//Item.fromJSON(retreived_item.toJSONPretty())
            .withString("text" + new_version.toString(), resource_string)
            .withMap("json-document", new ObjectMapper().readValue(resource_string, LinkedHashMap.class));
    PutItemSpec putItemSpec = new PutItemSpec().withItem(item_to_upload);
    table.putItem(putItemSpec);
    return id;
}

From source file:com.envirover.spl.stream.DynamoDBOutputStream.java

License:Open Source License

@Override
public void writePacket(String deviceId, Date time, MAVLinkPacket packet) throws IOException {
    if (deviceId == null || deviceId.isEmpty() || time == null || packet == null) {
        return;//  w w w  .j a  va  2  s.  co  m
    }

    Table table = dynamoDB.getTable(tableName);

    table.putItem(new Item().withPrimaryKey(ATTR_DEVICE_ID, deviceId, ATTR_TIME, time.getTime())
            .withNumber(ATTR_MSG_ID, packet.msgid).withJSON(ATTR_MESSAGE, toJSON(packet)));
}

From source file:com.innoq.hagmans.bachelor.DynamoDBUtils.java

License:Open Source License

/**
 * Persists the given temperatures on DynamoDB
 * /* www  .  j a v  a2s. co  m*/
 * @param tableName
 *            The name of the table, where the records will be persisted
 * @param temperatureMap
 *            A map containing the sensor names as the key, and as the value
 *            a hashmap with the timestamp of the temperature as the key and
 *            the temperature as the value
 * @param timestamp
 *            The timestamp of the run
 */
public void putTemperatures(String tableName, HashMap<String, HashMap<String, String>> temperatureMap,
        long timestamp) {

    Table table = dynamoDB.getTable(tableName);

    for (String sensor : temperatureMap.keySet()) {
        QuerySpec spec = new QuerySpec().withHashKey(ATTRIBUTE_NAME_HASH_KEY, sensor).withRangeKeyCondition(
                new RangeKeyCondition(ATTRIBUTE_NAME_RANGE_KEY).eq(String.valueOf(timestamp)));

        ItemCollection<QueryOutcome> items = table.query(spec);

        Iterator<Item> iterator = items.iterator();
        Item item = null;
        Map<String, String> temperatures = null;
        while (iterator.hasNext()) {
            item = iterator.next();
            temperatures = item.getMap(ATTRIBUTE_NAME_TEMPERATURE);
        }

        if (temperatures == null) {
            temperatures = new HashMap<>();
        }
        temperatures.putAll(temperatureMap.get(sensor));
        table.putItem(new Item().withPrimaryKey(ATTRIBUTE_NAME_HASH_KEY, sensor, ATTRIBUTE_NAME_RANGE_KEY,
                String.valueOf(timestamp)).withMap(ATTRIBUTE_NAME_TEMPERATURE, temperatures));
        System.out.println("PutItem succeeded!");
    }
}

From source file:com.kitac.kinesissamples.consumer.communication.DynamoDBManager.java

License:Open Source License

/**
 * Puts an item that represents the UnitRecord object 
 * to the DynamoDB table whose name is specified by 'tableName' parameter.
 * @param record The UnitRecord object as an item of the DynamoDB table.
 * @param tableName Name of the DynamoDB table.
 * @return a PutItemOutcome object.//from   w ww  .  j a v a2s. c o  m
 */
public PutItemOutcome putRecord(UnitRecord record, String tableName) {
    PutItemOutcome outcome = null;
    try {
        Table table = dynamoDBConnection.getTable(tableName);

        Item item = new Item()
                .withPrimaryKey("unit_id", record.getUnitId(), "datetime", record.getTime().toString())
                .withLong("out", record.getOut()).withLong("safe", record.getSafe())
                .withLong("start", record.getStart()).withLong("prize1", record.getPrize1())
                .withLong("prize2", record.getPrize2()).withLong("prize3", record.getPrize3())
                .withLong("input1", record.getInput1()).withLong("input2", record.getInput2())
                .withLong("cheat1", record.getCheat1()).withLong("cheat2", record.getCheat2())
                .withLong("cheat3", record.getCheat3()).withLong("cheat4", record.getCheat4())
                .withLong("sales1", record.getSales1()).withLong("sales2", record.getSales2())
                .withLong("sales3", record.getSales3()).withLong("sales4", record.getSales4());

        record.getAdvancedDataTable().entrySet().stream()
                .forEach(entrySet -> item.withLong(entrySet.getKey().name(), entrySet.getValue()));

        outcome = table.putItem(item);
    } catch (ResourceNotFoundException e) {
        throw e;
    }

    return outcome;
}

From source file:DynamoDB.sample.CreateTablesLoadData.java

License:Open Source License

private static void loadSampleProducts(String tableName) {

    Table table = dynamoDB.getTable(tableName);

    try {/*  www .  j a v a 2s .co  m*/

        System.out.println("Adding data to " + tableName);

        Item item = new Item().withPrimaryKey("Id", 101).withString("Title", "Book 101 Title")
                .withString("ISBN", "111-1111111111")
                .withStringSet("Authors", new HashSet<String>(Arrays.asList("Author1"))).withNumber("Price", 2)
                .withString("Dimensions", "8.5 x 11.0 x 0.5").withNumber("PageCount", 500)
                .withBoolean("InPublication", true).withString("ProductCategory", "Book");
        table.putItem(item);

        item = new Item().withPrimaryKey("Id", 102).withString("Title", "Book 102 Title")
                .withString("ISBN", "222-2222222222")
                .withStringSet("Authors", new HashSet<String>(Arrays.asList("Author1", "Author2")))
                .withNumber("Price", 20).withString("Dimensions", "8.5 x 11.0 x 0.8")
                .withNumber("PageCount", 600).withBoolean("InPublication", true)
                .withString("ProductCategory", "Book");
        table.putItem(item);

        item = new Item().withPrimaryKey("Id", 103).withString("Title", "Book 103 Title")
                .withString("ISBN", "333-3333333333")
                .withStringSet("Authors", new HashSet<String>(Arrays.asList("Author1", "Author2")))
                // Intentional. Later we'll run Scan to find price error. Find
                // items > 1000 in price.
                .withNumber("Price", 2000).withString("Dimensions", "8.5 x 11.0 x 1.5")
                .withNumber("PageCount", 600).withBoolean("InPublication", false)
                .withString("ProductCategory", "Book");
        table.putItem(item);

        // Add bikes.

        item = new Item().withPrimaryKey("Id", 201).withString("Title", "18-Bike-201")
                // Size, followed by some title.
                .withString("Description", "201 Description").withString("BicycleType", "Road")
                .withString("Brand", "Mountain A")
                // Trek, Specialized.
                .withNumber("Price", 100)
                .withStringSet("Color", new HashSet<String>(Arrays.asList("Red", "Black")))
                .withString("ProductCategory", "Bicycle");
        table.putItem(item);

        item = new Item().withPrimaryKey("Id", 202).withString("Title", "21-Bike-202")
                .withString("Description", "202 Description").withString("BicycleType", "Road")
                .withString("Brand", "Brand-Company A").withNumber("Price", 200)
                .withStringSet("Color", new HashSet<String>(Arrays.asList("Green", "Black")))
                .withString("ProductCategory", "Bicycle");
        table.putItem(item);

        item = new Item().withPrimaryKey("Id", 203).withString("Title", "19-Bike-203")
                .withString("Description", "203 Description").withString("BicycleType", "Road")
                .withString("Brand", "Brand-Company B").withNumber("Price", 300)
                .withStringSet("Color", new HashSet<String>(Arrays.asList("Red", "Green", "Black")))
                .withString("ProductCategory", "Bicycle");
        table.putItem(item);

        item = new Item().withPrimaryKey("Id", 204).withString("Title", "18-Bike-204")
                .withString("Description", "204 Description").withString("BicycleType", "Mountain")
                .withString("Brand", "Brand-Company B").withNumber("Price", 400)
                .withStringSet("Color", new HashSet<String>(Arrays.asList("Red")))
                .withString("ProductCategory", "Bicycle");
        table.putItem(item);

        item = new Item().withPrimaryKey("Id", 205).withString("Title", "20-Bike-205")
                .withString("Description", "205 Description").withString("BicycleType", "Hybrid")
                .withString("Brand", "Brand-Company C").withNumber("Price", 500)
                .withStringSet("Color", new HashSet<String>(Arrays.asList("Red", "Black")))
                .withString("ProductCategory", "Bicycle");
        table.putItem(item);

    } catch (Exception e) {
        System.err.println("Failed to create item in " + tableName);
        System.err.println(e.getMessage());
    }

}

From source file:DynamoDB.sample.CreateTablesLoadData.java

License:Open Source License

private static void loadSampleForums(String tableName) {

    Table table = dynamoDB.getTable(tableName);

    try {//from  w w w  .  j  a v a 2  s  . c  om

        System.out.println("Adding data to " + tableName);

        Item item = new Item().withPrimaryKey("Name", "Amazon DynamoDB")
                .withString("Category", "Amazon Web Services").withNumber("Threads", 2)
                .withNumber("Messages", 4).withNumber("Views", 1000);
        table.putItem(item);

        item = new Item().withPrimaryKey("Name", "Amazon S3").withString("Category", "Amazon Web Services")
                .withNumber("Threads", 0);
        table.putItem(item);

    } catch (Exception e) {
        System.err.println("Failed to create item in " + tableName);
        System.err.println(e.getMessage());
    }
}

From source file:DynamoDB.sample.CreateTablesLoadData.java

License:Open Source License

private static void loadSampleThreads(String tableName) {
    try {//from  ww  w.j av  a  2s. co  m
        long time1 = (new Date()).getTime() - (7 * 24 * 60 * 60 * 1000); // 7
        // days
        // ago
        long time2 = (new Date()).getTime() - (14 * 24 * 60 * 60 * 1000); // 14
        // days
        // ago
        long time3 = (new Date()).getTime() - (21 * 24 * 60 * 60 * 1000); // 21
        // days
        // ago

        Date date1 = new Date();
        date1.setTime(time1);

        Date date2 = new Date();
        date2.setTime(time2);

        Date date3 = new Date();
        date3.setTime(time3);

        dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));

        Table table = dynamoDB.getTable(tableName);

        System.out.println("Adding data to " + tableName);

        Item item = new Item().withPrimaryKey("ForumName", "Amazon DynamoDB")
                .withString("Subject", "DynamoDB Thread 1").withString("Message", "DynamoDB thread 1 message")
                .withString("LastPostedBy", "User A")
                .withString("LastPostedDateTime", dateFormatter.format(date2)).withNumber("Views", 0)
                .withNumber("Replies", 0).withNumber("Answered", 0)
                .withStringSet("Tags", new HashSet<String>(Arrays.asList("index", "primarykey", "table")));
        table.putItem(item);

        item = new Item().withPrimaryKey("ForumName", "Amazon DynamoDB")
                .withString("Subject", "DynamoDB Thread 2").withString("Message", "DynamoDB thread 2 message")
                .withString("LastPostedBy", "User A")
                .withString("LastPostedDateTime", dateFormatter.format(date3)).withNumber("Views", 0)
                .withNumber("Replies", 0).withNumber("Answered", 0)
                .withStringSet("Tags", new HashSet<String>(Arrays.asList("index", "partitionkey", "sortkey")));
        table.putItem(item);

        item = new Item().withPrimaryKey("ForumName", "Amazon S3").withString("Subject", "S3 Thread 1")
                .withString("Message", "S3 Thread 3 message").withString("LastPostedBy", "User A")
                .withString("LastPostedDateTime", dateFormatter.format(date1)).withNumber("Views", 0)
                .withNumber("Replies", 0).withNumber("Answered", 0)
                .withStringSet("Tags", new HashSet<String>(Arrays.asList("largeobjects", "multipart upload")));
        table.putItem(item);

    } catch (Exception e) {
        System.err.println("Failed to create item in " + tableName);
        System.err.println(e.getMessage());
    }

}

From source file:DynamoDB.sample.CreateTablesLoadData.java

License:Open Source License

private static void loadSampleReplies(String tableName) {
    try {/*from  www  .j a va 2  s . c  om*/
        // 1 day ago
        long time0 = (new Date()).getTime() - (1 * 24 * 60 * 60 * 1000);
        // 7 days ago
        long time1 = (new Date()).getTime() - (7 * 24 * 60 * 60 * 1000);
        // 14 days ago
        long time2 = (new Date()).getTime() - (14 * 24 * 60 * 60 * 1000);
        // 21 days ago
        long time3 = (new Date()).getTime() - (21 * 24 * 60 * 60 * 1000);

        Date date0 = new Date();
        date0.setTime(time0);

        Date date1 = new Date();
        date1.setTime(time1);

        Date date2 = new Date();
        date2.setTime(time2);

        Date date3 = new Date();
        date3.setTime(time3);

        dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));

        Table table = dynamoDB.getTable(tableName);

        System.out.println("Adding data to " + tableName);

        // Add threads.

        Item item = new Item().withPrimaryKey("Id", "Amazon DynamoDB#DynamoDB Thread 1")
                .withString("ReplyDateTime", (dateFormatter.format(date3)))
                .withString("Message", "DynamoDB Thread 1 Reply 1 text").withString("PostedBy", "User A");
        table.putItem(item);

        item = new Item().withPrimaryKey("Id", "Amazon DynamoDB#DynamoDB Thread 1")
                .withString("ReplyDateTime", dateFormatter.format(date2))
                .withString("Message", "DynamoDB Thread 1 Reply 2 text").withString("PostedBy", "User B");
        table.putItem(item);

        item = new Item().withPrimaryKey("Id", "Amazon DynamoDB#DynamoDB Thread 2")
                .withString("ReplyDateTime", dateFormatter.format(date1))
                .withString("Message", "DynamoDB Thread 2 Reply 1 text").withString("PostedBy", "User A");
        table.putItem(item);

        item = new Item().withPrimaryKey("Id", "Amazon DynamoDB#DynamoDB Thread 2")
                .withString("ReplyDateTime", dateFormatter.format(date0))
                .withString("Message", "DynamoDB Thread 2 Reply 2 text").withString("PostedBy", "User A");
        table.putItem(item);

    } catch (Exception e) {
        System.err.println("Failed to create item in " + tableName);
        System.err.println(e.getMessage());

    }
}

From source file:io.ignitr.dispatchr.manager.core.data.ClientRepository.java

License:Apache License

/**
 * Saves a new client in DynamoDB./*from   w  w  w . j a  v  a  2  s  .  com*/
 *
 * @param clientId client identifier
 * @param ownerName name of the person who owns the client
 * @param ownerEmail email of the person who owns the client
 * @return an {@link Observable} that emits the newly saved client
 */
public Observable<Client> save(String clientId, String ownerName, String ownerEmail) {
    return Observable.create(subscriber -> {
        Table clientTable = dynamoDB.getTable(CLIENT_TABLE_NAME);

        if (clientTable.getItem("clientId", clientId) != null) {
            throw new ClientAlreadyRegisteredException(clientId);
        }

        Client client = new Client();
        client.setClientId(clientId);
        client.setOwnerName(ownerName);
        client.setOwnerEmail(ownerEmail);

        clientTable.putItem(new Item().withPrimaryKey("clientId", client.getClientId())
                .withString("ownerName", client.getOwnerName())
                .withString("ownerEmail", client.getOwnerEmail()));

        subscriber.onNext(client);
        subscriber.onCompleted();
    });
}

From source file:io.ignitr.dispatchr.manager.core.data.TopicRepository.java

License:Apache License

/**
 * Saves the metadata about a registered topic.
 *
 * @param topicName name of the topic//  w  ww  . j  av  a  2 s.  c  o  m
 * @param topicArn arn of the topic
 * @return an {@link Observable} of {@link Topic} containing the metadata about the topic that was saved
 */
public Observable<Topic> save(String topicName, String topicArn) {
    return Observable.create(subscriber -> {
        Topic metadata = new Topic();
        metadata.setName(topicName);
        metadata.setArn(topicArn);
        metadata.setRegistered(true);

        Table table = dynamoDB.getTable(TOPIC_TABLE_NAME);

        if (table.getItem("name", metadata.getName()) != null) {
            throw new TopicAlreadyRegisteredException(metadata.getName());
        }

        table.putItem(new Item().withPrimaryKey("name", metadata.getName()).withString("arn", metadata.getArn())
                .withLong("register_timestamp", System.currentTimeMillis()));

        subscriber.onNext(metadata);
        subscriber.onCompleted();
    });
}