Search in sources :

Example 61 with MapRecord

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

the class TestRecordPath method testFormatDateWhenNotDate.

@Test
public void testFormatDateWhenNotDate() {
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("id", RecordFieldType.INT.getDataType()));
    fields.add(new RecordField("name", RecordFieldType.STRING.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);
    final Map<String, Object> values = new HashMap<>();
    values.put("id", 48);
    values.put("name", "John Doe");
    final Record record = new MapRecord(schema, values);
    assertEquals("John Doe", RecordPath.compile("format(/name, 'yyyy-MM')").evaluate(record).getSelectedFields().findFirst().get().getValue());
}
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) ArrayList(java.util.ArrayList) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Test(org.junit.Test)

Example 62 with MapRecord

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

the class TestRecordPath method testMultiArrayIndexWithRanges.

@Test
public void testMultiArrayIndexWithRanges() {
    final RecordSchema schema = new SimpleRecordSchema(getDefaultFields());
    final Map<String, Object> values = new HashMap<>();
    values.put("id", 48);
    values.put("numbers", new Object[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
    final Record record = new MapRecord(schema, values);
    List<FieldValue> fieldValues = RecordPath.compile("/numbers[0, 2, 4..7, 9]").evaluate(record).getSelectedFields().collect(Collectors.toList());
    for (final FieldValue fieldValue : fieldValues) {
        assertEquals("numbers", fieldValue.getField().getFieldName());
        assertEquals(record, fieldValue.getParentRecord().get());
    }
    int[] expectedValues = new int[] { 0, 2, 4, 5, 6, 7, 9 };
    assertEquals(expectedValues.length, fieldValues.size());
    for (int i = 0; i < expectedValues.length; i++) {
        assertEquals(expectedValues[i], fieldValues.get(i).getValue());
    }
    fieldValues = RecordPath.compile("/numbers[0..-1]").evaluate(record).getSelectedFields().collect(Collectors.toList());
    for (final FieldValue fieldValue : fieldValues) {
        assertEquals("numbers", fieldValue.getField().getFieldName());
        assertEquals(record, fieldValue.getParentRecord().get());
    }
    expectedValues = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    assertEquals(expectedValues.length, fieldValues.size());
    for (int i = 0; i < expectedValues.length; i++) {
        assertEquals(expectedValues[i], fieldValues.get(i).getValue());
    }
    fieldValues = RecordPath.compile("/numbers[-1..-1]").evaluate(record).getSelectedFields().collect(Collectors.toList());
    for (final FieldValue fieldValue : fieldValues) {
        assertEquals("numbers", fieldValue.getField().getFieldName());
        assertEquals(record, fieldValue.getParentRecord().get());
    }
    expectedValues = new int[] { 9 };
    assertEquals(expectedValues.length, fieldValues.size());
    for (int i = 0; i < expectedValues.length; i++) {
        assertEquals(expectedValues[i], fieldValues.get(i).getValue());
    }
    fieldValues = RecordPath.compile("/numbers[*]").evaluate(record).getSelectedFields().collect(Collectors.toList());
    for (final FieldValue fieldValue : fieldValues) {
        assertEquals("numbers", fieldValue.getField().getFieldName());
        assertEquals(record, fieldValue.getParentRecord().get());
    }
    expectedValues = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    assertEquals(expectedValues.length, fieldValues.size());
    for (int i = 0; i < expectedValues.length; i++) {
        assertEquals(expectedValues[i], fieldValues.get(i).getValue());
    }
    fieldValues = RecordPath.compile("/xx[1,2,3]").evaluate(record).getSelectedFields().collect(Collectors.toList());
    assertEquals(0, fieldValues.size());
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) MapRecord(org.apache.nifi.serialization.record.MapRecord) HashMap(java.util.HashMap) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Test(org.junit.Test)

Example 63 with MapRecord

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

the class TestRecordPath method testPredicateWithAbsolutePath.

@Test
public void testPredicateWithAbsolutePath() {
    final List<RecordField> addressFields = new ArrayList<>();
    addressFields.add(new RecordField("city", RecordFieldType.STRING.getDataType()));
    addressFields.add(new RecordField("state", RecordFieldType.STRING.getDataType()));
    addressFields.add(new RecordField("zip", RecordFieldType.STRING.getDataType()));
    final RecordSchema addressSchema = new SimpleRecordSchema(addressFields);
    final List<RecordField> detailsFields = new ArrayList<>();
    detailsFields.add(new RecordField("position", RecordFieldType.STRING.getDataType()));
    detailsFields.add(new RecordField("preferredState", RecordFieldType.STRING.getDataType()));
    final RecordSchema detailsSchema = new SimpleRecordSchema(detailsFields);
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("name", RecordFieldType.STRING.getDataType()));
    fields.add(new RecordField("address1", RecordFieldType.RECORD.getRecordDataType(addressSchema)));
    fields.add(new RecordField("address2", RecordFieldType.RECORD.getRecordDataType(addressSchema)));
    fields.add(new RecordField("details", RecordFieldType.RECORD.getRecordDataType(detailsSchema)));
    final RecordSchema recordSchema = new SimpleRecordSchema(fields);
    final Record record = new MapRecord(recordSchema, new HashMap<>());
    record.setValue("name", "John Doe");
    final Record address1Record = new MapRecord(addressSchema, new HashMap<>());
    address1Record.setValue("city", "San Francisco");
    address1Record.setValue("state", "CA");
    address1Record.setValue("zip", "12345");
    record.setValue("address1", address1Record);
    final Record address2Record = new MapRecord(addressSchema, new HashMap<>());
    address2Record.setValue("city", "New York");
    address2Record.setValue("state", "NY");
    address2Record.setValue("zip", "01234");
    record.setValue("address2", address2Record);
    final Record detailsRecord = new MapRecord(detailsSchema, new HashMap<>());
    detailsRecord.setValue("position", "Developer");
    detailsRecord.setValue("preferredState", "NY");
    record.setValue("details", detailsRecord);
    final RecordPath recordPath = RecordPath.compile("/*[./state = /details/preferredState]");
    final RecordPathResult result = recordPath.evaluate(record);
    final List<FieldValue> fieldValues = result.getSelectedFields().collect(Collectors.toList());
    assertEquals(1, fieldValues.size());
    final FieldValue fieldValue = fieldValues.get(0);
    assertEquals("address2", fieldValue.getField().getFieldName());
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordField(org.apache.nifi.serialization.record.RecordField) ArrayList(java.util.ArrayList) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Test(org.junit.Test)

Example 64 with MapRecord

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

the class TestRecordPath method testToDateFromString.

@Test
public void testToDateFromString() {
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("id", RecordFieldType.INT.getDataType()));
    fields.add(new RecordField("date", RecordFieldType.DATE.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);
    final Map<String, Object> values = new HashMap<>();
    values.put("id", 48);
    values.put("date", "2017-10-20T11:00:00Z");
    final Record record = new MapRecord(schema, values);
    assertTrue(RecordPath.compile("toDate(/date, \"yyyy-MM-dd'T'HH:mm:ss'Z'\")").evaluate(record).getSelectedFields().findFirst().get().getValue() instanceof Date);
}
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) ArrayList(java.util.ArrayList) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Date(java.sql.Date) Test(org.junit.Test)

Example 65 with MapRecord

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

the class TestRecordPath method testFormatDateFromLong.

@Test
public void testFormatDateFromLong() throws ParseException {
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("id", RecordFieldType.INT.getDataType()));
    fields.add(new RecordField("date", RecordFieldType.LONG.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);
    final DateFormat dateFormat = DataTypeUtils.getDateFormat("yyyy-MM-dd");
    final long dateValue = dateFormat.parse("2017-10-20").getTime();
    final Map<String, Object> values = new HashMap<>();
    values.put("id", 48);
    values.put("date", dateValue);
    final Record record = new MapRecord(schema, values);
    assertEquals("2017-10-20", RecordPath.compile("format(/date, 'yyyy-MM-dd' )").evaluate(record).getSelectedFields().findFirst().get().getValue());
    final FieldValue fieldValueUnchanged = RecordPath.compile("format(/date, 'INVALID' )").evaluate(record).getSelectedFields().findFirst().get();
    assertEquals(dateValue, fieldValueUnchanged.getValue());
}
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) ArrayList(java.util.ArrayList) DateFormat(java.text.DateFormat) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Test(org.junit.Test)

Aggregations

MapRecord (org.apache.nifi.serialization.record.MapRecord)101 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)89 SimpleRecordSchema (org.apache.nifi.serialization.SimpleRecordSchema)88 Record (org.apache.nifi.serialization.record.Record)82 Test (org.junit.Test)79 HashMap (java.util.HashMap)73 RecordField (org.apache.nifi.serialization.record.RecordField)71 ArrayList (java.util.ArrayList)59 LinkedHashMap (java.util.LinkedHashMap)29 ByteArrayOutputStream (java.io.ByteArrayOutputStream)20 SchemaNameAsAttribute (org.apache.nifi.schema.access.SchemaNameAsAttribute)17 DataType (org.apache.nifi.serialization.record.DataType)12 ComponentLog (org.apache.nifi.logging.ComponentLog)11 Date (java.sql.Date)7 CSVFormat (org.apache.commons.csv.CSVFormat)7 SchemaValidationResult (org.apache.nifi.serialization.record.validation.SchemaValidationResult)7 Time (java.sql.Time)6 Timestamp (java.sql.Timestamp)6 DateFormat (java.text.DateFormat)6 Map (java.util.Map)6