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

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

Introduction

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

Prototype

public AttributeValue(java.util.List<String> sS) 

Source Link

Document

Constructs a new AttributeValue object.

Usage

From source file:NYSELoad.java

License:Open Source License

private static Map<String, AttributeValue> newItem(NyseParser nyseParser) {
    Map<String, AttributeValue> item = new HashMap<String, AttributeValue>();
    item.put("stockTicker", new AttributeValue(nyseParser.getStockTicker()));
    item.put("tradeDate", new AttributeValue(nyseParser.getTransactionDate()));
    item.put("lp", new AttributeValue().withN(Float.toString(nyseParser.getLowPrice())));
    item.put("op", new AttributeValue().withN(Float.toString(nyseParser.getOpenPrice())));
    item.put("cp", new AttributeValue().withN(Float.toString(nyseParser.getClosePrice())));
    item.put("hp", new AttributeValue().withN(Float.toString(nyseParser.getHighPrice())));
    item.put("v", new AttributeValue().withN(Float.toString(nyseParser.getVolume())));

    return item;//from  w  ww.  j  a v a  2  s  .c  o m
}

From source file:whgHelper.java

License:Open Source License

public static Map<String, AttributeValue> newAlert(String alertMessageBody) {
    Map<String, AttributeValue> alert = new HashMap<String, AttributeValue>();

    // parse JSON in message body
    System.out.println("");
    System.out.println("Alert Attributes: " + String.valueOf(System.currentTimeMillis()));
    System.out.println("   sourceID: " + String.valueOf(System.currentTimeMillis()));
    System.out.println("   something: adt" + String.valueOf(System.currentTimeMillis()));
    System.out.println("   alertmessagebody: " + alertMessageBody);

    Random rn = new Random();
    int source = rn.nextInt(10) + 1;

    alert.put("alertId", new AttributeValue(String.valueOf(System.currentTimeMillis())));
    alert.put("alertSourceId", new AttributeValue(String.valueOf(source)));
    alert.put("alertDateTime", new AttributeValue("adt" + String.valueOf(System.currentTimeMillis())));
    alert.put("alertMessageBody", new AttributeValue(alertMessageBody));
    alert.put("alertPersistedDateTime",
            new AttributeValue().withN(Double.toString(System.currentTimeMillis())));
    return alert;
}

From source file:AmazonDynamoDBSample_PutThrottled.java

License:Open Source License

private static Thread newItemCreationThread(String tableName, final int thrdNo) {
    final int ITEM_COUNTS = 1000 * 1000;
    return new Thread(() -> {
        for (int c = 0; c < ITEM_COUNTS; c++) {
            Map<String, AttributeValue> item = new HashMap<>();
            String pk = String.format("pk name: thrd %d creates %dth item", thrdNo, c);
            item.put("name", new AttributeValue(pk));

            PutItemRequest req = new PutItemRequest(tableName, item);
            req.setReturnValues(ReturnValue.ALL_OLD);
            PutItemResult res = null;//from   ww  w . j a  v a  2s.  c  o m
            long t = System.currentTimeMillis();
            try {
                res = dynamoDB.putItem(req);
            } catch (Exception exob) {
                exob.printStackTrace();
            }
            assert 0 == res.getAttributes().size();
            System.out.printf("%s. takes %d ms\n", pk, System.currentTimeMillis() - t);
        }
    });
}

From source file:AmazonDynamoDBSample_PutThrottled.java

License:Open Source License

private static Map<String, AttributeValue> newItem(String name, int year, String rating, String... fans) {
    Map<String, AttributeValue> item = new HashMap<String, AttributeValue>();
    item.put("name", new AttributeValue(name));
    item.put("year", new AttributeValue().withN(Integer.toString(year)));
    item.put("rating", new AttributeValue(rating));
    item.put("fans", new AttributeValue().withSS(fans));

    return item;/* w  ww .  jav  a 2  s  . c  o  m*/
}

From source file:ProductCategoryPriceIndex.java

License:Open Source License

private static Map<String, AttributeValue> newItem(String category, int priceIndex, String... products) {
    Map<String, AttributeValue> item = new HashMap<String, AttributeValue>();
    item.put("category", new AttributeValue(category));
    item.put("priceIndex", new AttributeValue().withN(Integer.toString(priceIndex)));
    item.put("products", new AttributeValue().withSS(products));

    return item;/*from  ww w  .  j av a2s  . c  om*/
}

From source file:VideoServlet.java

private static String getItem(String keyVal) {

    Map<String, AttributeValue> key = new HashMap<String, AttributeValue>();
    key.put("ID", new AttributeValue(keyVal));

    GetItemRequest getItemRequest = new GetItemRequest().withTableName(TABLENAME).withKey(key);

    GetItemResult item = dynamoDBClient.getItem(getItemRequest);

    System.out.println("Get Result: " + item);
    return item.toString();
}

From source file:amazonsensors.LambdaAmazonSensor.java

@Override
public String handleRequest(AmazonSensor sensor, Context cntxt) {

    AmazonDynamoDB amazonDDB = AmazonDynamoDBClientBuilder.standard().withRegion(Regions.US_WEST_2).build();

    Map<String, AttributeValue> item = new HashMap();
    item.put(SENSOR_FIELD, new AttributeValue(sensor.getSensorID()));
    item.put(TEMPERATURE_FIELD, new AttributeValue(sensor.getTemperature()));

    amazonDDB.putItem(TABLE_NAME, item);

    return sensor.toString();
}

