Java Code Examples for com.amazonaws.services.dynamodbv2.document.Table#putItem()

The following examples show how to use com.amazonaws.services.dynamodbv2.document.Table#putItem() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DynamoDBInstalledAppContextStore.java    From smartapp-sdk-java with Apache License 2.0 6 votes vote down vote up
@Override
public void add(DefaultInstalledAppContext context) {
    Table table = dynamoDB.getTable(tableName);
    InstalledApp installedApp = context.getInstalledApp();
    Token token = context.getToken();
    String configJson;
    try {
        configJson = mapper.writeValueAsString(installedApp.getConfig());
    } catch (JsonProcessingException e) {
        log.error("problem mapping config as JSON", e);
        configJson = null;
    }
    Item item = new Item().withPrimaryKey("installedAppId", context.getInstalledAppId())
            .with("authToken", token.getAccessToken()) // named to match NodeSDK version
            .with("authTokenExpiration", token.getAccessTokenExpiration().toEpochMilli())
            .with("refreshToken", token.getRefreshToken())
            .with("refreshTokenExpiration", token.getRefreshTokenExpiration().toEpochMilli())
            .with("locationId", installedApp.getLocationId()).with("config", configJson)
            .with("permissions", installedApp.getPermissions());
    PutItemOutcome outcome = table.putItem(item);
    if (log.isDebugEnabled()) {
        log.debug("put item outcome = " + outcome);
        log.debug("put item outcome.getItem() = " + outcome.getItem());
        log.debug("put item outcome.getPutItemResult() = " + outcome.getPutItemResult());
    }
}
 
Example 2
Source File: TryDaxHelper.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
void writeData(String tableName, DynamoDB client, int pkmax, int skmax) {
    Table table = client.getTable(tableName);
    System.out.println("Writing data to the table...");

    int stringSize = 1000;
    StringBuilder sb = new StringBuilder(stringSize);
    for (int i = 0; i < stringSize; i++) {
        sb.append('X');
    }
    String someData = sb.toString();

    try {
        for (Integer ipk = 1; ipk <= pkmax; ipk++) {
            System.out.println(("Writing " + skmax + " items for partition key: " + ipk));
            for (Integer isk = 1; isk <= skmax; isk++) {
                 table.putItem(new Item()
                         .withPrimaryKey("pk", ipk, "sk", isk)
                         .withString("someData", someData));
            }
        }
    } catch (Exception e) {
        System.err.println("Unable to write item:");
        e.printStackTrace();
    }
}
 
Example 3
Source File: CreateTablesLoadData.java    From aws-dynamodb-examples with Apache License 2.0 6 votes vote down vote up
private static void loadSampleForums(String tableName) {

        Table table = dynamoDB.getTable(tableName);

        try {

            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());
        }
    }
 
Example 4
Source File: SampleDataLoad.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
private static void loadSampleForums(String tableName) {

        Table table = dynamoDB.getTable(tableName);

        try {

            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());
        }
    }
 
Example 5
Source File: MoviesItemOps01.java    From aws-dynamodb-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args)  {

        AmazonDynamoDBClient client = new AmazonDynamoDBClient();
        client.setEndpoint("http://localhost:8000");
        DynamoDB dynamoDB = new DynamoDB(client);

        Table table = dynamoDB.getTable("Movies");
        
        int year = 2015;
        String title = "The Big New Movie";

        try {
            table.putItem(new Item()
                .withPrimaryKey("year", year, "title", title)
                .withJSON("info", "{\"plot\" : \"Something happens.\"}"));
            System.out.println("PutItem succeeded: " + 
                table.getItem("year", year, "title", title).toJSONPretty());

        } catch (Exception e) {
            System.out.println("PutItem failed");
            e.printStackTrace();
        }       
    }
 
