Search in sources :

Example 1 with RecordFieldType

use of org.apache.nifi.serialization.record.RecordFieldType in project nifi by apache.

the class TestWriteCSVResult method testDataTypes.

@Test
public void testDataTypes() throws IOException {
    final CSVFormat csvFormat = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL).withRecordSeparator("\n");
    final StringBuilder headerBuilder = new StringBuilder();
    final List<RecordField> fields = new ArrayList<>();
    for (final RecordFieldType fieldType : RecordFieldType.values()) {
        if (fieldType == RecordFieldType.CHOICE) {
            final List<DataType> possibleTypes = new ArrayList<>();
            possibleTypes.add(RecordFieldType.INT.getDataType());
            possibleTypes.add(RecordFieldType.LONG.getDataType());
            fields.add(new RecordField(fieldType.name().toLowerCase(), fieldType.getChoiceDataType(possibleTypes)));
        } else {
            fields.add(new RecordField(fieldType.name().toLowerCase(), fieldType.getDataType()));
        }
        headerBuilder.append('"').append(fieldType.name().toLowerCase()).append('"').append(",");
    }
    final RecordSchema schema = new SimpleRecordSchema(fields);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final long now = System.currentTimeMillis();
    try (final WriteCSVResult result = new WriteCSVResult(csvFormat, schema, new SchemaNameAsAttribute(), baos, RecordFieldType.DATE.getDefaultFormat(), RecordFieldType.TIME.getDefaultFormat(), RecordFieldType.TIMESTAMP.getDefaultFormat(), true, "UTF-8")) {
        final Map<String, Object> valueMap = new HashMap<>();
        valueMap.put("string", "a孟bc李12儒3");
        valueMap.put("boolean", true);
        valueMap.put("byte", (byte) 1);
        valueMap.put("char", 'c');
        valueMap.put("short", (short) 8);
        valueMap.put("int", 9);
        valueMap.put("bigint", BigInteger.valueOf(8L));
        valueMap.put("long", 8L);
        valueMap.put("float", 8.0F);
        valueMap.put("double", 8.0D);
        valueMap.put("date", new Date(now));
        valueMap.put("time", new Time(now));
        valueMap.put("timestamp", new Timestamp(now));
        valueMap.put("record", null);
        valueMap.put("choice", 48L);
        valueMap.put("array", null);
        final Record record = new MapRecord(schema, valueMap);
        final RecordSet rs = RecordSet.of(schema, record);
        result.write(rs);
    }
    final String output = new String(baos.toByteArray(), StandardCharsets.UTF_8);
    headerBuilder.deleteCharAt(headerBuilder.length() - 1);
    final String headerLine = headerBuilder.toString();
    final String[] splits = output.split("\n");
    assertEquals(2, splits.length);
    assertEquals(headerLine, splits[0]);
    final String values = splits[1];
    final StringBuilder expectedBuilder = new StringBuilder();
    expectedBuilder.append("\"a孟bc李12儒3\",\"true\",\"1\",\"c\",\"8\",\"9\",\"8\",\"8\",\"8.0\",\"8.0\",");
    final String dateValue = getDateFormat(RecordFieldType.DATE.getDefaultFormat()).format(now);
    final String timeValue = getDateFormat(RecordFieldType.TIME.getDefaultFormat()).format(now);
    final String timestampValue = getDateFormat(RecordFieldType.TIMESTAMP.getDefaultFormat()).format(now);
    expectedBuilder.append('"').append(dateValue).append('"').append(',');
    expectedBuilder.append('"').append(timeValue).append('"').append(',');
    expectedBuilder.append('"').append(timestampValue).append('"').append(',');
    expectedBuilder.append(",\"48\",,");
    final String expectedValues = expectedBuilder.toString();
    assertEquals(expectedValues, values);
}
Also used : 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) Time(java.sql.Time) Timestamp(java.sql.Timestamp) DataType(org.apache.nifi.serialization.record.DataType) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordSet(org.apache.nifi.serialization.record.RecordSet) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) MapRecord(org.apache.nifi.serialization.record.MapRecord) SchemaNameAsAttribute(org.apache.nifi.schema.access.SchemaNameAsAttribute) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Date(java.sql.Date) CSVFormat(org.apache.commons.csv.CSVFormat) RecordFieldType(org.apache.nifi.serialization.record.RecordFieldType) Test(org.junit.Test)

