2015-05-09 9 views
5

Ho una tabella e la struttura si presenta così:DynamoDB: Come usare un filtro di query per verificare le condizioni in un MAP

DynamoDB document

Quando faccio una domanda, vorrei essere in grado fare un filtro di query sulla mappa dei dati; ma non sono esattamente sicuro su come impostare la query.

Questo è quello che ho finora:

HashMap<String, AttributeValue> map = new HashMap<String, AttributeValue>(); 
map.put("byUserId", new AttributeValue().withS("vl49uga5ljjcoln65rcaspmg8u")); 

queryExpression 
    .withQueryFilterEntry("data", new Condition() 
    .withAttributeValueList(new AttributeValue().withM(map)) 
    .withComparisonOperator(ComparisonOperator.CONTAINS)); 

ma il modo in cui sto costruendo il filtro non è corretto e continuo a correre nel seguente errore:

Exception in thread "main" com.amazonaws.AmazonServiceException: One or more parameter values were invalid: ComparisonOperator CONTAINS is not valid for M AttributeValue type (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: CHIOME68L1HVGO81URD7CIOS6BVV4KQNSO5AEMVJF66Q9ASUAAJG) 
    at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:1077) 
    at com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:725) 
    at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:460) 
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:295) 
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:3106) 
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.query(AmazonDynamoDBClient.java:1118) 

Allora, qual è un operatore di confronto che dovrei usare (poiché IN è per i tipi di lista), e come faccio a costruire il filtro di query in modo tale da poter specificare un confronto all'interno della MAP.

Grazie!

risposta

5

Provate a confrontare l'attributo mappa data.byUserId al posto di tutta la struttura mappa:

Table table = dynamoDB.getTable(tableName); 

Map<String, Object> expressionAttributeValues = new HashMap<String, Object>(); 
expressionAttributeValues.put(":x", "vl49uga5ljjcoln65rcaspmg8u"); 


QuerySpec spec = new QuerySpec() 
    .withHashKey("HashKeyAttribute", "HashKeyAttributeValue") 
    .withFilterExpression("data.byUserId = :x") 
    .withValueMap(expressionAttributeValues); 


ItemCollection<QueryOutcome> items = table.query(spec); 

Iterator<Item> iterator = items.iterator(); 

while (iterator.hasNext()) { 
    System.out.println(iterator.next().toJSONPretty()); 
} 

Alcune importanti linee guida:

  1. Assicurarsi che la variabile tableName ha il nome della tabella corretta rappresentato come un string.

  2. Assicurarsi di sostituire la stringa HashKeyAttribute con il nome dell'attributo che rappresenta lo Hash Key.

  3. Assicurarsi di sostituire la stringa HashKeyAttributeValue con il valore che rappresenta la chiave hash che si desidera abbinare.

  4. Verificare che il record corrispondente abbia il valore data.byUserId con il valore fornito nell'espressione di confronto. Nell'esempio è stato fornito il valore "vl49uga5ljjcoln65rcaspmg8u".

Ecco un altro esempio avere l'attributo id come il tasto cancelletto:

Table table = dynamoDB.getTable(tableName); 

Map<String, Object> expressionAttributeValues = new HashMap<String, Object>(); 
expressionAttributeValues.put(":x", "vl49uga5ljjcoln65rcaspmg8u"); 


QuerySpec spec = new QuerySpec() 
    .withHashKey("id", "25g77vmummpr4mc5mb9vq36q43") 
    .withFilterExpression("data.byUserId = :x") 
    .withValueMap(expressionAttributeValues); 


ItemCollection<QueryOutcome> items = table.query(spec); 

Iterator<Item> iterator = items.iterator(); 

while (iterator.hasNext()) { 
    System.out.println(iterator.next().toJSONPretty()); 
} 
+0

Grazie per la risposta, ma purtroppo non ha funzionato. :( – ThePedestrian

+0

Potete fornire maggiori dettagli? – bsd

+0

Qual è il risultato finale con la risposta fornita? Errore? Nessun risultato restituito? – bsd

Problemi correlati