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());
}
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());
}
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());
}
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);
}
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());
}
Aggregations