use of org.apache.apex.malhar.lib.util.FieldInfo in project apex-malhar by apache.
the class JDBCLoaderTest method testMysqlDBLookup.
@Test
public void testMysqlDBLookup() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
ArrayList<FieldInfo> lookupKeys = new ArrayList<>();
lookupKeys.add(new FieldInfo("ID", "ID", FieldInfo.SupportType.INTEGER));
ArrayList<FieldInfo> includeKeys = new ArrayList<>();
includeKeys.add(new FieldInfo("NAME", "NAME", FieldInfo.SupportType.STRING));
includeKeys.add(new FieldInfo("AGE", "AGE", FieldInfo.SupportType.INTEGER));
includeKeys.add(new FieldInfo("ADDRESS", "ADDRESS", FieldInfo.SupportType.STRING));
testMeta.dbloader.setFieldInfo(lookupKeys, includeKeys);
latch.await(1000, TimeUnit.MILLISECONDS);
ArrayList<Object> keys = new ArrayList<>();
keys.add(4);
ArrayList<Object> columnInfo = (ArrayList<Object>) testMeta.dbloader.get(keys);
Assert.assertEquals("NAME", "Mark", columnInfo.get(0).toString().trim());
Assert.assertEquals("AGE", 25, columnInfo.get(1));
Assert.assertEquals("ADDRESS", "Rich-Mond", columnInfo.get(2).toString().trim());
}
use of org.apache.apex.malhar.lib.util.FieldInfo 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 org.apache.apex.malhar.lib.util.FieldInfo in project apex-malhar by apache.
the class JDBCLoader method getDataFrmResult.
protected ArrayList<Object> getDataFrmResult(Object result) throws RuntimeException {
try {
ResultSet resultSet = (ResultSet) result;
if (resultSet.next()) {
ResultSetMetaData rsdata = resultSet.getMetaData();
// If the includefields is empty, populate it from ResultSetMetaData
if (CollectionUtils.isEmpty(includeFieldInfo)) {
if (includeFieldInfo == null) {
includeFieldInfo = new ArrayList<>();
}
for (int i = 1; i <= rsdata.getColumnCount(); i++) {
String columnName = rsdata.getColumnName(i);
// TODO: Take care of type conversion.
includeFieldInfo.add(new FieldInfo(columnName, columnName, FieldInfo.SupportType.OBJECT));
}
}
ArrayList<Object> res = new ArrayList<Object>();
for (FieldInfo f : includeFieldInfo) {
res.add(getConvertedData(resultSet.getObject(f.getColumnName()), f));
}
return res;
} else {
return null;
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
use of org.apache.apex.malhar.lib.util.FieldInfo in project apex-malhar by apache.
the class JDBCLoader method generateQueryStmt.
private String generateQueryStmt() {
String stmt = "select * from " + tableName + " where ";
boolean first = true;
for (FieldInfo fieldInfo : lookupFieldInfo) {
if (first) {
first = false;
} else {
stmt += " and ";
}
stmt += fieldInfo.getColumnName() + " = ?";
}
logger.info("generateQueryStmt: {}", stmt);
return stmt;
}
use of org.apache.apex.malhar.lib.util.FieldInfo in project apex-malhar by apache.
the class CassandraPOJOOutputOperator method activate.
@Override
public void activate(Context.OperatorContext context) {
com.datastax.driver.core.ResultSet rs = store.getSession().execute("select * from " + store.keyspace + "." + tablename);
final ColumnDefinitions rsMetaData = rs.getColumnDefinitions();
if (fieldInfos == null) {
populateFieldInfosFromPojo(rsMetaData);
}
for (FieldInfo fieldInfo : getFieldInfos()) {
// get the designated column's data type.
final DataType type = rsMetaData.getType(fieldInfo.getColumnName());
columnDataTypes.add(type);
final Object getter;
final String getterExpr = fieldInfo.getPojoFieldExpression();
switch(type.getName()) {
case ASCII:
case TEXT:
case VARCHAR:
getter = PojoUtils.createGetter(pojoClass, getterExpr, String.class);
break;
case BOOLEAN:
getter = PojoUtils.createGetterBoolean(pojoClass, getterExpr);
break;
case INT:
getter = PojoUtils.createGetterInt(pojoClass, getterExpr);
break;
case BIGINT:
case COUNTER:
getter = PojoUtils.createGetterLong(pojoClass, getterExpr);
break;
case FLOAT:
getter = PojoUtils.createGetterFloat(pojoClass, getterExpr);
break;
case DOUBLE:
getter = PojoUtils.createGetterDouble(pojoClass, getterExpr);
break;
case DECIMAL:
getter = PojoUtils.createGetter(pojoClass, getterExpr, BigDecimal.class);
break;
case SET:
getter = PojoUtils.createGetter(pojoClass, getterExpr, Set.class);
break;
case MAP:
getter = PojoUtils.createGetter(pojoClass, getterExpr, Map.class);
break;
case LIST:
getter = PojoUtils.createGetter(pojoClass, getterExpr, List.class);
break;
case TIMESTAMP:
getter = PojoUtils.createGetter(pojoClass, getterExpr, Date.class);
break;
case UUID:
getter = PojoUtils.createGetter(pojoClass, getterExpr, UUID.class);
break;
default:
getter = PojoUtils.createGetter(pojoClass, getterExpr, Object.class);
break;
}
getters.add(getter);
}
super.activate(context);
}
Aggregations