Search in sources :

Example 6 with MockRecordWriter

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

the class TestPartitionRecord method setup.

@Before
public void setup() throws InitializationException {
    readerService = new MockRecordParser();
    writerService = new MockRecordWriter(null, false);
    runner = TestRunners.newTestRunner(PartitionRecord.class);
    runner.addControllerService("reader", readerService);
    runner.enableControllerService(readerService);
    runner.addControllerService("writer", writerService);
    runner.enableControllerService(writerService);
    runner.setProperty(PartitionRecord.RECORD_READER, "reader");
    runner.setProperty(PartitionRecord.RECORD_WRITER, "writer");
    readerService.addSchemaField("name", RecordFieldType.STRING);
    readerService.addSchemaField("age", RecordFieldType.INT);
    readerService.addSchemaField("sports", RecordFieldType.ARRAY);
}
Also used : MockRecordWriter(org.apache.nifi.serialization.record.MockRecordWriter) MockRecordParser(org.apache.nifi.serialization.record.MockRecordParser) Before(org.junit.Before)

Example 7 with MockRecordWriter

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

the class TestQueryRecord method testAggregateFunction.

@Test
public void testAggregateFunction() throws InitializationException, IOException {
    final MockRecordParser parser = new MockRecordParser();
    parser.addSchemaField("name", RecordFieldType.STRING);
    parser.addSchemaField("points", RecordFieldType.INT);
    parser.addRecord("Tom", 1);
    parser.addRecord("Jerry", 2);
    parser.addRecord("Tom", 99);
    final MockRecordWriter writer = new MockRecordWriter("\"name\",\"points\"");
    final TestRunner runner = TestRunners.newTestRunner(QueryRecord.class);
    runner.addControllerService("parser", parser);
    runner.enableControllerService(parser);
    runner.addControllerService("writer", writer);
    runner.enableControllerService(writer);
    runner.setProperty(REL_NAME, "select name, sum(points) as points from FLOWFILE GROUP BY name");
    runner.setProperty(QueryRecord.RECORD_READER_FACTORY, "parser");
    runner.setProperty(QueryRecord.RECORD_WRITER_FACTORY, "writer");
    runner.enqueue("");
    runner.run();
    runner.assertTransferCount(REL_NAME, 1);
    final MockFlowFile flowFileOut = runner.getFlowFilesForRelationship(ExecuteProcess.REL_SUCCESS).get(0);
    flowFileOut.assertContentEquals("\"name\",\"points\"\n\"Tom\",\"100\"\n\"Jerry\",\"2\"\n");
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) TestRunner(org.apache.nifi.util.TestRunner) MockRecordWriter(org.apache.nifi.serialization.record.MockRecordWriter) MockRecordParser(org.apache.nifi.serialization.record.MockRecordParser) Test(org.junit.Test)

Example 8 with MockRecordWriter

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

the class TestQueryRecord method testSimple.

@Test
public void testSimple() throws InitializationException, IOException, SQLException {
    final MockRecordParser parser = new MockRecordParser();
    parser.addSchemaField("name", RecordFieldType.STRING);
    parser.addSchemaField("age", RecordFieldType.INT);
    parser.addRecord("Tom", 49);
    final MockRecordWriter writer = new MockRecordWriter("\"name\",\"points\"");
    final TestRunner runner = TestRunners.newTestRunner(QueryRecord.class);
    runner.addControllerService("parser", parser);
    runner.enableControllerService(parser);
    runner.addControllerService("writer", writer);
    runner.enableControllerService(writer);
    runner.setProperty(REL_NAME, "select name, age from FLOWFILE WHERE name <> ''");
    runner.setProperty(QueryRecord.RECORD_READER_FACTORY, "parser");
    runner.setProperty(QueryRecord.RECORD_WRITER_FACTORY, "writer");
    final int numIterations = 1;
    for (int i = 0; i < numIterations; i++) {
        runner.enqueue(new byte[0]);
    }
    runner.setThreadCount(4);
    runner.run(2 * numIterations);
    runner.assertTransferCount(REL_NAME, 1);
    final MockFlowFile out = runner.getFlowFilesForRelationship(REL_NAME).get(0);
    System.out.println(new String(out.toByteArray()));
    out.assertContentEquals("\"name\",\"points\"\n\"Tom\",\"49\"\n");
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) TestRunner(org.apache.nifi.util.TestRunner) MockRecordWriter(org.apache.nifi.serialization.record.MockRecordWriter) MockRecordParser(org.apache.nifi.serialization.record.MockRecordParser) Test(org.junit.Test)

Example 9 with MockRecordWriter

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

the class TestQueryRecord method testParseFailure.

