我正在使用The AWS Command Line Interface for DynamoDB。
当我们查询一个项目时,我们会得到一个非常详细的JSON输出,如下所示(它是从get-item
构建的,为了几乎详尽(NULL
类型被省略)aws command line help:
{
"Count": 1,
"Items": [
{
"Id": {
"S": "app1"
},
"Parameters": {
"M": {
"nfs": {
"M": {
"IP" : {
"S" : "172.16.0.178"
},
"defaultPath": {
"S": "/mnt/ebs/"
},
"key": {
"B": "dGhpcyB0ZXh0IGlzIGJhc2U2NC1lbmNvZGVk"
},
"activated": {
"BOOL": true
}
}
},
"ws" : {
"M" : {
"number" : {
"N" : "5"
},
"values" : {
"L" : [
{ "S" : "12253456346346"},
{ "S" : "23452353463464"},
{ "S" : "23523453461232"},
{ "S" : "34645745675675"},
{ "S" : "46456745757575"}
]
}
}
}
}
},
"Oldtypes": {
"typeSS" : {"SS" : ["foo", "bar", "baz"]},
"typeNS" : {"NS" : ["0", "1", "2", "3", "4", "5"]},
"typeBS" : {"BS" : ["VGVybWluYXRvcgo=", "VGVybWluYXRvciAyOiBKdWRnbWVudCBEYXkK", "VGVybWluYXRvciAzOiBSaXNlIG9mIHRoZSBNYWNoaW5lcwo=", "VGVybWluYXRvciA0OiBTYWx2YXRpb24K","VGVybWluYXRvciA1OiBHZW5lc2lzCg=="]}
}
}
],
"ScannedCount": 1,
"ConsumedCapacity": null
}
有没有什么方法可以为Items
器件获得更简单的输出?
{
"ConsumedCapacity": null,
"Count": 1,
"Items": [
{
"Id": "app1",
"Parameters": {
"nfs": {
"IP": "172.16.0.178",
"activated": true,
"defaultPath": "/mnt/ebs/",
"key": "dGhpcyB0ZXh0IGlzIGJhc2U2NC1lbmNvZGVk"
},
"ws": {
"number": 5,
"values": ["12253456346346","23452353463464","23523453461232","34645745675675","46456745757575"]
}
},
"Oldtypes": {
"typeBS": ["VGVybWluYXRvcgo=", "VGVybWluYXRvciAyOiBKdWRnbWVudCBEYXkK", "VGVybWluYXRvciAzOiBSaXNlIG9mIHRoZSBNYWNoaW5lcwo=", "VGVybWluYXRvciA0OiBTYWx2YXRpb24K", "VGVybWluYXRvciA1OiBHZW5lc2lzCg=="],
"typeNS": [0, 1, 2, 3, 4, 5],
"typeSS": ["foo","bar","baz"]
}
}
],
"ScannedCount": 1
}
dynamodb - AWS CLI 1.7.10 documentation中没有任何帮助。
我们必须从命令行得到结果。如果需要的话,我愿意使用其他命令行工具,如jq
,但这样的jq
Map对我来说似乎很复杂。
更新1:基于jq
的解决方案(借助DanielH的答案)
使用jq
很容易,但不太漂亮,您可以执行以下操作:
$> aws dynamodb query --table-name ConfigCatalog --key-conditions '{ "Id" : {"AttributeValueList": [{"S":"app1"}], "ComparisonOperator": "EQ"}}' | jq -r '.Items[0].Parameters.M."nfs#IP".S'
结果将为:172.16.0.178
jq
-r
选项提供原始输出。
更新2:基于jq
的解决方案(在@jeff-mercado的帮助下)
以下是Jeff Mercado jq
函数的更新和注解版本,用于解组DynamoDB输出。它将给予预期输出:
$> cat unmarshal_dynamodb.jq
def unmarshal_dynamodb:
# DynamoDB string type
(objects | .S)
# DynamoDB blob type
// (objects | .B)
# DynamoDB number type
// (objects | .N | strings | tonumber)
# DynamoDB boolean type
// (objects | .BOOL)
# DynamoDB map type, recursion on each item
// (objects | .M | objects | with_entries(.value |= unmarshal_dynamodb))
# DynamoDB list type, recursion on each item
// (objects | .L | arrays | map(unmarshal_dynamodb))
# DynamoDB typed list type SS, string set
// (objects | .SS | arrays | map(unmarshal_dynamodb))
# DynamoDB typed list type NS, number set
// (objects | .NS | arrays | map(tonumber))
# DynamoDB typed list type BS, blob set
// (objects | .BS | arrays | map(unmarshal_dynamodb))
# managing others DynamoDB output entries: "Count", "Items", "ScannedCount" and "ConsumedCapcity"
// (objects | with_entries(.value |= unmarshal_dynamodb))
// (arrays | map(unmarshal_dynamodb))
# leaves values
// .
;
unmarshal_dynamodb
如果您将DynamoDB
查询输出保存到一个文件中,比如ddb-query-result.json
,您可以执行以获得所需的结果:
$> jq -f unmarshal_dynamodb.jq ddb-query-result.json
6条答案
按热度按时间2ic8powd1#
你可以用一个精心设计的函数递归地解码这些值。看起来键的名称对应于一个类型:
如果可能的话,处理你想要解码的每一个案例,否则过滤掉它。你可以利用各种类型过滤器和替代运算符来完成这一任务。
x一个一个一个一个x一个一个二个一个x一个一个三个一个
7nbnzgx92#
另一种实现post目标的方法是使用
node.js
扩展,如node-dynamodb
或dynamodb-marshaler
,并构建一个node
命令行工具。使用
commander
package构建node.js
命令行应用程序的有趣教程:Creating Your First Node.js Command-line Application下面是一个快速而简单的oneliner,它从stdin读取一条记录,并以简化的形式打印出来:
sbtkgmzw3#
下面是可以处理空值的
jq
解决方案的更新版本。原始版本归功于@jeff-mercado和@herve。
mrwjdhj34#
据我所知,没有其他输出像您发布的“冗长”的输出。因此,我认为,您不能避免中间工具,如
jq
或sed
在这篇文章中有几个转换原始发电机数据的建议:
从DynamoDB导出数据
也许您可以将其中一个脚本与
jq
或sed
结合使用nhaq1z215#
这是另一种方法。这可能有点残酷,但它展示了基本思想。
在线试用!
样品运行(假设过滤器在
filter.jq
中,数据在data.json
中)41zrol4v6#
下面是node中的一个脚本。
我将文件命名为
reformat.js
,但您可以随意命名它步骤1)通过CLI运行dynamoDB查询并将其保存到JSON文件中。要保存来自CLI的响应,只需添加
> somefile.json
。expression-attributes.json
如果您需要有关如何查询DynamoDB的更多信息,请查看文档www.example.com中的这些示例https://docs.aws.amazon.com/cli/latest/reference/dynamodb/query.html#examples
现在您已经有了需要重新格式化的数据的JSON文件,请从终端运行format.js脚本
步骤2)
如果您想要JSON对象输出,只需在脚本末尾的
console.dir
中放入JSON.stringify(result)
,就应该有一个干净的JS对象输出