use of com.datatorrent.api.Context.OperatorContext in project apex-malhar by apache.
the class JdbcPojoOperatorTest method testJdbcOutputOperator.
@Test
public void testJdbcOutputOperator() {
JdbcTransactionalStore transactionalStore = new JdbcTransactionalStore();
transactionalStore.setDatabaseDriver(DB_DRIVER);
transactionalStore.setDatabaseUrl(URL);
com.datatorrent.api.Attribute.AttributeMap.DefaultAttributeMap attributeMap = new com.datatorrent.api.Attribute.AttributeMap.DefaultAttributeMap();
attributeMap.put(DAG.APPLICATION_ID, APP_ID);
OperatorContext context = mockOperatorContext(OPERATOR_ID, attributeMap);
TestOutputOperator outputOperator = new TestOutputOperator();
outputOperator.setBatchSize(3);
outputOperator.setStore(transactionalStore);
outputOperator.setup(context);
outputOperator.activate(context);
List<TestEvent> events = Lists.newArrayList();
for (int i = 0; i < 10; i++) {
events.add(new TestEvent(i));
}
outputOperator.beginWindow(0);
for (TestEvent event : events) {
outputOperator.input.process(event);
}
outputOperator.endWindow();
Assert.assertEquals("rows in db", 10, outputOperator.getNumOfEventsInStore());
cleanTable();
}
use of com.datatorrent.api.Context.OperatorContext in project apex-malhar by apache.
the class JdbcPojoOperatorTest method testJdbcPojoOutputOperator.
@Test
public void testJdbcPojoOutputOperator() {
JdbcTransactionalStore transactionalStore = new JdbcTransactionalStore();
transactionalStore.setDatabaseDriver(DB_DRIVER);
transactionalStore.setDatabaseUrl(URL);
com.datatorrent.api.Attribute.AttributeMap.DefaultAttributeMap attributeMap = new com.datatorrent.api.Attribute.AttributeMap.DefaultAttributeMap();
attributeMap.put(DAG.APPLICATION_ID, APP_ID);
OperatorContext context = mockOperatorContext(OPERATOR_ID, attributeMap);
TestPOJOOutputOperator outputOperator = new TestPOJOOutputOperator();
outputOperator.setBatchSize(3);
outputOperator.setTablename(TABLE_POJO_NAME);
List<JdbcFieldInfo> fieldInfos = Lists.newArrayList();
fieldInfos.add(new JdbcFieldInfo("ID", "id", null, Types.INTEGER));
fieldInfos.add(new JdbcFieldInfo("NAME", "name", null, Types.VARCHAR));
outputOperator.setFieldInfos(fieldInfos);
outputOperator.setStore(transactionalStore);
outputOperator.setup(context);
Attribute.AttributeMap.DefaultAttributeMap portAttributes = new Attribute.AttributeMap.DefaultAttributeMap();
portAttributes.put(Context.PortContext.TUPLE_CLASS, TestPOJOEvent.class);
TestPortContext tpc = new TestPortContext(portAttributes);
outputOperator.input.setup(tpc);
outputOperator.activate(context);
List<TestPOJOEvent> events = Lists.newArrayList();
for (int i = 0; i < 10; i++) {
events.add(new TestPOJOEvent(i, "test" + i));
}
outputOperator.beginWindow(0);
for (TestPOJOEvent event : events) {
outputOperator.input.process(event);
}
outputOperator.endWindow();
Assert.assertEquals("rows in db", 10, outputOperator.getNumOfEventsInStore(TABLE_POJO_NAME));
}
use of com.datatorrent.api.Context.OperatorContext in project apex-malhar by apache.
the class JdbcPojoOperatorTest method testJdbcPojoInputOperator.
@Test
public void testJdbcPojoInputOperator() {
JdbcStore store = new JdbcStore();
store.setDatabaseDriver(DB_DRIVER);
store.setDatabaseUrl(URL);
Attribute.AttributeMap.DefaultAttributeMap attributeMap = new Attribute.AttributeMap.DefaultAttributeMap();
attributeMap.put(DAG.APPLICATION_ID, APP_ID);
OperatorContext context = mockOperatorContext(OPERATOR_ID, attributeMap);
insertEvents(10, true, 0);
JdbcPOJOInputOperator inputOperator = new JdbcPOJOInputOperator();
inputOperator.setStore(store);
inputOperator.setTableName(TABLE_POJO_NAME);
List<FieldInfo> fieldInfos = Lists.newArrayList();
fieldInfos.add(new FieldInfo("ID", "id", null));
fieldInfos.add(new FieldInfo("STARTDATE", "startDate", null));
fieldInfos.add(new FieldInfo("STARTTIME", "startTime", null));
fieldInfos.add(new FieldInfo("STARTTIMESTAMP", "startTimestamp", null));
fieldInfos.add(new FieldInfo("SCORE", "score", FieldInfo.SupportType.DOUBLE));
inputOperator.setFieldInfos(fieldInfos);
inputOperator.setFetchSize(5);
CollectorTestSink<Object> sink = new CollectorTestSink<>();
inputOperator.outputPort.setSink(sink);
Attribute.AttributeMap.DefaultAttributeMap portAttributes = new Attribute.AttributeMap.DefaultAttributeMap();
portAttributes.put(Context.PortContext.TUPLE_CLASS, TestPOJOEvent.class);
TestPortContext tpc = new TestPortContext(portAttributes);
inputOperator.setup(context);
inputOperator.outputPort.setup(tpc);
inputOperator.activate(context);
inputOperator.beginWindow(0);
inputOperator.emitTuples();
inputOperator.endWindow();
Assert.assertEquals("rows from db", 5, sink.collectedTuples.size());
int i = 0;
for (Object tuple : sink.collectedTuples) {
TestPOJOEvent pojoEvent = (TestPOJOEvent) tuple;
Assert.assertTrue("i=" + i, pojoEvent.getId() == i);
Assert.assertTrue("date", pojoEvent.getStartDate() instanceof Date);
Assert.assertTrue("time", pojoEvent.getStartTime() instanceof Time);
Assert.assertTrue("timestamp", pojoEvent.getStartTimestamp() instanceof Timestamp);
i++;
}
sink.collectedTuples.clear();
inputOperator.beginWindow(1);
inputOperator.emitTuples();
inputOperator.endWindow();
Assert.assertEquals("rows from db", 5, sink.collectedTuples.size());
for (Object tuple : sink.collectedTuples) {
TestPOJOEvent pojoEvent = (TestPOJOEvent) tuple;
Assert.assertTrue("i=" + i, pojoEvent.getId() == i);
Assert.assertTrue("date", pojoEvent.getStartDate() instanceof Date);
Assert.assertTrue("time", pojoEvent.getStartTime() instanceof Time);
Assert.assertTrue("timestamp", pojoEvent.getStartTimestamp() instanceof Timestamp);
Assert.assertTrue("score", pojoEvent.getScore() == 55.4);
i++;
}
sink.collectedTuples.clear();
inputOperator.beginWindow(2);
inputOperator.emitTuples();
inputOperator.endWindow();
Assert.assertEquals("rows from db", 0, sink.collectedTuples.size());
// Insert 3 more tuples and check if they are read successfully.
insertEvents(3, false, 10);
inputOperator.beginWindow(3);
inputOperator.emitTuples();
inputOperator.endWindow();
Assert.assertEquals("rows from db", 3, sink.collectedTuples.size());
for (Object tuple : sink.collectedTuples) {
TestPOJOEvent pojoEvent = (TestPOJOEvent) tuple;
Assert.assertTrue("i=" + i, pojoEvent.getId() == i);
Assert.assertTrue("date", pojoEvent.getStartDate() instanceof Date);
Assert.assertTrue("time", pojoEvent.getStartTime() instanceof Time);
Assert.assertTrue("timestamp", pojoEvent.getStartTimestamp() instanceof Timestamp);
Assert.assertTrue("score", pojoEvent.getScore() == 55.4);
i++;
}
}
use of com.datatorrent.api.Context.OperatorContext in project apex-malhar by apache.
the class JdbcPojoOperatorTest method testJdbcPojoInsertOutputOperator.
/**
* This test will assume direct mapping for POJO fields to DB columns All
* fields in DB present in POJO
*/
@Test
public void testJdbcPojoInsertOutputOperator() {
JdbcTransactionalStore transactionalStore = new JdbcTransactionalStore();
transactionalStore.setDatabaseDriver(DB_DRIVER);
transactionalStore.setDatabaseUrl(URL);
com.datatorrent.api.Attribute.AttributeMap.DefaultAttributeMap attributeMap = new com.datatorrent.api.Attribute.AttributeMap.DefaultAttributeMap();
attributeMap.put(DAG.APPLICATION_ID, APP_ID);
OperatorContext context = mockOperatorContext(OPERATOR_ID, attributeMap);
TestPOJOOutputOperator outputOperator = new TestPOJOOutputOperator();
outputOperator.setBatchSize(3);
outputOperator.setTablename(TABLE_POJO_NAME);
outputOperator.setStore(transactionalStore);
outputOperator.setup(context);
Attribute.AttributeMap.DefaultAttributeMap portAttributes = new Attribute.AttributeMap.DefaultAttributeMap();
portAttributes.put(Context.PortContext.TUPLE_CLASS, TestPOJOEvent.class);
TestPortContext tpc = new TestPortContext(portAttributes);
outputOperator.input.setup(tpc);
CollectorTestSink<Object> errorSink = new CollectorTestSink<>();
TestUtils.setSink(outputOperator.error, errorSink);
outputOperator.activate(context);
List<TestPOJOEvent> events = Lists.newArrayList();
for (int i = 0; i < 10; i++) {
events.add(new TestPOJOEvent(i, "test" + i));
}
// Records violating PK constraint
events.add(new TestPOJOEvent(0, "test0"));
// Records violating PK constraint
events.add(new TestPOJOEvent(2, "test2"));
// Clean record
events.add(new TestPOJOEvent(10, "test10"));
// Clean record
events.add(new TestPOJOEvent(11, "test11"));
// Records violating PK constraint
events.add(new TestPOJOEvent(3, "test3"));
// Clean record
events.add(new TestPOJOEvent(12, "test12"));
outputOperator.beginWindow(0);
for (TestPOJOEvent event : events) {
outputOperator.input.process(event);
}
outputOperator.endWindow();
Assert.assertEquals("rows in db", 13, outputOperator.getNumOfEventsInStore(TABLE_POJO_NAME));
Assert.assertEquals("Error tuples", 3, errorSink.collectedTuples.size());
}
use of com.datatorrent.api.Context.OperatorContext in project apex-malhar by apache.
the class JdbcPojoOperatorTest method testJdbcPojoInsertOutputOperatorExactlyOnce.
/**
* This test will assume direct mapping for POJO fields to DB columns All
* fields in DB present in POJO and will test it for exactly once criteria
*/
@Test
public void testJdbcPojoInsertOutputOperatorExactlyOnce() {
JdbcTransactionalStore transactionalStore = new JdbcTransactionalStore();
transactionalStore.setDatabaseDriver(DB_DRIVER);
transactionalStore.setDatabaseUrl(URL);
com.datatorrent.api.Attribute.AttributeMap.DefaultAttributeMap attributeMap = new com.datatorrent.api.Attribute.AttributeMap.DefaultAttributeMap();
attributeMap.put(DAG.APPLICATION_ID, APP_ID);
OperatorContext context = mockOperatorContext(OPERATOR_ID, attributeMap);
TestPOJOOutputOperator outputOperator = new TestPOJOOutputOperator();
outputOperator.setBatchSize(3);
outputOperator.setTablename(TABLE_POJO_NAME);
outputOperator.setStore(transactionalStore);
outputOperator.setup(context);
Attribute.AttributeMap.DefaultAttributeMap portAttributes = new Attribute.AttributeMap.DefaultAttributeMap();
portAttributes.put(Context.PortContext.TUPLE_CLASS, TestPOJOEvent.class);
TestPortContext tpc = new TestPortContext(portAttributes);
outputOperator.input.setup(tpc);
CollectorTestSink<Object> errorSink = new CollectorTestSink<>();
TestUtils.setSink(outputOperator.error, errorSink);
outputOperator.activate(context);
List<TestPOJOEvent> events = Lists.newArrayList();
for (int i = 0; i < 70; i++) {
events.add(new TestPOJOEvent(i, "test" + i));
}
outputOperator.beginWindow(0);
for (int i = 0; i < 10; i++) {
outputOperator.input.process(events.get(i));
}
outputOperator.endWindow();
outputOperator.beginWindow(1);
for (int i = 10; i < 20; i++) {
outputOperator.input.process(events.get(i));
}
outputOperator.endWindow();
outputOperator.beginWindow(2);
for (int i = 20; i < 30; i++) {
outputOperator.input.process(events.get(i));
}
outputOperator.endWindow();
outputOperator.setup(context);
outputOperator.input.setup(tpc);
outputOperator.activate(context);
outputOperator.beginWindow(0);
for (int i = 30; i < 40; i++) {
outputOperator.input.process(events.get(i));
}
outputOperator.endWindow();
outputOperator.beginWindow(1);
for (int i = 40; i < 50; i++) {
outputOperator.input.process(events.get(i));
}
outputOperator.endWindow();
outputOperator.beginWindow(2);
for (int i = 50; i < 60; i++) {
outputOperator.input.process(events.get(i));
}
outputOperator.beginWindow(3);
for (int i = 60; i < 70; i++) {
outputOperator.input.process(events.get(i));
}
outputOperator.endWindow();
outputOperator.deactivate();
outputOperator.teardown();
Assert.assertEquals("rows in db", 40, outputOperator.getNumOfEventsInStore(TABLE_POJO_NAME));
}
Aggregations