@Test
public void testParseFailure() throws InitializationException, IOException, SQLException {
    final MockRecordParser parser = new MockRecordParser();
    parser.addSchemaField("name", RecordFieldType.STRING);
    parser.addSchemaField("age", RecordFieldType.INT);
    parser.addRecord("Tom", 49);
    final MockRecordWriter writer = new MockRecordWriter("\"name\",\"points\"");
    final TestRunner runner = TestRunners.newTestRunner(QueryRecord.class);
    runner.addControllerService("parser", parser);
    runner.enableControllerService(parser);
    runner.addControllerService("writer", writer);
    runner.enableControllerService(writer);
    runner.setProperty(REL_NAME, "select name, age from FLOWFILE WHERE name <> ''");
    runner.setProperty(QueryRecord.RECORD_READER_FACTORY, "parser");
    runner.setProperty(QueryRecord.RECORD_WRITER_FACTORY, "writer");
    final int numIterations = 1;
    for (int i = 0; i < numIterations; i++) {
        runner.enqueue(new byte[0]);
    }
    runner.setThreadCount(4);
    runner.run(2 * numIterations);
    runner.assertTransferCount(REL_NAME, 1);
    final MockFlowFile out = runner.getFlowFilesForRelationship(REL_NAME).get(0);
    System.out.println(new String(out.toByteArray()));
    out.assertContentEquals("\"name\",\"points\"\n\"Tom\",\"49\"\n");
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) TestRunner(org.apache.nifi.util.TestRunner) MockRecordWriter(org.apache.nifi.serialization.record.MockRecordWriter) MockRecordParser(org.apache.nifi.serialization.record.MockRecordParser) Test(org.junit.Test)

Example 10 with MockRecordWriter

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

the class TestQueryRecord method testTransformCalc.

@Test
public void testTransformCalc() throws InitializationException, IOException, SQLException {
    final MockRecordParser parser = new MockRecordParser();
    parser.addSchemaField("ID", RecordFieldType.INT);
    parser.addSchemaField("AMOUNT1", RecordFieldType.FLOAT);
    parser.addSchemaField("AMOUNT2", RecordFieldType.FLOAT);
    parser.addSchemaField("AMOUNT3", RecordFieldType.FLOAT);
    parser.addRecord("008", 10.05F, 15.45F, 89.99F);
    parser.addRecord("100", 20.25F, 25.25F, 45.25F);
    parser.addRecord("105", 20.05F, 25.05F, 45.05F);
    parser.addRecord("200", 34.05F, 25.05F, 75.05F);
    final MockRecordWriter writer = new MockRecordWriter("\"NAME\",\"POINTS\"");
    final TestRunner runner = TestRunners.newTestRunner(QueryRecord.class);
    runner.addControllerService("parser", parser);
    runner.enableControllerService(parser);
    runner.addControllerService("writer", writer);
    runner.enableControllerService(writer);
    runner.setProperty(REL_NAME, "select ID, AMOUNT1+AMOUNT2+AMOUNT3 as TOTAL from FLOWFILE where ID=100");
    runner.setProperty(QueryRecord.RECORD_READER_FACTORY, "parser");
    runner.setProperty(QueryRecord.RECORD_WRITER_FACTORY, "writer");
    runner.enqueue(new byte[0]);
    runner.run();
    runner.assertTransferCount(REL_NAME, 1);
    final MockFlowFile out = runner.getFlowFilesForRelationship(REL_NAME).get(0);
    out.assertContentEquals("\"NAME\",\"POINTS\"\n\"100\",\"90.75\"\n");
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) TestRunner(org.apache.nifi.util.TestRunner) MockRecordWriter(org.apache.nifi.serialization.record.MockRecordWriter) MockRecordParser(org.apache.nifi.serialization.record.MockRecordParser) Test(org.junit.Test)

Aggregations

MockRecordWriter (org.apache.nifi.serialization.record.MockRecordWriter)21 MockRecordParser (org.apache.nifi.serialization.record.MockRecordParser)16 Test (org.junit.Test)13 TestRunner (org.apache.nifi.util.TestRunner)12 MockFlowFile (org.apache.nifi.util.MockFlowFile)11 Before (org.junit.Before)7 JsonTreeReader (org.apache.nifi.json.JsonTreeReader)2 RecordReaderFactory (org.apache.nifi.serialization.RecordReaderFactory)2 RecordSetWriterFactory (org.apache.nifi.serialization.RecordSetWriterFactory)2 List (java.util.List)1 MockDruidTranquilityController (org.apache.nifi.controller.druid.MockDruidTranquilityController)1 StandardEvent (org.apache.nifi.processor.util.listen.event.StandardEvent)1 InitializationException (org.apache.nifi.reporting.InitializationException)1 CommaSeparatedRecordReader (org.apache.nifi.serialization.record.CommaSeparatedRecordReader)1 RecordFieldType (org.apache.nifi.serialization.record.RecordFieldType)1 TestRunners (org.apache.nifi.util.TestRunners)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertTrue (org.junit.Assert.assertTrue)1