Example 2 with RecordFieldType

use of org.apache.nifi.serialization.record.RecordFieldType in project nifi by apache.

the class TestJsonPathRowRecordReader method testElementWithNestedArray.

@Test
public void testElementWithNestedArray() throws IOException, MalformedRecordException {
    final LinkedHashMap<String, JsonPath> jsonPaths = new LinkedHashMap<>(allJsonPaths);
    jsonPaths.put("accounts", JsonPath.compile("$.accounts"));
    final DataType accountRecordType = RecordFieldType.RECORD.getRecordDataType(getAccountSchema());
    final DataType accountsType = RecordFieldType.ARRAY.getArrayDataType(accountRecordType);
    final List<RecordField> fields = getDefaultFields();
    fields.add(new RecordField("accounts", accountsType));
    final RecordSchema schema = new SimpleRecordSchema(fields);
    try (final InputStream in = new FileInputStream(new File("src/test/resources/json/single-element-nested-array.json"));
        final JsonPathRowRecordReader reader = new JsonPathRowRecordReader(jsonPaths, schema, in, Mockito.mock(ComponentLog.class), dateFormat, timeFormat, timestampFormat)) {
        final List<String> fieldNames = schema.getFieldNames();
        final List<String> expectedFieldNames = Arrays.asList(new String[] { "id", "name", "balance", "address", "city", "state", "zipCode", "country", "accounts" });
        assertEquals(expectedFieldNames, fieldNames);
        final List<RecordFieldType> dataTypes = schema.getDataTypes().stream().map(dt -> dt.getFieldType()).collect(Collectors.toList());
        final List<RecordFieldType> expectedTypes = Arrays.asList(new RecordFieldType[] { RecordFieldType.INT, RecordFieldType.STRING, RecordFieldType.DOUBLE, RecordFieldType.STRING, RecordFieldType.STRING, RecordFieldType.STRING, RecordFieldType.STRING, RecordFieldType.STRING, RecordFieldType.ARRAY });
        assertEquals(expectedTypes, dataTypes);
        final Object[] firstRecordValues = reader.nextRecord().getValues();
        final Object[] nonArrayValues = Arrays.copyOfRange(firstRecordValues, 0, firstRecordValues.length - 1);
        Assert.assertArrayEquals(new Object[] { 1, "John Doe", null, "123 My Street", "My City", "MS", "11111", "USA" }, nonArrayValues);
        final Object lastRecord = firstRecordValues[firstRecordValues.length - 1];
        assertTrue(Object[].class.isAssignableFrom(lastRecord.getClass()));
        final Object[] array = (Object[]) lastRecord;
        assertEquals(2, array.length);
        final Object firstElement = array[0];
        assertTrue(firstElement instanceof Record);
        final Record firstRecord = (Record) firstElement;
        assertEquals(42, firstRecord.getValue("id"));
        assertEquals(4750.89D, firstRecord.getValue("balance"));
        final Object secondElement = array[1];
        assertTrue(secondElement instanceof Record);
        final Record secondRecord = (Record) secondElement;
        assertEquals(43, secondRecord.getValue("id"));
        assertEquals(48212.38D, secondRecord.getValue("balance"));
        assertNull(reader.nextRecord());
    }
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) Arrays(java.util.Arrays) DataType(org.apache.nifi.serialization.record.DataType) ComponentLog(org.apache.nifi.logging.ComponentLog) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Record(org.apache.nifi.serialization.record.Record) Before(org.junit.Before) MalformedRecordException(org.apache.nifi.serialization.MalformedRecordException) Assert.assertNotNull(org.junit.Assert.assertNotNull) RecordField(org.apache.nifi.serialization.record.RecordField) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) Test(org.junit.Test) FileInputStream(java.io.FileInputStream) JsonPath(com.jayway.jsonpath.JsonPath) Collectors(java.util.stream.Collectors) File(java.io.File) Mockito(org.mockito.Mockito) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Assert(org.junit.Assert) RecordFieldType(org.apache.nifi.serialization.record.RecordFieldType) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) RecordField(org.apache.nifi.serialization.record.RecordField) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JsonPath(com.jayway.jsonpath.JsonPath) ComponentLog(org.apache.nifi.logging.ComponentLog) FileInputStream(java.io.FileInputStream) LinkedHashMap(java.util.LinkedHashMap) DataType(org.apache.nifi.serialization.record.DataType) Record(org.apache.nifi.serialization.record.Record) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) File(java.io.File) RecordFieldType(org.apache.nifi.serialization.record.RecordFieldType) Test(org.junit.Test)

