首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将现有的dynamo表架构导出到json?

如何将现有的dynamo表架构导出到json?
EN

Stack Overflow用户
提问于 2017-02-07 21:20:25
回答 3查看 21.9K关注 0票数 31

为了测试的目的,我想将一些只有模式的dynamodb表复制到本地环境中。首先我试过:

aws dynamodb describe-table --table-name Foo > FooTable.json

但是很明显,输出模式与来自create-table命令的输入模式不兼容:

aws dynamodb create-table --cli-input-json file://FooTable.json --endpoint=http://localhost:8000

我想避免的是用aws dynamodb create-table --generate-cli-skeleton生成几十个骨架,并手动填充它们:/

是否有一种方法可以使表模式以一种“有用”的格式进行娱乐?我觉得难以置信的是,没有任何直接的方法可以通过web图形界面或标准的aws命令行--在听到他们的服务是多么的“好”之后。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-02-08 13:54:43

我刚刚用bchew/dynamodump完成了一个完整的转储和“恢复”

代码语言:javascript
复制
git clone git@github.com:bchew/dynamodump.git

注意Documentshttps://github.com/bchew/dynamodump中的--schemaOnly选项。指挥部是:

代码语言:javascript
复制
./dynamodump.py -m backup --schemaOnly --region foo-region --host localhost --srcTable '*' --port 8000 --accessKey fooKey --secretKey barKey

然后可以使用-m restore模式将数据或模式放回本地dynamodb或任何需要的地方:)

话虽如此,我仍然觉得亚马逊的dynamodb工具链有多么糟糕,令人难以置信。来吧伙计们。

票数 17
EN

Stack Overflow用户

发布于 2021-05-25 21:44:02

这将获取aws dynamodb describe-table输出,并将其转换为aws dynamodb create-table --cli-input-json的输入格式。

代码语言:javascript
复制
AWS_PROFILE=xyz aws dynamodb describe-table --table-name MyTable > mytable_full.json

# Pull out just what is minimally needed for table-creation:
#
#  TableName
#  KeySchema
#  AttributeDefinitions (must only contain attributes used in keys)
#  Global/Local Secondary Indexes
#  Defaults BillingMode to PAY_PER_REQUEST
#   (any provisioning can be set up manually based on load)

jq <mytable_full.json '.Table | {TableName, KeySchema, AttributeDefinitions} + (try {LocalSecondaryIndexes: [ .LocalSecondaryIndexes[] | {IndexName, KeySchema, Projection} ]} // {}) + (try {GlobalSecondaryIndexes: [ .GlobalSecondaryIndexes[] | {IndexName, KeySchema, Projection} ]} // {}) + {BillingMode: "PAY_PER_REQUEST"}' ​>mytable.json

AWS_PROFILE=xyz aws dynamodb create-table --cli-input-json file://mytable.json

您还可以将json粘贴到python中( python语法与json非常匹配)

代码语言:javascript
复制
import boto3

dynamodb = boto3.resource("dynamodb")

tabledef = {
    "TableName": "MyTable",
    "KeySchema": [
...
}

table = dynamodb.create_table(**tabledef)
print("Table status: ", table.table_status)

参考文献:

https://docs.aws.amazon.com/cli/latest/reference/dynamodb/describe-table.html https://docs.aws.amazon.com/cli/latest/reference/dynamodb/create-table.html CreateTable.html https://boto3.amazonaws.com/v1/documentation/api/latest/guide/dynamodb.html#creating-a-new-table

票数 3
EN

Stack Overflow用户

发布于 2021-07-06 18:00:42

下面是一个在Windows上使用C#、AWS和Newtonsoft的版本。首先运行以下命令:-

代码语言:javascript
复制
aws dynamodb describe-table --table-name TheTable --profile SourceAWSCredsProfile > TheTable.json

获取文件,反序列化并序列化到--cli-input-json友好类:

代码语言:javascript
复制
TableContainer tableContainer;

string sourceFile = "TheTable.json";
string destFile = "TheTable.cli-input.json";

using (StreamReader file = File.OpenText(sourceFile))
{
    JsonSerializer serializer = new JsonSerializer();
    tableContainer = (TableContainer)serializer.Deserialize(file, typeof(TableContainer));
}

File.WriteAllText(destFile, JsonConvert.SerializeObject(tableContainer.Table, Formatting.Indented, new JsonSerializerSettings
    {
        NullValueHandling = NullValueHandling.Ignore
    }
));

现在运行以下命令导入表定义:-

代码语言:javascript
复制
aws dynamodb create-table --cli-input-json file://TheTable.cli-input.json --profile DestinationAWSCredsProfile

TableContainer类定义如下。缺少某些属性将清除--cli-input-json参数不需要的所有内容。您可以在任何时候重新创建这个类,方法是:-

代码语言:javascript
复制
aws dynamodb create-table --generate-cli-skeleton

然后使用Visual中非常方便的Paste Special... Paste JSON as Classes特性,将输出复制并粘贴到一个新的类文件中。

代码语言:javascript
复制
public class TableContainer
{
    public DynamoTableCLIJSON Table { get; set; }
}

public class DynamoTableCLIJSON
{
    public Attributedefinition[] AttributeDefinitions { get; set; }
    public string TableName { get; set; }
    public Keyschema[] KeySchema { get; set; }
    public Localsecondaryindex[] LocalSecondaryIndexes { get; set; }
    public Globalsecondaryindex[] GlobalSecondaryIndexes { get; set; }
    public string BillingMode { get; set; }
    public Provisionedthroughput ProvisionedThroughput { get; set; }
    public Streamspecification StreamSpecification { get; set; }
    public Ssespecification SSESpecification { get; set; }
    public Tag[] Tags { get; set; }
}

public class Provisionedthroughput
{
    public int ReadCapacityUnits { get; set; }
    public int WriteCapacityUnits { get; set; }
}

public class Streamspecification
{
    public bool StreamEnabled { get; set; }
    public string StreamViewType { get; set; }
}

public class Ssespecification
{
    public bool Enabled { get; set; }
    public string SSEType { get; set; }
    public string KMSMasterKeyId { get; set; }
}

public class Attributedefinition
{
    public string AttributeName { get; set; }
    public string AttributeType { get; set; }
}

public class Keyschema
{
    public string AttributeName { get; set; }
    public string KeyType { get; set; }
}

public class Localsecondaryindex
{
    public string IndexName { get; set; }
    public Keyschema1[] KeySchema { get; set; }
    public Projection Projection { get; set; }
}

public class Projection
{
    public string ProjectionType { get; set; }
    public string[] NonKeyAttributes { get; set; }
}

public class Keyschema1
{
    public string AttributeName { get; set; }
    public string KeyType { get; set; }
}

public class Globalsecondaryindex
{
    public string IndexName { get; set; }
    public Keyschema2[] KeySchema { get; set; }
    public Projection1 Projection { get; set; }
    public Provisionedthroughput1 ProvisionedThroughput { get; set; }
}

public class Projection1
{
    public string ProjectionType { get; set; }
    public string[] NonKeyAttributes { get; set; }
}

public class Provisionedthroughput1
{
    public int ReadCapacityUnits { get; set; }
    public int WriteCapacityUnits { get; set; }
}

public class Keyschema2
{
    public string AttributeName { get; set; }
    public string KeyType { get; set; }
}

public class Tag
{
    public string Key { get; set; }
    public string Value { get; set; }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42100210

复制
相关文章

相似问题

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