Example 6
Source File: DocumentAPIItemBinaryExample.java    From aws-dynamodb-examples with Apache License 2.0 6 votes vote down vote up
public static void createItem(String threadId, String replyDateTime) throws IOException {
    
    Table table = dynamoDB.getTable(tableName);

    // Craft a long message
    String messageInput = "Long message to be compressed in a lengthy forum reply";
    
    // Compress the long message
    ByteBuffer compressedMessage = compressString(messageInput.toString());
    
    table.putItem(new Item()
        .withPrimaryKey("Id", threadId)
        .withString("ReplyDateTime", replyDateTime)
        .withString("Message", "Long message follows")
        .withBinary("ExtendedMessage", compressedMessage)
        .withString("PostedBy", "User A"));
}
 
Example 7
Source File: CreateTablesLoadData.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
private static void loadSampleForums(String tableName) {

        Table table = dynamoDB.getTable(tableName);

        try {

            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());
        }
    }
 
Example 8
Source File: DocumentAPIParallelScan.java    From aws-dynamodb-examples with Apache License 2.0 6 votes vote down vote up
private static void uploadProduct(String tableName, int productIndex) {

        Table table = dynamoDB.getTable(tableName);

        try {
            System.out.println("Processing record #" + productIndex);

            Item item = new Item()
                .withPrimaryKey("Id", productIndex)
                .withString("Title", "Book " + productIndex + " 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);

        }   catch (Exception e) {
            System.err.println("Failed to create item " + productIndex + " in " + tableName);
            System.err.println(e.getMessage());
        }
    }
 
Example 9
Source File: DocumentAPIItemCRUDExample.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
private static void createItems() {

        Table table = dynamoDB.getTable(tableName);
        try {

            Item item = new Item().withPrimaryKey("Id", 120).withString("Title", "Book 120 Title")
                .withString("ISBN", "120-1111111111")
                .withStringSet("Authors", new HashSet<String>(Arrays.asList("Author12", "Author22")))
                .withNumber("Price", 20).withString("Dimensions", "8.5x11.0x.75").withNumber("PageCount", 500)
                .withBoolean("InPublication", false).withString("ProductCategory", "Book");
            table.putItem(item);

            item = new Item().withPrimaryKey("Id", 121).withString("Title", "Book 121 Title")
                .withString("ISBN", "121-1111111111")
                .withStringSet("Authors", new HashSet<String>(Arrays.asList("Author21", "Author 22")))
                .withNumber("Price", 20).withString("Dimensions", "8.5x11.0x.75").withNumber("PageCount", 500)
                .withBoolean("InPublication", true).withString("ProductCategory", "Book");
            table.putItem(item);

        }
        catch (Exception e) {
            System.err.println("Create items failed.");
            System.err.println(e.getMessage());

        }
    }
 
Example 10
Source File: DocumentAPIParallelScan.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
private static void uploadProduct(String tableName, int productIndex) {

        Table table = dynamoDB.getTable(tableName);

        try {
            System.out.println("Processing record #" + productIndex);

            Item item = new Item().withPrimaryKey("Id", productIndex)
                .withString("Title", "Book " + productIndex + " 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);

        }
        catch (Exception e) {
            System.err.println("Failed to create item " + productIndex + " in " + tableName);
            System.err.println(e.getMessage());
        }
    }
 
Example 11
Source File: DynamoDBServiceImpl1.java    From Serverless-Programming-Cookbook with MIT License 5 votes vote down vote up
@Override
public final Response putItem(final Request request) {

    Table table = dynamoDB.getTable(request.getTableName());

    if (request.isWaitForActive()) {
        try {
            table.waitForActive();
        } catch (InterruptedException e) {
            return new Response(null,
                    "Error while waiting for table to become active with API version V2: " + e.getMessage());
        }
    }

    Item item = new Item()
            .withPrimaryKey(request.getPartitionKey(), request.getPartitionKeyValue(),
                    request.getSortKey(), request.getSortKeyValue());

    if (request.getStringData() != null) {
        request.getStringData().forEach((k, v) -> item.withString(k, v));
    }

    if (request.getIntegerData() != null) {
        request.getIntegerData().forEach((k, v) -> item.withInt(k, v));
    }

    table.putItem(item);

    return new Response("Item added into " + request.getTableName() + " with API version V1.", null);
}
 
Example 12
Source File: DocumentAPIItemCRUDExample.java    From aws-dynamodb-examples with Apache License 2.0 5 votes vote down vote up
private static void createItems() {

        Table table = dynamoDB.getTable(tableName);
        try {

            Item item = new Item()
                .withPrimaryKey("Id", 120)
                .withString("Title", "Book 120 Title")
                .withString("ISBN", "120-1111111111")
                .withStringSet( "Authors", 
                    new HashSet<String>(Arrays.asList("Author12", "Author22")))
                .withNumber("Price", 20)
                .withString("Dimensions", "8.5x11.0x.75")
                .withNumber("PageCount", 500)
                .withBoolean("InPublication", false)
                .withString("ProductCategory", "Book");
            table.putItem(item);

            item = new Item()
                .withPrimaryKey("Id", 121)
                .withString("Title", "Book 121 Title")
                .withString("ISBN", "121-1111111111")
                .withStringSet( "Authors",
                    new HashSet<String>(Arrays.asList("Author21", "Author 22")))
                .withNumber("Price", 20)
                .withString("Dimensions", "8.5x11.0x.75")
                .withNumber("PageCount", 500)
                .withBoolean("InPublication", true)
                .withString("ProductCategory", "Book");
            table.putItem(item);

        } catch (Exception e) {
            System.err.println("Create items failed.");
            System.err.println(e.getMessage());

        }
    }
 
Example 13
Source File: DocumentAPIItemBinaryExample.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void createItem(String threadId, String replyDateTime) throws IOException {

        Table table = dynamoDB.getTable(tableName);

        // Craft a long message
        String messageInput = "Long message to be compressed in a lengthy forum reply";

        // Compress the long message
        ByteBuffer compressedMessage = compressString(messageInput.toString());

        table.putItem(new Item().withPrimaryKey("Id", threadId).withString("ReplyDateTime", replyDateTime)
            .withString("Message", "Long message follows").withBinary("ExtendedMessage", compressedMessage)
            .withString("PostedBy", "User A"));
    }
 
Example 14
Source File: MoviesItemOps01.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
            .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2"))
            .build();

        DynamoDB dynamoDB = new DynamoDB(client);

        Table table = dynamoDB.getTable("Movies");

        int year = 2015;
        String title = "The Big New Movie";

        final Map<String, Object> infoMap = new HashMap<String, Object>();
        infoMap.put("plot", "Nothing happens at all.");
        infoMap.put("rating", 0);

        try {
            System.out.println("Adding a new item...");
            PutItemOutcome outcome = table
                .putItem(new Item().withPrimaryKey("year", year, "title", title).withMap("info", infoMap));

            System.out.println("PutItem succeeded:\n" + outcome.getPutItemResult());

        }
        catch (Exception e) {
            System.err.println("Unable to add item: " + year + " " + title);
            System.err.println(e.getMessage());
        }

    }
 
Example 15
Source File: MoviesItemOps02.java    From aws-dynamodb-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args)  {

        AmazonDynamoDBClient client = new AmazonDynamoDBClient();
        client.setEndpoint("http://localhost:8000");
        DynamoDB dynamoDB = new DynamoDB(client);

        Table table = dynamoDB.getTable("Movies");

        int year = 2015;
        String title = "The Big New Movie";

        final Map<String, Object> infoMap = new HashMap<String, Object>();
        infoMap.put("plot",  "Nothing happens at all.");
        infoMap.put("rating",  0.0);
        Item item = new Item()
            .withPrimaryKey(new PrimaryKey("year", year, "title", title))
            .withMap("info", infoMap);
        
        // Attempt a conditional write.  We expect this to fail.

        PutItemSpec putItemSpec = new PutItemSpec()
            .withItem(item)
            .withConditionExpression("attribute_not_exists(#yr) and attribute_not_exists(title)")
            .withNameMap(new NameMap()
                .with("#yr", "year"));
        
        System.out.println("Attempting a conditional write...");
        try {
            table.putItem(putItemSpec);
            System.out.println("PutItem succeeded: " + table.getItem("year", year, "title", title).toJSONPretty());
        } catch (ConditionalCheckFailedException e) {
            e.printStackTrace(System.err);
            System.out.println("PutItem failed");
        }
    }
 
