Search in sources :

Example 16 with FieldInfo

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());
}
Also used : ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) FieldInfo(org.apache.apex.malhar.lib.util.FieldInfo) Test(org.junit.Test)

Example 17 with FieldInfo

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++;
    }
}
Also used : Attribute(com.datatorrent.api.Attribute) TestPortContext(org.apache.apex.malhar.lib.helper.TestPortContext) Time(java.sql.Time) Timestamp(java.sql.Timestamp) Date(java.sql.Date) OperatorContextTestHelper.mockOperatorContext(org.apache.apex.malhar.lib.helper.OperatorContextTestHelper.mockOperatorContext) OperatorContext(com.datatorrent.api.Context.OperatorContext) FieldInfo(org.apache.apex.malhar.lib.util.FieldInfo) CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink) Test(org.junit.Test)

Example 18 with FieldInfo

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);
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) FieldInfo(org.apache.apex.malhar.lib.util.FieldInfo)

Example 19 with FieldInfo

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;
}
Also used : FieldInfo(org.apache.apex.malhar.lib.util.FieldInfo)

Example 20 with FieldInfo

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);
}
Also used : ColumnDefinitions(com.datastax.driver.core.ColumnDefinitions) Set(java.util.Set) BigDecimal(java.math.BigDecimal) Date(java.util.Date) LocalDate(com.datastax.driver.core.LocalDate) DataType(com.datastax.driver.core.DataType) ArrayList(java.util.ArrayList) List(java.util.List) UUID(java.util.UUID) Map(java.util.Map) FieldInfo(org.apache.apex.malhar.lib.util.FieldInfo)

Aggregations

FieldInfo (org.apache.apex.malhar.lib.util.FieldInfo)36 Test (org.junit.Test)14 ArrayList (java.util.ArrayList)11 Attribute (com.datatorrent.api.Attribute)6 OperatorContext (com.datatorrent.api.Context.OperatorContext)6 OperatorContextTestHelper.mockOperatorContext (org.apache.apex.malhar.lib.helper.OperatorContextTestHelper.mockOperatorContext)6 TestPortContext (org.apache.apex.malhar.lib.helper.TestPortContext)6 CollectorTestSink (org.apache.apex.malhar.lib.testbench.CollectorTestSink)6 SQLException (java.sql.SQLException)5 Date (java.util.Date)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Partitioner (com.datatorrent.api.Partitioner)3 Date (java.sql.Date)3 ResultSetMetaData (java.sql.ResultSetMetaData)3 UUID (java.util.UUID)3 ActiveFieldInfo (org.apache.apex.malhar.lib.db.jdbc.JdbcPOJOInputOperator.ActiveFieldInfo)3 ColumnDefinitions (com.datastax.driver.core.ColumnDefinitions)2 DataType (com.datastax.driver.core.DataType)2 ResultSet (com.datastax.driver.core.ResultSet)2 Field (java.lang.reflect.Field)2