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);
}
}
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;
}
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;
}
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);
}
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()));
}
}
}
}
Aggregations