简体   繁体   English

Java 在表中显示 DynamoDB 扫描属性值

[英]Java Displaying DynamoDB Scan AttributeValues in table

I am trying to do a scan of a DynamoDB table and then display user selected fields in a table.我正在尝试扫描 DynamoDB 表,然后在表中显示用户选择的字段。 The problem is how can I do this if data is returned as AttributeValues, since not all values will be a string, and I have no idea what type each value is before hand.问题是,如果数据作为 AttributeValues 返回,我该怎么做,因为并非所有值都是字符串,而且我事先不知道每个值是什么类型。

The code I have:我的代码:

 ScanRequest request =
            ScanRequest
            .builder()
            .tableName(tableName))
            .build();

    ScanIterable response = client.scanPaginator(request);
    List<Map<String, AttributeValue>> data = new ArrayList<>();

    for (ScanResponse page : response) {
        for (Map<String, AttributeValue> item : page.items()) {
            data.add(item);
        }
    }

then to get the values:然后获取值:

       for (Map<String, AttributeValue> map : items) {
        for (Map.Entry<String, AttributeValue> entry : map.entrySet()) {
            //get the entry.getValue() and append it to a string
        }
    }

If I use:如果我使用:

entry.getValue().toString()

I will get a string containing the attribute value type and value eg 'N': '127' If I use:我将得到一个包含属性值类型和值的字符串,例如 'N': '127' 如果我使用:

entry.getValue().s()

I will get null values when the type is not 'S'当类型不是“S”时,我将得到 null 个值

In that case as you do not want to deal with the DynamoDB JSON you should use a Document Interface .在那种情况下,因为您不想处理 DynamoDB JSON,所以您应该使用文档界面

Many AWS SDKs provide a document interface, allowing you to perform data plane operations (create, read, update, delete) on tables and indexes.许多 AWS 开发工具包提供文档接口,允许您对表和索引执行数据平面操作(创建、读取、更新、删除)。 With a document interface, you do not need to specify Data type descriptors.使用文档接口,您不需要指定数据类型描述符。 The data types are implied by the semantics of the data itself.数据类型由数据本身的语义暗示。 These AWS SDKs also provide methods to easily convert JSON documents to and from native Amazon DynamoDB data types.这些 AWS 开发工具包还提供了轻松将 JSON 文档与原生 Amazon DynamoDB 数据类型相互转换的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM