查询dynamodb表数据:
val tableName = "mytable"
val table = new Table(DynamoDBClientUtil.getDynamoDBClient, tableName)
val conditionAllQuery = new StringBuilder()
.append("record_key").append("=").append(":record_key")
.append(" and #timestamp > :timestamp")
.toString()
val querySpec = new QuerySpec()
.withKeyConditionExpression(conditionAllQuery)
.withConsistentRead(true)
.withNameMap(new NameMap().`with`("#timestamp", "timestamp"))
.withValueMap(new ValueMap()
.withString(":record_key", "mykey")
.withLong(":timestamp", "mytime".toLong))
val iterator = table.query(querySpec).iterator()
while (iterator.hasNext){
println(iterator.next())
}
当用QuerySpec查询dynamodb 表用iterator遍历时,如果是在java环境下,不会报下面错误,但是在scala环境下,就会报下面错误,主要还是包的访问权限造成的
Exception in thread "main" java.lang.IllegalAccessError: tried to access class com.amazonaws.services.dynamodbv2.document.internal.IteratorSupport from class
解决办法:可采用dynamodb mapper方式接口进行查询,或者用QueryRequest方式:
val expressionAttributesNames = new util.HashMap[String,String]()
expressionAttributesNames.put("#record_key","record_key");
expressionAttributesNames.put("#timestamp","timestamp");
val expressionAttributeValues = new util.HashMap[String,AttributeValue]()
expressionAttributeValues.put(":record_key",new AttributeValue().withS("mykey"));
expressionAttributeValues.put(":timestamp",new AttributeValue().withN("mytime"));
val query = new QueryRequest()
.withTableName(tableName)
.withKeyConditionExpression("#record_key = :record_key and #timestamp > :timestamp ")
.withExpressionAttributeNames(expressionAttributesNames)
.withExpressionAttributeValues(expressionAttributeValues);
val queryResult = DynamoDBClientUtil.getDynamoDBClient.query(query).getItems