gpt4 book ai didi

java - DynamoDb HashKey 属性名称

转载 作者:行者123 更新时间:2023-11-30 02:29:04 30 4
gpt4 key购买 nike

java中有没有办法获取dynamodb表的HashKey的属性名称?

e.g., if the dyanmodb table schema is 
(
"HashKey, String", SSNId
"SortKey, Long", Pincode
"String", Name
)
So I should be able to get the output like this:-
getHashKeyAttributeName(String tableName) --> SSNId
getSortkeyAttributeName(String tableName) --> Pincode
getOtherAttributeList(String tableName) --> Name

最佳答案

您只需迭代 keySchemasattributeDefinitions当您描述表格时。

DynamodbTable 描述具有以下结构,(我正在使用 clojure-aws ,您可以使用 aws cli 查看表结构)

user=> (db/describe-table {:profile "aws-profile" :endpoint "us-west-2"} "KeyValueLookupTable1")
{:table {:key-schema [{:key-type "HASH", :attribute-name "leaseKey"}], :table-size-bytes 201, :attribute-definitions [{:attribute-name "leaseKey", :attribute-type "S"}], :creation-date-time #object[org.joda.time.DateTime 0x4c6ece3 "2017-06-07T15:50:35.057-07:00"], :item-count 1, :table-status "ACTIVE", :table-name "KeyValueLookupTable1", :provisioned-throughput {:read-capacity-units 10, :write-capacity-units 10, :number-of-decreases-today 0}, :table-arn "arn:aws:dynamodb:us-west-2:033814027302:table/KeyValueLookupTable1"}}

在哪里可以看到 key-schemaattribute-definitions您需要迭代的键。

1) 请参阅 TableDescription#getKeySchema 的文档获取 HASHRANGE键。

使用 java8 的示例

  DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient())

String getHashKeyAttributeName(String tableName) {

TableDescription tableSchema = dynamoDB.getTable(tableName).describe();

return tableSchema.getKeySchema().stream()
.filter(x -> x.getKeyType().equals(KeyType.HASH.toString()))
.findFirst().get().getAttributeName();
}

String getSortkeyAttributeName(String tableName) {

TableDescription tableSchema = dynamoDB.getTable(tableName).describe();

return tableSchema.getKeySchema().stream()
.filter(x -> x.getKeyType().equals(KeyType.RANGE.toString()))
.findFirst().get().getAttributeName();
}

2) 对于其他字段,需要迭代 List<AttributeDefinitions>你得到的 tableDescription.getAttributeDefinitions

List<String> getOtherAttributeList(String tableName) {

DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient());
TableDescription tableSchema = dynamoDB.getTable(tableName).describe();

return tableSchema.getAttributeDefinitions().stream()
.map(AttributeDefinition::getAttributeName)
.collect(Collectors.toList());
}

关于java - DynamoDb HashKey 属性名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44731800/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com