use of org.apache.nifi.serialization.SimpleRecordSchema in project nifi by apache.
the class ResultSetRecordSet method createSchema.
private static RecordSchema createSchema(final ResultSet rs, final RecordSchema readerSchema) throws SQLException {
final ResultSetMetaData metadata = rs.getMetaData();
final int numCols = metadata.getColumnCount();
final List<RecordField> fields = new ArrayList<>(numCols);
for (int i = 0; i < numCols; i++) {
final int column = i + 1;
final int sqlType = metadata.getColumnType(column);
final DataType dataType = getDataType(sqlType, rs, column, readerSchema);
final String fieldName = metadata.getColumnLabel(column);
final int nullableFlag = metadata.isNullable(column);
final boolean nullable;
if (nullableFlag == ResultSetMetaData.columnNoNulls) {
nullable = false;
} else {
nullable = true;
}
final RecordField field = new RecordField(fieldName, dataType, nullable);
fields.add(field);
}
return new SimpleRecordSchema(fields);
}
use of org.apache.nifi.serialization.SimpleRecordSchema in project nifi by apache.
the class DataTypeUtils method merge.
public static RecordSchema merge(final RecordSchema thisSchema, final RecordSchema otherSchema) {
if (thisSchema == null) {
return otherSchema;
}
if (otherSchema == null) {
return thisSchema;
}
if (thisSchema == otherSchema) {
return thisSchema;
}
final List<RecordField> otherFields = otherSchema.getFields();
if (otherFields.isEmpty()) {
return thisSchema;
}
final List<RecordField> thisFields = thisSchema.getFields();
if (thisFields.isEmpty()) {
return otherSchema;
}
final Map<String, Integer> fieldIndices = new HashMap<>();
final List<RecordField> fields = new ArrayList<>();
for (int i = 0; i < thisFields.size(); i++) {
final RecordField field = thisFields.get(i);
final Integer index = Integer.valueOf(i);
fieldIndices.put(field.getFieldName(), index);
for (final String alias : field.getAliases()) {
fieldIndices.put(alias, index);
}
fields.add(field);
}
for (final RecordField otherField : otherFields) {
Integer fieldIndex = fieldIndices.get(otherField.getFieldName());
// if one exists.
if (fieldIndex == null) {
for (final String alias : otherField.getAliases()) {
fieldIndex = fieldIndices.get(alias);
if (fieldIndex != null) {
break;
}
}
}
// If there is no field with the same name then just add 'otherField'.
if (fieldIndex == null) {
fields.add(otherField);
continue;
}
// Merge the two fields, if necessary
final RecordField thisField = fields.get(fieldIndex);
if (isMergeRequired(thisField, otherField)) {
final RecordField mergedField = merge(thisField, otherField);
fields.set(fieldIndex, mergedField);
}
}
return new SimpleRecordSchema(fields);
}
use of org.apache.nifi.serialization.SimpleRecordSchema in project nifi by apache.
the class TestMapRecord method testDefaultValueWithAliasesDefined.
@Test
public void testDefaultValueWithAliasesDefined() {
final List<RecordField> fields = new ArrayList<>();
fields.add(new RecordField("foo", RecordFieldType.STRING.getDataType(), "hello", set("bar", "baz")));
final RecordSchema schema = new SimpleRecordSchema(fields);
final Map<String, Object> values = new HashMap<>();
final Record record = new MapRecord(schema, values);
assertEquals("hello", record.getValue("foo"));
assertEquals("hello", record.getValue("bar"));
assertEquals("hello", record.getValue("baz"));
}
use of org.apache.nifi.serialization.SimpleRecordSchema in project nifi by apache.
the class TestMapRecord method testAliasConflictingAliasValues.
@Test
public void testAliasConflictingAliasValues() {
final List<RecordField> fields = new ArrayList<>();
fields.add(new RecordField("foo", RecordFieldType.STRING.getDataType(), null, set("bar", "baz")));
final RecordSchema schema = new SimpleRecordSchema(fields);
final Map<String, Object> values = new HashMap<>();
values.put("baz", 1);
values.put("bar", 33);
final Record record = new MapRecord(schema, values);
assertEquals(33, record.getValue("foo"));
assertEquals(33, record.getValue("bar"));
assertEquals(33, record.getValue("baz"));
}
use of org.apache.nifi.serialization.SimpleRecordSchema in project nifi by apache.
the class TestMapRecord method testAliasConflictingValues.
@Test
public void testAliasConflictingValues() {
final List<RecordField> fields = new ArrayList<>();
fields.add(new RecordField("foo", RecordFieldType.STRING.getDataType(), null, set("bar", "baz")));
final RecordSchema schema = new SimpleRecordSchema(fields);
final Map<String, Object> values = new HashMap<>();
values.put("bar", 1);
values.put("foo", null);
final Record record = new MapRecord(schema, values);
assertEquals(1, record.getValue("foo"));
assertEquals(1, record.getValue("bar"));
assertEquals(1, record.getValue("baz"));
}
Aggregations