Example 3 with RecordFieldType

use of org.apache.nifi.serialization.record.RecordFieldType in project nifi by apache.

the class TestJsonPathRowRecordReader method testReadArrayDifferentSchemas.

@Test
public void testReadArrayDifferentSchemas() throws IOException, MalformedRecordException {
    final RecordSchema schema = new SimpleRecordSchema(getDefaultFields());
    try (final InputStream in = new FileInputStream(new File("src/test/resources/json/bank-account-array-different-schemas.json"));
        final JsonPathRowRecordReader reader = new JsonPathRowRecordReader(allJsonPaths, schema, in, Mockito.mock(ComponentLog.class), dateFormat, timeFormat, timestampFormat)) {
        final List<String> fieldNames = schema.getFieldNames();
        final List<String> expectedFieldNames = Arrays.asList(new String[] { "id", "name", "balance", "address", "city", "state", "zipCode", "country" });
        assertEquals(expectedFieldNames, fieldNames);
        final List<RecordFieldType> dataTypes = schema.getDataTypes().stream().map(dt -> dt.getFieldType()).collect(Collectors.toList());
        final List<RecordFieldType> expectedTypes = Arrays.asList(new RecordFieldType[] { RecordFieldType.INT, RecordFieldType.STRING, RecordFieldType.DOUBLE, RecordFieldType.STRING, RecordFieldType.STRING, RecordFieldType.STRING, RecordFieldType.STRING, RecordFieldType.STRING });
        assertEquals(expectedTypes, dataTypes);
        final Object[] firstRecordValues = reader.nextRecord().getValues();
        Assert.assertArrayEquals(new Object[] { 1, "John Doe", 4750.89, "123 My Street", "My City", "MS", "11111", "USA" }, firstRecordValues);
        final Object[] secondRecordValues = reader.nextRecord().getValues();
        Assert.assertArrayEquals(new Object[] { 2, "Jane Doe", 4820.09, "321 Your Street", "Your City", "NY", "33333", null }, secondRecordValues);
        final Object[] thirdRecordValues = reader.nextRecord().getValues();
        Assert.assertArrayEquals(new Object[] { 3, "Jake Doe", 4751.89, "124 My Street", "My City", "MS", "11111", "USA" }, thirdRecordValues);
        assertNull(reader.nextRecord());
    }
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) Arrays(java.util.Arrays) DataType(org.apache.nifi.serialization.record.DataType) ComponentLog(org.apache.nifi.logging.ComponentLog) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Record(org.apache.nifi.serialization.record.Record) Before(org.junit.Before) MalformedRecordException(org.apache.nifi.serialization.MalformedRecordException) Assert.assertNotNull(org.junit.Assert.assertNotNull) RecordField(org.apache.nifi.serialization.record.RecordField) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) Test(org.junit.Test) FileInputStream(java.io.FileInputStream) JsonPath(com.jayway.jsonpath.JsonPath) Collectors(java.util.stream.Collectors) File(java.io.File) Mockito(org.mockito.Mockito) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Assert(org.junit.Assert) RecordFieldType(org.apache.nifi.serialization.record.RecordFieldType) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ComponentLog(org.apache.nifi.logging.ComponentLog) FileInputStream(java.io.FileInputStream) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) File(java.io.File) RecordFieldType(org.apache.nifi.serialization.record.RecordFieldType) Test(org.junit.Test)

