Search in sources :

Example 21 with FieldInfo

use of org.apache.apex.malhar.lib.util.FieldInfo in project apex-malhar by apache.

the class CassandraPOJOInputOperator method activate.

@Override
public void activate(OperatorContext context) {
    Long keyToken;
    if (startRow != null) {
        if ((keyToken = fetchKeyTokenFromDB(startRow)) != null) {
            startRowToken = keyToken;
        }
    }
    com.datastax.driver.core.ResultSet rs = store.getSession().execute("select * from " + store.keyspace + "." + tablename + " LIMIT " + 1);
    ColumnDefinitions rsMetaData = rs.getColumnDefinitions();
    if (query.contains("%t")) {
        query = query.replace("%t", tablename);
    }
    if (query.contains("%p")) {
        query = query.replace("%p", primaryKeyColumn);
    }
    if (query.contains("%l")) {
        query = query.replace("%l", limit + "");
    }
    LOG.debug("query is {}", query);
    for (FieldInfo fieldInfo : fieldInfos) {
        // Get the designated column's data type.
        DataType type = rsMetaData.getType(fieldInfo.getColumnName());
        columnDataTypes.add(type);
        Object setter;
        final String setterExpr = fieldInfo.getPojoFieldExpression();
        switch(type.getName()) {
            case ASCII:
            case TEXT:
            case VARCHAR:
                setter = PojoUtils.createSetter(pojoClass, setterExpr, String.class);
                break;
            case BOOLEAN:
                setter = PojoUtils.createSetterBoolean(pojoClass, setterExpr);
                break;
            case INT:
                setter = PojoUtils.createSetterInt(pojoClass, setterExpr);
                break;
            case BIGINT:
            case COUNTER:
                setter = PojoUtils.createSetterLong(pojoClass, setterExpr);
                break;
            case FLOAT:
                setter = PojoUtils.createSetterFloat(pojoClass, setterExpr);
                break;
            case DOUBLE:
                setter = PojoUtils.createSetterDouble(pojoClass, setterExpr);
                break;
            case DECIMAL:
                setter = PojoUtils.createSetter(pojoClass, setterExpr, BigDecimal.class);
                break;
            case SET:
                setter = PojoUtils.createSetter(pojoClass, setterExpr, Set.class);
                break;
            case MAP:
                setter = PojoUtils.createSetter(pojoClass, setterExpr, Map.class);
                break;
            case LIST:
                setter = PojoUtils.createSetter(pojoClass, setterExpr, List.class);
                break;
            case TIMESTAMP:
                setter = PojoUtils.createSetter(pojoClass, setterExpr, Date.class);
                break;
            case UUID:
                setter = PojoUtils.createSetter(pojoClass, setterExpr, UUID.class);
                break;
            default:
                setter = PojoUtils.createSetter(pojoClass, setterExpr, Object.class);
                break;
        }
        setters.add(setter);
    }
}
Also used : ColumnDefinitions(com.datastax.driver.core.ColumnDefinitions) ResultSet(com.datastax.driver.core.ResultSet) Set(java.util.Set) BigDecimal(java.math.BigDecimal) Date(java.util.Date) ResultSet(com.datastax.driver.core.ResultSet) SetterLong(org.apache.apex.malhar.lib.util.PojoUtils.SetterLong) 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)

Example 22 with FieldInfo

use of org.apache.apex.malhar.lib.util.FieldInfo in project apex-malhar by apache.

the class AvroToPojo method createFieldInfoMap.

/**
 * Creates a map representing fieldName in POJO:field in Generic Record:Data
 * type
 *
 * @return List of FieldInfo
 */
private List<FieldInfo> createFieldInfoMap(String str) {
    fieldInfos = new ArrayList<FieldInfo>();
    StringTokenizer strtok = new StringTokenizer(str, RECORD_SEPARATOR);
    while (strtok.hasMoreTokens()) {
        String[] token = strtok.nextToken().split(FIELD_SEPARATOR);
        try {
            fieldInfos.add(new FieldInfo(token[0], token[1], SupportType.valueOf(token[2])));
        } catch (Exception e) {
            LOG.error("Invalid support type", e);
        }
    }
    return fieldInfos;
}
Also used : StringTokenizer(java.util.StringTokenizer) FieldInfo(org.apache.apex.malhar.lib.util.FieldInfo) AvroRuntimeException(org.apache.avro.AvroRuntimeException)

