Search in sources :

Example 6 with SchemaNameAsAttribute

use of org.apache.nifi.schema.access.SchemaNameAsAttribute in project nifi by apache.

the class TestWriteCSVResult method testExtraFieldInWriteRecord.

@Test
public void testExtraFieldInWriteRecord() throws IOException {
    final CSVFormat csvFormat = CSVFormat.DEFAULT.withEscape('\\').withQuoteMode(QuoteMode.NONE).withRecordSeparator("\n");
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("id", RecordFieldType.STRING.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);
    final Map<String, Object> values = new HashMap<>();
    values.put("id", "1");
    values.put("name", "John");
    final Record record = new MapRecord(schema, values);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final String output;
    try (final WriteCSVResult writer = new WriteCSVResult(csvFormat, schema, new SchemaNameAsAttribute(), baos, RecordFieldType.DATE.getDefaultFormat(), RecordFieldType.TIME.getDefaultFormat(), RecordFieldType.TIMESTAMP.getDefaultFormat(), true, "ASCII")) {
        writer.beginRecordSet();
        writer.write(record);
        writer.finishRecordSet();
        writer.flush();
        output = baos.toString();
    }
    assertEquals("id\n1\n", output);
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordField(org.apache.nifi.serialization.record.RecordField) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SchemaNameAsAttribute(org.apache.nifi.schema.access.SchemaNameAsAttribute) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CSVFormat(org.apache.commons.csv.CSVFormat) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) Test(org.junit.Test)

Example 7 with SchemaNameAsAttribute

use of org.apache.nifi.schema.access.SchemaNameAsAttribute in project nifi by apache.

the class TestWriteCSVResult method testMissingFieldWriteRawRecord.

@Test
public void testMissingFieldWriteRawRecord() throws IOException {
    final CSVFormat csvFormat = CSVFormat.DEFAULT.withEscape('\\').withQuoteMode(QuoteMode.NONE).withRecordSeparator("\n");
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("id", RecordFieldType.STRING.getDataType()));
    fields.add(new RecordField("name", RecordFieldType.STRING.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);
    final Map<String, Object> values = new LinkedHashMap<>();
    values.put("id", "1");
    final Record record = new MapRecord(schema, values);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final String output;
    try (final WriteCSVResult writer = new WriteCSVResult(csvFormat, schema, new SchemaNameAsAttribute(), baos, RecordFieldType.DATE.getDefaultFormat(), RecordFieldType.TIME.getDefaultFormat(), RecordFieldType.TIMESTAMP.getDefaultFormat(), true, "ASCII")) {
        writer.beginRecordSet();
        writer.writeRawRecord(record);
        writer.finishRecordSet();
        writer.flush();
        output = baos.toString();
    }
    assertEquals("id,name\n1,\n", output);
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordField(org.apache.nifi.serialization.record.RecordField) SchemaNameAsAttribute(org.apache.nifi.schema.access.SchemaNameAsAttribute) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LinkedHashMap(java.util.LinkedHashMap) CSVFormat(org.apache.commons.csv.CSVFormat) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) Test(org.junit.Test)

Example 8 with SchemaNameAsAttribute

use of org.apache.nifi.schema.access.SchemaNameAsAttribute in project nifi by apache.

the class TestWriteJsonResult method testExtraFieldInWriteRecord.

@Test
public void testExtraFieldInWriteRecord() throws IOException {
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("id", RecordFieldType.STRING.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);
    final Map<String, Object> values = new HashMap<>();
    values.put("id", "1");
    values.put("name", "John");
    final Record record = new MapRecord(schema, values);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (final WriteJsonResult writer = new WriteJsonResult(Mockito.mock(ComponentLog.class), schema, new SchemaNameAsAttribute(), baos, false, NullSuppression.NEVER_SUPPRESS, null, null, null)) {
        writer.beginRecordSet();
        writer.writeRecord(record);
        writer.finishRecordSet();
    }
    final byte[] data = baos.toByteArray();
    final String expected = "[{\"id\":\"1\"}]";
    final String output = new String(data, StandardCharsets.UTF_8);
    assertEquals(expected, output);
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordField(org.apache.nifi.serialization.record.RecordField) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SchemaNameAsAttribute(org.apache.nifi.schema.access.SchemaNameAsAttribute) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ComponentLog(org.apache.nifi.logging.ComponentLog) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) Test(org.junit.Test)

