Search in sources :

Example 66 with DynamoDB

use of com.amazonaws.services.dynamodbv2.document.DynamoDB in project nifi by apache.

the class ITAbstractDynamoDBTest method beforeClass.

@BeforeClass
public static void beforeClass() throws Exception {
    FileInputStream fis = new FileInputStream(CREDENTIALS_FILE);
    final PropertiesCredentials credentials = new PropertiesCredentials(fis);
    amazonDynamoDBClient = new AmazonDynamoDBClient(credentials);
    dynamoDB = new DynamoDB(amazonDynamoDBClient);
    amazonDynamoDBClient.setRegion(Region.getRegion(Regions.US_WEST_2));
    ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
    attributeDefinitions.add(new AttributeDefinition().withAttributeName("hashS").withAttributeType("S"));
    attributeDefinitions.add(new AttributeDefinition().withAttributeName("rangeS").withAttributeType("S"));
    ArrayList<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>();
    keySchema.add(new KeySchemaElement().withAttributeName("hashS").withKeyType(KeyType.HASH));
    keySchema.add(new KeySchemaElement().withAttributeName("rangeS").withKeyType(KeyType.RANGE));
    CreateTableRequest request = new CreateTableRequest().withTableName(stringHashStringRangeTableName).withKeySchema(keySchema).withAttributeDefinitions(attributeDefinitions).withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(5L).withWriteCapacityUnits(6L));
    Table stringHashStringRangeTable = dynamoDB.createTable(request);
    stringHashStringRangeTable.waitForActive();
    attributeDefinitions = new ArrayList<AttributeDefinition>();
    attributeDefinitions.add(new AttributeDefinition().withAttributeName("hashN").withAttributeType("N"));
    attributeDefinitions.add(new AttributeDefinition().withAttributeName("rangeN").withAttributeType("N"));
    keySchema = new ArrayList<KeySchemaElement>();
    keySchema.add(new KeySchemaElement().withAttributeName("hashN").withKeyType(KeyType.HASH));
    keySchema.add(new KeySchemaElement().withAttributeName("rangeN").withKeyType(KeyType.RANGE));
    request = new CreateTableRequest().withTableName(numberHashNumberRangeTableName).withKeySchema(keySchema).withAttributeDefinitions(attributeDefinitions).withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(5L).withWriteCapacityUnits(6L));
    Table numberHashNumberRangeTable = dynamoDB.createTable(request);
    numberHashNumberRangeTable.waitForActive();
    attributeDefinitions = new ArrayList<AttributeDefinition>();
    attributeDefinitions.add(new AttributeDefinition().withAttributeName("hashN").withAttributeType("N"));
    keySchema = new ArrayList<KeySchemaElement>();
    keySchema.add(new KeySchemaElement().withAttributeName("hashN").withKeyType(KeyType.HASH));
    request = new CreateTableRequest().withTableName(numberHashOnlyTableName).withKeySchema(keySchema).withAttributeDefinitions(attributeDefinitions).withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(5L).withWriteCapacityUnits(6L));
    Table numberHashOnlyTable = dynamoDB.createTable(request);
    numberHashOnlyTable.waitForActive();
}
Also used : Table(com.amazonaws.services.dynamodbv2.document.Table) ArrayList(java.util.ArrayList) PropertiesCredentials(com.amazonaws.auth.PropertiesCredentials) DynamoDB(com.amazonaws.services.dynamodbv2.document.DynamoDB) AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) AmazonDynamoDBClient(com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest) FileInputStream(java.io.FileInputStream) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement) BeforeClass(org.junit.BeforeClass)

Example 67 with DynamoDB

use of com.amazonaws.services.dynamodbv2.document.DynamoDB in project nifi by apache.

the class PutDynamoDBTest method testStringHashStringRangePutThrowsClientException.

