List of usage examples for com.amazonaws.services.dynamodbv2.model QueryRequest QueryRequest
public QueryRequest()
From source file:com.makariev.dynamodb.preferences.UserPreferenceLowLevelAPIService.java
License:Open Source License
@Override public List<UserPreference> queryByFirstName(String firstName, String lastName) { QueryRequest queryRequest = new QueryRequest().withTableName(UserPreference.TABLE_NAME) .withIndexName(UserPreference.NAME_INDEX).withSelect("ALL_ATTRIBUTES").withScanIndexForward(true); final HashMap<String, Condition> keyConditions = new HashMap<>(); //ComparisonOperator.CONTAINS) cannot be used with indexes ( no query, only scan ) keyConditions.put("firstName", new Condition().withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue().withS(firstName))); keyConditions.put("lastName", new Condition().withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue().withS(lastName))); queryRequest.setKeyConditions(keyConditions); QueryResult result = dynamoDb.query(queryRequest); List<Map<String, AttributeValue>> items = result.getItems(); return toUserPreferenceList(items); }
From source file:com.makariev.dynamodb.preferences.UserPreferenceObjectMapperService.java
License:Open Source License
private List<UserPreference> queryByFirstAndLastNameMixedLevel(String firstName, String lastName) { QueryRequest queryRequest = new QueryRequest().withTableName(UserPreference.TABLE_NAME) .withIndexName(UserPreference.NAME_INDEX).withSelect("ALL_ATTRIBUTES").withScanIndexForward(true); final HashMap<String, Condition> keyConditions = new HashMap<>(); //ComparisonOperator.CONTAINS) cannot be used with indexes ( no query, only scan ) keyConditions.put("firstName", new Condition().withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue().withS(firstName))); keyConditions.put("lastName", new Condition().withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue().withS(lastName))); queryRequest.setKeyConditions(keyConditions); final QueryResult result = dynamoDb.query(queryRequest); final List<Map<String, AttributeValue>> items = result.getItems(); final List<UserPreference> list = mapper.marshallIntoObjects(UserPreference.class, items); return list;/*from w ww .jav a 2s . co m*/ }
From source file:com.numenta.taurus.service.TaurusClient.java
License:Open Source License
/** * Get list of tweets for the given metric filtered by the given time range returning the * results as they become available asynchronously. * * @param metricName The metric name to retrieve the tweets from * @param from The start time (aggregated) inclusive. * @param to The end time (aggregated) inclusive. * @param callback Callback for asynchronous call. It will be called on every {@link Tweet} *///from ww w . ja v a2s. c o m public void getTweets(String metricName, Date from, Date to, DataCallback<Tweet> callback) throws GrokException, IOException { if (metricName == null) { throw new ObjectNotFoundException("Cannot get tweets without metric name"); } final TaurusDataFactory dataFactory = TaurusApplication.getInstance().getDataFactory(); final SimpleDateFormat timestampFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US); timestampFormat.setTimeZone(TimeZone.getTimeZone("UTC")); // Key conditions Map<String, Condition> keyConditions = new HashMap<>(); // uid = modelId Condition modelIdCond = new Condition().withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue(metricName)); keyConditions.put("metric_name", modelIdCond); Condition timestampCondition; if (from != null && to != null) { // timestamp >= from and timestamp <=to timestampCondition = new Condition().withComparisonOperator(ComparisonOperator.BETWEEN) .withAttributeValueList(new AttributeValue().withS(timestampFormat.format(from)), new AttributeValue().withS(timestampFormat.format(to))); keyConditions.put("agg_ts", timestampCondition); } else if (from != null) { // timestamp >= from timestampCondition = new Condition().withComparisonOperator(ComparisonOperator.GT) .withAttributeValueList(new AttributeValue().withS(timestampFormat.format(from))); keyConditions.put("agg_ts", timestampCondition); } else if (to != null) { // timestamp <= to timestampCondition = new Condition().withComparisonOperator(ComparisonOperator.LT) .withAttributeValueList(new AttributeValue().withS(timestampFormat.format(to))); keyConditions.put("agg_ts", timestampCondition); } // Prepare query request QueryRequest query = new QueryRequest().withTableName(TWEETS_TABLE) .withAttributesToGet("tweet_uid", "userid", "text", "username", "agg_ts", "created_at", "retweet_count") .withKeyConditions(keyConditions).withScanIndexForward(false) .withIndexName("taurus.metric_data-metric_name_index"); QueryResult result; String tweetId; String userId; String userName; String text; Date created; Date aggregated; AttributeValue retweet; int retweetCount; Map<String, AttributeValue> lastKey; try { do { // Get results result = _awsClient.query(query); for (Map<String, AttributeValue> item : result.getItems()) { tweetId = item.get("tweet_uid").getS(); userId = item.get("userid").getS(); text = item.get("text").getS(); userName = item.get("username").getS(); aggregated = DataUtils.parseGrokDate(item.get("agg_ts").getS()); created = DataUtils.parseGrokDate(item.get("created_at").getS()); // "retweet_count" is optional retweet = item.get("retweet_count"); if (retweet != null && retweet.getN() != null) { retweetCount = Integer.parseInt(retweet.getN()); } else { retweetCount = 0; } if (!callback.onData(dataFactory.createTweet(tweetId, aggregated, created, userId, userName, text, retweetCount))) { // Canceled by the user break; } } // Make sure to get all pages lastKey = result.getLastEvaluatedKey(); query.setExclusiveStartKey(lastKey); } while (lastKey != null); } catch (AmazonClientException e) { // Wraps Amazon's unchecked exception as IOException throw new IOException(e); } }
From source file:com.numenta.taurus.service.TaurusClient.java
License:Open Source License
/** * Get Metric values only from DynamoDB/*from ww w. j a v a 2 s .com*/ * * @param modelId The model to get the data from * @param from The starting timestamp * @param to The ending timestamp * @param ascending Specifies ascending (true) or descending (false) * @param callback User defined callback to receive data */ public void getMetricValues(@NonNull String modelId, @NonNull Date from, @NonNull Date to, boolean ascending, @NonNull MetricValuesCallback callback) throws GrokException, IOException { // Get metric from cache ConcurrentSkipListMap<Long, CachedMetricValue> cache = _cachedMetricValues.get(modelId); if (cache == null) { cache = new ConcurrentSkipListMap<>(); ConcurrentSkipListMap<Long, CachedMetricValue> oldValues = _cachedMetricValues.putIfAbsent(modelId, cache); if (oldValues != null) { // Found old cached values cache = oldValues; } } // Try to get metric values from cache ConcurrentNavigableMap<Long, CachedMetricValue> cached = cache.subMap(from.getTime(), true, to.getTime(), true); if (!cached.isEmpty()) { Log.d(TAG, "from=" + from.getTime() + ", firstKey=" + cache.firstKey()); Log.d(TAG, "to=" + to.getTime() + ", lastKey=" + cache.lastKey()); // Check if we found the values in the cache if (!cached.isEmpty()) { // Return cached values sorted based on "ascending" order Set<Map.Entry<Long, CachedMetricValue>> values; if (ascending) { values = cached.entrySet(); } else { values = cached.descendingMap().entrySet(); } for (Map.Entry<Long, CachedMetricValue> metricValue : values) { if (!callback.onData(modelId, metricValue.getKey(), metricValue.getValue().value, metricValue.getValue().anomaly)) { // Canceled by the user break; } } return; } } final SimpleDateFormat timestampFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US); timestampFormat.setTimeZone(TimeZone.getTimeZone("UTC")); // Key conditions Map<String, Condition> keyConditions = new HashMap<>(); // uid = modelId keyConditions.put("uid", new Condition().withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue(modelId))); // timestamp >= from and timestamp <=to Condition timestampCondition = new Condition().withComparisonOperator(ComparisonOperator.BETWEEN); if (from.compareTo(to) <= 0) { timestampCondition.withAttributeValueList(new AttributeValue().withS(timestampFormat.format(from)), new AttributeValue().withS(timestampFormat.format(to))); } else { // FIXME This should not happen. Log.e(TAG, "TaurusClient#getMetricValues: 'from date' should not be greater than 'to date"); timestampCondition.withAttributeValueList(new AttributeValue().withS(timestampFormat.format(to)), new AttributeValue().withS(timestampFormat.format(from))); } keyConditions.put("timestamp", timestampCondition); // Prepare query request QueryRequest query = new QueryRequest().withTableName(METRIC_DATA_TABLE) .withAttributesToGet("timestamp", "metric_value", "anomaly_score").withKeyConditions(keyConditions) .withScanIndexForward(ascending); QueryResult result; Map<String, AttributeValue> lastKey; try { do { long timestamp; // Get results result = _awsClient.query(query); for (Map<String, AttributeValue> item : result.getItems()) { CachedMetricValue metricValue = new CachedMetricValue(); timestamp = DataUtils.parseGrokDate(item.get("timestamp").getS()).getTime(); metricValue.value = Float.parseFloat(item.get("metric_value").getN()); metricValue.anomaly = Float.parseFloat(item.get("anomaly_score").getN()); cache.put(timestamp, metricValue); if (!callback.onData(modelId, timestamp, metricValue.value, metricValue.anomaly)) { // Canceled by the user break; } } // Make sure to get all pages lastKey = result.getLastEvaluatedKey(); query.setExclusiveStartKey(lastKey); } while (lastKey != null); } catch (AmazonClientException e) { // Wraps Amazon's unchecked exception as IOException throw new IOException(e); } }
From source file:com.numenta.taurus.service.TaurusClient.java
License:Open Source License
/** * Get hourly aggregated data for all instances for a single day for the given time range * * @param date The date to get the data from * @param fromHour The start hour/*ww w .j a va2s. com*/ * @param toHour The end hour * @param ascending Specifies ascending (true) or descending (false) * @param callback User defined callback to receive instance data */ public void getAllInstanceDataForDate(@NonNull Date date, int fromHour, int toHour, boolean ascending, @NonNull DataCallback<InstanceData> callback) throws GrokException, IOException { Map<String, Condition> keyConditions = new HashMap<>(); // Use "date" as hash key SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US); dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); keyConditions.put("date", new Condition().withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue(dateFormat.format(date)))); String start = fromHour > 9 ? Integer.toString(fromHour) : "0" + fromHour; if (fromHour == toHour) { // One single hour keyConditions.put("hour", new Condition().withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue(start))); } else { // Use "hour" as range key String end = toHour > 9 ? Integer.toString(toHour) : "0" + toHour; keyConditions.put("hour", new Condition().withComparisonOperator(ComparisonOperator.BETWEEN) .withAttributeValueList(new AttributeValue(start), new AttributeValue(end))); } // Prepare query request QueryRequest query = new QueryRequest().withTableName(INSTANCE_DATA_HOURLY_TABLE) .withAttributesToGet("instance_id", "date_hour", "anomaly_score").withKeyConditions(keyConditions) .withScanIndexForward(ascending).withIndexName("taurus.instance_data_hourly-date_hour_index"); Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); QueryResult result; String instanceId; float anomalyScore; float score; Map<String, AttributeValue> scores; Map<String, AttributeValue> lastKey; Matcher match; EnumSet<MetricType> metricMask; TaurusDataFactory dataFactory = TaurusApplication.getInstance().getDataFactory(); try { do { // Get data from DynamoDB result = _awsClient.query(query); for (Map<String, AttributeValue> item : result.getItems()) { // Convert "date_hour" to java milliseconds time match = DATE_HOUR_FORMAT_REGEX.matcher(item.get("date_hour").getS()); if (match.matches()) { calendar.clear(); calendar.set(Integer.parseInt(match.group(1)), Integer.parseInt(match.group(2)) - 1, Integer.parseInt(match.group(3)), Integer.parseInt(match.group(4)), 0, 0); instanceId = item.get("instance_id").getS(); // Get max anomaly scores scores = item.get("anomaly_score").getM(); anomalyScore = 0; double scaledScore; metricMask = EnumSet.noneOf(MetricType.class); for (Map.Entry<String, AttributeValue> entry : scores.entrySet()) { score = Float.parseFloat(entry.getValue().getN()); scaledScore = DataUtils.logScale(Math.abs(score)); if (scaledScore >= TaurusApplication.getYellowBarFloor()) { metricMask.add(MetricType.valueOf(entry.getKey())); } anomalyScore = Math.max(score, anomalyScore); } if (!callback.onData(dataFactory.createInstanceData(instanceId, AggregationType.Day, calendar.getTimeInMillis(), anomalyScore, metricMask))) { // Canceled by the user break; } } } // Make sure to get all pages lastKey = result.getLastEvaluatedKey(); query.setExclusiveStartKey(lastKey); } while (lastKey != null); } catch (AmazonClientException e) { // Wraps Amazon's unchecked exception as IOException throw new IOException(e); } }
From source file:com.rapid7.diskstorage.dynamodb.DynamoDBStore.java
License:Open Source License
private QueryRequest createQueryRequest(StaticBuffer hashKey, SliceQuery rangeQuery, boolean consistentRead, String tableName) {/*from w ww . ja v a 2 s . c o m*/ Expression keyConditionExpression = new ConditionExpressionBuilder().hashKey(hashKey) .rangeKey(rangeQuery.getSliceStart(), rangeQuery.getSliceEnd()).build(); final QueryRequest request = new QueryRequest().withConsistentRead(consistentRead).withTableName(tableName) .withKeyConditionExpression(keyConditionExpression.getConditionExpression()) .withExpressionAttributeValues(keyConditionExpression.getAttributeValues()) .withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL); return request; }
From source file:com.trk.aboutme.DynamoDB.DynamoDBManagerBooks.java
License:Open Source License
public static ArrayList<Books> getBooksList(String tableName, String catagory) { AmazonDynamoDBClient ddb = Shelf.clientManager.ddb(); try {//from w w w . j a v a 2 s. com String bookId = catagory; long twoWeeksAgoMilli = (new Date()).getTime() - (5L * 24L * 60L * 60L * 1000L); Date twoWeeksAgo = new Date(); twoWeeksAgo.setTime(twoWeeksAgoMilli); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); String twoWeeksAgoStr = df.format(twoWeeksAgo); ArrayList<Books> resultList = new ArrayList<Books>(); Map<String, AttributeValue> lastEvaluatedKey = null; do { Condition hashKeyCondition = new Condition() .withComparisonOperator(ComparisonOperator.EQ.toString()) .withAttributeValueList(new AttributeValue().withS(bookId)); Condition rangeKeyCondition = new Condition() .withComparisonOperator(ComparisonOperator.GT.toString()) .withAttributeValueList(new AttributeValue().withS(twoWeeksAgoStr)); Map<String, Condition> keyConditions = new HashMap<String, Condition>(); keyConditions.put("bookID", hashKeyCondition); keyConditions.put("posteddate", rangeKeyCondition); QueryRequest queryRequest = new QueryRequest().withTableName(tableName) .withKeyConditions(keyConditions).withSelect("title, datecreated").withLimit(5) .withExclusiveStartKey(lastEvaluatedKey); QueryResult result = ddb.query(queryRequest); for (Map<String, AttributeValue> item : result.getItems()) { for (Map.Entry<String, AttributeValue> i : item.entrySet()) { Books b = new Books(); AttributeValue value = i.getValue(); String vs = value.getS(); if (i.getKey().equals("title")) b.setM_title(vs == null ? "" : vs); if (i.getKey().equals("datecreated")) b.setM_dateCreated(vs == null ? "" : vs); resultList.add(b); } } lastEvaluatedKey = result.getLastEvaluatedKey(); } while (lastEvaluatedKey != null); return resultList; } catch (AmazonServiceException ex) { Shelf.clientManager.wipeCredentialsOnAuthError(ex); } return null; }
From source file:com.trk.aboutme.DynamoDB.DynamoDBManagerBooks.java
License:Open Source License
public static ArrayList<Pages> getPageList(String tableName, String catagory, String book) { AmazonDynamoDBClient ddb = Shelf.clientManager.ddb(); try {// ww w.j a v a 2 s. com String pageId = catagory + "#" + book; long twoWeeksAgoMilli = (new Date()).getTime() - (5L * 24L * 60L * 60L * 1000L); Date twoWeeksAgo = new Date(); twoWeeksAgo.setTime(twoWeeksAgoMilli); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); String twoWeeksAgoStr = df.format(twoWeeksAgo); ArrayList<Pages> resultList = new ArrayList<Pages>(); Map<String, AttributeValue> lastEvaluatedKey = null; do { Condition hashKeyCondition = new Condition() .withComparisonOperator(ComparisonOperator.EQ.toString()) .withAttributeValueList(new AttributeValue().withS(pageId)); Condition rangeKeyCondition = new Condition() .withComparisonOperator(ComparisonOperator.GT.toString()) .withAttributeValueList(new AttributeValue().withS(twoWeeksAgoStr)); Map<String, Condition> keyConditions = new HashMap<String, Condition>(); keyConditions.put("pageID", hashKeyCondition); keyConditions.put("datecreated", rangeKeyCondition); QueryRequest queryRequest = new QueryRequest().withTableName(tableName) .withKeyConditions(keyConditions).withSelect("title, message, authorphoto, posteddate") .withLimit(5).withExclusiveStartKey(lastEvaluatedKey); QueryResult result = ddb.query(queryRequest); for (Map<String, AttributeValue> item : result.getItems()) { for (Map.Entry<String, AttributeValue> i : item.entrySet()) { Pages p = new Pages(); AttributeValue value = i.getValue(); String vs = value.getS(); if (i.getKey().equals("title")) p.setM_title(vs == null ? "" : vs); if (i.getKey().equals("message")) p.setM_message(vs == null ? "" : vs); if (i.getKey().equals("authorphoto")) p.setM_authorPhoto(vs == null ? "" : vs); if (i.getKey().equals("posteddate")) p.setM_postedDate(vs == null ? "" : vs); resultList.add(p); } } lastEvaluatedKey = result.getLastEvaluatedKey(); } while (lastEvaluatedKey != null); return resultList; } catch (AmazonServiceException ex) { Shelf.clientManager.wipeCredentialsOnAuthError(ex); } return null; }
From source file:com.tsatsatzu.subwar.game.logic.dynamo.DynamoIODriver.java
License:Apache License
@Override public List<SWUserBean> getTopUsers(int total) { List<SWUserBean> top = new ArrayList<>(); QueryRequest queryRequest = new QueryRequest().withTableName(USER_TABLE_NAME).withLimit(total) .withScanIndexForward(false); QueryResult result = mClient.query(queryRequest); for (Map<String, AttributeValue> item : result.getItems()) { SWUserBean user = new SWUserBean(); user.fromMap(item);/* w ww .j a v a 2 s. c om*/ if (mUserCache.containsKey(user.getUserID())) { user = mUserCache.get(user.getUserID()); user.fromMap(item); } else mUserCache.put(user.getUserID(), user); mUserCacheFetch.put(user.getUserID(), System.currentTimeMillis()); top.add(user); } return top; }
From source file:Database.CustomerData.java
License:Open Source License
public static String QueryCustomer(String customerId) throws Exception { String r = null;/* w ww. j a va 2s . c o m*/ init(); try { Condition hashKeyCondition = new Condition().withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue().withS(customerId)); Map<String, Condition> keyConditions = new HashMap<String, Condition>(); keyConditions.put("Id", hashKeyCondition); QueryRequest queryRequest = new QueryRequest().withTableName("MemberData") .withKeyConditions(keyConditions); System.out.println(customerId); QueryResult result = dynamoDB.query(queryRequest); for (Map<String, AttributeValue> item : result.getItems()) { r = result.toString(); } } catch (AmazonServiceException ase) { System.err.println("Failed to find item in " + tableName + " " + ase); } return r; }