/** * Get ticket. * * @param ticketId the ticket id * @return the ticket */ public Ticket get(final String ticketId) { final TicketDefinition metadata = this.ticketCatalog.find(ticketId); if (metadata != null) { final Map<String, AttributeValue> keys = new HashMap<>(); keys.put(ColumnNames.ID.getName(), new AttributeValue(ticketId)); final GetItemRequest request = new GetItemRequest() .withKey(keys) .withTableName(metadata.getProperties().getStorageName()); LOGGER.debug("Submitting request [{}] to get ticket item [{}]", request, ticketId); final Map<String, AttributeValue> returnItem = amazonDynamoDBClient.getItem(request).getItem(); if (returnItem != null) { final Ticket ticket = deserializeTicket(returnItem); LOGGER.debug("Located ticket [{}]", ticket); return ticket; } } else { LOGGER.warn("No ticket definition could be found in the catalog to match [{}]", ticketId); } return null; }
private Collection<String> fetchChannelLanguages(final String channel) { final String id = "channel:" + channel + ":languages"; final GetItemRequest getItemRequest = new GetItemRequest() .withAttributesToGet(Collections.singletonList("value")) .withKey(Collections.singletonMap("id", new AttributeValue(id))) .withTableName(TableName); final GetItemResult getItemResult = ddb.getItem(getItemRequest); final Optional<String> maybeValue = Optional.ofNullable(getItemResult.getItem()) .map(i -> i.get("value")) .map(AttributeValue::getS); if (!maybeValue.isPresent()) return Collections.emptyList(); return Arrays.asList(maybeValue.get().trim().split(" +")); }
private void fetchTheValue() { final GetItemRequest req = new GetItemRequest().withAttributesToGet("value") .withTableName(TableName) .withKey(Collections.singletonMap("id", new AttributeValue(this.id))); try { final GetItemResult result = ddb.getItem(req); synchronized (this.monitor) { if (result.getItem() == null) { this.x = new RuntimeException("not found: id=" + this.id); } else { this.v = result.getItem().get("value").getS(); if (this.v == null) { this.x = new RuntimeException("found but no value for: id=" + this.id); } } } } catch (final RuntimeException x) { synchronized (this.monitor) { this.x = x; } } }
protected DDBTaskResult doInBackground(Void... params) { Log.i("doInBackground", "Starting DDBGettask"); DDBTaskResult result = new DDBTaskResult(); try { // Need to specify the key of our item, which is a Map of our primary key attribute(s) Map<String, AttributeValue> key = new HashMap<String, AttributeValue>(); key.put("userid", new AttributeValue().withS("user1234")); key.put("recordid", new AttributeValue().withS("highScore")); GetItemRequest getItemRequest = new GetItemRequest(AWSClientManager.DDB_TABLE_NAME,key); GetItemResult getItemResult = ddb.getItem(getItemRequest); result.setAttributeNumber(Integer.parseInt(getItemResult.getItem().get("data").getN())); } catch (AmazonServiceException ex) { ex.printStackTrace(); Log.e("ddb-get-doInBackground", ex.getMessage()); } return result; }
/** * Get item of table with provided key-value. * * @param tableName * @param combinedKey * @return */ public static Row getRowByKey(String tableName, Key... combinedKey) { Map<String, AttributeValue> transformedKey = new HashMap<>(); for (Key key : combinedKey) { transformedKey.put(key.getName(), new AttributeValue().withS(key.getValue())); } GetItemResult result = DynamoDbHandler.CLIENT .getItem(new GetItemRequest(tableName, transformedKey)); List<Attribute> attributes = new ArrayList<>(); for (String resultKey : result.getItem().keySet()) { attributes.add(new Attribute(resultKey, result.getItem() .get(resultKey).getS())); } return new Row(attributes); }
private Map<String, AttributeValue> readRow(String key, String appid) { if (StringUtils.isBlank(key) || StringUtils.isBlank(appid)) { return null; } Map<String, AttributeValue> row = null; try { GetItemRequest getItemRequest = new GetItemRequest(getTableNameForAppid(appid), Collections.singletonMap(Config._KEY, new AttributeValue(getKeyForAppid(key, appid)))); GetItemResult res = client().getItem(getItemRequest); if (res != null && res.getItem() != null && !res.getItem().isEmpty()) { row = res.getItem(); } } catch (Exception e) { logger.error("Could not read row from DB - appid={}, key={}", appid, key, e); } return (row == null || row.isEmpty()) ? null : row; }
public <T> T get(String tableName, Map<String, AttributeValue> key, final ObjectExtractor<T> extractor, String... columnsToInclude) throws EmptyResultDataAccessException { Assert.notNull(tableName, "Table must not be null"); Assert.notNull(extractor, "ObjectExtractor must not be null"); if (logger.isDebugEnabled()) { logger.debug("Executing query on " + tableName + " for " + renderKey(key)); } GetItemRequest request = new GetItemRequest(tableName, key, true); if (columnsToInclude != null && columnsToInclude.length > 0) { request.setAttributesToGet(Arrays.asList(columnsToInclude)); } GetItemResult result = client.getItem(request); Map<String, AttributeValue> item = result.getItem(); if (item == null) { throw new EmptyResultDataAccessException("No results found in " + tableName + "for " + renderKey(key)); } return extractor.extract(item); }
private static void retrieveItem() { try { HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>(); key.put("Id", new AttributeValue().withN("120")); GetItemRequest getItemRequest = new GetItemRequest() .withTableName(tableName) .withKey(key) .withProjectionExpression("Id, ISBN, Title, Authors"); GetItemResult result = client.getItem(getItemRequest); // Check the response. System.out.println("Printing item after retrieving it...."); printItem(result.getItem()); } catch (AmazonServiceException ase) { System.err.println("Failed to retrieve item in " + tableName); } }
public static void retrieveItem(String threadId, String replyDateTime) throws IOException { HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>(); key.put("Id", new AttributeValue().withS(threadId)); key.put("ReplyDateTime", new AttributeValue().withS(replyDateTime)); GetItemRequest getReplyRequest = new GetItemRequest() .withTableName(tableName) .withKey(key) .withConsistentRead(true); GetItemResult getReplyResult = client.getItem(getReplyRequest); // Decompress the reply message and print Map<String, AttributeValue> reply = getReplyResult.getItem(); String message = decompressString(reply.get("ExtendedMessage").getB()); System.out.println("Reply message:\n" + " Id: " + reply.get("Id").getS() + "\n" + " ReplyDateTime: " + reply.get("ReplyDateTime").getS() + "\n" + " PostedBy: " + reply.get("PostedBy").getS() + "\n" + " Message: " + reply.get("Message").getS() + "\n" + " ExtendedMessage (decompressed): " + message); }
/** * Returns the list of usernames stored in the user table. */ public void describeUser(String username, String userTable) { HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>(); key.put("username", new AttributeValue().withS(username)); GetItemRequest getItemRequest = new GetItemRequest() .withTableName(userTable) .withKey(key); Map<String, AttributeValue> list = ddb.getItem(getItemRequest).getItem(); if (list.isEmpty()) { System.err.println("No record found for username '" + username + "'"); return; } for (Entry<String, AttributeValue> entry : list.entrySet()) { System.out.println(entry.getKey() + " = " + entry.getValue().getS()); } }
@Override protected Row executePrimaryKeyLookupQuery(Table table, List<SelectItem> selectItems, Column primaryKeyColumn, Object keyValue) { final List<String> attributeNames = new ArrayList<>(); for (SelectItem selectItem : selectItems) { attributeNames.add(selectItem.getColumn().getName()); } final GetItemRequest getItemRequest = new GetItemRequest(table.getName(), Collections.singletonMap( primaryKeyColumn.getName(), DynamoDbUtils.toAttributeValue(keyValue))).withAttributesToGet( attributeNames); final GetItemResult item = _dynamoDb.getItem(getItemRequest); final Object[] values = new Object[selectItems.size()]; for (int i = 0; i < values.length; i++) { final AttributeValue attributeValue = item.getItem().get(attributeNames.get(i)); values[i] = DynamoDbUtils.toValue(attributeValue); } return new DefaultRow(new SimpleDataSetHeader(selectItems), values); }
public GetPointResult getPoint(GetPointRequest getPointRequest) { long geohash = S2Manager.generateGeohash(getPointRequest.getGeoPoint()); long hashKey = S2Manager.generateHashKey(geohash, config.getHashKeyLength()); GetItemRequest getItemRequest = getPointRequest.getGetItemRequest(); getItemRequest.setTableName(config.getTableName()); AttributeValue hashKeyValue = new AttributeValue().withN(String.valueOf(hashKey)); getItemRequest.getKey().put(config.getHashKeyAttributeName(), hashKeyValue); getItemRequest.getKey().put(config.getRangeKeyAttributeName(), getPointRequest.getRangeKeyValue()); GetItemResult getItemResult = config.getDynamoDBClient().getItem(getItemRequest); GetPointResult getPointResult = new GetPointResult(getItemResult); return getPointResult; }
private RegisteredService getRegisteredServiceByKeys(final Map<String, AttributeValue> keys) { final GetItemRequest request = new GetItemRequest() .withKey(keys) .withTableName(TABLE_NAME); LOGGER.debug("Submitting request [{}] to get service with keys [{}]", request, keys); final Map<String, AttributeValue> returnItem = amazonDynamoDBClient.getItem(request).getItem(); if (returnItem != null) { final RegisteredService service = deserializeServiceFromBinaryBlob(returnItem); LOGGER.debug("Located service [{}]", service); return service; } return null; }
private Mono<GetItemResult> getItemResultMono(String url) { GetItemRequest getItemRequest = new GetItemRequest(); getItemRequest.setTableName(Utils.table.websites); HashMap<String, AttributeValue> key = new HashMap<>(); key.put(Utils.params.url, new AttributeValue(url)); getItemRequest.setKey(key); return Mono.fromFuture( Utils.makeCompletableFuture( dynamoDBAsync.getItemAsync(getItemRequest))) .doOnError((throwable -> LOG.error(Utils.error.failed_dynamo_get, url))); }
private GetItemRequest getItemRequest(String url) { GetItemRequest request = new GetItemRequest(); request.setTableName(Utils.table.websites); HashMap<String, AttributeValue> key = new HashMap<>(); key.put(Utils.params.url, new AttributeValue(url)); request.setKey(key); return request; }
private Optional<String> googleToken(final String team) { final String id = "team:" + team + ":googletoken"; final GetItemRequest getItemRequest = new GetItemRequest() .withAttributesToGet(Collections.singletonList("value")) .withKey(Collections.singletonMap("id", new AttributeValue(id))) .withTableName(TableName); final GetItemResult getItemResult = ddb.getItem(getItemRequest); return Optional.ofNullable(getItemResult.getItem()).map(i -> i.get("value")).map(AttributeValue::getS); }
public CompletableFuture<Map<String, Object>> getItem(final GetItemRequest getItemRequest) { return asyncExecutor.execute(new Callable<Map<String, Object>>() { @Override public Map<String, Object> call() throws Exception { return dbExecutor.getItem(getItemRequest); } }); }
public <T> CompletableFuture<T> getItem(final Class<T> targetClass, final GetItemRequest getItemRequest) { return asyncExecutor.execute(new Callable<T>() { @Override public T call() throws Exception { return dbExecutor.getItem(targetClass, getItemRequest); } }); }
@Override public void execute() { GetItemResult result = ddbClient.getItem(new GetItemRequest() .withKey(determineKey()) .withTableName(determineTableName()) .withAttributesToGet(determineAttributeNames()) .withConsistentRead(determineConsistentRead())); addAttributesToResult(result.getItem()); }
GetItemResult getItem(final GetItemRequest request) throws BackendException { setUserAgent(request); GetItemResult result; timedReadThrottle(GET_ITEM, request.getTableName(), estimateCapacityUnits(GET_ITEM, request.getTableName())); final Timer.Context apiTimerContext = getTimerContext(GET_ITEM, request.getTableName()); try { result = client.getItem(request); } catch (Exception e) { throw processDynamoDbApiException(e, GET_ITEM, request.getTableName()); } finally { apiTimerContext.stop(); } meterConsumedCapacity(GET_ITEM, result.getConsumedCapacity()); return result; }
@Override public EntryList getSlice(final KeySliceQuery query, final StoreTransaction txh) throws BackendException { log.debug("Entering getSliceKeySliceQuery table:{} query:{} txh:{}", getTableName(), encodeForLog(query), txh); final GetItemRequest request = super.createGetItemRequest().withKey(new ItemBuilder().hashKey(query.getKey()).build()); final GetItemResult result = new ExponentialBackoff.GetItem(request, client.getDelegate()).runWithBackoff(); final List<Entry> filteredEntries = extractEntriesFromGetItemResult(result, query.getSliceStart(), query.getSliceEnd(), query.getLimit()); log.debug("Exiting getSliceKeySliceQuery table:{} query:{} txh:{} returning:{}", getTableName(), encodeForLog(query), txh, filteredEntries.size()); return StaticArrayEntryList.of(filteredEntries); }
/** * Get user info for the username * * @param username * Unique user identifier * @return UserInfo for the user, null otherwise */ public UserInfo getUserInfo(String username) throws DataAccessException { HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>(); key.put(ATTRIBUTE_USERNAME, new AttributeValue().withS(username)); GetItemRequest getItemRequest = new GetItemRequest() .withTableName(USER_TABLE) .withKey(key); try { return UserInfo.fromData(ddb.getItem(getItemRequest).getItem()); } catch (AmazonClientException e) { throw new DataAccessException("Failed to get item username: " + username, e); } }
/** * Returns device info for given device ID (UID) * * @param uid * Unique device identifier * @return device info for the given uid */ public DeviceInfo getDeviceInfo(String uid) throws DataAccessException { HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>(); key.put(ATTRIBUTE_UID, new AttributeValue().withS(uid)); GetItemRequest getItemRequest = new GetItemRequest() .withTableName(DEVICE_TABLE) .withKey(key); try { return DeviceInfo.fromData(ddb.getItem(getItemRequest).getItem()); } catch (AmazonClientException e) { throw new DataAccessException("Failed to get device: " + uid, e); } }
/** * * Get the item from table by their hash key and range key * */ public Map<String, AttributeValue> getItem(String tableName, String hashKeyName, AttributeValue hashkeyValue, String rangeKeyName, AttributeValue rangeKeyValue) { HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>(); key.put(hashKeyName, hashkeyValue); if (rangeKeyValue != null) key.put(rangeKeyName, rangeKeyValue); GetItemRequest getItemRequest = new GetItemRequest().withTableName(tableName).withKey(key); GetItemResult result = client.getItem(getItemRequest); return result.getItem(); }
public String getValue(long key) { Map<String, AttributeValue> result = client.getItem(new GetItemRequest() .withTableName("example_table") .withKey(Collections.singletonMap(sequenceNumber.getAttributeName(), new AttributeValue().withN(Long.toString(key))))).getItem(); return result.get(valueAttribute.getAttributeName()).getS(); }
@Override public User find(String email) throws DataSourceTableDoesNotExistException { try { User user = null; // Create a request to find a User by email address GetItemRequest getItemRequest = new GetItemRequest() .withTableName( config.getProperty(ConfigurationSettings.ConfigProps.DDB_USERS_TABLE)) .addKeyEntry(HASH_KEY_NAME, new AttributeValue(email)); // Issue the request to find the User in DynamoDB GetItemResult getItemResult = dynamoClient.getItem(getItemRequest); // If an item was found if (getItemResult.getItem() != null) { // Marshal the Map<String, AttributeValue> structure returned in // the // GetItemResult to a User object user = getUserFromMap(getItemResult.getItem()); } return user; } catch (ResourceNotFoundException rnfe) { // The ResourceNotFoundException method is thrown by the getItem() // method // if the DynamoDB table doesn't exist. This exception is re-thrown // as a // custom, more specific DataSourceTableDoesNotExistException that // users // of this DAO understand. throw new DataSourceTableDoesNotExistException(config.getProperty(ConfigurationSettings.ConfigProps.DDB_USERS_TABLE)); } }
@Override public Map<String, AttributeValue> getItem(String tableName, HashMap<String, AttributeValue> primaryKey) { LOG.info("Retrieves a set of Attributes for an item that matches the primary key " + primaryKey + " from the table " + tableName); try { GetItemRequest getItemRequest = new GetItemRequest().withTableName(tableName) .withKey(primaryKey) .withConsistentRead(true); GetItemResult getItemResult = dynamoDBClient.getItem(getItemRequest); Map<String, AttributeValue> item = getItemResult.getItem(); if (item == null || item.isEmpty()) { LOG.warn("Could not find any item for the given UUID: " + primaryKey + " from " + tableName); return Collections.emptyMap(); } LOG.info("Listing collection from " + tableName + ": " + (item.size() / 8) + " items"); return item; } catch (ResourceNotFoundException rnfe) { LOG.error("Requested resource " + tableName + " not found ", rnfe); } catch (Exception ex) { LOG.error("Failed to get item into the " + tableName, ex); } return Collections.emptyMap(); }
protected static Map<String, AttributeValue> getValue( final AmazonDynamoDB dynamoClient, final String tableName, final UpdateKey key) { GetItemRequest req = new GetItemRequest().withTableName(tableName) .withKey(getTableKey(key)); return dynamoClient.getItem(req).getItem(); }
@Override public boolean userExists(String username) { GetItemRequest request = new GetItemRequest(schema.getTableName(), Collections.singletonMap(schema.getColumnUsername(), new AttributeValue(username))) // .withAttributesToGet(schema.getColumnUsername()); GetItemResult result = client.getItem(request); return result.getItem() != null; }
@Override public void beforeRequest(Request<?> request) { /* Things to do just before a request is executed */ if (request.getOriginalRequest() instanceof PutItemRequest) { /* Throw throuhgput exceeded exception for 50% of put requests */ if (rnd.nextInt(2) == 0) { logger.info("Injecting ProvisionedThroughputExceededException"); throw new ProvisionedThroughputExceededException("Injected Error"); } } /* Add latency to some Get requests */ if (request.getOriginalRequest() instanceof GetItemRequest) { /* Delay 50% of GetItem requests by 500 ms */ if (rnd.nextInt(2) == 0) { /* Delay on average 50% of the requests from client perspective */ try { logger.info("Injecting 500 ms delay"); Thread.sleep(500); } catch (InterruptedException ie) { logger.info(ie); throw new RuntimeException(ie); } } } }
@Override public void afterResponse(Request<?> request, Response<?> response) { /* * The following is a hit and miss for multi-threaded clients as the * cache size is only 50 entries */ String awsRequestId = dynamoDBClient.getCachedResponseMetadata(request.getOriginalRequest()).getRequestId(); logger.info("AWS RequestID: " + awsRequestId); /* * Here you could inspect and alter the response object to see how your * application behaves for specific data */ if (request.getOriginalRequest() instanceof GetItemRequest) { GetItemResult result = (GetItemResult) response.getAwsResponse(); Map<String, AttributeValue> item = result.getItem(); if (item.get("name").getS().equals("Airplane")) { // Alter the item item.put("name", new AttributeValue("newAirplane")); item.put("new attr", new AttributeValue("new attr")); // Add some delay try { Thread.sleep(500); } catch (InterruptedException ie) { logger.info(ie); throw new RuntimeException(ie); } } } }
/** * Returns device info for given device ID (UID) * * @param uid * Unique device identifier * @return device info for the given uid * @throws DataAccessException */ public DeviceInfo getDeviceInfo(String uid) throws DataAccessException { HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>(); key.put(ATTRIBUTE_UID, new AttributeValue().withS(uid)); GetItemRequest getItemRequest = new GetItemRequest() .withTableName(DEVICE_TABLE) .withKey(key); try { return DeviceInfo.fromData(ddb.getItem(getItemRequest).getItem()); } catch (AmazonClientException e) { throw new DataAccessException("Failed to get device: " + uid, e); } }
@Override public AttributeValue get(final String attr) throws IOException { final String attrib = String.format(Locale.ENGLISH, attr); AttributeValue value = this.attributes.get(attrib); if (value == null) { final AmazonDynamoDB aws = this.credentials.aws(); try { final GetItemRequest request = this.makeItemRequestFor(attrib); final long start = System.currentTimeMillis(); final GetItemResult result = aws.getItem(request); value = result.getItem().get(attrib); Logger.info( this, // @checkstyle LineLength (1 line) "#get('%s'): loaded '%[text]s' from DynamoDB, %s, in %[ms]s", attrib, value, new PrintableConsumedCapacity( result.getConsumedCapacity() ).print(), System.currentTimeMillis() - start ); } catch (final AmazonClientException ex) { throw new IOException( String.format( "failed to get \"%s\" from \"%s\" by %s", attr, this.name, this.keys ), ex ); } finally { aws.shutdown(); } } if (value == null) { throw new NoSuchElementException( String.format("attribute \"%s\" not found", attr) ); } return value; }
/** * Makes a GetItemRequest for a given attribute. * @param attr Attribute name * @return GetItemRequest */ private GetItemRequest makeItemRequestFor(final String attr) { final GetItemRequest request = new GetItemRequest(); request.setTableName(this.name); request.setAttributesToGet(Collections.singletonList(attr)); request.setKey(this.attributes.only(this.keys)); request.setReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL); request.setConsistentRead(true); return request; }
@Override public <T extends Message> T findOne(T matcher, Modifier... modifiers) throws DataStoreException { DynamoClassMapping<T> tableInfo = getClassMapping(matcher); log.debug("findOne {} {}", matcher.getClass().getSimpleName(), matcher); Map<FieldDescriptor, Object> matcherFields = matcher.getAllFields(); // TODO: Remove PK fields GetItemRequest request = new GetItemRequest(); // TODO: Modifier for eventually consistent read? request.setConsistentRead(true); request.setTableName(tableInfo.getDynamoTableName()); request.setKey(tableInfo.buildCompleteKey(matcher)); for (Modifier modifier : modifiers) { throw new UnsupportedOperationException(); } GetItemResult result = dynamoDB.getItem(request); Map<String, AttributeValue> itemData = result.getItem(); if (itemData == null) { return null; } T item = tableInfo.mapFromDb(itemData); if (DataStore.matches(matcherFields, item)) { log.debug("found item: {}", item); return item; } return null; }
public GetPointRequest(GeoPoint geoPoint, AttributeValue rangeKeyValue) { getItemRequest = new GetItemRequest(); getItemRequest.setKey(new HashMap<String, AttributeValue>()); this.geoPoint = geoPoint; this.rangeKeyValue = rangeKeyValue; }
public Map<String, Object> getItem(final GetItemRequest getItemRequest) { return getItem(Map.class, getItemRequest); }