Search in sources :

Example 61 with SimpleRecordSchema

use of org.apache.nifi.serialization.SimpleRecordSchema in project nifi by apache.

the class TestMapRecord method testDefaultValueWithAliasValue.

@Test
public void testDefaultValueWithAliasValue() {
    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<>();
    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 62 with SimpleRecordSchema

use of org.apache.nifi.serialization.SimpleRecordSchema in project nifi by apache.

the class TestMapRecord method testDefaultValueInGivenField.

@Test
public void testDefaultValueInGivenField() {
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("noDefault", RecordFieldType.STRING.getDataType()));
    fields.add(new RecordField("defaultOfHello", RecordFieldType.STRING.getDataType(), "hello"));
    final RecordSchema schema = new SimpleRecordSchema(fields);
    final Map<String, Object> values = new HashMap<>();
    final Record record = new MapRecord(schema, values);
    assertNull(record.getValue("noDefault"));
    assertEquals("hello", record.getValue("defaultOfHello"));
    final RecordField newField = new RecordField("noDefault", RecordFieldType.STRING.getDataType(), "new");
    assertEquals("new", record.getValue(newField));
}
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 63 with SimpleRecordSchema

use of org.apache.nifi.serialization.SimpleRecordSchema in project nifi by apache.

the class TestRecordPath method testRelativePath.

@Test
public void testRelativePath() {
    final Map<String, Object> accountValues = new HashMap<>();
    accountValues.put("id", 1);
    accountValues.put("balance", 123.45D);
    final Record accountRecord = new MapRecord(getAccountSchema(), accountValues);
    final RecordSchema schema = new SimpleRecordSchema(getDefaultFields());
    final Map<String, Object> values = new HashMap<>();
    values.put("id", 48);
    values.put("name", "John Doe");
    values.put("mainAccount", accountRecord);
    final Record record = new MapRecord(schema, values);
    final List<FieldValue> fieldValues = RecordPath.compile("/mainAccount/././balance/.").evaluate(record).getSelectedFields().collect(Collectors.toList());
    assertEquals(1, fieldValues.size());
    final FieldValue fieldValue = fieldValues.get(0);
    assertEquals(accountRecord, fieldValue.getParentRecord().get());
    assertEquals(123.45D, fieldValue.getValue());
    assertEquals("balance", fieldValue.getField().getFieldName());
    RecordPath.compile("/mainAccount/././balance/.").evaluate(record).getSelectedFields().forEach(field -> field.updateValue(123.44D));
    assertEquals(123.44D, accountValues.get("balance"));
}
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 64 with SimpleRecordSchema

use of org.apache.nifi.serialization.SimpleRecordSchema in project nifi by apache.

the class TestRecordPath method testToDateFromNonDateString.

@Test
public void testToDateFromNonDateString() {
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("id", RecordFieldType.INT.getDataType()));
    fields.add(new RecordField("name", RecordFieldType.DATE.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);
    // since the field is a string it shouldn't do the conversion and should return the value unchanged
    final FieldValue fieldValue = RecordPath.compile("toDate(/name, \"yyyy-MM-dd'T'HH:mm:ss'Z'\")").evaluate(record).getSelectedFields().findFirst().get();
    assertEquals("John Doe", fieldValue.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 65 with SimpleRecordSchema

use of org.apache.nifi.serialization.SimpleRecordSchema in project nifi by apache.

the class TestRecordPath method testEscapedFieldName.

@Test
public void testEscapedFieldName() {
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("id", RecordFieldType.INT.getDataType()));
    fields.add(new RecordField("name,date", RecordFieldType.STRING.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);
    final Map<String, Object> values = new HashMap<>();
    values.put("id", 48);
    values.put("name,date", "John Doe");
    final Record record = new MapRecord(schema, values);
    final FieldValue fieldValue = RecordPath.compile("/'name,date'").evaluate(record).getSelectedFields().findFirst().get();
    assertEquals("name,date", fieldValue.getField().getFieldName());
    assertEquals("John Doe", fieldValue.getValue());
    assertEquals(record, fieldValue.getParentRecord().get());
}
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)

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