Example 16
Source File: GettingStartedLoadData.java    From aws-dynamodb-examples with Apache License 2.0 4 votes vote down vote up
private static void loadSampleThreads(String tableName) {
    try {
        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", "primarykey", "rangekey")));
        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());
    }

}
 
Example 17
Source File: CreateTablesLoadData.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
private static void loadSampleProducts(String tableName) {

        Table table = dynamoDB.getTable(tableName);

        try {

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

    }
 
Example 18
Source File: SampleDataLoad.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
private static void loadSampleReplies(String tableName) {
    try {
        // 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());

    }
}
 
Example 19
Source File: MCAWS.java    From aws-big-data-blog with Apache License 2.0 4 votes vote down vote up
public static void dynamoInsertDicom(String dicomJson, String mirthTable, String mirthId, String mirthDate) {
       System.out.println( "Performing insert into DynamoDB" );
       String firstName = "EMPTY";
       String lastName = "EMPTY";
       String dob = "EMPTY";
       String docType = "dicom";

       AmazonDynamoDBClient client = new AmazonDynamoDBClient();
       client.withRegion(Regions.US_WEST_2);
       DynamoDB dynamoDB = new DynamoDB(client);
       Table table = dynamoDB.getTable(mirthTable);

       try {
       JSONObject obj = new JSONObject(dicomJson);

//DICOM stores patient name in tag 00100010
       if(obj.has("00100010")) {
               if(obj.getJSONObject("00100010").has("Value")) {
                       JSONArray lastNameArray = obj.getJSONObject("00100010").getJSONArray("Value");
                       if(lastNameArray !=null) {
                               JSONObject lastNameObj = lastNameArray.getJSONObject(0);
                               if(lastNameObj.has("Alphabetic")) {
                                       String patientName = lastNameObj.getString("Alphabetic");
                                       String[] patientNameArray = patientName.split("\\^");

				//some sample DICOM files only have one name string rather than 
				//delimited strings like in production messages.
				//in that case, we use that string as the last name
				//the else statement covers when we have a first and last name
                                       if(patientNameArray.length == 1) {
                                               lastName = lastNameObj.getString("Alphabetic");
                                       } else if(patientNameArray.length > 1) {
                                               lastName = patientNameArray[0];
                                               firstName = patientNameArray[1];
                                               }
                                       }
                               }
                       }
               }

//DICOM stores Date of Birth in tag 00100030
       if(obj.has("00100030")) {
               if(obj.getJSONObject("00100030").has("Value")) {
                       JSONArray dobArray = obj.getJSONObject("00100030").getJSONArray("Value");
                       if(dobArray !=null) {
			dob = dobArray.getString(0);
                               }
                       }
               }

       } catch (org.json.JSONException e) { System.out.println("JSON ERROR"); }

       //ccdJson = ccdJson.replaceAll("\"\"","\"NONE\"");

       Item item =
           new Item()
               .withPrimaryKey("mirthid", mirthId)
               .withString("mirthdate", mirthDate)
               .withString("type", docType)
               .withString("FirstName", firstName)
               .withString("LastName", lastName)
               .withString("DOB", dob)
               .withString("Processed", "N")
               .withJSON("document", dicomJson);

       table.putItem(item);
   }
 
Example 20
Source File: GettingStartedLoadData.java    From aws-dynamodb-examples with Apache License 2.0 4 votes vote down vote up
private static void loadSampleReplies(String tableName) {
    try {
        // 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());

    }
}