Example 23 with FieldInfo

use of org.apache.apex.malhar.lib.util.FieldInfo in project apex-malhar by apache.

the class JdbcPollerApplication method addFieldInfos.

/**
 * This method can be modified to have field mappings based on user defined
 * class
 */
private List<FieldInfo> addFieldInfos() {
    List<FieldInfo> fieldInfos = Lists.newArrayList();
    fieldInfos.add(new FieldInfo("ACCOUNT_NO", "accountNumber", SupportType.INTEGER));
    fieldInfos.add(new FieldInfo("NAME", "name", SupportType.STRING));
    fieldInfos.add(new FieldInfo("AMOUNT", "amount", SupportType.INTEGER));
    return fieldInfos;
}
Also used : FieldInfo(org.apache.apex.malhar.lib.util.FieldInfo)

Example 24 with FieldInfo

use of org.apache.apex.malhar.lib.util.FieldInfo in project apex-malhar by apache.

the class JdbcPOJOInputOperator method populateColumnDataTypes.

protected void populateColumnDataTypes() throws SQLException {
    columnDataTypes = Lists.newArrayList();
    preparedStatement.setMaxRows(0);
    setRuntimeParams();
    try (ResultSet rs = preparedStatement.executeQuery()) {
        Map<String, Integer> nameToType = Maps.newHashMap();
        ResultSetMetaData rsMetaData = rs.getMetaData();
        LOG.debug("resultSet MetaData column count {}", rsMetaData.getColumnCount());
        for (int i = 1; i <= rsMetaData.getColumnCount(); i++) {
            int type = rsMetaData.getColumnType(i);
            String name = rsMetaData.getColumnName(i);
            LOG.debug("column name {} type {}", name, type);
            if (query == null) {
                columnDataTypes.add(type);
            } else {
                // when it is a custom query we need to ensure the types are in the same order as field infos
                nameToType.put(name, type);
            }
        }
        if (query != null) {
            for (FieldInfo fieldInfo : fieldInfos) {
                columnDataTypes.add(nameToType.get(fieldInfo.getColumnName()));
            }
        }
    }
    preparedStatement.setFetchSize(fetchSize);
    preparedStatement.setMaxRows(fetchSize);
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) ResultSet(java.sql.ResultSet) FieldInfo(org.apache.apex.malhar.lib.util.FieldInfo)

Example 25 with FieldInfo

use of org.apache.apex.malhar.lib.util.FieldInfo in project apex-malhar by apache.

the class JdbcPOJOPollInputOperator method populateColumnDataTypes.

protected void populateColumnDataTypes() throws SQLException {
    columnDataTypes = Lists.newArrayList();
    try (PreparedStatement stmt = store.getConnection().prepareStatement(buildRangeQuery(null, 1, 1))) {
        Map<String, Integer> nameToType = Maps.newHashMap();
        ResultSetMetaData rsMetaData = stmt.getMetaData();
        LOG.debug("resultSet MetaData column count {}", rsMetaData.getColumnCount());
        for (int i = 1; i <= rsMetaData.getColumnCount(); i++) {
            int type = rsMetaData.getColumnType(i);
            String name = rsMetaData.getColumnName(i);
            LOG.debug("column name {} type {}", name, type);
            nameToType.put(name.toUpperCase(), type);
        }
        for (FieldInfo fieldInfo : fieldInfos) {
            if (nameToType.containsKey(fieldInfo.getColumnName().toUpperCase())) {
                columnDataTypes.add(nameToType.get(fieldInfo.getColumnName().toUpperCase()));
            }
        }
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) PreparedStatement(java.sql.PreparedStatement) ActiveFieldInfo(org.apache.apex.malhar.lib.db.jdbc.JdbcPOJOInputOperator.ActiveFieldInfo) 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