如何找到AWS DynamoDB中所有表的大小?

问题描述 投票:0回答:3

我在 DynamoDB 中有大约 50 个表,我正在寻找一种方法来查找所有表的大小。

aws dynamodb describe-table --table-name [table name]

我知道上面的命令返回 TableSizeBytes,但这是获取表大小的唯一方法吗? 我是否必须对图中的所有表运行此命令? 另外,运行这个命令的成本是多少?

amazon-web-services amazon-dynamodb dynamodb-queries
3个回答
3
投票

DescribeTable
API 可在 DynamoDB 上免费调用。您唯一的方法是迭代所有表格并对值求和,但我不明白为什么这可能是一个问题。

如果您不想手动列出所有表格,这里有一个衬垫,只是为了好玩:

aws dynamodb list-tables --region us-east-1 | \
  jq -r '.TableNames[]' | \
  xargs -L1 -I'{}' bash -c 'aws dynamodb describe-table --table-name {} --region us-east-1 | jq -r ".Table.TableSizeBytes"' | \
  awk '{S+=$1} END {print S}'

2
投票

只需编写一个脚本并循环遍历所有表格。如前所述,运行该命令是免费的。

例如:

#!/usr/bin/env bash

YOUR_DYNAMODB_TABLES=( "table1" "table2" "table3" )
TOTAL_SIZE=0
for TABLE in "${YOUR_DYNAMODB_TABLES[@]}"
do
    SIZE=$(aws dynamodb describe-table --table-name "$TABLE" | jq '.Table.TableSizeBytes')
    TOTAL_SIZE=$((TOTAL_SIZE + SIZE))
done
echo $TOTAL_SIZE

0
投票
#!/bin/bash

# Assuming you have the AWS CLI installed and configured with your credentials

# Get the list of DynamoDB tables
tables=$(aws dynamodb list-tables --query "TableNames" --output text)

# Initialize the total item count
total_item_count=0

# Loop through the tables and print their names and item counts
echo "DynamoDB Tables:"
for table in $tables; do
  item_count=$(aws dynamodb describe-table --table-name $table --query "Table.ItemCount" --output text)
  echo "- $table has $item_count items"
  total_item_count=$((total_item_count + item_count))
done

# Get the number of tables
num_tables=$(echo "$tables" | wc -w)

# Print the total item count and number of tables
echo ""
echo "Total item count across all tables: $total_item_count"
echo "Number of DynamoDB tables: $num_tables"

执行步骤:

  1. 复制上面的代码并制作一个shell脚本为ddb_all_count.sh
  2. 从 DynamoDB 表所在的账户和区域打开 AWS CloudShell 终端
  3. 通过上传选项将脚本上传到终端
  4. 上传后,运行命令
    bash ddb_all_count.sh
© www.soinside.com 2019 - 2024. All rights reserved.