2012-02-13 7 views
5

La entidad que estoy consultando tiene un HashKey & a RangeKey (Número). Cuando uso batchGetItem en él, me sale el siguiente error:Error en la API batchGetItem en Java

AWS Error Code: ValidationException, AWS Error Message: One or more parameter values were invalid: Mismatching attribute types between location and schema

esquema:

Table: Daily

Hash Key: CustId (String)

Range Key: Dated (Number)

datos:

CustId : VisioNerdy

Dated : 1329071400000

Código:

List<Key> fkeys = new ArrayList<Key>(); //tableName="Daily", keys=["VisioNerdy"], ranges=[1329071400000] 
    Map<String, KeysAndAttributes> requestItems = new HashMap<String, KeysAndAttributes>(); 
    for(int i = 0; i < keys.size(); i++) 
    { 
     String key = keys.get(i); 
     if(ranges == null) 
      fkeys.add(new Key().withHashKeyElement(new AttributeValue().withS(key))); 
     else 
      fkeys.add(new Key().withHashKeyElement(new AttributeValue().withS(key)) 
        .withRangeKeyElement(new AttributeValue().withS(ranges.get(i).toString()))); 
    } 
    requestItems.put(tableName, new KeysAndAttributes().withKeys(fkeys)); 
    BatchGetItemRequest batchGetItemRequest = new BatchGetItemRequest().withRequestItems(requestItems); 
    BatchGetItemResult result = client.batchGetItem(batchGetItemRequest); 

Alguna pista?

+0

Podría, por favor añadir su esquema (finalmente condensada) y el fragmento de código que se ejecuta la consulta para aliviar ¿análisis? ¡Gracias! –

+1

Han editado la pregunta para incluirlos. ¡Gracias! –

+0

Tiene "if (ranges == null)", pero si una tabla tiene una clave de rango, se requiere un valor; no puedes omitirlo –

Respuesta

8

Ha definido el atributo del alcance de su Hash and Range Type Primary Key tipo Número, sin embargo, preparar su valor de atributo a través de withS() tipo cadena para su solicitud:

fkeys.add(new Key().withHashKeyElement(new AttributeValue().withS(key)) 
     .withRangeKeyElement(new AttributeValue().withS(ranges.get(i).toString()))); 

Cambio withS(String s)-withN(String s) debe poner remedio a su problema en consecuencia (confusamente, ambos métodos requieren un parámetro del tipo String):

fkeys.add(new Key().withHashKeyElement(new AttributeValue().withS(key)) 
     .withRangeKeyElement(new AttributeValue().withN(ranges.get(i).toString()))); 

Es cierto que la tipificación débil implícita de la presentación DynamoDB data types basado en cadena parámetros sólo no alivia exactamente en desarrollo;)

+0

¡Entendido! ¡Gracias! –

+0

@Mani: Me alegro de haber podido ayudar. ¿Podría usted, por favor, votar y aceptar la respuesta, si esto resuelve su problema? Esto se desea aquí, tanto para mantener su problema fuera de la vista como para dejar espacio a los problemas abiertos de los demás, así como para asegurar que los usuarios se mantengan motivados para ayudarse mutuamente;) Consulte las preguntas frecuentes [¿Qué es la reputación?] (Http: // stackoverflow .com/faq # reputation) para más detalles - ¡gracias! –

+1

¡Hecho! Gracias por guiar! –