Example 9 with SchemaNameAsAttribute

use of org.apache.nifi.schema.access.SchemaNameAsAttribute in project nifi by apache.

the class TestWriteJsonResult method testMissingAndExtraFieldInWriteRawRecord.

@Test
public void testMissingAndExtraFieldInWriteRawRecord() throws IOException {
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("id", RecordFieldType.STRING.getDataType()));
    fields.add(new RecordField("name", RecordFieldType.STRING.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);
    final Map<String, Object> values = new LinkedHashMap<>();
    values.put("id", "1");
    values.put("dob", "1/1/1970");
    final Record record = new MapRecord(schema, values);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (final WriteJsonResult writer = new WriteJsonResult(Mockito.mock(ComponentLog.class), schema, new SchemaNameAsAttribute(), baos, false, NullSuppression.NEVER_SUPPRESS, null, null, null)) {
        writer.beginRecordSet();
        writer.writeRawRecord(record);
        writer.finishRecordSet();
    }
    final byte[] data = baos.toByteArray();
    final String expected = "[{\"id\":\"1\",\"dob\":\"1/1/1970\"}]";
    final String output = new String(data, StandardCharsets.UTF_8);
    assertEquals(expected, output);
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordField(org.apache.nifi.serialization.record.RecordField) SchemaNameAsAttribute(org.apache.nifi.schema.access.SchemaNameAsAttribute) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ComponentLog(org.apache.nifi.logging.ComponentLog) LinkedHashMap(java.util.LinkedHashMap) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) Test(org.junit.Test)

Example 10 with SchemaNameAsAttribute

use of org.apache.nifi.schema.access.SchemaNameAsAttribute in project nifi by apache.

the class TestWriteJsonResult method testMissingFieldInWriteRecord.

@Test
public void testMissingFieldInWriteRecord() throws IOException {
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("id", RecordFieldType.STRING.getDataType()));
    fields.add(new RecordField("name", RecordFieldType.STRING.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);
    final Map<String, Object> values = new LinkedHashMap<>();
    values.put("id", "1");
    final Record record = new MapRecord(schema, values);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (final WriteJsonResult writer = new WriteJsonResult(Mockito.mock(ComponentLog.class), schema, new SchemaNameAsAttribute(), baos, false, NullSuppression.NEVER_SUPPRESS, null, null, null)) {
        writer.beginRecordSet();
        writer.writeRecord(record);
        writer.finishRecordSet();
    }
    final byte[] data = baos.toByteArray();
    final String expected = "[{\"id\":\"1\",\"name\":null}]";
    final String output = new String(data, StandardCharsets.UTF_8);
    assertEquals(expected, output);
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordField(org.apache.nifi.serialization.record.RecordField) SchemaNameAsAttribute(org.apache.nifi.schema.access.SchemaNameAsAttribute) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ComponentLog(org.apache.nifi.logging.ComponentLog) LinkedHashMap(java.util.LinkedHashMap) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) Test(org.junit.Test)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)17 ArrayList (java.util.ArrayList)17 LinkedHashMap (java.util.LinkedHashMap)17 SchemaNameAsAttribute (org.apache.nifi.schema.access.SchemaNameAsAttribute)17 SimpleRecordSchema (org.apache.nifi.serialization.SimpleRecordSchema)17 MapRecord (org.apache.nifi.serialization.record.MapRecord)17 Record (org.apache.nifi.serialization.record.Record)17 RecordField (org.apache.nifi.serialization.record.RecordField)17 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)17 Test (org.junit.Test)17 ComponentLog (org.apache.nifi.logging.ComponentLog)10 CSVFormat (org.apache.commons.csv.CSVFormat)7 HashMap (java.util.HashMap)5 RecordSet (org.apache.nifi.serialization.record.RecordSet)4 Date (java.sql.Date)3 Time (java.sql.Time)3 Timestamp (java.sql.Timestamp)3 DataType (org.apache.nifi.serialization.record.DataType)2 RecordFieldType (org.apache.nifi.serialization.record.RecordFieldType)2 DateFormat (java.text.DateFormat)1