List of usage examples for com.amazonaws.services.dynamodbv2.document Item getInt
public int getInt(String attrName)
int
. From source file:com.achow101.bittipaddr.server.addressServiceImpl.java
License:Open Source License
@Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Get the id String requestURI = request.getRequestURI(); String id = requestURI.substring(requestURI.lastIndexOf("/") + 1); // Setup the aws dynamo db client AmazonDynamoDBClient client = new AmazonDynamoDBClient(); DynamoDB dynamoDB = new DynamoDB(client); // Setup blockcypher API BlockCypherContext blockCypherContext = new BlockCypherContext("v1", "btc", "main", "4d3109a5c07f426da9ccc2943da39244"); // Lookup ID and get current address, increment index String address = ""; Table table = dynamoDB.getTable("Bittipaddrs"); int currAddrInx = 0; try {//from ww w. jav a 2 s. c om Item item = table.getItem("ID", id); currAddrInx = item.getInt("AddrIndex"); int origIndx = currAddrInx; List<String> addresses = item.getList("Addresses"); if (currAddrInx < addresses.size()) { address = addresses.get(currAddrInx); while (blockCypherContext.getAddressService().getAddress(address).getnTx() > 0 && currAddrInx < addresses.size()) { // Increment index and get next address currAddrInx++; address = addresses.get(currAddrInx); // Wait one third of a second to prevent rate limiting Thread.sleep(334); } } else { address = addresses.get(addresses.size() - 1); } // Update index if DB if it has changed if (currAddrInx != origIndx) { UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey("ID", id) .withUpdateExpression("set AddrIndex=:i") .withValueMap(new ValueMap().withNumber(":i", currAddrInx)); table.updateItem(updateItemSpec); } } // Deal with rate limiting from BlockCypher catch (BlockCypherException e) { UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey("ID", id) .withUpdateExpression("set AddrIndex=:i") .withValueMap(new ValueMap().withNumber(":i", currAddrInx)); table.updateItem(updateItemSpec); } catch (Exception e) { System.out.println("Error in getting item."); } // Send them a redirect to the bitcoin uri if redirect is set if (request.getParameter("redirect") != null) { response.sendRedirect("bitcoin:" + address); } else { // Set response content type response.setContentType("text/html"); // Actual logic goes here. PrintWriter out = response.getWriter(); out.println("<a href=bitcoin:" + address + ">" + address + "</a>"); } }
From source file:com.eho.dynamodb.DynamoDBConnection.java
public static PutItemOutcome upload_resource_old(String resource) throws Exception { String id;/* w ww . j av a2 s.c om*/ JSONObject json_resource = new JSONObject(resource); //does the resource have a primary key? if (json_resource.has(PRIMARY_KEY))//if it does not have a primary key, create one using uuid id = json_resource.getString(PRIMARY_KEY); else id = UUID.randomUUID().toString(); 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); } Integer new_version = retreived_item.getInt("version") + 1; retreived_item.withInt("version", new_version); Item item_to_upload = Item.fromJSON(retreived_item.toJSONPretty()).withJSON("Document", resource); PutItemSpec putItemSpec = new PutItemSpec().withItem(item_to_upload).withReturnValues(ReturnValue.NONE); return table.putItem(putItemSpec); }
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 www . j a v 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.eho.dynamodb.DynamoDBConnection.java
public static UpdateItemOutcome update_resource(String resource) throws Exception { String id;// w w w. ja v a 2 s . c o m JSONObject json_resource = new JSONObject(resource); //does the resource have a primary key? if (json_resource.has(PRIMARY_KEY))//if it does not have a primary key, create one using uuid id = json_resource.getString(PRIMARY_KEY); else id = UUID.randomUUID().toString(); 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); } Integer new_version = retreived_item.getInt("version") + 1; retreived_item.withInt("version", new_version); String new_version_str = new_version.toString(); UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey(PRIMARY_KEY, id) .withUpdateExpression("SET " + new_version_str + "= :newval") .withValueMap(new ValueMap().withString(":newval", resource)).withReturnValues(ReturnValue.ALL_NEW); return table.updateItem(updateItemSpec); }
From source file:com.eho.fhir.econsult.RestControllers.PatientController.java
@RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = "text/plain;charset=UTF-8") public ResponseEntity<String> patientGET(@PathVariable String id) throws Exception { Item item = DynamoDBConnection.get_item_by_ID(id); if (item == null) return new ResponseEntity(HttpStatus.NOT_FOUND); Integer last_version = item.getInt("version"); return new ResponseEntity(item.get("text" + last_version.toString()), HttpStatus.OK); }
From source file:com.eho.fhir.econsult.RestControllers.PractitionerController.java
@RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = "text/plain;charset=UTF-8") public ResponseEntity<String> practitionerGET(@PathVariable String id) throws Exception { Item item = DynamoDBConnection.get_item_by_ID(id); if (item == null) return new ResponseEntity(HttpStatus.NOT_FOUND); Integer last_version = item.getInt("version"); return new ResponseEntity(item.get("text" + last_version.toString()), HttpStatus.OK); }
From source file:com.github.sdmcraft.slingdynamo.impl.DynamoDBResourceProvider.java
License:Open Source License
/** * Returns a resource from this resource provider or null if the resource provider cannot find it. * The table-name, id and child-ids are parsed out from the path and queried against dynamodb to fetch the specified resource. * * @param resolver the ResourceResolver to which the returned Resource is attached. * @param req the HttpServletRequest made to get this resource * @param path the path of the resource. The path is of the format <table-name>/<id>/[<child-id1>/.../<child-idn>] * @return the resource at the specified path if it exists else returns null *//*from w ww. j a v a 2s.co m*/ public Resource getResource(ResourceResolver resolver, HttpServletRequest req, String path) { Resource resource = null; try { Map<String, Object> resourceProps = new HashMap<String, Object>(); ResourceMetadata resourceMetadata = new ResourceMetadata(); resourceMetadata.setResolutionPath(path); if (!path.contains(".")) { if (path.length() > root.length()) { String subPath = path.substring(root.length() + 1); String[] subPathSplits = subPath.split("/"); String table = subPathSplits[0]; resourceMetadata.put("table", table); Table dbtable = dynamoDB.getTable(table); if (subPathSplits.length == 1) { DescribeTableRequest describeTableRequest = new DescribeTableRequest(table); DescribeTableResult describeTableResult = null; describeTableResult = dynamoDBClient.describeTable(describeTableRequest); Date creationDate = describeTableResult.getTable().getCreationDateTime(); long itemCount = describeTableResult.getTable().getItemCount(); resourceProps.put("creation-date", creationDate); resourceProps.put("record-count", itemCount); resourceProps.put("table-name", table); } else if (subPathSplits.length == 2) { int id = Integer.parseInt(subPathSplits[1]); ScanFilter idFilter = new ScanFilter("id").eq(id); ItemCollection<ScanOutcome> items = dbtable.scan(idFilter); Iterator<Item> itemItr = items.iterator(); Item item = itemItr.next(); resourceProps = itemToMap(item); } else if (subPathSplits.length > 2) { int parent = Integer.parseInt(subPathSplits[1]); Item item = null; for (int i = 2; i < subPathSplits.length; i++) { int child = Integer.parseInt(subPathSplits[i]); ScanFilter parentFilter = new ScanFilter("parent").eq(parent); ScanFilter childFilter = new ScanFilter("child_id").eq(child); ItemCollection<ScanOutcome> items = dbtable.scan(parentFilter, childFilter); Iterator<Item> itemItr = items.iterator(); item = itemItr.next(); parent = item.getInt("id"); } resourceProps = itemToMap(item); } } resourceProps.put("hello", "world"); ModifiableValueMapDecorator valueMap = new ModifiableValueMapDecorator(resourceProps); resource = new DynamoDBResource(resolver, resourceMetadata, valueMap, resourceType); } } catch (Throwable ex) { LOGGER.error(ex.getMessage(), ex); throw new RuntimeException(ex); } return resource; }