从 JSON 创建 dynamodb table
Create dynamodb table from JSON
我正在尝试通过包含我的 table 结构的 JSON 文件在我的代码中创建发电机 table。现在我正在使用以下代码创建 table:
amazonClient.createTable(
CreateTableRequest()
.withTableName(tableName)
.withKeySchema(KeySchemaElement("id", KeyType.HASH))
.withAttributeDefinitions(...)
.withProvisionedThroughput(...)
.withGlobalSecondaryIndexes(...))
是否可以通过 JSON 创建它?
我也试图实现类似的东西,但我没有找到任何用于该目的的库,因此我花了几分钟自己编写代码。
提供了一个 json 文件,其结构与 documentation example 中的相同:
{
TableName : "Music",
KeySchema: [
{
AttributeName: "Artist",
KeyType: "HASH",
},
{
AttributeName: "SongTitle",
KeyType: "RANGE"
}
],
AttributeDefinitions: [
{
AttributeName: "Artist",
AttributeType: "S"
},
{
AttributeName: "SongTitle",
AttributeType: "S"
}
],
ProvisionedThroughput: {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1
}
}
下面的代码有效。注意:
- createTable 方法需要一个 json 字符串,您可以使用
Files.readString(Path.of("path/to/your/file.json"))
之类的方法获取该字符串
- 如果您不使用 Lombok,则必须自己创建 constructors/getters/setters
- 仅当 class 字段名称与 json 字段名称不完全匹配时才需要
@JsonProperty
注释(在这种情况下,首字母大写),explained here.
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.model.*;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@RequiredArgsConstructor
public class CreateTable {
private final AmazonDynamoDB amazonDynamoDB;
public void createTable(String json) throws JsonProcessingException {
Table table = readTable(json);
amazonDynamoDB.createTable(getTableRequest(table));
}
private CreateTableRequest getTableRequest(Table table) {
return new CreateTableRequest()
.withTableName(table.getTableName())
.withKeySchema(toKeySchemaElement(table.getKeySchema()))
.withAttributeDefinitions(toAttributeDefinition(table.getAttributeDefinitions()))
.withProvisionedThroughput(toProvisionedThroughput(table.getProvisionedThroughput()));
}
private Table readTable(String json) throws JsonProcessingException {
return new ObjectMapper().readValue(json, Table.class);
}
private ProvisionedThroughput toProvisionedThroughput(ProvisionedTp provisionedTp) {
log.info("creating provisioned throughput read: {} write: {}", provisionedTp.getReadCapacityUnits(), provisionedTp.getWriteCapacityUnits());
return new ProvisionedThroughput(provisionedTp.getReadCapacityUnits(), provisionedTp.getWriteCapacityUnits());
}
private Collection<AttributeDefinition> toAttributeDefinition(Collection<AttributeDef> attributeDefs) {
return attributeDefs.stream().map(this::toAttributeDefinition).collect(Collectors.toList());
}
private AttributeDefinition toAttributeDefinition(AttributeDef attributeDef) {
log.info("creating new attribute definition {}: {}", attributeDef.getAttributeName(), attributeDef.getAttributeType());
return new AttributeDefinition().withAttributeName(attributeDef.getAttributeName()).withAttributeType(attributeDef.getAttributeType());
}
private Collection<KeySchemaElement> toKeySchemaElement(Collection<KeySchema> keySchemas) {
return keySchemas.stream().map(this::toKeySchemaElement).collect(Collectors.toList());
}
private KeySchemaElement toKeySchemaElement(KeySchema keySchema) {
log.info("creating new key schema element {}: {}", keySchema.getAttributeName(), keySchema.getKeyType());
return new KeySchemaElement().withAttributeName(keySchema.getAttributeName()).withKeyType(keySchema.getKeyType());
}
@Data
@NoArgsConstructor
public static class Table {
@JsonProperty("TableName")
private String tableName;
@JsonProperty("KeySchema")
private List<KeySchema> keySchema;
@JsonProperty("AttributeDefinitions")
private List<AttributeDef> attributeDefinitions;
@JsonProperty("ProvisionedThroughput")
private ProvisionedTp provisionedThroughput;
}
@Data
@NoArgsConstructor
public static class KeySchema {
@JsonProperty("AttributeName")
private String attributeName;
@JsonProperty("KeyType")
private String keyType;
}
@Data
@NoArgsConstructor
public static class AttributeDef {
@JsonProperty("AttributeName")
private String attributeName;
@JsonProperty("AttributeType")
private String attributeType;
}
@Data
@NoArgsConstructor
public static class ProvisionedTp {
@JsonProperty("ReadCapacityUnits")
private long readCapacityUnits;
@JsonProperty("WriteCapacityUnits")
private long writeCapacityUnits;
}
}
我正在尝试通过包含我的 table 结构的 JSON 文件在我的代码中创建发电机 table。现在我正在使用以下代码创建 table:
amazonClient.createTable(
CreateTableRequest()
.withTableName(tableName)
.withKeySchema(KeySchemaElement("id", KeyType.HASH))
.withAttributeDefinitions(...)
.withProvisionedThroughput(...)
.withGlobalSecondaryIndexes(...))
是否可以通过 JSON 创建它?
我也试图实现类似的东西,但我没有找到任何用于该目的的库,因此我花了几分钟自己编写代码。
提供了一个 json 文件,其结构与 documentation example 中的相同:
{
TableName : "Music",
KeySchema: [
{
AttributeName: "Artist",
KeyType: "HASH",
},
{
AttributeName: "SongTitle",
KeyType: "RANGE"
}
],
AttributeDefinitions: [
{
AttributeName: "Artist",
AttributeType: "S"
},
{
AttributeName: "SongTitle",
AttributeType: "S"
}
],
ProvisionedThroughput: {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1
}
}
下面的代码有效。注意:
- createTable 方法需要一个 json 字符串,您可以使用
Files.readString(Path.of("path/to/your/file.json"))
之类的方法获取该字符串
- 如果您不使用 Lombok,则必须自己创建 constructors/getters/setters
- 仅当 class 字段名称与 json 字段名称不完全匹配时才需要
@JsonProperty
注释(在这种情况下,首字母大写),explained here.
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.model.*;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@RequiredArgsConstructor
public class CreateTable {
private final AmazonDynamoDB amazonDynamoDB;
public void createTable(String json) throws JsonProcessingException {
Table table = readTable(json);
amazonDynamoDB.createTable(getTableRequest(table));
}
private CreateTableRequest getTableRequest(Table table) {
return new CreateTableRequest()
.withTableName(table.getTableName())
.withKeySchema(toKeySchemaElement(table.getKeySchema()))
.withAttributeDefinitions(toAttributeDefinition(table.getAttributeDefinitions()))
.withProvisionedThroughput(toProvisionedThroughput(table.getProvisionedThroughput()));
}
private Table readTable(String json) throws JsonProcessingException {
return new ObjectMapper().readValue(json, Table.class);
}
private ProvisionedThroughput toProvisionedThroughput(ProvisionedTp provisionedTp) {
log.info("creating provisioned throughput read: {} write: {}", provisionedTp.getReadCapacityUnits(), provisionedTp.getWriteCapacityUnits());
return new ProvisionedThroughput(provisionedTp.getReadCapacityUnits(), provisionedTp.getWriteCapacityUnits());
}
private Collection<AttributeDefinition> toAttributeDefinition(Collection<AttributeDef> attributeDefs) {
return attributeDefs.stream().map(this::toAttributeDefinition).collect(Collectors.toList());
}
private AttributeDefinition toAttributeDefinition(AttributeDef attributeDef) {
log.info("creating new attribute definition {}: {}", attributeDef.getAttributeName(), attributeDef.getAttributeType());
return new AttributeDefinition().withAttributeName(attributeDef.getAttributeName()).withAttributeType(attributeDef.getAttributeType());
}
private Collection<KeySchemaElement> toKeySchemaElement(Collection<KeySchema> keySchemas) {
return keySchemas.stream().map(this::toKeySchemaElement).collect(Collectors.toList());
}
private KeySchemaElement toKeySchemaElement(KeySchema keySchema) {
log.info("creating new key schema element {}: {}", keySchema.getAttributeName(), keySchema.getKeyType());
return new KeySchemaElement().withAttributeName(keySchema.getAttributeName()).withKeyType(keySchema.getKeyType());
}
@Data
@NoArgsConstructor
public static class Table {
@JsonProperty("TableName")
private String tableName;
@JsonProperty("KeySchema")
private List<KeySchema> keySchema;
@JsonProperty("AttributeDefinitions")
private List<AttributeDef> attributeDefinitions;
@JsonProperty("ProvisionedThroughput")
private ProvisionedTp provisionedThroughput;
}
@Data
@NoArgsConstructor
public static class KeySchema {
@JsonProperty("AttributeName")
private String attributeName;
@JsonProperty("KeyType")
private String keyType;
}
@Data
@NoArgsConstructor
public static class AttributeDef {
@JsonProperty("AttributeName")
private String attributeName;
@JsonProperty("AttributeType")
private String attributeType;
}
@Data
@NoArgsConstructor
public static class ProvisionedTp {
@JsonProperty("ReadCapacityUnits")
private long readCapacityUnits;
@JsonProperty("WriteCapacityUnits")
private long writeCapacityUnits;
}
}