public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{ try{ String customerID = request.getParameter("customerID"); BasicAWSCredentials awsCredentials = new BasicAWSCredentials("test", "test"); AmazonSimpleDBClient sdbc = new AmazonSimpleDBClient(awsCredentials); String query = "select * from invoices where customerID = '" + customerID; SelectResult sdbResult = sdbc.select(new SelectRequest(query)); //BAD SelectResult sdbResult2 = sdbc.select(new SelectRequest(query, false)); //BAD SelectRequest sdbRequest = new SelectRequest(); SelectResult sdbResult3 = sdbc.select(sdbRequest.withSelectExpression(query)); //BAD String query2 = "select * from invoices where customerID = 123"; SelectResult sdbResult4 = sdbc.select(new SelectRequest(query2)); //OK }catch(Exception e){ System.out.println(e); } }
/** * @{inheritDoc */ @Override public PagedDatabaseResult getPagedItems(String tableName, Object token, String minRange, String maxRange, String instanceId, String jobId) { List<Item> ret = new ArrayList<Item>(); String whereClause = null; if (minRange != null && maxRange != null) { whereClause = " Timestamp between '" + minRange + "' and '" + maxRange + "' "; } else if (minRange != null) { whereClause = " Timestamp >= '" + minRange + "' "; } else if (maxRange != null) { whereClause = " Timestamp < '" + maxRange + "' "; } else { whereClause = ""; } SelectRequest request = new SelectRequest("SELECT * from `" + tableName + "`" + whereClause).withConsistentRead(true); String nextToken = (String) token; request.withNextToken(nextToken); SelectResult result = db.select(request); for (com.amazonaws.services.simpledb.model.Item item : result.getItems()) { ret.add(resultToItem(item)); } nextToken = result.getNextToken(); return new PagedDatabaseResult(ret, result.getNextToken()); }
@Override public ImmutableSet<CassandraInstance> findAll(String ring) { List<CassandraInstance> instances = Lists.newArrayList(); String nextToken = null; do { SelectRequest request = new SelectRequest(String.format(ALL_QUERY, domain(ring))) .withNextToken(nextToken); SelectResult result = client.select(request); for (Item item : result.getItems()) { instances.add(transform(item)); } nextToken = result.getNextToken(); } while (nextToken != null); return ImmutableSet.copyOf(instances); }
@Test public void findAll() { List<Item> items = createItems(); SelectResult result = createMock(SelectResult.class); Capture<SelectRequest> requestCapture = new Capture<SelectRequest>(); expect(domainFactory.createFromRing(RING_NAME)).andReturn(domain(RING_NAME)); expect(simpleDbClient.select(capture(requestCapture))).andReturn(result); expect(result.getItems()).andReturn(items); expect(result.getNextToken()).andReturn(null); replayAll(); Set<CassandraInstance> expected = transform(items); assertEquals(expected, dao.findAll(RING_NAME)); assertEquals(ALL_QUERY, requestCapture.getValue().getSelectExpression()); assertNull(requestCapture.getValue().getNextToken()); }
@Test public void findById() { List<Item> items = ImmutableList.of(createItem(1)); SelectResult result = createMock(SelectResult.class); Capture<SelectRequest> requestCapture = new Capture<SelectRequest>(); expect(domainFactory.createFromRing(RING_NAME)).andReturn(domain(RING_NAME)); expect(simpleDbClient.select(capture(requestCapture))).andReturn(result); expect(result.getItems()).andStubReturn(items); replayAll(); Set<CassandraInstance> expected = transform(items); assertEquals(expected.iterator().next(), dao.findById(RING_NAME, ID)); assertEquals(String.format(SdbCassandraInstanceDao.INSTANCE_QUERY, DOMAIN, ID), requestCapture.getValue().getSelectExpression()); assertNull(requestCapture.getValue().getNextToken()); }
private <T> long invokeCountImpl(boolean consistentRead, SimpleDbEntityInformation<T, ?> entityInformation, final String countQuery) { LOGGER.debug("Count items for query " + countQuery); validateSelectQuery(countQuery); final String escapedQuery = getEscapedQuery(countQuery, entityInformation); final SelectResult selectResult = invokeFindImpl(consistentRead, escapedQuery); for (Item item : selectResult.getItems()) { if (item.getName().equals("Domain")) { for (Attribute attribute : item.getAttributes()) { if (attribute.getName().equals("Count")) { return Long.parseLong(attribute.getValue()); } } } } return 0; }
@Test public void testGetAllItemsCorrectlyCallsSimpleDB() throws Exception { // ARRANGE initialiseOptimisticPersister(); SelectRequest selectRequest = new SelectRequest(); selectRequest.setConsistentRead(true); selectRequest.setSelectExpression("select * from `" + testSimpleDBDomainName + "`"); // Configure select result with an item to be returned: SelectResult selectResult = new SelectResult(); Set<Item> items = new HashSet<>(); Item item = new Item(); String itemDate = "2016-07-23"; item.setName(itemDate); item.setAttributes(allAttributes); items.add(item); selectResult.setItems(items); mockery.checking(new Expectations() { { oneOf(mockSimpleDBClient).select(with(equal(selectRequest))); will(returnValue(selectResult)); } }); List<ImmutablePair<String, List<Attribute>>> expectedItems = new ArrayList<>(); ImmutablePair<String, List<Attribute>> pair = new ImmutablePair<>(itemDate, new ArrayList<>( activeNonVersionAttributes)); expectedItems.add(pair); // ACT List<ImmutablePair<String, List<Attribute>>> actualItems = optimisticPersister.getAllItems(); // ASSERT assertTrue("OptimisticPersister should return the correct items. Actual: " + actualItems + ", Expected: " + expectedItems, actualItems.equals(expectedItems)); }
public void execute() { SelectRequest request = new SelectRequest() .withSelectExpression(determineSelectExpression()) .withConsistentRead(determineConsistentRead()) .withNextToken(determineNextToken()); log.trace("Sending request [{}] for exchange [{}]...", request, exchange); SelectResult result = this.sdbClient.select(request); log.trace("Received result [{}]", result); Message msg = getMessageForResponse(exchange); msg.setHeader(SdbConstants.ITEMS, result.getItems()); msg.setHeader(SdbConstants.NEXT_TOKEN, result.getNextToken()); }
@Override public SelectResult select(SelectRequest selectRequest) throws AmazonServiceException, AmazonClientException { this.selectRequest = selectRequest; SelectResult result = new SelectResult(); result.setNextToken("TOKEN2"); result.getItems().add(new Item("ITEM1", null)); result.getItems().add(new Item("ITEM2", null)); return result; }
private Iterator<Item> searchAmazonSimpleDB(boolean users,Filter filter,ArrayList<Attribute> attributes) { StringBuffer sqlWhere = new StringBuffer(); ArrayList<Object> vals = new ArrayList<Object>(); this.stringFilter(filter.getRoot(),sqlWhere, vals); StringBuffer SQL = new StringBuffer(); SQL.append("SELECT "); if (attributes.size() == 0) { SQL.append("* "); } else if (attributes.size() == 1 && attributes.get(0).equals("*")) { SQL.append("* "); } else if (attributes.size() == 1 && attributes.get(0).getAttribute().getName().equals("1.1")) { SQL.append("uid "); } else { for(Attribute attr : attributes) { SQL.append(attr.getAttribute().getName()).append(','); } SQL.setLength(SQL.length() - 1); } SQL.append(" FROM ").append('`'); if (users) { SQL.append(this.userDomain); } else { SQL.append(this.groupDomain); } SQL.append("` WHERE ").append(sqlWhere); if (logger.isDebugEnabled()) { logger.debug("SQL : " + SQL.toString()); } SelectResult res = this.sdb.select(new SelectRequest(SQL.toString())); return res.getItems().iterator(); }
/** * @{inheritDoc */ @Override public boolean hasJobData(String tableName, String jobId) { boolean ret = false; if (hasTable(tableName)) { SelectRequest request = new SelectRequest("SELECT * from `" + tableName + "`"); SelectResult result = db.select(request); ret = !result.getItems().isEmpty(); } return ret; }
private<T> List<T> internalQuery(Class<T> dataClass, SelectRequest selectRequest) throws WPBIOException { List<T> result = new ArrayList<T>(); try { SelectResult selectResult = null; do { if (selectResult != null) { selectRequest.setNextToken(selectResult.getNextToken()); } selectResult = sdbClient.select(selectRequest); List<Item> items = selectResult.getItems(); for(Item item: items) { T t = copyAttributesToInstance(dataClass, item.getAttributes()); result.add(t); } } while (selectResult.getNextToken() != null); } catch (Exception e) { throw new WPBIOException("cannot get all records " + dataClass.getSimpleName(), e); } return result; }
@Test public void testSelect() throws Exception { SelectResult result = new SelectResult(); result.setItems(mockResult()); String query = "select * from item where attribute > 'name'"; Mockito.stub(connection.performSelect(Mockito.anyString(), Mockito.anyString())).toReturn(result); Command cmd = utility.parseCommand(query); ExecutionContext context = Mockito.mock(ExecutionContext.class); ResultSetExecution exec = translator.createResultSetExecution((Select)cmd, context, Mockito.mock(RuntimeMetadata.class), connection); exec.execute(); exec.next(); Mockito.verify(connection).performSelect("SELECT attribute, somedate, strarray FROM item WHERE attribute > 'name'", null); }
@Test public void testDirectExecution() throws Exception { SelectResult result = new SelectResult(); result.setItems(mockResult()); String query = "select * from item where attribute > 'name'"; Mockito.stub(connection.performSelect(Mockito.anyString(), Mockito.anyString())).toReturn(result); Command cmd = utility.parseCommand(query); ExecutionContext context = Mockito.mock(ExecutionContext.class); List<Argument> arguments = new ArrayList<Argument>(); Argument arg = new Argument(Direction.IN, String.class, Mockito.mock(ProcedureParameter.class)); arg.setArgumentValue(LanguageFactory.INSTANCE.createLiteral(query, String.class)); arguments.add(arg); ResultSetExecution exec = translator.createDirectExecution(arguments, cmd, context, Mockito.mock(RuntimeMetadata.class), connection); exec.execute(); List row = exec.next(); Mockito.verify(connection).performSelect("select * from item where attribute > 'name'", null); Object[] results = (Object[])row.get(0); assertEquals("a1", results[0]); assertEquals("[a2, a22]", results[1]); }
@Test @Ignore public void getAttributeNamesEmptyStringTest() throws Exception { DomainMetadataResult metadataResult = mock(DomainMetadataResult.class); SelectResult result = mock(SelectResult.class); // List itemsList = mock(ArrayList.class); // Iterator iterator = mock(Iterator.class); // Attribute // when(itemsList.iterator()).thenReturn(iterator); when(metadataResult.getAttributeNameCount()).thenReturn(3); when(client.select(any(SelectRequest.class))).thenReturn(result); // when(client.domainMetadata(any(DomainMetadataRequest.class))).thenReturn(metadataResult); System.out.println(simpleDbApi.getAttributeNames(null)); }
@Override public @Nullable CassandraInstance findById(String ring, int id) { SelectRequest request = new SelectRequest(String.format(INSTANCE_QUERY, domain(ring), id)); SelectResult result = client.select(request); if (result.getItems().size() == 0) { return null; } return transform(result.getItems().get(0)); }
@Test public void findAll_paginated() { List<Item> items1 = createItems(0); List<Item> items2 = createItems(10); SelectResult result = createMock(SelectResult.class); Capture<SelectRequest> requestCapture = new Capture<SelectRequest>(CaptureType.ALL); expect(domainFactory.createFromRing(RING_NAME)).andReturn(domain(RING_NAME)).times(2); expect(simpleDbClient.select(capture(requestCapture))).andReturn(result); expect(result.getItems()).andReturn(items1); expect(result.getNextToken()).andReturn(NEXT_TOKEN); expect(simpleDbClient.select(capture(requestCapture))).andReturn(result); expect(result.getItems()).andReturn(items2); expect(result.getNextToken()).andReturn(null); replayAll(); Set<CassandraInstance> expected = Sets.newHashSet( Iterables.concat(transform(items1), transform(items2))); assertEquals(expected, dao.findAll(RING_NAME)); List<SelectRequest> requests = requestCapture.getValues(); assertEquals(2, requests.size()); assertEquals(ALL_QUERY, requests.get(0).getSelectExpression()); assertNull(requests.get(0).getNextToken()); assertEquals(ALL_QUERY, requests.get(1).getSelectExpression()); assertEquals(NEXT_TOKEN, requests.get(1).getNextToken()); }
@Test public void findById_notFound() { List<Item> items = ImmutableList.of(); SelectResult result = createMock(SelectResult.class); Capture<SelectRequest> requestCapture = new Capture<SelectRequest>(); expect(domainFactory.createFromRing(RING_NAME)).andReturn(domain(RING_NAME)); expect(simpleDbClient.select(capture(requestCapture))).andReturn(result); expect(result.getItems()).andStubReturn(items); replayAll(); assertNull(dao.findById(RING_NAME, ID)); }
public List<T> populateDomainItems(SimpleDbEntityInformation<T, ?> entityInformation, SelectResult selectResult) { final List<T> allItems = new ArrayList<T>(); for(Item item : selectResult.getItems()) { allItems.add(populateDomainItem(entityInformation, item)); } return allItems; }
public SelectResult select(SelectRequest selectRequest) { SelectResult response = null; return response; }
@Override public List<ImmutablePair<String, List<Attribute>>> getAllItems() { if (!initialised) { throw new IllegalStateException("The optimistic persister has not been initialised"); } // Query database to get items List<ImmutablePair<String, List<Attribute>>> items = new ArrayList<>(); AmazonSimpleDB client = getSimpleDBClient(); SelectRequest selectRequest = new SelectRequest(); // N.B. Think if results are paged, second and subsequent pages will always // be eventually-consistent only. This is currently used only to back up the // database - so being eventually-consistent is good enough - after all - // even if we were fully consistent, someone could still add a new booking // right after our call anyway. selectRequest.setConsistentRead(true); // Query all items in the domain selectRequest.setSelectExpression("select * from `" + simpleDbDomainName + "`"); String nextToken = null; do { SelectResult selectResult = client.select(selectRequest); selectResult.getItems().forEach( item -> { List<Attribute> attributes = new ArrayList<>(); item.getAttributes() .stream() // Do not return the version attribute or inactive attributes .filter( attribute -> (!attribute.getName().equals(versionAttributeName) && !attribute .getValue().startsWith("Inactive"))).forEach(attribute -> { attributes.add(attribute); }); items.add(new ImmutablePair<>(item.getName(), attributes)); }); nextToken = selectResult.getNextToken(); selectRequest.setNextToken(nextToken); } while (nextToken != null); return items; }
protected void executeDirect(String sql, String next) throws TranslatorException { SelectResult result = connection.performSelect(sql, next); this.nextToken = result.getNextToken(); this.listIterator = result.getItems().iterator(); }
private void domainToCSV(String domainPrefix, File csvFile) { log.info("Storing data from SDB domains starting with " + domainPrefix + " to file " + csvFile); Set<String> attributes = null; List<String> domains = getDbClient().listDomains().getDomainNames(); int c = 0; for (String domainName : domains) { if (domainName.startsWith(domainPrefix)) { c++; log.info("Exporting from " + domainName + " (" + c + "/" + domains.size() + ")"); long domainCount = getDbClient().domainMetadata( new DomainMetadataRequest(domainName)).getItemCount(); if (domainCount < MIN_RESULTS) { log.info("Ignoring " + domainName + ", less than " + MIN_RESULTS + " entries."); continue; } if (attributes == null) { attributes = getSdbAttributes(getDbClient(), domainName, MIN_RESULTS); } long total = 0; String select = "select * from `" + domainName + "` limit 2500"; String nextToken = null; SelectResult res; do { res = getDbClient().select( new SelectRequest(select).withNextToken(nextToken) .withConsistentRead(false)); for (Item i : res.getItems()) { Map<String, Object> csvEntry = new HashMap<String, Object>(); csvEntry.put("_key", i.getName()); for (String attr : attributes) { csvEntry.put(attr, ""); } for (Attribute a : i.getAttributes()) { csvEntry.put(a.getName(), a.getValue()); } CSVExport.writeToFile(csvEntry, csvFile); } nextToken = res.getNextToken(); total += res.getItems().size(); log.info("Exported " + total + " of " + domainCount); } while (nextToken != null); log.info("Finished exporting from " + domainName); } } CSVExport.closeWriter(csvFile); }
private static Set<String> getSdbAttributes(AmazonSimpleDBClient client, String domainName, int sampleSize) { if (!client.listDomains().getDomainNames().contains(domainName)) { throw new IllegalArgumentException("SimpleDB domain '" + domainName + "' not accessible from given client instance"); } int domainCount = client.domainMetadata( new DomainMetadataRequest(domainName)).getItemCount(); if (domainCount < sampleSize) { throw new IllegalArgumentException("SimpleDB domain '" + domainName + "' does not have enough entries for accurate sampling."); } int avgSkipCount = domainCount / sampleSize; int processedCount = 0; String nextToken = null; Set<String> attributeNames = new HashSet<String>(); Random r = new Random(); do { int nextSkipCount = r.nextInt(avgSkipCount * 2) + 1; SelectResult countResponse = client.select(new SelectRequest( "select count(*) from `" + domainName + "` limit " + nextSkipCount).withNextToken(nextToken)); nextToken = countResponse.getNextToken(); processedCount += Integer.parseInt(countResponse.getItems().get(0) .getAttributes().get(0).getValue()); SelectResult getResponse = client.select(new SelectRequest( "select * from `" + domainName + "` limit 1") .withNextToken(nextToken)); nextToken = getResponse.getNextToken(); processedCount++; if (getResponse.getItems().size() > 0) { for (Attribute a : getResponse.getItems().get(0) .getAttributes()) { attributeNames.add(a.getName()); } } } while (domainCount > processedCount); return attributeNames; }
@Override public SelectResult invokeFindImpl(boolean consistentRead, String escapedQuery) { LOGGER.debug("Query: {}", escapedQuery); return getDB().select(new SelectRequest(escapedQuery, consistentRead)); }
@Override public <T> List<T> findImpl(SimpleDbEntityInformation<T, ?> entityInformation, String query, String nextToken, boolean consistentRead) { LOGGER.debug("Find All Domain \"{}\" isConsistent=\"{}\", with token!", entityInformation.getDomain(), consistentRead); final DomainItemBuilder<T> domainItemBuilder = new DomainItemBuilder<T>(); validateSelectQuery(query); final String escapedQuery = getEscapedQuery(query, entityInformation); SelectRequest selectRequest = new SelectRequest(escapedQuery, consistentRead); selectRequest.setNextToken(nextToken); final SelectResult selectResult = getDB().select(selectRequest); return domainItemBuilder.populateDomainItems(entityInformation, selectResult); }
private String getNextToken(String query, boolean consistentRead) { LOGGER.debug("Get next token for query: " + query); Assert.isTrue(query.contains("limit"), "Only queries with limit have a next token!"); final SelectResult selectResult = getDB().select(new SelectRequest(query, consistentRead)); return selectResult.getNextToken(); }
@Override public <T> List<T> recursiveFindImpl(Class<T> entityClass, String query, boolean consistentRead, SimpleDbEntityInformation<T, ?> entityInformation) { LOGGER.debug("Find All Domain \"{}\" isConsistent=\"{}\"", entityInformation.getDomain(), consistentRead); validateSelectQuery(query); final String escapedQuery = getEscapedQuery(query, entityInformation); List<T> result = new ArrayList<T>(); List<String> referenceFieldsNames = ReflectionUtils.getReferencedAttributeNames(entityClass); final DomainItemBuilder<T> domainItemBuilder = new DomainItemBuilder<T>(); final SelectResult selectResult = invokeFindImpl(consistentRead, escapedQuery); if (referenceFieldsNames.isEmpty()) { return domainItemBuilder.populateDomainItems(entityInformation, selectResult); } for (Item item : selectResult.getItems()) { T populatedItem = domainItemBuilder.populateDomainItem(entityInformation, item); result.add(populatedItem); for (Attribute attribute : item.getAttributes()) { if (!referenceFieldsNames.contains(attribute.getName())) { continue; } Class<?> referenceEntityClazz = ReflectionUtils.getFieldClass(entityClass, attribute.getName()); Object referenceEntity = read(attribute.getValue(), referenceEntityClazz); ReflectionUtils.callSetter(populatedItem, attribute.getName(), referenceEntity); } } return result; }
public abstract SelectResult invokeFindImpl(boolean consistentRead, String escapedQuery);