/** * A string informing Amazon SimpleDB where to start the next list of <code>ItemNames</code>. * * @param nextToken * A string informing Amazon SimpleDB where to start the next list of <code>ItemNames</code>. * @return Returns a reference to this object so that method calls can be chained together. */ public SelectRequest withNextToken(String nextToken) { setNextToken(nextToken); return this; }
/** * The expression used to query the domain. * * @param selectExpression * The expression used to query the domain. * @return Returns a reference to this object so that method calls can be chained together. */ public SelectRequest withSelectExpression(String selectExpression) { setSelectExpression(selectExpression); return this; }
/** * Constructs a new SelectRequest object. Callers should use the setter or fluent setter (with...) methods to * initialize any additional object members. * * @param selectExpression * The expression used to query the domain. * @param consistentRead * Determines whether or not strong consistency should be enforced when data is read from SimpleDB. If * <code>true</code>, any data previously written to SimpleDB will be returned. Otherwise, results will be * consistent eventually, and the client may not see data that was written immediately before your read. */ public SelectRequest(String selectExpression, Boolean consistentRead) { setSelectExpression(selectExpression); setConsistentRead(consistentRead); }
@Override public int hashCode() { final int prime = 31; int hashCode = 1; hashCode = prime * hashCode + ((getSelectExpression() == null) ? 0 : getSelectExpression().hashCode()); hashCode = prime * hashCode + ((getNextToken() == null) ? 0 : getNextToken().hashCode()); hashCode = prime * hashCode + ((getConsistentRead() == null) ? 0 : getConsistentRead().hashCode()); return hashCode; }
public List<Item> getItems(){ AmazonSimpleDBClient client = new AmazonSimpleDBClient(...); List<Item> items = new ArrayList<>(); String nextToken = null; do { final SelectRequest request = new SelectRequest(); request.setSelectExpression("MY SELECT QUERY"); // SimpleDB can paginate the result. For paginated result NextToken value will be not null request.setNextToken(nextToken); nextToken = null; SelectResult result = client.select(request); if (result != null) { nextToken = (result.getNextToken() != null) ? result.getNextToken() : null; items.addAll(result.getItems()); } } while (nextToken != null); return items; }
/** * Performs select expression. This expression must be in format which is understandable to SimpleDB database * @param selectExpression * @param columns * @return Iterator of List<String> results */ @Override public SelectResult performSelect(String selectExpression, String nextToken) throws TranslatorException{ try { SelectRequest selectRequest = new SelectRequest(selectExpression); if (nextToken != null) { selectRequest.setNextToken(nextToken); } selectRequest.setConsistentRead(true); return client.select(selectRequest); } catch (AmazonServiceException e) { throw new TranslatorException(e); } catch (AmazonClientException e) { throw new TranslatorException(e); } }
/** * Get the set of all nodes in the cluster * * @param app Cluster name * @return the set of all instances in the given {@code app} */ public Set<PriamInstance> getAllIds(String app) { AmazonSimpleDB simpleDBClient = getSimpleDBClient(); Set<PriamInstance> inslist = new HashSet<>(); String nextToken = null; do { SelectRequest request = new SelectRequest(String.format(ALL_QUERY, app)); request.setNextToken(nextToken); SelectResult result = simpleDBClient.select(request); nextToken = result.getNextToken(); for (Item item : result.getItems()) { inslist.add(transform(item)); } } while (nextToken != null); return inslist; }
/** * Get the instance details from SimpleDB * * @param app Cluster name * @param id Node ID * @return the node with the given {@code id}, or {@code null} if no such node exists */ public PriamInstance getInstance(String app, String dc, int id) { AmazonSimpleDB simpleDBClient = getSimpleDBClient(); SelectRequest request = new SelectRequest(String.format(INSTANCE_QUERY, app, dc, id)); SelectResult result = simpleDBClient.select(request); if (result.getItems().size() == 0) return null; return transform(result.getItems().get(0)); }
/** * Simple wrapper around executing a query. * @param db * @param query * @param nextToken * @param consistentRead true to read consistently, false to use eventual consistency * @return * @throws AmazonClientException */ public static SelectResult selectItems(AmazonSimpleDB db, String query, String nextToken, boolean consistentRead) throws AmazonClientException { SelectResult results = db.select(new SelectRequest() .withConsistentRead(consistentRead) .withSelectExpression(query) .withNextToken(nextToken)); return results; }
@Override public int hashCode() { final int prime = 31; int hashCode = 1; hashCode = prime * hashCode + ((getSelectExpression() == null) ? 0 : getSelectExpression().hashCode()); hashCode = prime * hashCode + ((getNextToken() == null) ? 0 : getNextToken().hashCode()); hashCode = prime * hashCode + ((isConsistentRead() == null) ? 0 : isConsistentRead().hashCode()); return hashCode; }
/** * Determines whether or not strong consistency should be enforced when data is read from SimpleDB. If * <code>true</code>, any data previously written to SimpleDB will be returned. Otherwise, results will be * consistent eventually, and the client may not see data that was written immediately before your read. * * @param consistentRead * Determines whether or not strong consistency should be enforced when data is read from SimpleDB. If * <code>true</code>, any data previously written to SimpleDB will be returned. Otherwise, results will be * consistent eventually, and the client may not see data that was written immediately before your read. * @return Returns a reference to this object so that method calls can be chained together. */ public SelectRequest withConsistentRead(Boolean consistentRead) { setConsistentRead(consistentRead); return this; }
@Override public void initialize(final String asgName, final String region) { super.initialize(asgName, region); // End point is us-east-1 AmazonSimpleDB simpleDBClient = AmazonSimpleDBClient.builder() .withCredentials(provider.getAwsCredentialProvider()) .build(); String nextToken = null; String appid = asgName.lastIndexOf('-') > 0 ? asgName.substring(0, asgName.indexOf('-')) : asgName; logger.info("appid used to fetch properties is: {}", appid); do { String ALL_QUERY = "select * from " + DOMAIN + " where " + Attributes.APP_ID + "='%s'"; SelectRequest request = new SelectRequest(String.format(ALL_QUERY, appid)); request.setNextToken(nextToken); SelectResult result = simpleDBClient.select(request); nextToken = result.getNextToken(); for (Item item : result.getItems()) addProperty(item); } while (nextToken != null); }
String id = request.getParameter("userid"); if (id !- null && id.length() > 0) { SelectRequest request1 = new SelectRequest( "select * from Member where Account = '" + id + "'", true ); } else { // Do something }
/** * Runs a query on the passed in domain with the passed in whereClause. If the nextToken is included for pagination. * * @param db * @param domainName * @param whereClause * @param nextToken * @param consistentRead true to read consistently, false to use eventual consistency * @return * @throws AmazonClientException */ public static SelectResult selectItems(AmazonSimpleDB db, String domainName, String whereClause, String nextToken, boolean consistentRead) throws AmazonClientException { String selectExpression = "select * from `" + domainName + "`"; if(whereClause != null) { selectExpression += " where " + whereClause; } SelectResult results = db.select(new SelectRequest() .withConsistentRead(consistentRead) .withSelectExpression(selectExpression) .withNextToken(nextToken)); return results; }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (obj instanceof SelectRequest == false) return false; SelectRequest other = (SelectRequest) obj; if (other.getSelectExpression() == null ^ this.getSelectExpression() == null) return false; if (other.getSelectExpression() != null && other.getSelectExpression().equals(this.getSelectExpression()) == false) return false; if (other.getNextToken() == null ^ this.getNextToken() == null) return false; if (other.getNextToken() != null && other.getNextToken().equals(this.getNextToken()) == false) return false; if (other.getConsistentRead() == null ^ this.getConsistentRead() == null) return false; if (other.getConsistentRead() != null && other.getConsistentRead().equals(this.getConsistentRead()) == false) return false; return true; }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (obj instanceof SelectRequest == false) return false; SelectRequest other = (SelectRequest)obj; if (other.getSelectExpression() == null ^ this.getSelectExpression() == null) return false; if (other.getSelectExpression() != null && other.getSelectExpression().equals(this.getSelectExpression()) == false) return false; if (other.getNextToken() == null ^ this.getNextToken() == null) return false; if (other.getNextToken() != null && other.getNextToken().equals(this.getNextToken()) == false) return false; if (other.isConsistentRead() == null ^ this.isConsistentRead() == null) return false; if (other.isConsistentRead() != null && other.isConsistentRead().equals(this.isConsistentRead()) == false) return false; return true; }
/** * Determines whether or not strong consistency should be enforced when data is read from SimpleDB. If * <code>true</code>, any data previously written to SimpleDB will be returned. Otherwise, results will be * consistent eventually, and the client may not see data that was written immediately before your read. * * @param consistentRead * Determines whether or not strong consistency should be enforced when data is read from SimpleDB. If * <code>true</code>, any data previously written to SimpleDB will be returned. Otherwise, results will be * consistent eventually, and the client may not see data that was written immediately before your read. * @return Returns a reference to this object so that method calls can be chained together. */ public SelectRequest withConsistentRead(Boolean consistentRead) { setConsistentRead(consistentRead); return this; }
//1. Creating a SimpleDB Client AWSCredentials credentials = new BasicAWSCredentials( ACCESS_KEY, SECRET_KEY ); AmazonSimpleDBClient sdbClient = new AmazonSimpleDBClient( credentials); sdbClient.setEndpoint("sdb.us-west-1.amazonaws.com"); // to define Data Center Region //2. Select String nextToken = null; SelectRequest selectRequest = new SelectRequest( "select * from myDomain" ).withConsistentRead( true ); selectRequest.setNextToken( nextToken ); SelectResult response = sdbClient.select( selectRequest ); nextToken = response.getNextToken();
SelectRequest selectRequestNames = new SelectRequest("select followedName from Following where followedBy = '" + myName + "'").withConsistentRead(true); List<Item> names = getInstance().select(selectRequestNames).getItems(); String set = "("; for (int j = 0; j < names.size(); j++) { set += "'" + names.get(j).getAttributes().get(0).getValue() + "',"; } set = set.substring(0, set.length() - 1) + ")"; SelectRequest selectRequest = new SelectRequest("select itemName() from Quotes where fbName in " + set + " and timestamp is not null order by timestamp desc").withConsistentRead(true);
/** * A utility method for loading up all the items in a domain. * * @param db * @param domainName * @param consistentRead true to read consistently, false to use eventual consistency * @return * @throws AmazonClientException */ public static List<Item> listAllItems(AmazonSimpleDB db, String domainName, boolean consistentRead) throws AmazonClientException { SelectResult results = new SelectResult(); List<Item> items = new ArrayList<Item>(); do { results = db.select(new SelectRequest() .withConsistentRead(consistentRead) .withSelectExpression("select * from `" + domainName + "`") .withNextToken(results.getNextToken())); items.addAll(results.getItems()); } while(results.getNextToken() != null); return items; }