use of com.ibm.watson.discovery.v2.model.QueryResult in project jcabi-dynamo by jcabi.
the class QueryValveTest method fetchesData.
/**
* QueryValve can fetch data from AWS.
* @throws Exception If some problem inside
*/
@Test
@SuppressWarnings("unchecked")
public void fetchesData() throws Exception {
final Valve valve = new QueryValve();
final Credentials credentials = Mockito.mock(Credentials.class);
final ImmutableMap<String, AttributeValue> item = new ImmutableMap.Builder<String, AttributeValue>().build();
final AmazonDynamoDB aws = Mockito.mock(AmazonDynamoDB.class);
Mockito.doReturn(aws).when(credentials).aws();
Mockito.doReturn(new QueryResult().withItems(Collections.<Map<String, AttributeValue>>singletonList(item)).withConsumedCapacity(new ConsumedCapacity().withCapacityUnits(1.0d))).when(aws).query(Mockito.any(QueryRequest.class));
final Dosage dosage = valve.fetch(credentials, "table", new Conditions(), new ArrayList<String>(0));
MatcherAssert.assertThat(dosage.hasNext(), Matchers.is(false));
MatcherAssert.assertThat(dosage.items(), Matchers.hasItem(item));
}
use of com.ibm.watson.discovery.v2.model.QueryResult in project jcabi-dynamo by jcabi.
the class QueryValve method fetch.
// @checkstyle ParameterNumber (5 lines)
@Override
public Dosage fetch(final Credentials credentials, final String table, final Map<String, Condition> conditions, final Collection<String> keys) throws IOException {
final AmazonDynamoDB aws = credentials.aws();
try {
final Collection<String> attrs = new HashSet<String>(Arrays.asList(this.attributes));
attrs.addAll(keys);
QueryRequest request = new QueryRequest().withTableName(table).withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL).withKeyConditions(conditions).withConsistentRead(this.consistent).withScanIndexForward(this.forward).withSelect(this.select).withLimit(this.limit);
if (this.select.equals(Select.SPECIFIC_ATTRIBUTES.toString())) {
request = request.withAttributesToGet(attrs);
}
if (!this.index.isEmpty()) {
request = request.withIndexName(this.index);
}
final long start = System.currentTimeMillis();
final QueryResult result = aws.query(request);
Logger.info(this, // @checkstyle LineLength (1 line)
"#items(): loaded %d item(s) from '%s' and stopped at %s, using %s, %s, in %[ms]s", result.getCount(), table, result.getLastEvaluatedKey(), conditions, new PrintableConsumedCapacity(result.getConsumedCapacity()).print(), System.currentTimeMillis() - start);
return new QueryValve.NextDosage(credentials, request, result);
} catch (final AmazonClientException ex) {
throw new IOException(String.format("Failed to fetch from \"%s\" by %s and %s", table, conditions, keys), ex);
} finally {
aws.shutdown();
}
}
use of com.ibm.watson.discovery.v2.model.QueryResult in project java-sdk by watson-developer-cloud.
the class DiscoveryV2Example method main.
public static void main(String[] args) throws IOException {
Authenticator authenticator = new BearerTokenAuthenticator("{bearer_token}");
Discovery service = new Discovery("2019-11-22", authenticator);
service.setServiceUrl("{url}");
// This example assumes you have a project and collection set up which can accept documents.
// Paste those
// IDs below.
String projectId = "";
String collectionId = "";
// Add a new document to our collection. Fill in the file path with the file you want to send.
InputStream file = new FileInputStream("");
AddDocumentOptions addDocumentOptions = new AddDocumentOptions.Builder().projectId(projectId).collectionId(collectionId).file(file).filename("example-file").build();
DocumentAccepted addResponse = service.addDocument(addDocumentOptions).execute().getResult();
String documentId = addResponse.getDocumentId();
// Query your collection with the new document inside.
QueryOptions queryOptions = new QueryOptions.Builder().projectId(projectId).addCollectionIds(collectionId).naturalLanguageQuery(// Feel free to replace this to query something different.
"Watson").build();
QueryResponse queryResponse = service.query(queryOptions).execute().getResult();
System.out.println(queryResponse.getMatchingResults() + " results were returned by the query!");
// See if the added document got returned by the query.
for (QueryResult result : queryResponse.getResults()) {
if (result.getDocumentId().equals(documentId)) {
System.out.println("Our new document matched the query!");
}
}
// Delete our uploaded document from the collection.
DeleteDocumentOptions deleteDocumentOptions = new DeleteDocumentOptions.Builder().projectId(projectId).collectionId(collectionId).documentId(documentId).build();
service.deleteDocument(deleteDocumentOptions).execute();
}
use of com.ibm.watson.discovery.v2.model.QueryResult in project geowave by locationtech.
the class AsyncPaginatedQuery method nextIterator.
/**
* Get the next query data If the last request is equal to null then we have no more query
* requests to fire
*
* <p> If asyncQueryResults is not empty, we have already fetched the next query data that can be
* read immediately
*
* <p> If due to max async query limit, we couldn't fire async requests, we fire the request now
*/
@Override
protected Iterator<? extends Map<String, AttributeValue>> nextIterator(final int arg0) {
synchronized (monitorLock) {
if ((lastRequest == null) && asyncQueryResults.isEmpty()) {
return null;
}
QueryResult result = null;
if ((lastRequest != null) && (asyncRequestsInProgress == 0)) {
makeAsyncQuery();
}
while (asyncQueryResults.isEmpty()) {
try {
monitorLock.wait();
} catch (final InterruptedException e) {
LOGGER.error("Exception in Async paginated query " + e);
e.printStackTrace();
}
}
result = asyncQueryResults.remove();
return result == null ? null : result.getItems().iterator();
}
}
use of com.ibm.watson.discovery.v2.model.QueryResult in project aws-sdk-android by aws-amplify.
the class Search method getNextQueryResultSet.
private List<Document> getNextQueryResultSet() {
final List<Document> returnValue = new ArrayList<Document>();
final QueryRequest request = new QueryRequest();
request.withExclusiveStartKey(nextKey).withAttributesToGet(attributesToGet).withLimit(limit).withTableName(tableName).withConsistentRead(isConsistentRead).withIndexName(this.indexName);
if (select != null) {
request.withSelect(select);
}
Expression.applyExpression(request, table, keyExpression, filterExpression);
if (this.filter != null) {
final Map<String, Condition> keyConditions = getKeyConditions((QueryFilter) this.filter, request.getIndexName());
final Map<String, Condition> filterConditions = getFilterConditions((QueryFilter) this.filter, request.getIndexName());
if (!keyConditions.isEmpty()) {
request.withKeyConditions(keyConditions);
}
if (!filterConditions.isEmpty()) {
request.withQueryFilter(filterConditions);
}
} else {
request.withKeyConditions(null).withQueryFilter(null);
}
if (request.getQueryFilter() != null && request.getQueryFilter().size() > 1) {
request.withConditionalOperator(this.conditionalOperator);
} else {
request.withConditionalOperator((String) null);
}
Table.appendDynamoDBDocumentUserAgentString(request);
final QueryResult result = table.getClient().query(request);
for (final Map<String, AttributeValue> item : result.getItems()) {
final Document doc = Document.fromAttributeMap(item);
returnValue.add(doc);
if (this.collectResults) {
this.matches.add(doc);
}
}
nextKey = result.getLastEvaluatedKey();
if (nextKey == null || nextKey.size() == 0) {
isDone = true;
}
return returnValue;
}
Aggregations