@Test
public void testStringHashStringRangePutThrowsClientException() {
    final DynamoDB mockDynamoDB = new DynamoDB(Regions.AP_NORTHEAST_1) {

        @Override
        public BatchWriteItemOutcome batchWriteItem(TableWriteItems... tableWriteItems) {
            throw new AmazonClientException("clientException");
        }
    };
    putDynamoDB = new PutDynamoDB() {

        @Override
        protected DynamoDB getDynamoDB() {
            return mockDynamoDB;
        }
    };
    final TestRunner putRunner = TestRunners.newTestRunner(putDynamoDB);
    putRunner.setProperty(AbstractDynamoDBProcessor.ACCESS_KEY, "abcd");
    putRunner.setProperty(AbstractDynamoDBProcessor.SECRET_KEY, "cdef");
    putRunner.setProperty(AbstractDynamoDBProcessor.REGION, REGION);
    putRunner.setProperty(AbstractDynamoDBProcessor.TABLE, stringHashStringRangeTableName);
    putRunner.setProperty(AbstractDynamoDBProcessor.HASH_KEY_NAME, "hashS");
    putRunner.setProperty(AbstractDynamoDBProcessor.HASH_KEY_VALUE, "h1");
    putRunner.setProperty(AbstractDynamoDBProcessor.RANGE_KEY_NAME, "rangeS");
    putRunner.setProperty(AbstractDynamoDBProcessor.RANGE_KEY_VALUE, "r1");
    putRunner.setProperty(AbstractWriteDynamoDBProcessor.JSON_DOCUMENT, "document");
    String document = "{\"name\":\"john\"}";
    putRunner.enqueue(document.getBytes());
    putRunner.run(1);
    putRunner.assertAllFlowFilesTransferred(AbstractDynamoDBProcessor.REL_FAILURE, 1);
    List<MockFlowFile> flowFiles = putRunner.getFlowFilesForRelationship(AbstractDynamoDBProcessor.REL_FAILURE);
    for (MockFlowFile flowFile : flowFiles) {
        assertEquals("clientException", flowFile.getAttribute(AbstractDynamoDBProcessor.DYNAMODB_ERROR_EXCEPTION_MESSAGE));
    }
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) TableWriteItems(com.amazonaws.services.dynamodbv2.document.TableWriteItems) TestRunner(org.apache.nifi.util.TestRunner) AmazonClientException(com.amazonaws.AmazonClientException) DynamoDB(com.amazonaws.services.dynamodbv2.document.DynamoDB) Test(org.junit.Test)

Example 68 with DynamoDB

use of com.amazonaws.services.dynamodbv2.document.DynamoDB in project nifi by apache.

the class PutDynamoDBTest method testStringHashStringRangePutThrowsServiceException.

@Test
public void testStringHashStringRangePutThrowsServiceException() {
    final DynamoDB mockDynamoDB = new DynamoDB(Regions.AP_NORTHEAST_1) {

        @Override
        public BatchWriteItemOutcome batchWriteItem(TableWriteItems... tableWriteItems) {
            throw new AmazonServiceException("serviceException");
        }
    };
    putDynamoDB = new PutDynamoDB() {

        @Override
        protected DynamoDB getDynamoDB() {
            return mockDynamoDB;
        }
    };
    final TestRunner putRunner = TestRunners.newTestRunner(putDynamoDB);
    putRunner.setProperty(AbstractDynamoDBProcessor.ACCESS_KEY, "abcd");
    putRunner.setProperty(AbstractDynamoDBProcessor.SECRET_KEY, "cdef");
    putRunner.setProperty(AbstractDynamoDBProcessor.REGION, REGION);
    putRunner.setProperty(AbstractDynamoDBProcessor.TABLE, stringHashStringRangeTableName);
    putRunner.setProperty(AbstractDynamoDBProcessor.HASH_KEY_NAME, "hashS");
    putRunner.setProperty(AbstractDynamoDBProcessor.HASH_KEY_VALUE, "h1");
    putRunner.setProperty(AbstractDynamoDBProcessor.RANGE_KEY_NAME, "rangeS");
    putRunner.setProperty(AbstractDynamoDBProcessor.RANGE_KEY_VALUE, "r1");
    putRunner.setProperty(AbstractWriteDynamoDBProcessor.JSON_DOCUMENT, "document");
    String document = "{\"name\":\"john\"}";
    putRunner.enqueue(document.getBytes());
    putRunner.run(1);
    putRunner.assertAllFlowFilesTransferred(AbstractDynamoDBProcessor.REL_FAILURE, 1);
    List<MockFlowFile> flowFiles = putRunner.getFlowFilesForRelationship(AbstractDynamoDBProcessor.REL_FAILURE);
    for (MockFlowFile flowFile : flowFiles) {
        assertEquals("serviceException (Service: null; Status Code: 0; Error Code: null; Request ID: null)", flowFile.getAttribute(AbstractDynamoDBProcessor.DYNAMODB_ERROR_EXCEPTION_MESSAGE));
    }
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) TableWriteItems(com.amazonaws.services.dynamodbv2.document.TableWriteItems) TestRunner(org.apache.nifi.util.TestRunner) AmazonServiceException(com.amazonaws.AmazonServiceException) DynamoDB(com.amazonaws.services.dynamodbv2.document.DynamoDB) Test(org.junit.Test)

Example 69 with DynamoDB

