/** * Creates key getters and setters * @param pKeySchemaThe key schema for a specific table * @param pIdenNumber of spaces used for indentation * @throws IOException */ private void setKeyMethods(KeySchema pKeySchema, int pIden) throws IOException{ KeySchemaElement hashKey = pKeySchema.getHashKeyElement(); KeySchemaElement rangeKey = pKeySchema.getRangeKeyElement(); StringBuilder strBuilder = new StringBuilder(); // hash key if(hashKey != null){ strBuilder.append("@DynamoDBHashKey(attributeName=\"" + hashKey.getAttributeName() + "\") \n"); strBuilder.append(" public String getHashKey() { return " + hashKey.getAttributeName() + "; } \n"); strBuilder.append(" public void setHashKey(" + (hashKey.getAttributeType().equals("S")?"String ":"double ")); strBuilder.append("p" + camelCasify(hashKey.getAttributeName()) + "){ this." + hashKey.getAttributeName()); strBuilder.append(" = p" + camelCasify(hashKey.getAttributeName()) + "; }"); line(pIden, strBuilder.toString()); } strBuilder.delete(0, strBuilder.length()); // range key if(rangeKey != null){ strBuilder.append("@DynamoDBRangeKey(attributeName=\"" + rangeKey.getAttributeName() + "\") \n"); strBuilder.append(" public String getRangeKey() { return " + rangeKey.getAttributeName() + "; } \n"); strBuilder.append(" public void setRangeKey(" + (rangeKey.getAttributeType().equals("S")?"String ":"double ")); strBuilder.append("p" + camelCasify(rangeKey.getAttributeName()) + "){ this." + rangeKey.getAttributeName()); strBuilder.append(" = p" + camelCasify(rangeKey.getAttributeName()) + "; }"); line(pIden, strBuilder.toString()); } line(0, ""); }
/** * Creates the key attributes within the generated class * @param pKeySchemaKey schema * @param pIdenNumber of spaces used for indentation * @throws IOException */ private void setKeyAttributes(KeySchema pKeySchema, int pIden) throws IOException{ KeySchemaElement hashKey = pKeySchema.getHashKeyElement(); KeySchemaElement rangeKey = pKeySchema.getRangeKeyElement(); StringBuilder strBuilder = new StringBuilder(); // hash key if(hashKey != null){ strBuilder.append("private " + (hashKey.getAttributeType().equals("S")?"String ":"double ")); strBuilder.append(hashKey.getAttributeName() + ";"); line(pIden, strBuilder.toString()); } strBuilder.delete(0, strBuilder.length()); // range key if(rangeKey != null){ strBuilder.append("private " + (rangeKey.getAttributeType().equals("S")?"String ":"double ")); strBuilder.append(rangeKey.getAttributeName() + ";"); line(pIden, strBuilder.toString()); } line(0, ""); }
/** * Verifies is a table has a key schema defined * @param tableName Table name to determine which key schema to obtain * @return */ private boolean verifyKeySchema(String tableName){ KeySchema kSchema = tablesToKeySchemas.get(tableName); if (kSchema == null) return false; KeySchemaElement rangeKey = kSchema.getRangeKeyElement(); KeySchemaElement hashKey = kSchema.getHashKeyElement(); // A range key must have a hash key as well if (rangeKey != null){ if (hashKey != null) return true; else return false; } // A hash key may exist by itself if (hashKey != null) return true; return false; }
/** * Creates the table in DynamoDB. The hash+range key is: * * Hash (String) | Range (String) * File Path File Name * Example: s3n://netflix/data test.xml * * The table also includes one attribute for epoch (time created), but * that is only defined during the put operation (see add()). * */ private void createTable() { log.info("Creating table in DynamoDB: " + tableName); CreateTableRequest createRequest = new CreateTableRequest(); createRequest.withTableName(tableName); //Key KeySchemaElement pathKey = new KeySchemaElement().withAttributeName(HASH_KEY).withAttributeType(ScalarAttributeType.S); KeySchemaElement fileKey = new KeySchemaElement().withAttributeName(RANGE_KEY).withAttributeType(ScalarAttributeType.S); KeySchema schema = new KeySchema(); schema.setHashKeyElement(pathKey); schema.setRangeKeyElement(fileKey); createRequest.setKeySchema(schema); //Throughput ProvisionedThroughput tp = new ProvisionedThroughput(); tp.setReadCapacityUnits(readUnits); tp.setWriteCapacityUnits(writeUnits); createRequest.setProvisionedThroughput(tp); db.createTable(createRequest); }
@Test public void testMissingPrimaryKey() throws IOException, InterruptedException { String tableName = "mortar_test_foo_table"; String awsAccessKeyId = "XXXXXXXXXXXXX"; String awsSecretKey = "YYYYYYYYYYYYYY"; ResourceSchema schema = new ResourceSchema(Utils.getSchemaFromString("my_field:int")); // mock dynamo client AmazonDynamoDBClient dynamo = mock(AmazonDynamoDBClient.class); DescribeTableResult describeResult = new DescribeTableResult() .withTable( new TableDescription() .withProvisionedThroughput( new ProvisionedThroughputDescription().withWriteCapacityUnits(50L)) .withKeySchema(new KeySchema() .withHashKeyElement(new KeySchemaElement() .withAttributeName("not_the_key_you_will_find") .withAttributeType(ScalarAttributeType.N)))); when(dynamo.describeTable(any(DescribeTableRequest.class))).thenReturn(describeResult); DynamoDBStorage storage = new DynamoDBStorage(tableName, awsAccessKeyId, awsSecretKey, dynamo, null); try { storage.checkSchema(schema); Assert.fail("Expected schema validation to fail"); } catch(IOException e) { Assert.assertTrue("Expected " + e.getMessage() + " to contain hash msg", e.getMessage().contains("hash primary key")); } }