本文章僅作為個(gè)人筆記
DynamoDB官網(wǎng):https://us-west-2.console.aws.amazon.com/dynamodb/home
DynamoDB官方文檔:https://aws.amazon.com/cn/documentation/dynamodb/
DynamoDB官方j(luò)ava使用文檔:http://docs.aws.amazon.com/zh_cn/amazondynamodb/latest/developerguide/JavaDocumentAPICRUDExample.html
DynamoDB官方權(quán)限設(shè)置文檔:https://docs.aws.amazon.com/zh_cn/toolkit-for-eclipse/v1/user-guide/setup-credentials.html
DynamoDB個(gè)人理解:DynamoDB為aws下一個(gè)NoSql數(shù)據(jù)庫,用于處理簡單key-value數(shù)據(jù)。DynamoDB也支持索引蚯舱,目前一個(gè)表結(jié)構(gòu)最多支持5個(gè)索引。
記住一定要在本地生成證書確保DynamoDB權(quán)限(~/.aws/credentials)(C:\Users\USERNAME\.aws\credentials)格式如下:
另外在開發(fā)前,一定要在aws官網(wǎng)開通DynamoDB服務(wù)及創(chuàng)建相應(yīng)的DynamoDB數(shù)據(jù)表.
DynamoDB使用(gradle項(xiàng)目):
1.在項(xiàng)目build.gradle文件的dependencies選項(xiàng)下添加(compile 'com.amazonaws:aws-java-sdk-dynamodb:1.11.172')依賴
2.AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();//獲取DynamoDB連接(用于獲取mapper及DynamoDB對(duì)象)
3.DynamoDBMapper mapper = new DynamoDBMapper(client);//獲取Mapper(mapper可以非常方便,在使用最少代碼的情況下對(duì)數(shù)據(jù)進(jìn)行簡單的增刪改查)
4.DynamoDB dynamoDB = new DynamoDB(client);//獲取DynamoDB對(duì)象(可用于獲取Table對(duì)象操作單個(gè)表的復(fù)雜操作)
5.Table table = dynamoDB.getTable(TABL_NAME);//獲取Table對(duì)象(對(duì)數(shù)據(jù)表進(jìn)行復(fù)雜操作)
6.Index index= table.getIndex(INDEX_NAME);//獲取索引對(duì)象(對(duì)數(shù)據(jù)進(jìn)行索引查詢)
7.編寫數(shù)據(jù)表對(duì)應(yīng)數(shù)據(jù)類型對(duì)象(如下是一個(gè)非常簡單的DynamoDB數(shù)據(jù)表對(duì)象):
8.mapper.save(data);//保存數(shù)據(jù)
9.mapper.load(DATA.class, key,DynamoDBMapperConfig.ConsistentReads.CONSISTENT.config());//根據(jù)key獲取數(shù)據(jù)并轉(zhuǎn)換為相應(yīng)數(shù)據(jù)對(duì)象
10.QuerySpec spec = new QuerySpec().withKeyConditionExpression("index = :indexMark").withValueMap(new ValueMap().withString(":indexMark", indexMark)).withMaxResultSize(MAX_RESULT);//創(chuàng)建查詢對(duì)象
11.ItemCollection<QueryOutcome> items = index.query(spec);//根據(jù)index索引對(duì)象及spec查詢對(duì)象獲取查詢結(jié)果,最后可以使用Iterator<Item> iter = items.iterator();遍歷獲取每個(gè)對(duì)象
12.最后數(shù)據(jù)庫數(shù)據(jù)對(duì)象除了使用@DynamoDBTable及@DynamoDBHashKey外,還有@DynamoDBAttribute使用方法與其它的差不多减俏,區(qū)別在于這個(gè)是用來修飾普通字段的,還有@DynamoDBIndexHashKey是用來修飾索引字段的碱工。
13.最后附上個(gè)人aws簡單demo的git地址 https://github.com/makai554892700/AWSDemo.git