java实现DynamoDB创表[全局索引]

导读:本篇文章讲解 java实现DynamoDB创表[全局索引],希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

什么是Amazon DynamoDB?

Amazon DynamoDB 是一种完全托管的 NoSQL 数据库服务。

Amazon DynamoDB有哪些核心组件?

Amazon DynamoDB的核心组件有三个:表(Table)、项目(Item)、属性(Attribute)。

Table、Item、Attribute组件之间有什么关系?

举个栗子:结合你使用的关系型数据库进行理解,Table指的表就不用说了,Item相当于表中的一个对象(一行数据),Attribute就是对象的每个属性(每一列)。

总的来说就是:item是attribute的集合,table是item的集合。

创表时需要设置主键,唯一标识每一个项目,同时可设置多个排序键以及本地索引和全局二级索引。

欲知更多,请疯狂点击访问官网:Amazon DynamoDB官方文档

以下是创表的具体实现:SpringBoot采用2.2.3版本。

一、 Maven依赖
<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>com.amazonaws</groupId>
				<artifactId>aws-java-sdk-bom</artifactId>
				<version>1.11.710</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>com.github.derjust</groupId>
			<artifactId>spring-data-dynamodb</artifactId>
			<version>5.0.4</version>
		</dependency>
		<dependency>
			<groupId>com.amazonaws</groupId>
			<artifactId>aws-java-sdk-dynamodb</artifactId>
		</dependency>

		<dependency>
			<groupId>com.amazonaws</groupId>
			<artifactId>aws-java-sdk-iot</artifactId>
		</dependency>
	</dependencies>
二、创表
    /**
     * create global index table
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/createGlobalIndexTable")
    public ResponseEntity<String> createGlobalIndexTable() throws Exception {
        // 表名
        String tableName = "testOnly";
        // 属性定义
        ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
        attributeDefinitions.add(new AttributeDefinition().withAttributeName("id").withAttributeType("S"));
        attributeDefinitions.add(new AttributeDefinition().withAttributeName("name").withAttributeType("S"));
        attributeDefinitions.add(new AttributeDefinition().withAttributeName("email").withAttributeType("S"));
        // Key schema for table
        ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>();
        // 分区键
        tableKeySchema.add(new KeySchemaElement().withAttributeName("id").withKeyType(KeyType.HASH)); // Partition
        // 排序键
        tableKeySchema.add(new KeySchemaElement().withAttributeName("email").withKeyType(KeyType.RANGE)); // Sort
        // Initial provisioned throughput settings for the indexes
        ProvisionedThroughput ptIndex = new ProvisionedThroughput().withReadCapacityUnits(1L)
                .withWriteCapacityUnits(1L);
        // 全局二级索引
        GlobalSecondaryIndex createEmailIndex = new GlobalSecondaryIndex().withIndexName("GlobalEmailIndex")
                .withProvisionedThroughput(ptIndex)
                .withKeySchema(new KeySchemaElement().withAttributeName("email").withKeyType(KeyType.HASH), // Partition
                        // key
                        new KeySchemaElement().withAttributeName("name").withKeyType(KeyType.RANGE)) // Sort
                // key
                .withProjection(
                        new Projection().withProjectionType("ALL"));
        CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName)
                .withProvisionedThroughput(
                        new ProvisionedThroughput().withReadCapacityUnits((long) 1).withWriteCapacityUnits((long) 1))
                .withAttributeDefinitions(attributeDefinitions).withKeySchema(tableKeySchema)
                .withGlobalSecondaryIndexes(createEmailIndex);

        TableUtils.createTableIfNotExists(dynamoDB, createTableRequest);
        TableUtils.waitUntilActive(dynamoDB, tableName);

        return new ResponseEntity<>(HttpStatus.OK);
    }

查看
在这里插入图片描述
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/115784.html

(0)
Java光头强的头像Java光头强

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!