Search in sources :

Example 6 with FieldInfo

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

the class JdbcHDFSApp 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 7 with FieldInfo

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

the class JdbcToJdbcApp method addFieldInfos.

/**
 * This method can be modified to have field mappings based on used 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 8 with FieldInfo

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

the class JdbcPOJOInsertOutputOperator method activate.

@Override
public void activate(OperatorContext context) {
    if (getFieldInfos() == null || getFieldInfos().size() == 0) {
        Field[] fields = pojoClass.getDeclaredFields();
        // Create fieldInfos in case of direct mapping
        List<JdbcFieldInfo> fieldInfos = Lists.newArrayList();
        for (int i = 0; i < columnNames.size(); i++) {
            String columnName = columnNames.get(i);
            String pojoField = getMatchingField(fields, columnName);
            if (columnNullabilities.get(i) == DatabaseMetaData.columnNoNulls && (pojoField == null || pojoField.length() == 0)) {
                throw new RuntimeException("Data for a non-nullable field: " + columnName + " not found in POJO");
            } else {
                if (pojoField != null && pojoField.length() != 0) {
                    JdbcFieldInfo fi = new JdbcFieldInfo(columnName, pojoField, null, Types.NULL);
                    fieldInfos.add(fi);
                } else {
                    columnDataTypes.remove(i);
                    columnNames.remove(i);
                    columnNullabilities.remove(i);
                    i--;
                }
            }
        }
        setFieldInfos(fieldInfos);
    }
    for (FieldInfo fi : getFieldInfos()) {
        columnFieldGetters.add(new ActiveFieldInfo(fi));
    }
    StringBuilder columns = new StringBuilder();
    StringBuilder values = new StringBuilder();
    for (int i = 0; i < columnNames.size(); i++) {
        columns.append(columnNames.get(i));
        values.append("?");
        if (i < columnNames.size() - 1) {
            columns.append(",");
            values.append(",");
        }
    }
    insertStatement = "INSERT INTO " + getTablename() + " (" + columns.toString() + ")" + " VALUES (" + values.toString() + ")";
    LOG.debug("insert statement is {}", insertStatement);
    super.activate(context);
}
Also used : Field(java.lang.reflect.Field) FieldInfo(org.apache.apex.malhar.lib.util.FieldInfo)

Example 9 with FieldInfo

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

the class JdbcPOJOPollInputOperator method setup.

@Override
public void setup(OperatorContext context) {
    super.setup(context);
    try {
        if (getColumnsExpression() == null) {
            StringBuilder columns = new StringBuilder();
            for (int i = 0; i < fieldInfos.size(); i++) {
                columns.append(fieldInfos.get(i).getColumnName());
                if (i < fieldInfos.size() - 1) {
                    columns.append(",");
                }
            }
            setColumnsExpression(columns.toString());
            LOG.debug("select expr {}", columns.toString());
        }
        if (columnDataTypes == null) {
            populateColumnDataTypes();
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
    for (FieldInfo fi : fieldInfos) {
        columnFieldSetters.add(new ActiveFieldInfo(fi));
    }
}
Also used : ActiveFieldInfo(org.apache.apex.malhar.lib.db.jdbc.JdbcPOJOInputOperator.ActiveFieldInfo) SQLException(java.sql.SQLException) ActiveFieldInfo(org.apache.apex.malhar.lib.db.jdbc.JdbcPOJOInputOperator.ActiveFieldInfo) FieldInfo(org.apache.apex.malhar.lib.util.FieldInfo)

Example 10 with FieldInfo

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

the class JdbcPOJOInputOperator method setFieldInfosItem.

/**
 * Function to initialize the list of {@link FieldInfo} externally from configuration/properties file.
 * Example entry in the properties/configuration file:
 *   <property>
 *   <name>dt.operator.JdbcPOJOInput.fieldInfosItem[0]</name>
 *   <value>
 *   {
 *   "columnName":"ID",
 *   "pojoFieldExpression": "id",
 *   "type":"INTEGER"
 *   }
 *   </value>
 *   </property>
 * @param index is the index in the list which is to be initialized.
 * @param value is the JSON String with appropriate mappings for {@link FieldInfo}.
 */
public void setFieldInfosItem(int index, String value) {
    try {
        JSONObject jo = new JSONObject(value);
        FieldInfo fieldInfo = new FieldInfo(jo.getString("columnName"), jo.getString("pojoFieldExpression"), FieldInfo.SupportType.valueOf(jo.getString("type")));
        final int need = index - fieldInfos.size() + 1;
        for (int i = 0; i < need; i++) {
            fieldInfos.add(null);
        }
        fieldInfos.set(index, fieldInfo);
    } catch (Exception e) {
        throw new RuntimeException("Exception in setting FieldInfo " + value + " " + e.getMessage());
    }
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) FieldInfo(org.apache.apex.malhar.lib.util.FieldInfo) SQLException(java.sql.SQLException)

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