public CreateTableRequest constructCreateTableRequest() { ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<>(); attributeDefinitions.add(new AttributeDefinition().withAttributeName(partitionKeyName.toString()).withAttributeType("S")); attributeDefinitions.add(new AttributeDefinition().withAttributeName(sortKeyName.toString()).withAttributeType("N")); ArrayList<KeySchemaElement> keySchema = new ArrayList<>(); keySchema.add(new KeySchemaElement().withAttributeName(partitionKeyName.toString()).withKeyType(KeyType.HASH)); keySchema.add(new KeySchemaElement().withAttributeName(sortKeyName.toString()).withKeyType(KeyType.RANGE)); ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput() .withReadCapacityUnits(1L) .withWriteCapacityUnits(1L); CreateTableRequest request = new CreateTableRequest() .withTableName(tableName) .withKeySchema(keySchema) .withAttributeDefinitions(attributeDefinitions) .withProvisionedThroughput(provisionedThroughput); return request; }
private void createRecipientTable() { CreateTableRequest request = new CreateTableRequest() .withTableName(TABLE_NAME) .withAttributeDefinitions( new AttributeDefinition("_id", ScalarAttributeType.S) ) .withKeySchema( new KeySchemaElement("_id", KeyType.HASH) ) .withProvisionedThroughput(new ProvisionedThroughput(10L, 10L)); ddb.createTable(request); try { TableUtils.waitUntilActive(ddb, TABLE_NAME); } catch (InterruptedException e) { throw new RuntimeException(e); } }
@Override public void closeStorage() { try { dynamoDBConnection.getDynamoClient().describeTable(getTableName()); dynamoDBConnection.getDynamoClient().deleteTable(getTableName()); } catch(ResourceNotFoundException e) { } dynamoDBConnection.getDynamoDB().createTable(getTableName(), Collections.singletonList( new KeySchemaElement("_id", KeyType.HASH)), Collections.singletonList( new AttributeDefinition("_id", ScalarAttributeType.S)), new ProvisionedThroughput(1L, 1L)); }
@Override public RelDataType getRowType(RelDataTypeFactory typeFactory) { RelDataType type = super.getRowType(typeFactory); // force add the star field, this is a dynamic row type.getFieldCount(); // add the sort/partition keys that the mapper should produce if (key != null) { for (String field : key.getKeyNames()) { type.getField(field, true, false); } } // since we don't support pushing the key combination into the filter building, we need to // support the key schema elements in our key schema so hash/sort can be used in the filter. List<KeySchemaElement> keys = desc.getKeySchema(); for (KeySchemaElement elem : keys) { type.getField(elem.getAttributeName(), true, false); } return type; }
protected Table createHashAndSortTable(String pk, String sort) throws InterruptedException { ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<>(); ScalarAttributeType type = ScalarAttributeType.S; attributeDefinitions.add(new AttributeDefinition() .withAttributeName(pk).withAttributeType(type)); attributeDefinitions .add(new AttributeDefinition().withAttributeName(sort).withAttributeType(type)); ArrayList<KeySchemaElement> keySchema = new ArrayList<>(); keySchema.add(new KeySchemaElement().withAttributeName(pk).withKeyType(KeyType.HASH)); keySchema.add(new KeySchemaElement().withAttributeName(sort).withKeyType(KeyType.RANGE)); CreateTableRequest request = new CreateTableRequest() .withKeySchema(keySchema) .withAttributeDefinitions(attributeDefinitions); return createTable(request); }
public void deploy() { final AttributeDefinition idAttr = new AttributeDefinition().withAttributeName("id") .withAttributeType(ScalarAttributeType.S); final ProvisionedThroughput throughput = new ProvisionedThroughput().withReadCapacityUnits(5L) .withWriteCapacityUnits(5L); final KeySchemaElement idKey = new KeySchemaElement().withAttributeName("id").withKeyType(KeyType.HASH); final CreateTableRequest createTableRequest = new CreateTableRequest().withTableName("TranslateSlack") .withAttributeDefinitions(idAttr) .withKeySchema(idKey) .withProvisionedThroughput(throughput); ; ; ddb.createTable(createTableRequest); }
private DynamoDBQueryFilter getQueryFilter(JobConf conf, Map<String, String> hiveDynamoDBMapping, Map<String, String> hiveTypeMapping) throws IOException { if (hiveDynamoDBMapping == null) { /* * Column mapping may be null when user has mapped a DynamoDB item * onto a single hive map<string, string> column. */ return new DynamoDBQueryFilter(); } DynamoDBClient client = new DynamoDBClient(conf); String filterExprSerialized = conf.get(TableScanDesc.FILTER_EXPR_CONF_STR); if (filterExprSerialized == null) { return new DynamoDBQueryFilter(); } ExprNodeDesc filterExpr = ShimsLoader.getHiveShims().deserializeExpression(filterExprSerialized); DynamoDBFilterPushdown pushdown = new DynamoDBFilterPushdown(); List<KeySchemaElement> schema = client.describeTable(conf.get(DynamoDBConstants.TABLE_NAME)).getKeySchema(); DynamoDBQueryFilter queryFilter = pushdown.predicateToDynamoDBFilter( schema, hiveDynamoDBMapping, hiveTypeMapping, filterExpr); return queryFilter; }
private TableDescription getTableDescription(String hashType, String rangeType) { List<KeySchemaElement> keySchema = new ArrayList<>(); List<AttributeDefinition> definitions = new ArrayList<>(); keySchema.add(new KeySchemaElement().withAttributeName("hashKey").withKeyType(KeyType.HASH)); definitions.add(new AttributeDefinition().withAttributeName("hashKey").withAttributeType (hashType)); if (rangeType != null) { keySchema.add(new KeySchemaElement().withAttributeName("rangeKey").withKeyType(KeyType .RANGE)); definitions.add(new AttributeDefinition().withAttributeName("rangeKey").withAttributeType (rangeType)); } TableDescription description = new TableDescription().withKeySchema(keySchema) .withAttributeDefinitions(definitions).withProvisionedThroughput(new ProvisionedThroughputDescription().withReadCapacityUnits(1000L) .withWriteCapacityUnits(1000L)); return description; }
private static List<KeySchemaElement> toKeySchema(String tableName, IndexDescription index) { if ( null == index.getHashKey() ) { throw new NullPointerException( "Table ["+tableName+"] index ["+index.getIndexName()+"] contains null hashKey" ); } if ( null != index.getRangeKey() ) { return Arrays.asList( new KeySchemaElement() .withAttributeName(index.getHashKey().getAttrName()) .withKeyType(KeyType.HASH), new KeySchemaElement() .withAttributeName(index.getRangeKey().getAttrName()) .withKeyType(KeyType.RANGE)); } return Collections.singletonList( new KeySchemaElement() .withAttributeName(index.getHashKey().getAttrName()) .withKeyType(KeyType.HASH)); }
@Before public void setUp() { // Unique table for each run tableName = "table" + String.valueOf(tableCount++); dynamoDB.getDynamoDbClient().createTable( new CreateTableRequest() .withTableName(tableName) .withKeySchema(new KeySchemaElement("id", "HASH")) .withAttributeDefinitions( new AttributeDefinition("id", "S") ) .withProvisionedThroughput( new ProvisionedThroughput(1L, 1L) ) ); locker = new DynamoDbLocker( new DynamoDB(dynamoDB.getDynamoDbClient()), tableName, Clock.systemUTC() ); }
@Test public void testExecute() { command.execute(); List<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>(); keySchema.add(new KeySchemaElement().withAttributeName("name")); assertEquals("FULL_DESCRIBE_TABLE", ddbClient.describeTableRequest.getTableName()); assertEquals("FULL_DESCRIBE_TABLE", exchange.getIn().getHeader(DdbConstants.TABLE_NAME)); assertEquals("ACTIVE", exchange.getIn().getHeader(DdbConstants.TABLE_STATUS)); assertEquals(new Date(AmazonDDBClientMock.NOW), exchange.getIn().getHeader(DdbConstants.CREATION_DATE)); assertEquals(100L, exchange.getIn().getHeader(DdbConstants.ITEM_COUNT)); assertEquals(keySchema, exchange.getIn().getHeader(DdbConstants.KEY_SCHEMA)); assertEquals(20L, exchange.getIn().getHeader(DdbConstants.READ_CAPACITY)); assertEquals(10L, exchange.getIn().getHeader(DdbConstants.WRITE_CAPACITY)); assertEquals(1000L, exchange.getIn().getHeader(DdbConstants.TABLE_SIZE)); }
@Override public DescribeTableResult describeTable(DescribeTableRequest describeTableRequest) { this.describeTableRequest = describeTableRequest; String tableName = describeTableRequest.getTableName(); if ("activeTable".equals(tableName)) { return tableWithStatus(TableStatus.ACTIVE); } else if ("creatibleTable".equals(tableName) && createTableRequest != null) { return tableWithStatus(TableStatus.ACTIVE); } else if ("FULL_DESCRIBE_TABLE".equals(tableName)) { return new DescribeTableResult().withTable(new TableDescription() .withTableName(tableName) .withTableStatus(TableStatus.ACTIVE) .withCreationDateTime(new Date(NOW)) .withItemCount(100L) .withKeySchema(new KeySchemaElement().withAttributeName("name")) .withProvisionedThroughput(new ProvisionedThroughputDescription() .withReadCapacityUnits(20L) .withWriteCapacityUnits(10L)) .withTableSizeBytes(1000L)); } throw new ResourceNotFoundException(tableName + " is missing"); }
@Override public CreateTableRequest getTableSchema() { return super.getTableSchema() .withAttributeDefinitions( new AttributeDefinition() .withAttributeName(Constants.JANUSGRAPH_HASH_KEY) .withAttributeType(ScalarAttributeType.S), new AttributeDefinition() .withAttributeName(Constants.JANUSGRAPH_RANGE_KEY) .withAttributeType(ScalarAttributeType.S)) .withKeySchema( new KeySchemaElement() .withAttributeName(Constants.JANUSGRAPH_HASH_KEY) .withKeyType(KeyType.HASH), new KeySchemaElement() .withAttributeName(Constants.JANUSGRAPH_RANGE_KEY) .withKeyType(KeyType.RANGE)); }
/** * Used to create the Identity Table. This function only needs to be called * once. */ protected void createIdentityTable() throws DataAccessException { ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput() .withReadCapacityUnits(10L) .withWriteCapacityUnits(5L); ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>(); attributeDefinitions .add(new AttributeDefinition().withAttributeName(ATTRIBUTE_USERNAME).withAttributeType("S")); ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>(); tableKeySchema.add(new KeySchemaElement().withAttributeName(ATTRIBUTE_USERNAME).withKeyType(KeyType.HASH)); CreateTableRequest createTableRequest = new CreateTableRequest() .withTableName(USER_TABLE) .withProvisionedThroughput(provisionedThroughput) .withAttributeDefinitions(attributeDefinitions) .withKeySchema(tableKeySchema); try { ddb.createTable(createTableRequest); } catch (AmazonClientException e) { throw new DataAccessException("Failed to create table: " + USER_TABLE, e); } }
/** * Used to create the device table. This function only needs to be called * once. */ protected void createDeviceTable() throws DataAccessException { ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput() .withReadCapacityUnits(10L) .withWriteCapacityUnits(5L); ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>(); attributeDefinitions.add(new AttributeDefinition().withAttributeName( ATTRIBUTE_UID).withAttributeType("S")); ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>(); tableKeySchema.add(new KeySchemaElement().withAttributeName(ATTRIBUTE_UID) .withKeyType(KeyType.HASH)); CreateTableRequest createTableRequest = new CreateTableRequest() .withTableName(DEVICE_TABLE) .withProvisionedThroughput(provisionedThroughput) .withAttributeDefinitions(attributeDefinitions) .withKeySchema(tableKeySchema); try { ddb.createTable(createTableRequest); } catch (AmazonClientException e) { throw new DataAccessException("Failed to create table: " + DEVICE_TABLE, e); } }
public void createTable() { List<KeySchemaElement> keySchema = new ArrayList<>(); keySchema.add( new KeySchemaElement() .withAttributeName(sequenceNumber.getAttributeName()) .withKeyType(KeyType.HASH) ); ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput(); provisionedThroughput.setReadCapacityUnits(10L); provisionedThroughput.setWriteCapacityUnits(10L); CreateTableRequest request = new CreateTableRequest() .withTableName("example_table") .withKeySchema(keySchema) .withAttributeDefinitions(singleton(sequenceNumber)) .withProvisionedThroughput(provisionedThroughput); client.createTable(request); }
private CreateTableResult createTable() throws Exception { List<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>(); AttributeDefinition attributeDefinition = new AttributeDefinition() .withAttributeName(TEST_ATTRIBUTE) .withAttributeType(ScalarAttributeType.S); attributeDefinitions.add(attributeDefinition); String tableName = TEST_TABLE_NAME; List<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>(); KeySchemaElement keySchemaElement = new KeySchemaElement() .withAttributeName(TEST_ATTRIBUTE) .withKeyType(KeyType.HASH); ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput() .withReadCapacityUnits(UNITS) .withWriteCapacityUnits(UNITS); CreateTableResult result = dynamoDb.createTable(attributeDefinitions, tableName, keySchema, provisionedThroughput); return result; }
/** * Create a table with the given hashKey as row id * * @param tableName * @param primaryKey */ public static void createTable(String tableName, String primaryKey) { ArrayList<KeySchemaElement> ks = new ArrayList<KeySchemaElement>(); ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>(); ks.add(new KeySchemaElement().withAttributeName(primaryKey) .withKeyType(KeyType.HASH)); attributeDefinitions.add(new AttributeDefinition().withAttributeName( primaryKey).withAttributeType("S")); CreateTableRequest request = new CreateTableRequest() .withTableName(tableName).withKeySchema(ks) .withProvisionedThroughput(DEFAULT_PROVISIONED_THROUGHPUT); request.setAttributeDefinitions(attributeDefinitions); try { DynamoDbHandler.CLIENT.createTable(request); } catch (ResourceInUseException e) { //System.err.println("Table '" + tableName + "' already exists"); } }
/** * Generate a list of attribute names found in the Aggregator's dynamo * table. Assumes that all Items in the Aggregator table are of the same * structure. * * @param dynamoClient * Dynamo DB Client to use for connection to Dynamo DB. * @param dynamoTable * The Dynamo Table for the Aggregator * @return A list of attribute names from the Dynamo table * @throws Exception */ public static List<String> getDictionaryEntry( final AmazonDynamoDB dynamoClient, final String dynamoTable) throws Exception { // get a list of all columns in the table, with keys first List<String> columns = new ArrayList<>(); List<KeySchemaElement> keys = dynamoClient.describeTable(dynamoTable) .getTable().getKeySchema(); for (KeySchemaElement key : keys) { columns.add(key.getAttributeName()); } ScanResult scan = dynamoClient.scan(new ScanRequest() .withTableName(dynamoTable).withSelect(Select.ALL_ATTRIBUTES) .withLimit(1)); List<Map<String, AttributeValue>> scannedItems = scan.getItems(); for (Map<String, AttributeValue> map : scannedItems) { for (String s : map.keySet()) { if (!columns.contains(s)) columns.add(s); } } return columns; }
/** * Generate a list of attribute names found in the Aggregator's dynamo * table. Assumes that all Items in the Aggregator table are of the same * structure. * * @param dynamoClient Dynamo DB Client to use for connection to Dynamo DB. * @param dynamoTable The Dynamo Table for the Aggregator * @return A list of attribute names from the Dynamo table * @throws Exception */ protected List<String> getDictionaryEntry() throws Exception { // get a list of all columns in the table, with keys first List<String> columns = new ArrayList<>(); List<KeySchemaElement> keys = dynamoClient.describeTable(this.tableName).getTable().getKeySchema(); for (KeySchemaElement key : keys) { columns.add(key.getAttributeName()); } ScanResult scan = dynamoClient.scan(new ScanRequest().withTableName(this.tableName).withSelect( Select.ALL_ATTRIBUTES).withLimit(1)); List<Map<String, AttributeValue>> scannedItems = scan.getItems(); for (Map<String, AttributeValue> map : scannedItems) { for (String s : map.keySet()) { if (!columns.contains(s)) columns.add(s); } } return columns; }
protected void init() throws Exception { List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>() { { add(new AttributeDefinition().withAttributeName(InventoryModel.AGGREGATOR).withAttributeType( "S")); add(new AttributeDefinition().withAttributeName(InventoryModel.SHARD_ID).withAttributeType( "S")); } }; List<KeySchemaElement> key = new ArrayList<KeySchemaElement>() { { add(new KeySchemaElement().withAttributeName(InventoryModel.AGGREGATOR).withKeyType( KeyType.HASH)); add(new KeySchemaElement().withAttributeName(InventoryModel.SHARD_ID).withKeyType( KeyType.RANGE)); } }; DynamoUtils.initTable(dynamoClient, InventoryModel.TABLE_NAME, InventoryModel.READ_CAPACITY, InventoryModel.WRITE_CAPACITY, attributes, key, null); online = true; }
/** * Creates a table in AWS DynamoDB. * @param appid name of the {@link com.erudika.para.core.App} * @param readCapacity read capacity * @param writeCapacity write capacity * @return true if created */ public static boolean createTable(String appid, long readCapacity, long writeCapacity) { if (StringUtils.isBlank(appid)) { return false; } else if (StringUtils.containsWhitespace(appid)) { logger.warn("DynamoDB table name contains whitespace. The name '{}' is invalid.", appid); return false; } else if (existsTable(appid)) { logger.warn("DynamoDB table '{}' already exists.", appid); return false; } try { String table = getTableNameForAppid(appid); getClient().createTable(new CreateTableRequest().withTableName(table). withKeySchema(new KeySchemaElement(Config._KEY, KeyType.HASH)). withAttributeDefinitions(new AttributeDefinition(Config._KEY, ScalarAttributeType.S)). withProvisionedThroughput(new ProvisionedThroughput(readCapacity, writeCapacity))); logger.info("Created DynamoDB table '{}'.", table); } catch (Exception e) { logger.error(null, e); return false; } return true; }
/** * Builds hash key attribute from generic query received. * * @param qKey * * @returnAttributeValue build from query */ private Map<String, AttributeValue> buildHashKey(K qKey) { Map<String, AttributeValue> hashKey = new HashMap<>(); for (KeySchemaElement key : getKeySchema()) { AttributeValue attr = new AttributeValue(); if (key.getKeyType().equals(KeyType.HASH.toString())) { if (keyItems.get(key.getAttributeName()).equals("N")) { attr.withN(getHashKey(qKey).toString()); } else if (keyItems.get(key.getAttributeName()).equals("S")) { attr.withS(getHashKey(qKey).toString()); } else if (keyItems.get(key.getAttributeName()).equals("B")) { attr.withB(ByteBuffer.wrap(getHashKey(qKey).toString().getBytes(Charset.defaultCharset()))); } else { throw new IllegalArgumentException("Data type not supported for " + key.getAttributeName()); } hashKey.put(key.getAttributeName(), attr); } } if (hashKey.isEmpty()) { throw new IllegalStateException("No key value has been defined."); } return hashKey; }
/** * Builds range key attribute from generic query received. * * @param qKey * * @return */ private Map<String, AttributeValue> buildRangeKey(K qKey) { Map<String, AttributeValue> kAttrs = new HashMap<>(); for (KeySchemaElement key : getKeySchema()) { AttributeValue attr = new AttributeValue(); if (key.getKeyType().equals(KeyType.RANGE.toString())) { if (keyItems.get(key.getAttributeName()).equals("N")) { attr.withN(getRangeKey(qKey).toString()); } else if (keyItems.get(key.getAttributeName()).equals("S")) { attr.withS(getRangeKey(qKey).toString()); } else if (keyItems.get(key.getAttributeName()).equals("B")) { attr.withB(ByteBuffer.wrap(getRangeKey(qKey).toString().getBytes(Charset.defaultCharset()))); } else { throw new IllegalArgumentException("Data type not supported for " + key.getAttributeName()); } kAttrs.put(key.getAttributeName(), attr); } } return kAttrs; }
/** * Method in charge of compiling a specific table using a key schema and a set * of attributes * * @param pTableName * @param arrayList * @param map */ private void compile(String pTableName, ArrayList<KeySchemaElement> arrayList, Map<String, String> map){ try { startFile(pTableName, pTableName); setHeaders(packageName); line(0, ""); line(0, "@DynamoDBTable(tableName = \"" + pTableName + "\")"); line(0, "public class " + pTableName + " implements Persistent {"); for (KeySchemaElement pKeySchema : arrayList) { setKeyAttributes(pKeySchema, map.get(pKeySchema.getAttributeName()), 2); setKeyMethods(pKeySchema, map.get(pKeySchema.getAttributeName()), 2); map.remove(pKeySchema.getAttributeName()); } setItems(map, 2); setDefaultMethods(2, pTableName); line(0, "}"); out.flush(); out.close(); } catch (IOException e) { log.error("Error while compiling table {}",pTableName, e.getMessage()); throw new RuntimeException(e); } }
/** * Executes a create table request using the DynamoDB client and waits the * default time until it's been created. * * @param awsClient * @param keySchema * @param tableName * @param proThrou */ public static void executeCreateTableRequest(AmazonDynamoDB awsClient, String tableName, ArrayList<KeySchemaElement> keySchema, Map<String, String> attrs, ProvisionedThroughput proThrou) { CreateTableRequest createTableRequest = buildCreateTableRequest(tableName, keySchema, proThrou, attrs); // use the client to perform the request try { awsClient.createTable(createTableRequest).getTableDescription(); // wait for table to become active waitForTableToBecomeAvailable(awsClient, tableName); } catch (ResourceInUseException ex) { LOG.warn("Table '{}' already exists.", tableName); } finally { LOG.info("Table '{}' is available.", tableName); } }
/** * Builds the necessary requests to create tables * * @param tableName * @param keySchema * @param proThrou * @param attrs * @return */ public static CreateTableRequest buildCreateTableRequest(String tableName, ArrayList<KeySchemaElement> keySchema, ProvisionedThroughput proThrou, Map<String, String> attrs) { CreateTableRequest createTableRequest = new CreateTableRequest(); createTableRequest.setTableName(tableName); createTableRequest.setKeySchema(keySchema); ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>(); for (KeySchemaElement kEle : keySchema) { AttributeDefinition attrDef = new AttributeDefinition(); attrDef.setAttributeName(kEle.getAttributeName()); attrDef.setAttributeType(attrs.get(kEle.getAttributeName())); attributeDefinitions.add(attrDef); } createTableRequest.setAttributeDefinitions(attributeDefinitions); createTableRequest.setProvisionedThroughput(proThrou); return createTableRequest; }
@Override public void createStoreIfAbsent(String storeName, boolean bBinaryValues) { String tableName = storeToTableName(storeName); if (!Tables.doesTableExist(m_ddbClient, tableName)) { // Create a table with a primary hash key named '_key', which holds a string m_logger.info("Creating table: {}", tableName); CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName) .withKeySchema(new KeySchemaElement() .withAttributeName(ROW_KEY_ATTR_NAME) .withKeyType(KeyType.HASH)) .withAttributeDefinitions(new AttributeDefinition() .withAttributeName(ROW_KEY_ATTR_NAME) .withAttributeType(ScalarAttributeType.S)) .withProvisionedThroughput(new ProvisionedThroughput() .withReadCapacityUnits(READ_CAPACITY_UNITS) .withWriteCapacityUnits(WRITE_CAPACITY_UNITS)); m_ddbClient.createTable(createTableRequest).getTableDescription(); try { Tables.awaitTableToBecomeActive(m_ddbClient, tableName); } catch (InterruptedException e) { throw new RuntimeException(e); } } }
static void createExampleTable() { // Provide the initial provisioned throughput values as Java long data types ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput() .withReadCapacityUnits(5L) .withWriteCapacityUnits(6L); CreateTableRequest request = new CreateTableRequest() .withTableName(tableName) .withProvisionedThroughput(provisionedThroughput); ArrayList<AttributeDefinition> attributeDefinitions= new ArrayList<AttributeDefinition>(); attributeDefinitions.add(new AttributeDefinition().withAttributeName("Id").withAttributeType("N")); request.setAttributeDefinitions(attributeDefinitions); ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>(); tableKeySchema.add(new KeySchemaElement().withAttributeName("Id").withKeyType(KeyType.HASH)); request.setKeySchema(tableKeySchema); client.createTable(request); waitForTableToBecomeAvailable(tableName); getTableInformation(); }
public static void main(String[] args) throws Exception { AmazonDynamoDBClient client = new AmazonDynamoDBClient(); client.setEndpoint("http://localhost:8000"); DynamoDB dynamoDB = new DynamoDB(client); String tableName = "Movies"; Table table = dynamoDB.createTable(tableName, Arrays.asList( new KeySchemaElement("year", KeyType.HASH), new KeySchemaElement("title", KeyType.RANGE)), Arrays.asList( new AttributeDefinition("year", ScalarAttributeType.N), new AttributeDefinition("title", ScalarAttributeType.S)), new ProvisionedThroughput(10L, 10L)); try { TableUtils.waitUntilActive(client, tableName); System.out.println("Table status: " + table.getDescription().getTableStatus()); } catch (AmazonClientException e) { e.printStackTrace(); System.exit(1); } }
private static CreateTableResult createTable(AmazonDynamoDB ddb, String tableName, String hashKeyName) { List<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>(); attributeDefinitions.add(new AttributeDefinition(hashKeyName, ScalarAttributeType.S)); List<KeySchemaElement> ks = new ArrayList<KeySchemaElement>(); ks.add(new KeySchemaElement(hashKeyName, KeyType.HASH)); ProvisionedThroughput provisionedthroughput = new ProvisionedThroughput(1000L, 1000L); CreateTableRequest request = new CreateTableRequest() .withTableName(tableName) .withAttributeDefinitions(attributeDefinitions) .withKeySchema(ks) .withProvisionedThroughput(provisionedthroughput); return ddb.createTable(request); }
private void initializeHashAndRangeTable(String name, String hashName, String rangeName) { String tableName = quartzPrefix + name; if (!tableExists(tableName)) { log.info(String.format("Creating table '%s' with hash and range index.", tableName)); CreateTableRequest request = new CreateTableRequest() .withTableName(tableName) .withKeySchema( new KeySchemaElement().withKeyType(KeyType.HASH).withAttributeName(hashName), new KeySchemaElement().withKeyType(KeyType.RANGE).withAttributeName(rangeName)) .withAttributeDefinitions( new AttributeDefinition(hashName, ScalarAttributeType.S), new AttributeDefinition(rangeName, ScalarAttributeType.S)) .withProvisionedThroughput(new ProvisionedThroughput(1L, 1L)); client.createTable(request); waitForTable(tableName); } else { log.info(String.format("Table '%s' already exists.", tableName)); } }
private void initializeHashTable(String name, String hashName) { String tableName = quartzPrefix + name; if (!tableExists(tableName)) { log.info(String.format("Creating table '%s' with hash index.", tableName)); CreateTableRequest request = new CreateTableRequest() .withTableName(tableName) .withKeySchema( new KeySchemaElement().withKeyType(KeyType.HASH).withAttributeName(hashName)) .withAttributeDefinitions( new AttributeDefinition(hashName, ScalarAttributeType.S)) .withProvisionedThroughput(new ProvisionedThroughput(1L, 1L)); client.createTable(request); waitForTable(tableName); } else { log.info(String.format("Table '%s' already exists.", tableName)); } }
public static void createSessionTable(AmazonDynamoDBClient dynamo, String tableName, long readCapacityUnits, long writeCapacityUnits) { CreateTableRequest request = new CreateTableRequest().withTableName(tableName); request.withKeySchema(new KeySchemaElement().withAttributeName(DynamoSessionItem.SESSION_ID_ATTRIBUTE_NAME) .withKeyType(KeyType.HASH)); request.withAttributeDefinitions( new AttributeDefinition().withAttributeName(DynamoSessionItem.SESSION_ID_ATTRIBUTE_NAME) .withAttributeType(ScalarAttributeType.S)); request.setProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(readCapacityUnits) .withWriteCapacityUnits(writeCapacityUnits)); dynamo.createTable(request); }
/** * Get key schema elements. * @param json JSON input * @return Key schema elements */ private Collection<KeySchemaElement> keySchema(final JsonObject json) { final Collection<KeySchemaElement> keys = new LinkedList<KeySchemaElement>(); final JsonArray schema = json.getJsonArray("KeySchema"); for (final JsonValue value : schema) { final JsonObject element = (JsonObject) value; keys.add( new KeySchemaElement( element.getString("AttributeName"), element.getString("KeyType") ) ); } return keys; }
private CreateTableResult createTable() { final List<AttributeDefinition> attributeDefinitions = new ArrayList<>(); attributeDefinitions.add(new AttributeDefinition(RESOURCE_NAME_ATT, ScalarAttributeType.S)); attributeDefinitions.add(new AttributeDefinition(RDF_TRIPLE_ATT, ScalarAttributeType.S)); attributeDefinitions.add(new AttributeDefinition(RDF_PREDICATE_ATT, ScalarAttributeType.S)); attributeDefinitions.add(new AttributeDefinition(RDF_OBJECT_ATT, ScalarAttributeType.S)); final List<KeySchemaElement> keySchema = new ArrayList<>(); keySchema.add(new KeySchemaElement(RESOURCE_NAME_ATT, KeyType.HASH)); keySchema.add(new KeySchemaElement(RDF_TRIPLE_ATT, KeyType.RANGE)); final ProvisionedThroughput provisionedthroughput = new ProvisionedThroughput(10L, 10L); final LocalSecondaryIndex predicateIndex = new LocalSecondaryIndex() .withIndexName(PREDICATE_INDEX_NAME) .withKeySchema(new KeySchemaElement(RESOURCE_NAME_ATT, KeyType.HASH)) .withKeySchema(new KeySchemaElement(RDF_PREDICATE_ATT, KeyType.RANGE)) .withProjection(new Projection().withNonKeyAttributes(RDF_SUBJECT_ATT, RDF_OBJECT_ATT) .withProjectionType(ProjectionType.INCLUDE)); final GlobalSecondaryIndex objectIndex = new GlobalSecondaryIndex() .withIndexName(OBJECT_INDEX_NAME) .withKeySchema(new KeySchemaElement(RDF_OBJECT_ATT, KeyType.HASH)) .withKeySchema(new KeySchemaElement(RDF_PREDICATE_ATT, KeyType.RANGE)) .withProjection(new Projection().withNonKeyAttributes(RDF_SUBJECT_ATT) .withProjectionType(ProjectionType.INCLUDE)) .withProvisionedThroughput(new ProvisionedThroughput(10L, 10L)); final CreateTableRequest request = new CreateTableRequest() .withTableName(TABLE_NAME) .withAttributeDefinitions(attributeDefinitions) .withKeySchema(keySchema) .withProvisionedThroughput(provisionedthroughput) .withLocalSecondaryIndexes(predicateIndex) .withGlobalSecondaryIndexes(objectIndex); return dynamodbClient.createTable(request); }
/** * Create ticket tables. * * @param deleteTables the delete tables */ public void createTicketTables(final boolean deleteTables) { final Collection<TicketDefinition> metadata = this.ticketCatalog.findAll(); metadata.forEach(Unchecked.consumer(r -> { final CreateTableRequest request = new CreateTableRequest() .withAttributeDefinitions(new AttributeDefinition(ColumnNames.ID.getName(), ScalarAttributeType.S)) .withKeySchema(new KeySchemaElement(ColumnNames.ID.getName(), KeyType.HASH)) .withProvisionedThroughput(new ProvisionedThroughput(dynamoDbProperties.getReadCapacity(), dynamoDbProperties.getWriteCapacity())) .withTableName(r.getProperties().getStorageName()); if (deleteTables) { final DeleteTableRequest delete = new DeleteTableRequest(r.getProperties().getStorageName()); LOGGER.debug("Sending delete request [{}] to remove table if necessary", delete); TableUtils.deleteTableIfExists(amazonDynamoDBClient, delete); } LOGGER.debug("Sending delete request [{}] to create table", request); TableUtils.createTableIfNotExists(amazonDynamoDBClient, request); LOGGER.debug("Waiting until table [{}] becomes active...", request.getTableName()); TableUtils.waitUntilActive(amazonDynamoDBClient, request.getTableName()); final DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(request.getTableName()); LOGGER.debug("Sending request [{}] to obtain table description...", describeTableRequest); final TableDescription tableDescription = amazonDynamoDBClient.describeTable(describeTableRequest).getTable(); LOGGER.debug("Located newly created table with description: [{}]", tableDescription); })); }