一 AWS DynamoDb在java中的使用【建立连接】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 accessKey = “xxxxxxx”; secretKey = “xxxxxxxx” if (StringUtils.isNotBlank(accessKey) && StringUtils.isNotBlank(secretKey)) { logger.debug("accessKey和secretKey有值,不是写在系统配置里的方式" ); bac = new BasicAWSCredentials(accessKey, secretKey); } if (bac != null ) { client = new AmazonDynamoDBClient(bac); } else { client = new AmazonDynamoDBClient(); } String region= “china”;if (region!= null && region.trim().equals("china" )) { logger.debug("中国区域" ); regions = Regions.CN_NORTH_1; client.withRegion(Region.getRegion(Regions.CN_NORTH_1)); } else { logger.debug("us-west-2" ); regions = Regions.US_WEST_2; client.withRegion(Region.getRegion(Regions.US_WEST_2)); } dynamoDB = new DynamoDB(client); mapper = new DynamoDBMapper(client);
二 AWS DynamoDb在java中的使用【建表】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 public void createTable (String tableName, String key, String keyType, Long readCapacityUnits, Long writeCapacityUnits ) { try { ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>(); attributeDefinitions.add(new AttributeDefinition() .withAttributeName(key) .withAttributeType(keyType)); ArrayList<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>(); keySchema.add(new KeySchemaElement() .withAttributeName(key) .withKeyType(KeyType.HASH)); CreateTableRequest request = new CreateTableRequest() .withTableName(tableName) .withKeySchema(keySchema) .withAttributeDefinitions(attributeDefinitions) .withProvisionedThroughput(new ProvisionedThroughput() .withReadCapacityUnits(readCapacityUnits) .withWriteCapacityUnits(writeCapacityUnits)); System.out.println("Issuing CreateTable request for " + tableName); Table table = dynamoDB.createTable(request); System.out.println("Waiting for " + tableName + " to be created...this may take a while..." ); table.waitForActive(); } catch (Exception e) { System.err.println("CreateTable request failed for " + tableName); System.err.println(e.getMessage()); } }
三 AWS DynamoDb在java中的使用【获取表信息】1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public void getTableInformation ( ) { System.out.println("Describing " + tableName); TableDescription tableDescription = dynamoDB.getTable(tableName).describe(); System.out.format("Name: %s:\n" + "Status: %s \n" + "Provisioned Throughput (read capacity units/sec): %d \n" + "Provisioned Throughput (write capacity units/sec): %d \n" , tableDescription.getTableName(), tableDescription.getTableStatus(), tableDescription.getProvisionedThroughput().getReadCapacityUnits(), tableDescription.getProvisionedThroughput().getWriteCapacityUnits()); }
四 AWS DynamoDb在java中的使用【查询所有表】1 2 3 4 5 6 7 8 9 10 11 12 13 public void listMyTables ( ) { TableCollection<ListTablesResult> tables = dynamoDB.listTables(); Iterator<Table> iterator = tables.iterator(); System.out.println("Listing table names" ); while (iterator.hasNext()) { Table table = iterator.next(); System.out.println(table.getTableName()); } }
五 AWS DynamoDb在java中的使用【映射查询】1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public <T> List<T> query (T o, Class<T> clazz ) { DynamoDBQueryExpression<T> queryExpression = new DynamoDBQueryExpression<T>() .withHashKeyValues(o); List<T> itemList = mapper.query(clazz, queryExpression); return itemList; } public static void main (String [] args ) { AWSUtilAPI awsUtilAPI = AWSUtilAPI.getInstanceVersion3(); DynomadbObject dlc = new DynomadbObject(); dlc.setBatch("" ); List<DynomadbObject> dlcs= awsUtilAPI.query(dlc,DynomadbObject.class); }
类DynomadbObject1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 package com.test.util; import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey;import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;@DynamoDBTable(tableName="td1" ) public class DynomadbObject { private String batch; private String job; private String category; private int length; private String runname; private String status; private String progress; @DynamoDBHashKey(attributeName="batch" ) public String getBatch ( ) { return batch; } public void setBatch (String batch ) { this .batch = batch; } @DynamoDBRangeKey(attributeName="job" ) public String getJob ( ) { return job; } public void setJob (String job ) { this .job = job; } @DynamoDBAttribute(attributeName="category" ) public String getCategory ( ) { return category; } public void setCategory (String category ) { this .category = category; } @DynamoDBAttribute(attributeName="length" ) public int getLength ( ) { return length; } public void setLength (int length ) { this .length = length; } @DynamoDBAttribute(attributeName="runname" ) public String getRunname ( ) { return runname; } public void setRunname (String runname ) { this .runname = runname; } @DynamoDBAttribute(attributeName="status" ) public String getStatus ( ) { return status; } public void setStatus (String status ) { this .status = status; } @DynamoDBAttribute(attributeName="progress" ) public String getProgress ( ) { return progress; } public void setProgress (String progress ) { try { this .progress = progress; }catch (Exception e){ e.printStackTrace(); } } }
六 根据key查询1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 public Item query (String key, Object keyValue ) { logger.debug("进入query()" ); logger.debug("table:" +tableName); Table table = dynamoDB.getTable(tableName); GetItemSpec spec = null ; if (keyValue instanceof Number ) { if (keyValue instanceof Integer) { int value = (Integer) keyValue; spec = new GetItemSpec().withPrimaryKey(key, keyValue); } else if (keyValue instanceof Double) { double value = (Double) keyValue; spec = new GetItemSpec().withPrimaryKey(key, value); } } else { spec = new GetItemSpec().withPrimaryKey(key, keyValue); } try { Item outcome = table.getItem(spec); return outcome; } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); } return null ; }
七 多主键联合查询1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 public Item query (String key, Object keyValue ) { logger.debug("进入query()" ); logger.debug("table:" +tableName); Table table = dynamoDB.getTable(tableName); GetItemSpec spec = null ; if (keyValue instanceof Number ) { if (keyValue instanceof Integer) { int value = (Integer) keyValue; spec = new GetItemSpec().withPrimaryKey(key, keyValue); } else if (keyValue instanceof Double) { double value = (Double) keyValue; spec = new GetItemSpec().withPrimaryKey(key, value); } } else { spec = new GetItemSpec().withPrimaryKey(key, keyValue); } try { Item outcome = table.getItem(spec); return outcome; } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); } return null ; }
八 scan方式查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 public List<Map <String ,String >> scan (String tableName,String filterColumn,String filterValue,String columns,String reservedKeyWordColumn ) { List<Map <String ,String >> list = new ArrayList<>(); Map <String , AttributeValue> expressionAttributeValues = new HashMap<>(); expressionAttributeValues.put(":val" , new AttributeValue().withS(filterValue)); Map <String , String > expressionAttributeNames = new HashMap<>(); StringBuffer rColumns = new StringBuffer("" ); if (StringUtils.isNotBlank(reservedKeyWordColumn)){ String [] rKeys = reservedKeyWordColumn.split("," ); for (String rKey:rKeys){ expressionAttributeNames.put("#" +rKey,rKey); rColumns.append("," ); rColumns.append("#" +rKey); } } String projections = columns + rColumns.toString(); ScanRequest scanRequest = new ScanRequest() .withTableName(tableName) .withFilterExpression(filterColumn+" = :val" ) .withProjectionExpression(projections) .withExpressionAttributeValues(expressionAttributeValues) .withLimit(50 ); if (expressionAttributeNames.size() > 0 ){ scanRequest.withExpressionAttributeNames(expressionAttributeNames); } ScanResult result = client.scan(scanRequest); String [] columnNames = columns.split("," ); String [] rColumnNames = reservedKeyWordColumn.split("," ); List<Map <String ,String >> list1 = parseToList(result,columnNames,rColumnNames); list.addAll(list1); while (result.getLastEvaluatedKey()!=null ){ scanRequest.setExclusiveStartKey(result.getLastEvaluatedKey()); result = client.scan(scanRequest); List<Map <String ,String >> list2 = parseToList(result,columnNames,rColumnNames); list.addAll(list2); } return list; }
九 根据key查询某个字段的值1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public String getValuesByParam (String key, Object keyValue, String param ) { Item item = query(key, keyValue); if (item == null ) { return null ; } String value = item.getString(param); return value; }
十 推送aws(插入记录)1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 public boolean insertItem (String path, Batch batch ) { logger.debug("准备插入batch表:" +tableName); List<String > list = getFilePaths(path); checkAwsIllegalToken(list); String task_list = makeCode(list, batch.getStage()); int calculationTime = evalCalculationTime(path, batch.getStage()); batch.setCalculation_time(calculationTime); Date date = new Date (); DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" ); String time = format.format(date); batch.setCreated(time); batch.setTotalDlcCount(list.size()); batch.setTask_list(task_list); List<Item> items = new ArrayList<Item>(); Item item = new Item() .withPrimaryKey("batch" , batch.getBatch()) .withString("created" , batch.getCreated()) .withNumber("total_dlc_count" , batch.getTotalDlcCount()) .withNumber("calculation_time" , batch.getCalculation_time()) .withString("owner" , batch.getOwner()) .withString("version" , batch.getVersion()) .withString("status" , batch.getStatus()) .withString("task_list" , batch.getTask_list()); items.add(item); logger.debug("createItems start..." ); return createItems(tableName, items); } public boolean createItems (String tableName, List<Item> list ) { Table table = dynamoDB.getTable(tableName); try { for (int i = 0 ; i < list.size(); i++) { Item item = list.get(i); PutItemOutcome outcome = table.putItem(item); logger.debug("AWS 生风任务推送成功:\n" + outcome.getPutItemResult()); } return true ; } catch (Exception e) { logger.debug("AWS 生风任务推送失败." ); logger.debug(e.getMessage()); } return false ; } public void createItem (String tableName, Item item ) { Table table = dynamoDB.getTable(tableName); try { PutItemOutcome outcome = table.putItem(item); System.out.println("PutItem succeeded:\n" + outcome.getPutItemResult()); } catch (Exception e) { System.err.println("Create items failed." ); System.err.println(e.getMessage()); } }