From source file:aws.example.dynamodb.DeleteItem.java

License:Open Source License

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    DeleteItem <table> <name>\n\n" + "Where:\n"
            + "    table - the table to delete the item from.\n"
            + "    name  - the item to delete from the table,\n"
            + "            using the primary key \"Name\"\n\n" + "Example:\n"
            + "    DeleteItem HelloTable World\n\n" + "**Warning** This program will actually delete the item\n"
            + "            that you specify!\n";

    if (args.length < 2) {
        System.out.println(USAGE);
        System.exit(1);/* www. j  av a2 s .c o  m*/
    }

    String table_name = args[0];
    String name = args[1];

    System.out.format("Deleting item \"%s\" from %s\n", name, table_name);

    HashMap<String, AttributeValue> key_to_get = new HashMap<String, AttributeValue>();

    key_to_get.put("Name", new AttributeValue(name));

    final AmazonDynamoDBClient ddb = new AmazonDynamoDBClient();

    try {
        ddb.deleteItem(table_name, key_to_get);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }

    System.out.println("Done!");
}

From source file:aws.example.dynamodb.GetItem.java

License:Open Source License

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    GetItem <table> <name> [projection_expression]\n\n"
            + "Where:\n" + "    table - the table to get an item from.\n" + "    name  - the item to get.\n\n"
            + "You can add an optional projection expression (a quote-delimited,\n"
            + "comma-separated list of attributes to retrieve) to limit the\n"
            + "fields returned from the table.\n\n" + "Example:\n" + "    GetItem HelloTable World\n"
            + "    GetItem SiteColors text \"default, bold\"\n";

    if (args.length < 2) {
        System.out.println(USAGE);
        System.exit(1);//from   w w  w  .  jav a 2 s .c  o  m
    }

    String table_name = args[0];
    String name = args[1];
    String projection_expression = null;

    // if a projection expression was included, set it.
    if (args.length == 3) {
        projection_expression = args[2];
    }

    System.out.format("Retrieving item \"%s\" from \"%s\"\n", name, table_name);

    HashMap<String, AttributeValue> key_to_get = new HashMap<String, AttributeValue>();

    key_to_get.put("Name", new AttributeValue(name));

    GetItemRequest request = null;
    if (projection_expression != null) {
        request = new GetItemRequest().withKey(key_to_get).withTableName(table_name)
                .withProjectionExpression(projection_expression);
    } else {
        request = new GetItemRequest().withKey(key_to_get).withTableName(table_name);
    }

    final AmazonDynamoDBClient ddb = new AmazonDynamoDBClient();

    try {
        Map<String, AttributeValue> returned_item = ddb.getItem(request).getItem();
        if (returned_item != null) {
            Set<String> keys = returned_item.keySet();
            for (String key : keys) {
                System.out.format("%s: %s\n", key, returned_item.get(key).toString());
            }
        } else {
            System.out.format("No item found with the key %s!\n", name);
        }
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
}

From source file:aws.example.dynamodb.PutItem.java

License:Open Source License

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    PutItem <table> <name> [field=value ...]\n\n" + "Where:\n"
            + "    table    - the table to put the item in.\n"
            + "    name     - a name to add to the table. If the name already\n"
            + "               exists, its entry will be updated.\n"
            + "Additional fields can be added by appending them to the end of the\n" + "input.\n\n"
            + "Example:\n" + "    PutItem Cellists Pau Language=ca Born=1876\n";

    if (args.length < 2) {
        System.out.println(USAGE);
        System.exit(1);/*from w  w  w.  j  a va2s .c  o m*/
    }

    String table_name = args[0];
    String name = args[1];
    ArrayList<String[]> extra_fields = new ArrayList<String[]>();

    // any additional args (fields to add to database)?
    for (int x = 2; x < args.length; x++) {
        String[] fields = args[x].split("=", 2);
        if (fields.length == 2) {
            extra_fields.add(fields);
        } else {
            System.out.format("Invalid argument: %s\n", args[x]);
            System.out.println(USAGE);
            System.exit(1);
        }
    }

    System.out.format("Adding \"%s\" to \"%s\"", name, table_name);
    if (extra_fields.size() > 0) {
        System.out.println("Additional fields:");
        for (String[] field : extra_fields) {
            System.out.format("  %s: %s\n", field[0], field[1]);
        }
    }

    HashMap<String, AttributeValue> item_values = new HashMap<String, AttributeValue>();

    item_values.put("Name", new AttributeValue(name));

    for (String[] field : extra_fields) {
        item_values.put(field[0], new AttributeValue(field[1]));
    }

    final AmazonDynamoDBClient ddb = new AmazonDynamoDBClient();

    try {
        ddb.putItem(table_name, item_values);
    } catch (ResourceNotFoundException e) {
        System.err.format("Error: The table \"%s\" can't be found.\n", table_name);
        System.err.println("Be sure that it exists and that you've typed its name correctly!");
        System.exit(1);
    } catch (AmazonServiceException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}