Example 4 with RecordFieldType

use of org.apache.nifi.serialization.record.RecordFieldType in project nifi by apache.

the class TestJsonPathRowRecordReader method testReadArray.

@Test
public void testReadArray() throws IOException, MalformedRecordException {
    final RecordSchema schema = new SimpleRecordSchema(getDefaultFields());
    try (final InputStream in = new FileInputStream(new File("src/test/resources/json/bank-account-array.json"));
        final JsonPathRowRecordReader reader = new JsonPathRowRecordReader(allJsonPaths, schema, in, Mockito.mock(ComponentLog.class), dateFormat, timeFormat, timestampFormat)) {
        final List<String> fieldNames = schema.getFieldNames();
        final List<String> expectedFieldNames = Arrays.asList(new String[] { "id", "name", "balance", "address", "city", "state", "zipCode", "country" });
        assertEquals(expectedFieldNames, fieldNames);
        final List<RecordFieldType> dataTypes = schema.getDataTypes().stream().map(dt -> dt.getFieldType()).collect(Collectors.toList());
        final List<RecordFieldType> expectedTypes = Arrays.asList(new RecordFieldType[] { RecordFieldType.INT, RecordFieldType.STRING, RecordFieldType.DOUBLE, RecordFieldType.STRING, RecordFieldType.STRING, RecordFieldType.STRING, RecordFieldType.STRING, RecordFieldType.STRING });
        assertEquals(expectedTypes, dataTypes);
        final Object[] firstRecordValues = reader.nextRecord().getValues();
        Assert.assertArrayEquals(new Object[] { 1, "John Doe", 4750.89, "123 My Street", "My City", "MS", "11111", "USA" }, firstRecordValues);
        final Object[] secondRecordValues = reader.nextRecord().getValues();
        Assert.assertArrayEquals(new Object[] { 2, "Jane Doe", 4820.09, "321 Your Street", "Your City", "NY", "33333", "USA" }, secondRecordValues);
        assertNull(reader.nextRecord());
    }
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) Arrays(java.util.Arrays) DataType(org.apache.nifi.serialization.record.DataType) ComponentLog(org.apache.nifi.logging.ComponentLog) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Record(org.apache.nifi.serialization.record.Record) Before(org.junit.Before) MalformedRecordException(org.apache.nifi.serialization.MalformedRecordException) Assert.assertNotNull(org.junit.Assert.assertNotNull) RecordField(org.apache.nifi.serialization.record.RecordField) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) Test(org.junit.Test) FileInputStream(java.io.FileInputStream) JsonPath(com.jayway.jsonpath.JsonPath) Collectors(java.util.stream.Collectors) File(java.io.File) Mockito(org.mockito.Mockito) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Assert(org.junit.Assert) RecordFieldType(org.apache.nifi.serialization.record.RecordFieldType) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ComponentLog(org.apache.nifi.logging.ComponentLog) FileInputStream(java.io.FileInputStream) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) File(java.io.File) RecordFieldType(org.apache.nifi.serialization.record.RecordFieldType) Test(org.junit.Test)

Example 5 with RecordFieldType

use of org.apache.nifi.serialization.record.RecordFieldType in project nifi by apache.

the class TestJsonPathRowRecordReader method testPrimitiveTypeArrays.

