首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获取aws-dynamoDB中的所有字段?

如何获取aws-dynamoDB中的所有字段?
EN

Stack Overflow用户
提问于 2017-07-03 16:14:48
回答 3查看 6.7K关注 0票数 5

我正在使用Node.js

代码语言:javascript
复制
var AWS = require('aws-sdk');

AWS.config.update({
  region: "region",
  endpoint: "https://dynamodb.region.amazonaws.com"
});
var dynamodb = new AWS.DynamoDB();
    var params = {
      TableName: "acc_new"
    };

    dynamodb.describeTable(params, function(err, data) {
        if (err) console.log(err, err.stack); // an error occurred
       else     console.log(JSON.stringify(data));
    });

输出:

代码语言:javascript
复制
{ AttributeDefinitions: [ { AttributeName: 'Id', AttributeType: 'S' } ],
  TableName: 'acc_new',
  KeySchema: [ { AttributeName: 'Id', KeyType: 'HASH' } ],
  ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 } }

输出只包含与表关联的键模式(仅哈希键和范围键),而不是所有属性。我想获取与表中的数据相关联的所有属性。

像这样:

代码语言:javascript
复制
 { AttributeName: 'AccountName', AttributeType: 'S' }
 { AttributeName: 'CreatedBy', AttributeType: 'S' }
 ...
 ...

有没有办法获得包含所有数据字段的dynamoDb表的描述?

EN

回答 3

Stack Overflow用户

发布于 2017-07-03 16:56:56

DynamoDB是一个NoSQL数据库。在创建表时,它并不期望在表中定义所有属性。此外,每一项都可以有任意数量的非键属性。只有键属性在表中的所有项中都是通用的或强制的。

由于上述原因,describe表不能提供表中存在的所有属性。

票数 3
EN

Stack Overflow用户

发布于 2019-01-29 04:30:00

没有传统的方法可以做到这一点,但如果你真的需要这样做,我会这样做:

  1. 在包含1列的SQL中创建一个DB表,该表上有一个PK。
  2. 使用AWS Pipeline将DynamoDB表中的所有数据提取到S3存储桶中。
  3. 创建一个Lambda函数,该函数在文件存储到S3存储桶时触发。
  4. lambda函数将执行以下操作:

<代码>G29

代码语言:javascript
复制
- Retrieve all rows from the SQL DB created in (1)
- Parse the file that fired it
- Extract all column names from each row, 1 row at a time
- Compare the column names to the ones found in the SQL DB table.  If any are not found, then insert them into the SQL DB.  Obviously duplicates would violate PK so be prepared to deal with that error.

在几天或更长时间内(取决于行数),DynamoDB表中的所有列名都将存储在SQL DB中。

票数 1
EN

Stack Overflow用户

发布于 2017-07-03 20:15:18

对于上述情况,不可能列出表中的所有属性。因为Dynamodb是预先定义的NoSql数据库属性,所以在创建表时只定义哈希键和范围键。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44880707

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档