DynamoDB高级扫描-JAVA [英] DynamoDB advanced scan - JAVA

查看:128
本文介绍了DynamoDB高级扫描-JAVA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要按位于一列中的子字段JSON扫描表。不幸的是,我找不到Java的任何示例,也不知道是否有可能。

I need to scan the tables by subfield JSON, which is located in one column. Unfortunately I can't find anywhere example in Java and do not know if it is possible.

这是我的数据,这个json表示对象-dynamodb中的一行。
json表示3个Java类:
-主类,包含类city和一些字符串记录
-city包含类road

This is my data and this json represents object - one row in dynamodb. The json represents 3 java classes: - main class which contains the class city and some string record - city contains a class road

是否可以扫描数据库并找到mainName = xyz并具有名为 Rockingham的城市记录的记录

Is it possible to scan the database and find the records with mainName = "xyz" and having a city record called "Rockingham"

{
"Id": "9",
"mainName": "xyz",
"floatVariable": 228.3,
"city": [
{
  "name": "Rockingham",
  "road": [
    {
      "roadName": "Traci",
      "roadLength": 118
    },
    {
      "roadName": "Watkins",
      "roadLength": 30
    }
  ]
 }
],

房子:{
huseName:温迪·卡森
}
}

"house": { "huseName": "Wendy Carson" } }

我有一些类似的东西,但这项工作不足以查询正确的数据。
Table table = dynamoDB.getTable(tableName);

I have some like this and this work but this is not enough to query correct data. Table table = dynamoDB.getTable(tableName);

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

        ItemCollection<ScanOutcome> items = table.scan(
                "floatVariable < :pr", //FilterExpression
                "Id, mainName, floatVariable, city" , //ProjectionExpression
                null, //ExpressionAttributeNames - not used in this example
                expressionAttributeValues);

        System.out.println("Scan of " + tableName + " for items with a price less than 300.");
        Iterator<Item> iterator = items.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next().toJSONPretty());
        }

我在php中看到了这样的示例,但不幸的是,它在Java。

I saw an example in php something like this but unfortunately it does not work in Java.

    ItemCollection<ScanOutcome> items = table.scan(
            " cites.name = :dupa  ", //FilterExpression
            "Id, mainName, floatVariable, city", //ProjectionExpression
            null, //ExpressionAttributeNames - not used in this example
            expressionAttributeValues);


推荐答案

如果您通过查询city.name 您的数据模型必须考虑到这一点。我建议每个表项有一个城市:

If you are querying by city.name your data model has to take that into consideration. I would suggest having one city per table item:

{
"Id": "9",
"mainName": "xyz",
"cityName": "Rockingham",
"floatVariable": 228.3,
"road": [
    {
      "roadName": "Traci",
      "roadLength": 118
    },
    {
      "roadName": "Watkins",
      "roadLength": 30
    }
  ]
 }
]}

哈希键为 cityName 属性,Range Key为使主键(哈希+范围键)唯一的其他任何属性,例如: ID

The Hash Key would be the cityName attribute, and the Range Key any other attribute that would make the primary key (hash + range key) unique, for instance : Id.

QuerySpec querySpec = new QuerySpec()
                        .withHashKey("cityName", "Rockingham")
                        .withProjectionExpression("Id, mainName, floatVariable, road");

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

作为第二种选择,您可以定义两个表:

As a second option you could define two tables:

表A

主键类型:哈希键+范围键

Primary Key Type: Hash Key + Range Key

哈希键: cityName
范围键: Id (参考表B项)

Hash Key : cityName Range Key : Id (Reference to Table B item)

{
    "cityName": "Rockingham",
    "Id" : 9,
    "road": [
        {
          "roadName": "Traci",
          "roadLength": 118
        },
        {
          "roadName": "Watkins",
          "roadLength": 30
        }
      ]
     }
    ]}

表B

主键类型:哈希键

哈希键: Id

{
    "Id": "9",
    "mainName": "xyz",
    "floatVariable": 228.3
}

检索城市项目后,您将通过ID vi查询表B 查询 GetItem BatchGetItem

After retrieving the city items you would query the Table B by Id via Query, GetItem or BatchGetItem.

这两个选项都将允许您使用 Query 操作而不是 Scan ,从而以更好的性能和更低的成本实现更简单的查询:

Both options will allow you to use the Query operation instead of Scan, enabling simpler queries with better performance and lower costs:


扫描操作始终扫描整个表或二级索引,然后扫描
过滤掉值以提供所需的结果,实际上
添加了从结果集中删除数据的额外步骤。避免
在大型表或索引上使用扫描操作,并在可能的情况下使用
删除许多结果的过滤器。另外,随着表或索引的增长,
的扫描操作也会变慢。扫描操作检查
所请求值的每个项目,并且可以在单个操作中用完
大表或索引的预配置吞吐量。为了缩短响应时间,
设计表和索引,以便您的应用程序可以使用查询
代替扫描。 (对于表,您还可以考虑使用GetItem
和BatchGetItem API。)。

A Scan operation always scans the entire table or secondary index, then filters out values to provide the desired result, essentially adding the extra step of removing data from the result set. Avoid using a Scan operation on a large table or index with a filter that removes many results, if possible. Also, as a table or index grows, the Scan operation slows. The Scan operation examines every item for the requested values, and can use up the provisioned throughput for a large table or index in a single operation. For faster response times, design your tables and indexes so that your applications can use Query instead of Scan. (For tables, you can also consider using the GetItem and BatchGetItem APIs.).

来源: http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Scan.html

这篇关于DynamoDB高级扫描-JAVA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