@Test
public void testPrimitiveTypeArrays() throws IOException, MalformedRecordException {
    final LinkedHashMap<String, JsonPath> jsonPaths = new LinkedHashMap<>(allJsonPaths);
    jsonPaths.put("accountIds", JsonPath.compile("$.accountIds"));
    final List<RecordField> fields = getDefaultFields();
    final DataType idsType = RecordFieldType.ARRAY.getArrayDataType(RecordFieldType.INT.getDataType());
    fields.add(new RecordField("accountIds", idsType));
    final RecordSchema schema = new SimpleRecordSchema(fields);
    try (final InputStream in = new FileInputStream(new File("src/test/resources/json/primitive-type-array.json"));
        final JsonPathRowRecordReader reader = new JsonPathRowRecordReader(jsonPaths, schema, in, Mockito.mock(ComponentLog.class), dateFormat, timeFormat, timestampFormat)) {
        final List<String> fieldNames = schema.getFieldNames();
        final List<String> expectedFieldNames = Arrays.asList(new String[] { "id", "name", "balance", "address", "city", "state", "zipCode", "country", "accountIds" });
        assertEquals(expectedFieldNames, fieldNames);
        final List<RecordFieldType> dataTypes = schema.getDataTypes().stream().map(dt -> dt.getFieldType()).collect(Collectors.toList());
        final List<RecordFieldType> expectedTypes = Arrays.asList(new RecordFieldType[] { RecordFieldType.INT, RecordFieldType.STRING, RecordFieldType.DOUBLE, RecordFieldType.STRING, RecordFieldType.STRING, RecordFieldType.STRING, RecordFieldType.STRING, RecordFieldType.STRING, RecordFieldType.ARRAY });
        assertEquals(expectedTypes, dataTypes);
        final Object[] firstRecordValues = reader.nextRecord().getValues();
        final Object[] nonArrayValues = Arrays.copyOfRange(firstRecordValues, 0, firstRecordValues.length - 1);
        Assert.assertArrayEquals(new Object[] { 1, "John Doe", 4750.89D, "123 My Street", "My City", "MS", "11111", "USA" }, nonArrayValues);
        final Object lastRecord = firstRecordValues[firstRecordValues.length - 1];
        assertNotNull(lastRecord);
        assertTrue(Object[].class.isAssignableFrom(lastRecord.getClass()));
        final Object[] array = (Object[]) lastRecord;
        Assert.assertArrayEquals(new Object[] { 1, 2, 3 }, array);
        assertNull(reader.nextRecord());
        assertNull(reader.nextRecord());
    }
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) Arrays(java.util.Arrays) DataType(org.apache.nifi.serialization.record.DataType) ComponentLog(org.apache.nifi.logging.ComponentLog) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Record(org.apache.nifi.serialization.record.Record) Before(org.junit.Before) MalformedRecordException(org.apache.nifi.serialization.MalformedRecordException) Assert.assertNotNull(org.junit.Assert.assertNotNull) RecordField(org.apache.nifi.serialization.record.RecordField) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) Test(org.junit.Test) FileInputStream(java.io.FileInputStream) JsonPath(com.jayway.jsonpath.JsonPath) Collectors(java.util.stream.Collectors) File(java.io.File) Mockito(org.mockito.Mockito) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Assert(org.junit.Assert) RecordFieldType(org.apache.nifi.serialization.record.RecordFieldType) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) RecordField(org.apache.nifi.serialization.record.RecordField) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JsonPath(com.jayway.jsonpath.JsonPath) ComponentLog(org.apache.nifi.logging.ComponentLog) FileInputStream(java.io.FileInputStream) LinkedHashMap(java.util.LinkedHashMap) DataType(org.apache.nifi.serialization.record.DataType) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) File(java.io.File) RecordFieldType(org.apache.nifi.serialization.record.RecordFieldType) Test(org.junit.Test)

Aggregations

DataType (org.apache.nifi.serialization.record.DataType)21 RecordFieldType (org.apache.nifi.serialization.record.RecordFieldType)21 RecordField (org.apache.nifi.serialization.record.RecordField)19 ArrayList (java.util.ArrayList)18 SimpleRecordSchema (org.apache.nifi.serialization.SimpleRecordSchema)18 Record (org.apache.nifi.serialization.record.Record)18 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)18 Test (org.junit.Test)18 ComponentLog (org.apache.nifi.logging.ComponentLog)16 File (java.io.File)15 FileInputStream (java.io.FileInputStream)15 IOException (java.io.IOException)15 InputStream (java.io.InputStream)15 Arrays (java.util.Arrays)15 List (java.util.List)15 Collectors (java.util.stream.Collectors)15 MalformedRecordException (org.apache.nifi.serialization.MalformedRecordException)15 Assert (org.junit.Assert)15 Assert.assertEquals (org.junit.Assert.assertEquals)15 Assert.assertNull (org.junit.Assert.assertNull)15