Search in sources :

Example 56 with SimpleRecordSchema

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);
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) ArrayList(java.util.ArrayList)

Example 57 with SimpleRecordSchema

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);
}
Also used : BigInteger(java.math.BigInteger) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) RecordField(org.apache.nifi.serialization.record.RecordField) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList)

Example 58 with SimpleRecordSchema

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"));
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) Test(org.junit.Test)

Example 59 with SimpleRecordSchema

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"));
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) Test(org.junit.Test)

Example 60 with SimpleRecordSchema

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"));
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) Test(org.junit.Test)

Aggregations

SimpleRecordSchema (org.apache.nifi.serialization.SimpleRecordSchema)171 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)152 Test (org.junit.Test)146 RecordField (org.apache.nifi.serialization.record.RecordField)127 Record (org.apache.nifi.serialization.record.Record)120 ArrayList (java.util.ArrayList)114 MapRecord (org.apache.nifi.serialization.record.MapRecord)86 HashMap (java.util.HashMap)82 InputStream (java.io.InputStream)58 FileInputStream (java.io.FileInputStream)53 ByteArrayInputStream (java.io.ByteArrayInputStream)52 ComponentLog (org.apache.nifi.logging.ComponentLog)50 LinkedHashMap (java.util.LinkedHashMap)33 File (java.io.File)30 DataType (org.apache.nifi.serialization.record.DataType)26 ByteArrayOutputStream (java.io.ByteArrayOutputStream)21 MalformedRecordException (org.apache.nifi.serialization.MalformedRecordException)19 RecordFieldType (org.apache.nifi.serialization.record.RecordFieldType)19 List (java.util.List)18 IOException (java.io.IOException)17