@Override public Map<String, Object> getUserPreferences(String principalName) { DynamoDB dynamoDB = new DynamoDB(documentstoreClient.getClient()); Table table = dynamoDB.getTable(USER_PREFERENCES); Item item = table.getItem(PRINCIPAL_NAME_KEY, principalName); return item == null ? null : item.asMap(); }
@Override public Map<String, Object> findInstitutionPreferences() { DynamoDB dynamoDB = new DynamoDB(documentstoreClient.getClient()); Table table = dynamoDB.getTable(INSTITUTION_PREFERENCES); Item item = table.getItem(INSTITUTION_ID_KEY, "1232413535"); return item == null ? null : item.asMap(); }
@Override public void saveUserPreferences(String principalName, String preferences) { Item item = Item.fromJSON(preferences); item = item.withString(PRINCIPAL_NAME_KEY, principalName); DynamoDB dynamoDB = new DynamoDB(documentstoreClient.getClient()); Table table = dynamoDB.getTable(USER_PREFERENCES); PutItemOutcome outcome = table.putItem(item); LOG.debug("Saved user preferences: " + outcome); }
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"); HashMap<String, String> nameMap = new HashMap<String, String>(); nameMap.put("#yr", "year"); HashMap<String, Object> valueMap = new HashMap<String, Object>(); valueMap.put(":yyyy", 1985); QuerySpec querySpec = new QuerySpec() .withKeyConditionExpression("#yr = :yyyy") .withNameMap(new NameMap().with("#yr", "year")) .withValueMap(valueMap); ItemCollection<QueryOutcome> items = table.query(querySpec); Iterator<Item> iterator = items.iterator(); Item item = null; System.out.println("Movies from 1985"); while (iterator.hasNext()) { item = iterator.next(); System.out.println(item.getNumber("year") + ": " + item.getString("title")); } valueMap.put(":yyyy", 1992); valueMap.put(":letter1", "A"); valueMap.put(":letter2", "L"); querySpec .withProjectionExpression("#yr, title, info.genres, info.actors[0]") .withKeyConditionExpression("#yr = :yyyy and title between :letter1 and :letter2") .withNameMap(nameMap) .withValueMap(valueMap); items = table.query(querySpec); iterator = items.iterator(); System.out.println("Movies from 1992 - titles A-L, with genres and lead actor"); while (iterator.hasNext()) { item = iterator.next(); System.out.println(item.toString()); } }
/** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub int change, empty; String tablename = "ParkingInfo"; HttpSession session = request.getSession(); String LotId = (String) session.getAttribute("lotid"); session.setAttribute("wronginfo", null); DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient()); Table table = dynamoDB.getTable(tablename); Item item = table.getItem("Parkurself", "parkurself", "Id", LotId); int newe = item.getInt("AvailableSpots"); change = Integer.parseInt(request.getParameter("carsin")); empty = newe - change; item.withInt("AvailableSpots", empty); table.deleteItem("Parkurself", "parkurself", "Id", LotId); table.putItem(item); newe = item.getInt("AvailableSpots"); change = Integer.parseInt(request.getParameter("carsout")); empty = newe + change; item.withInt("AvailableSpots", empty); table.deleteItem("Parkurself", "parkurself", "Id", LotId); table.putItem(item); newe = item.getInt("AvailableSpots"); if (item.getInt("AvailableSpots") > item.getInt("Total")) { item.withInt("AvailableSpots", item.getInt("Total")); table.deleteItem("Parkurself", "parkurself", "Id", LotId); table.putItem(item); session.setAttribute("wronginfo", "minus"); } else if (item.getInt("AvailableSpots") < 0) { int wrongnum = 0 - item.getInt("AvailableSpots"); item.withInt("AvailableSpots", 0); table.deleteItem("Parkurself", "parkurself", "Id", LotId); table.putItem(item); session.setAttribute("wronginfo", "exceed"); session.setAttribute("wrongnum", wrongnum); } session.setAttribute("olde", item.get("AvailableSpots")); session.setAttribute("newe", item.get("AvailableSpots")); response.sendRedirect("home.jsp"); }
public static void createItems(ChatData chatData) { Table table = dynamoDB.getTable(tableName); System.out.println(table.getTableName()); try { Item item = new Item() .withPrimaryKey("TimeStamp", dateNow()) .withString("user", chatData.getUser()) .withString("chat", chatData.getChat()); table.putItem(item); } catch (Exception e) { System.err.println("Create items failed."); System.err.println(e.getMessage()); } }
// @Test public void howToDeleteTable() throws InterruptedException { String TABLE_NAME = "myTableForMidLevelApi"; Table table = dynamo.getTable(TABLE_NAME); // Wait for the table to become active or deleted TableDescription desc = table.waitForActiveOrDelete(); if (desc == null) { System.out.println("Table " + table.getTableName() + " does not exist."); } else { table.delete(); // No need to wait, but you could table.waitForDelete(); System.out.println("Table " + table.getTableName() + " has been deleted"); } }