use of com.amazonaws.services.dynamodbv2.document.DynamoDB in project nifi by apache.

the class PutDynamoDBTest method setUp.

@Before
public void setUp() {
    outcome = new BatchWriteItemOutcome(result);
    result.setUnprocessedItems(new HashMap<String, List<WriteRequest>>());
    final DynamoDB mockDynamoDB = new DynamoDB(Regions.AP_NORTHEAST_1) {

        @Override
        public BatchWriteItemOutcome batchWriteItem(TableWriteItems... tableWriteItems) {
            return outcome;
        }
    };
    putDynamoDB = new PutDynamoDB() {

        @Override
        protected DynamoDB getDynamoDB() {
            return mockDynamoDB;
        }
    };
}
Also used : TableWriteItems(com.amazonaws.services.dynamodbv2.document.TableWriteItems) BatchWriteItemOutcome(com.amazonaws.services.dynamodbv2.document.BatchWriteItemOutcome) DynamoDB(com.amazonaws.services.dynamodbv2.document.DynamoDB) ArrayList(java.util.ArrayList) List(java.util.List) Before(org.junit.Before)

Example 70 with DynamoDB

use of com.amazonaws.services.dynamodbv2.document.DynamoDB in project nifi by apache.

the class PutDynamoDBTest method testStringHashStringRangePutOnlyHashFailure.

@Test
public void testStringHashStringRangePutOnlyHashFailure() {
    // Inject a mock DynamoDB to create the exception condition
    final DynamoDB mockDynamoDb = Mockito.mock(DynamoDB.class);
    // When writing, mock thrown service exception from AWS
    Mockito.when(mockDynamoDb.batchWriteItem(Matchers.<TableWriteItems>anyVararg())).thenThrow(getSampleAwsServiceException());
    putDynamoDB = new PutDynamoDB() {

        @Override
        protected DynamoDB getDynamoDB() {
            return mockDynamoDb;
        }
    };
    final TestRunner putRunner = TestRunners.newTestRunner(putDynamoDB);
    putRunner.setProperty(AbstractDynamoDBProcessor.ACCESS_KEY, "abcd");
    putRunner.setProperty(AbstractDynamoDBProcessor.SECRET_KEY, "cdef");
    putRunner.setProperty(AbstractDynamoDBProcessor.REGION, REGION);
    putRunner.setProperty(AbstractDynamoDBProcessor.TABLE, stringHashStringRangeTableName);
    putRunner.setProperty(AbstractDynamoDBProcessor.HASH_KEY_NAME, "hashS");
    putRunner.setProperty(AbstractDynamoDBProcessor.HASH_KEY_VALUE, "h1");
    putRunner.setProperty(AbstractDynamoDBProcessor.JSON_DOCUMENT, "document");
    String document = "{\"hello\": 2}";
    putRunner.enqueue(document.getBytes());
    putRunner.run(1);
    putRunner.assertAllFlowFilesTransferred(AbstractDynamoDBProcessor.REL_FAILURE, 1);
    List<MockFlowFile> flowFiles = putRunner.getFlowFilesForRelationship(AbstractDynamoDBProcessor.REL_FAILURE);
    for (MockFlowFile flowFile : flowFiles) {
        ITAbstractDynamoDBTest.validateServiceExceptionAttribute(flowFile);
    }
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) TestRunner(org.apache.nifi.util.TestRunner) DynamoDB(com.amazonaws.services.dynamodbv2.document.DynamoDB) Test(org.junit.Test)

Aggregations

DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)38 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)32 Table (com.amazonaws.services.dynamodbv2.document.Table)25 Item (com.amazonaws.services.dynamodbv2.document.Item)19 Test (org.junit.Test)18 AmazonServiceException (com.amazonaws.AmazonServiceException)16 HashMap (java.util.HashMap)15 TestRunner (org.apache.nifi.util.TestRunner)15 AmazonClientException (com.amazonaws.AmazonClientException)14 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)14 MockFlowFile (org.apache.nifi.util.MockFlowFile)14 ArrayList (java.util.ArrayList)11 Map (java.util.Map)11 TableWriteItems (com.amazonaws.services.dynamodbv2.document.TableWriteItems)10 KeySchemaElement (com.amazonaws.services.dynamodbv2.model.KeySchemaElement)10 IOException (java.io.IOException)10 TableKeysAndAttributes (com.amazonaws.services.dynamodbv2.document.TableKeysAndAttributes)9 AttributeDefinition (com.amazonaws.services.dynamodbv2.model.AttributeDefinition)9 ProvisionedThroughput (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput)9 List (java.util.List)9