Java Code Examples for org.apache.nifi.components.state.StateManager#setState()

The following examples show how to use org.apache.nifi.components.state.StateManager#setState() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QueryDatabaseTableRecordTest.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetQueryUsingPhoenixAdapter() throws Exception {
    Map<String, String> maxValues = new HashMap<>();
    StateManager stateManager = runner.getStateManager();
    processor.putColumnType("mytable" + AbstractDatabaseFetchProcessor.NAMESPACE_DELIMITER + "id", Types.INTEGER);
    processor.putColumnType("mytable" + AbstractDatabaseFetchProcessor.NAMESPACE_DELIMITER + "time_created", Types.TIME);
    processor.putColumnType("mytable" + AbstractDatabaseFetchProcessor.NAMESPACE_DELIMITER + "date_created", Types.TIMESTAMP);

    maxValues.put("id", "509");
    maxValues.put("time_created", "12:34:57");
    maxValues.put("date_created", "2016-03-07 12:34:56");
    stateManager.setState(maxValues, Scope.CLUSTER);

    dbAdapter = new PhoenixDatabaseAdapter();
    String query = processor.getQuery(dbAdapter, "myTable", null, Arrays.asList("id", "DATE_CREATED", "TIME_CREATED"), "type = \"CUSTOMER\"", stateManager.getState(Scope.CLUSTER).toMap());
    assertEquals("SELECT * FROM myTable WHERE id > 509 AND DATE_CREATED >= timestamp '2016-03-07 12:34:56' AND TIME_CREATED >= time '12:34:57' AND (type = \"CUSTOMER\")", query);
    // Cover the other path
    dbAdapter = new GenericDatabaseAdapter();
    query = processor.getQuery(dbAdapter, "myTable", null, Arrays.asList("id", "DATE_CREATED", "TIME_CREATED"), "type = \"CUSTOMER\"", stateManager.getState(Scope.CLUSTER).toMap());
    assertEquals("SELECT * FROM myTable WHERE id > 509 AND DATE_CREATED >= '2016-03-07 12:34:56' AND TIME_CREATED >= '12:34:57' AND (type = \"CUSTOMER\")", query);
}
 
Example 2
Source File: TestState.java    From nifi-scripting-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Demonstrates reading and writing processor state values
 * @throws Exception
 */
@Test
public void testStatePython() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new ExecuteScript());
    runner.setValidateExpressionUsage(false);
    runner.setProperty(SCRIPT_ENGINE, "python");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_FILE, "src/test/resources/executescript/state/state.py");
    runner.setProperty(ScriptingComponentUtils.MODULES, "src/test/resources/executescript");
    runner.assertValid();

    StateManager stateManager = runner.getStateManager();
    stateManager.clear(Scope.CLUSTER);
    Map<String, String> initialStateValues = new HashMap<>();
    initialStateValues.put("some-state", "foo");
    stateManager.setState(initialStateValues, Scope.CLUSTER);

    runner.enqueue("sample text".getBytes(StandardCharsets.UTF_8));
    runner.run();

    runner.assertAllFlowFilesTransferred("success", 1);
    StateMap resultStateValues = stateManager.getState(Scope.CLUSTER);
    Assert.assertEquals("foobar", resultStateValues.get("some-state"));
}
 
Example 3
Source File: TestState.java    From nifi-scripting-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Demonstrates reading and writing processor state values
 * @throws Exception
 */
@Test
public void testStateJavascript() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new ExecuteScript());
    runner.setValidateExpressionUsage(false);
    runner.setProperty(SCRIPT_ENGINE, "ECMAScript");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_FILE, "src/test/resources/executescript/state/state.js");
    runner.setProperty(ScriptingComponentUtils.MODULES, "src/test/resources/executescript");
    runner.assertValid();

    StateManager stateManager = runner.getStateManager();
    stateManager.clear(Scope.CLUSTER);
    Map<String, String> initialStateValues = new HashMap<>();
    initialStateValues.put("some-state", "foo");
    stateManager.setState(initialStateValues, Scope.CLUSTER);

    runner.enqueue("sample text".getBytes(StandardCharsets.UTF_8));
    runner.run();

    runner.assertAllFlowFilesTransferred("success", 1);
    StateMap resultStateValues = stateManager.getState(Scope.CLUSTER);
    Assert.assertEquals("foobar", resultStateValues.get("some-state"));
}
 
Example 4
Source File: ScrollElasticsearchHttp.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private void saveScrollId(StateManager stateManager, String scrollId) throws IOException {

        Map<String, String> state = new HashMap<>(2);
        state.put(SCROLL_ID_STATE, scrollId);

        getLogger().debug("Saving state with scrollId of {}", new Object[] { scrollId });
        stateManager.setState(state, Scope.LOCAL);
    }
 
Example 5
Source File: ScrollElasticsearchHttp.java    From nifi with Apache License 2.0 5 votes vote down vote up
private void finishQuery(StateManager stateManager) throws IOException {

        Map<String, String> state = new HashMap<>(2);
        state.put(FINISHED_QUERY_STATE, "true");

        getLogger().debug("Saving state with finishedQuery = true");
        stateManager.setState(state, Scope.LOCAL);
    }
 
Example 6
Source File: GetSplunk.java    From nifi with Apache License 2.0 5 votes vote down vote up
private void saveState(StateManager stateManager, TimeRange timeRange) throws IOException {
    final String earliest = StringUtils.isBlank(timeRange.getEarliestTime()) ? "" : timeRange.getEarliestTime();
    final String latest = StringUtils.isBlank(timeRange.getLatestTime()) ? "" : timeRange.getLatestTime();

    Map<String,String> state = new HashMap<>(2);
    state.put(EARLIEST_TIME_KEY, earliest);
    state.put(LATEST_TIME_KEY, latest);

    getLogger().debug("Saving state with earliestTime of {} and latestTime of {}", new Object[] {earliest, latest});
    stateManager.setState(state, Scope.CLUSTER);
}
 
Example 7
Source File: CaptureChangeMySQL.java    From nifi with Apache License 2.0 5 votes vote down vote up
private void updateState(StateManager stateManager, String binlogFile, long binlogPosition, long sequenceId) throws IOException {
    // Update state with latest values
    if (stateManager != null) {
        Map<String, String> newStateMap = new HashMap<>(stateManager.getState(Scope.CLUSTER).toMap());

        // Save current binlog filename and position to the state map
        if (binlogFile != null) {
            newStateMap.put(BinlogEventInfo.BINLOG_FILENAME_KEY, binlogFile);
        }
        newStateMap.put(BinlogEventInfo.BINLOG_POSITION_KEY, Long.toString(binlogPosition));
        newStateMap.put(EventWriter.SEQUENCE_ID_KEY, String.valueOf(sequenceId));
        stateManager.setState(newStateMap, Scope.CLUSTER);
    }
}
 
Example 8
Source File: TestGenerateTableFetch.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testBackwardsCompatibilityStateKeyStaticTableDynamicMaxValues() throws Exception {
    // load test data to database
    final Connection con = ((DBCPService) runner.getControllerService("dbcp")).getConnection();
    Statement stmt = con.createStatement();

    try {
        stmt.execute("drop table TEST_QUERY_DB_TABLE");
    } catch (final SQLException sqle) {
        // Ignore this error, probably a "table does not exist" since Derby doesn't yet support DROP IF EXISTS [DERBY-4842]
    }

    stmt.execute("create table TEST_QUERY_DB_TABLE (id integer not null, bucket integer not null)");
    stmt.execute("insert into TEST_QUERY_DB_TABLE (id, bucket) VALUES (0, 0)");
    stmt.execute("insert into TEST_QUERY_DB_TABLE (id, bucket) VALUES (1, 0)");

    runner.setProperty(GenerateTableFetch.TABLE_NAME, "TEST_QUERY_DB_TABLE");
    runner.setIncomingConnection(true);
    runner.setProperty(GenerateTableFetch.MAX_VALUE_COLUMN_NAMES, "${maxValueCol}");
    runner.enqueue("".getBytes(), new HashMap<String, String>() {{
        put("maxValueCol", "id");
    }});

    // Pre-populate the state with a key for column name (not fully-qualified)
    StateManager stateManager = runner.getStateManager();
    stateManager.setState(new HashMap<String, String>() {{
        put("id", "0");
    }}, Scope.CLUSTER);

    // Pre-populate the column type map with an entry for id (not fully-qualified)
    processor.columnTypeMap.put("id", 4);

    runner.run();

    runner.assertAllFlowFilesTransferred(REL_SUCCESS, 1);
    MockFlowFile flowFile = runner.getFlowFilesForRelationship(REL_SUCCESS).get(0);
    assertEquals("SELECT * FROM TEST_QUERY_DB_TABLE WHERE id > 0 AND id <= 1 ORDER BY id FETCH NEXT 10000 ROWS ONLY", new String(flowFile.toByteArray()));
}
 
Example 9
Source File: GetHDFSEvents.java    From nifi with Apache License 2.0 5 votes vote down vote up
private void updateClusterStateForTxId(StateManager stateManager) {
    try {
        Map<String, String> newState = new HashMap<>(stateManager.getState(Scope.CLUSTER).toMap());
        newState.put(LAST_TX_ID, String.valueOf(lastTxId));
        stateManager.setState(newState, Scope.CLUSTER);
    } catch (IOException e) {
        getLogger().warn("Failed to update cluster state for last txId. It is possible data replication may occur.", e);
    }
}
 
Example 10
Source File: ScrollElasticsearchHttp.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private void finishQuery(StateManager stateManager) throws IOException {

        Map<String, String> state = new HashMap<>(2);
        state.put(FINISHED_QUERY_STATE, "true");

        getLogger().debug("Saving state with finishedQuery = true");
        stateManager.setState(state, Scope.LOCAL);
    }
 
Example 11
Source File: GetHDFSEvents.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private void updateClusterStateForTxId(StateManager stateManager) {
    try {
        Map<String, String> newState = new HashMap<>(stateManager.getState(Scope.CLUSTER).toMap());
        newState.put(LAST_TX_ID, String.valueOf(lastTxId));
        stateManager.setState(newState, Scope.CLUSTER);
    } catch (IOException e) {
        getLogger().warn("Failed to update cluster state for last txId. It is possible data replication may occur.", e);
    }
}
 
Example 12
Source File: TestGenerateTableFetch.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testBackwardsCompatibilityStateKeyStaticTableDynamicMaxValues() throws Exception {
    // load test data to database
    final Connection con = ((DBCPService) runner.getControllerService("dbcp")).getConnection();
    Statement stmt = con.createStatement();

    try {
        stmt.execute("drop table TEST_QUERY_DB_TABLE");
    } catch (final SQLException sqle) {
        // Ignore this error, probably a "table does not exist" since Derby doesn't yet support DROP IF EXISTS [DERBY-4842]
    }

    stmt.execute("create table TEST_QUERY_DB_TABLE (id integer not null, bucket integer not null)");
    stmt.execute("insert into TEST_QUERY_DB_TABLE (id, bucket) VALUES (0, 0)");
    stmt.execute("insert into TEST_QUERY_DB_TABLE (id, bucket) VALUES (1, 0)");

    runner.setProperty(GenerateTableFetch.TABLE_NAME, "TEST_QUERY_DB_TABLE");
    runner.setIncomingConnection(true);
    runner.setProperty(GenerateTableFetch.MAX_VALUE_COLUMN_NAMES, "${maxValueCol}");
    runner.enqueue("".getBytes(), new HashMap<String, String>() {{
        put("maxValueCol", "id");
    }});

    // Pre-populate the state with a key for column name (not fully-qualified)
    StateManager stateManager = runner.getStateManager();
    stateManager.setState(new HashMap<String, String>() {{
        put("id", "0");
    }}, Scope.CLUSTER);

    // Pre-populate the column type map with an entry for id (not fully-qualified)
    processor.columnTypeMap.put("id", 4);

    runner.run();

    runner.assertAllFlowFilesTransferred(REL_SUCCESS, 1);
    MockFlowFile flowFile = runner.getFlowFilesForRelationship(REL_SUCCESS).get(0);
    assertEquals("SELECT * FROM TEST_QUERY_DB_TABLE WHERE id > 0 ORDER BY id FETCH NEXT 10000 ROWS ONLY", new String(flowFile.toByteArray()));
}
 
Example 13
Source File: QueryDatabaseTableTest.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetQuery() throws Exception {
    String query = processor.getQuery(dbAdapter, "myTable", null, null, null);
    assertEquals("SELECT * FROM myTable", query);
    query = processor.getQuery(dbAdapter, "myTable", "col1,col2", null, null);
    assertEquals("SELECT col1,col2 FROM myTable", query);

    query = processor.getQuery(dbAdapter, "myTable", null, Collections.singletonList("id"), null);
    assertEquals("SELECT * FROM myTable", query);

    Map<String, String> maxValues = new HashMap<>();
    maxValues.put("id", "509");
    StateManager stateManager = runner.getStateManager();
    stateManager.setState(maxValues, Scope.CLUSTER);
    processor.putColumnType("mytable" + AbstractDatabaseFetchProcessor.NAMESPACE_DELIMITER + "id", Types.INTEGER);
    query = processor.getQuery(dbAdapter, "myTable", null, Collections.singletonList("id"), stateManager.getState(Scope.CLUSTER).toMap());
    assertEquals("SELECT * FROM myTable WHERE id > 509", query);

    maxValues.put("date_created", "2016-03-07 12:34:56");
    stateManager.setState(maxValues, Scope.CLUSTER);
    processor.putColumnType("mytable" + AbstractDatabaseFetchProcessor.NAMESPACE_DELIMITER + "date_created", Types.TIMESTAMP);
    query = processor.getQuery(dbAdapter, "myTable", null, Arrays.asList("id", "DATE_CREATED"), stateManager.getState(Scope.CLUSTER).toMap());
    assertEquals("SELECT * FROM myTable WHERE id > 509 AND DATE_CREATED >= '2016-03-07 12:34:56'", query);

    // Test Oracle strategy
    dbAdapter = new OracleDatabaseAdapter();
    query = processor.getQuery(dbAdapter, "myTable", null, Arrays.asList("id", "DATE_CREATED"), stateManager.getState(Scope.CLUSTER).toMap());
    assertEquals("SELECT * FROM myTable WHERE id > 509 AND DATE_CREATED >= to_date('2016-03-07 12:34:56', 'yyyy-mm-dd HH24:MI:SS')", query);
}
 
Example 14
Source File: GetHBase.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
private void storeState(final ScanResult scanResult, final StateManager stateManager) throws IOException {
    stateManager.setState(scanResult.toFlatMap(), Scope.CLUSTER);
}
 
Example 15
Source File: TestGenerateTableFetch.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testBackwardsCompatibilityStateKeyDynamicTableStaticMaxValues() throws Exception {
    // load test data to database
    final Connection con = ((DBCPService) runner.getControllerService("dbcp")).getConnection();
    Statement stmt = con.createStatement();

    try {
        stmt.execute("drop table TEST_QUERY_DB_TABLE");
    } catch (final SQLException sqle) {
        // Ignore this error, probably a "table does not exist" since Derby doesn't yet support DROP IF EXISTS [DERBY-4842]
    }

    stmt.execute("create table TEST_QUERY_DB_TABLE (id integer not null, bucket integer not null)");
    stmt.execute("insert into TEST_QUERY_DB_TABLE (id, bucket) VALUES (0, 0)");
    stmt.execute("insert into TEST_QUERY_DB_TABLE (id, bucket) VALUES (1, 0)");

    runner.setProperty(GenerateTableFetch.TABLE_NAME, "${tableName}");
    runner.setIncomingConnection(true);
    runner.setProperty(GenerateTableFetch.MAX_VALUE_COLUMN_NAMES, "id");
    runner.enqueue("".getBytes(), new HashMap<String, String>() {{
        put("tableName", "TEST_QUERY_DB_TABLE");
    }});

    // Pre-populate the state with a key for column name (not fully-qualified)
    StateManager stateManager = runner.getStateManager();
    stateManager.setState(new HashMap<String, String>() {{
        put("id", "0");
    }}, Scope.CLUSTER);

    // Pre-populate the column type map with an entry for id (not fully-qualified)
    processor.columnTypeMap.put("id", 4);

    runner.run();

    runner.assertAllFlowFilesTransferred(REL_SUCCESS, 1);
    MockFlowFile flowFile = runner.getFlowFilesForRelationship(REL_SUCCESS).get(0);
    // Note there is no WHERE clause here. Because we are using dynamic tables, the old state key/value is not retrieved
    assertEquals("SELECT * FROM TEST_QUERY_DB_TABLE WHERE id <= 1 ORDER BY id FETCH NEXT 10000 ROWS ONLY", new String(flowFile.toByteArray()));

    runner.clearTransferState();
    stmt.execute("insert into TEST_QUERY_DB_TABLE (id, bucket) VALUES (2, 0)");

    runner.enqueue("".getBytes(), new HashMap<String, String>() {{
        put("tableName", "TEST_QUERY_DB_TABLE");
        put("maxValueCol", "id");
    }});
    runner.run();

    runner.assertAllFlowFilesTransferred(REL_SUCCESS, 1);
    flowFile = runner.getFlowFilesForRelationship(REL_SUCCESS).get(0);
    assertEquals("SELECT * FROM TEST_QUERY_DB_TABLE WHERE id > 1 AND id <= 2 ORDER BY id FETCH NEXT 10000 ROWS ONLY", new String(flowFile.toByteArray()));
}
 
Example 16
Source File: TestGenerateTableFetch.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testBackwardsCompatibilityStateKeyDynamicTableDynamicMaxValues() throws Exception {
    // load test data to database
    final Connection con = ((DBCPService) runner.getControllerService("dbcp")).getConnection();
    Statement stmt = con.createStatement();

    try {
        stmt.execute("drop table TEST_QUERY_DB_TABLE");
    } catch (final SQLException sqle) {
        // Ignore this error, probably a "table does not exist" since Derby doesn't yet support DROP IF EXISTS [DERBY-4842]
    }

    stmt.execute("create table TEST_QUERY_DB_TABLE (id integer not null, bucket integer not null)");
    stmt.execute("insert into TEST_QUERY_DB_TABLE (id, bucket) VALUES (0, 0)");
    stmt.execute("insert into TEST_QUERY_DB_TABLE (id, bucket) VALUES (1, 0)");

    runner.setProperty(GenerateTableFetch.TABLE_NAME, "${tableName}");
    runner.setIncomingConnection(true);
    runner.setProperty(GenerateTableFetch.MAX_VALUE_COLUMN_NAMES, "${maxValueCol}");
    runner.enqueue("".getBytes(), new HashMap<String, String>() {{
        put("tableName", "TEST_QUERY_DB_TABLE");
        put("maxValueCol", "id");
    }});

    // Pre-populate the state with a key for column name (not fully-qualified)
    StateManager stateManager = runner.getStateManager();
    stateManager.setState(new HashMap<String, String>() {{
        put("id", "0");
    }}, Scope.CLUSTER);

    // Pre-populate the column type map with an entry for id (not fully-qualified)
    processor.columnTypeMap.put("id", 4);

    runner.run();

    runner.assertAllFlowFilesTransferred(REL_SUCCESS, 1);
    MockFlowFile flowFile = runner.getFlowFilesForRelationship(REL_SUCCESS).get(0);
    // Note there is no WHERE clause here. Because we are using dynamic tables, the old state key/value is not retrieved
    assertEquals("SELECT * FROM TEST_QUERY_DB_TABLE WHERE id <= 1 ORDER BY id FETCH NEXT 10000 ROWS ONLY", new String(flowFile.toByteArray()));
    assertEquals("TEST_QUERY_DB_TABLE", flowFile.getAttribute("generatetablefetch.tableName"));
    assertEquals(null, flowFile.getAttribute("generatetablefetch.columnNames"));
    assertEquals("id <= 1", flowFile.getAttribute("generatetablefetch.whereClause"));
    assertEquals("id", flowFile.getAttribute("generatetablefetch.maxColumnNames"));
    assertEquals("10000", flowFile.getAttribute("generatetablefetch.limit"));
    assertEquals("0", flowFile.getAttribute("generatetablefetch.offset"));

    runner.clearTransferState();
    stmt.execute("insert into TEST_QUERY_DB_TABLE (id, bucket) VALUES (2, 0)");

    runner.enqueue("".getBytes(), new HashMap<String, String>() {{
        put("tableName", "TEST_QUERY_DB_TABLE");
        put("maxValueCol", "id");
    }});
    runner.run();

    runner.assertAllFlowFilesTransferred(REL_SUCCESS, 1);
    flowFile = runner.getFlowFilesForRelationship(REL_SUCCESS).get(0);
    assertEquals("SELECT * FROM TEST_QUERY_DB_TABLE WHERE id > 1 AND id <= 2 ORDER BY id FETCH NEXT 10000 ROWS ONLY", new String(flowFile.toByteArray()));
    assertEquals("TEST_QUERY_DB_TABLE", flowFile.getAttribute("generatetablefetch.tableName"));
    assertEquals(null, flowFile.getAttribute("generatetablefetch.columnNames"));
    assertEquals("id > 1 AND id <= 2", flowFile.getAttribute("generatetablefetch.whereClause"));
    assertEquals("id", flowFile.getAttribute("generatetablefetch.maxColumnNames"));
    assertEquals("10000", flowFile.getAttribute("generatetablefetch.limit"));
    assertEquals("0", flowFile.getAttribute("generatetablefetch.offset"));
}
 
Example 17
Source File: TestGenerateTableFetch.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testBackwardsCompatibilityStateKeyDynamicTableDynamicMaxValues() throws Exception {
    // load test data to database
    final Connection con = ((DBCPService) runner.getControllerService("dbcp")).getConnection();
    Statement stmt = con.createStatement();

    try {
        stmt.execute("drop table TEST_QUERY_DB_TABLE");
    } catch (final SQLException sqle) {
        // Ignore this error, probably a "table does not exist" since Derby doesn't yet support DROP IF EXISTS [DERBY-4842]
    }

    stmt.execute("create table TEST_QUERY_DB_TABLE (id integer not null, bucket integer not null)");
    stmt.execute("insert into TEST_QUERY_DB_TABLE (id, bucket) VALUES (0, 0)");
    stmt.execute("insert into TEST_QUERY_DB_TABLE (id, bucket) VALUES (1, 0)");

    runner.setProperty(GenerateTableFetch.TABLE_NAME, "${tableName}");
    runner.setIncomingConnection(true);
    runner.setProperty(GenerateTableFetch.MAX_VALUE_COLUMN_NAMES, "${maxValueCol}");
    runner.enqueue("".getBytes(), new HashMap<String, String>() {{
        put("tableName", "TEST_QUERY_DB_TABLE");
        put("maxValueCol", "id");
    }});

    // Pre-populate the state with a key for column name (not fully-qualified)
    StateManager stateManager = runner.getStateManager();
    stateManager.setState(new HashMap<String, String>() {{
        put("id", "0");
    }}, Scope.CLUSTER);

    // Pre-populate the column type map with an entry for id (not fully-qualified)
    processor.columnTypeMap.put("id", 4);

    runner.run();

    runner.assertAllFlowFilesTransferred(REL_SUCCESS, 1);
    MockFlowFile flowFile = runner.getFlowFilesForRelationship(REL_SUCCESS).get(0);
    // Note there is no WHERE clause here. Because we are using dynamic tables, the old state key/value is not retrieved
    assertEquals("SELECT * FROM TEST_QUERY_DB_TABLE ORDER BY id FETCH NEXT 10000 ROWS ONLY", new String(flowFile.toByteArray()));

    runner.clearTransferState();
    stmt.execute("insert into TEST_QUERY_DB_TABLE (id, bucket) VALUES (2, 0)");

    runner.enqueue("".getBytes(), new HashMap<String, String>() {{
        put("tableName", "TEST_QUERY_DB_TABLE");
        put("maxValueCol", "id");
    }});
    runner.run();

    runner.assertAllFlowFilesTransferred(REL_SUCCESS, 1);
    flowFile = runner.getFlowFilesForRelationship(REL_SUCCESS).get(0);
    assertEquals("SELECT * FROM TEST_QUERY_DB_TABLE WHERE id > 1 ORDER BY id FETCH NEXT 10000 ROWS ONLY", new String(flowFile.toByteArray()));
}
 
Example 18
Source File: QueryDatabaseTableRecordTest.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetQuery() throws Exception {
    String query = processor.getQuery(dbAdapter, "myTable", null, null, null, null);
    assertEquals("SELECT * FROM myTable", query);
    query = processor.getQuery(dbAdapter, "myTable", "col1,col2", null, null, null);
    assertEquals("SELECT col1,col2 FROM myTable", query);

    query = processor.getQuery(dbAdapter, "myTable", null, Collections.singletonList("id"), null, null);
    assertEquals("SELECT * FROM myTable", query);

    Map<String, String> maxValues = new HashMap<>();
    maxValues.put("id", "509");
    StateManager stateManager = runner.getStateManager();
    stateManager.setState(maxValues, Scope.CLUSTER);
    processor.putColumnType(AbstractDatabaseFetchProcessor.getStateKey("mytable", "id", dbAdapter), Types.INTEGER);
    query = processor.getQuery(dbAdapter, "myTable", null, Collections.singletonList("id"), null, stateManager.getState(Scope.CLUSTER).toMap());
    assertEquals("SELECT * FROM myTable WHERE id > 509", query);

    maxValues.put("date_created", "2016-03-07 12:34:56");
    stateManager.setState(maxValues, Scope.CLUSTER);
    processor.putColumnType(AbstractDatabaseFetchProcessor.getStateKey("mytable", "date_created", dbAdapter), Types.TIMESTAMP);
    query = processor.getQuery(dbAdapter, "myTable", null, Arrays.asList("id", "DATE_CREATED"), null, stateManager.getState(Scope.CLUSTER).toMap());
    assertEquals("SELECT * FROM myTable WHERE id > 509 AND DATE_CREATED >= '2016-03-07 12:34:56'", query);

    // Double quotes can be used to escape column and table names with most ANSI compatible database engines.
    maxValues.put("mytable@!@date-created", "2016-03-07 12:34:56");
    stateManager.setState(maxValues, Scope.CLUSTER);
    processor.putColumnType(AbstractDatabaseFetchProcessor.getStateKey("\"myTable\"", "\"DATE-CREATED\"", dbAdapter), Types.TIMESTAMP);
    query = processor.getQuery(dbAdapter, "\"myTable\"", null, Arrays.asList("id", "\"DATE-CREATED\""), null, stateManager.getState(Scope.CLUSTER).toMap());
    assertEquals("SELECT * FROM \"myTable\" WHERE id > 509 AND \"DATE-CREATED\" >= '2016-03-07 12:34:56'", query);

    // Back-ticks can be used to escape MySQL column and table names.
    dbAdapter = new MySQLDatabaseAdapter();
    processor.putColumnType(AbstractDatabaseFetchProcessor.getStateKey("`myTable`", "`DATE-CREATED`", dbAdapter), Types.TIMESTAMP);
    query = processor.getQuery(dbAdapter, "`myTable`", null, Arrays.asList("id", "`DATE-CREATED`"), null, stateManager.getState(Scope.CLUSTER).toMap());
    assertEquals("SELECT * FROM `myTable` WHERE id > 509 AND `DATE-CREATED` >= '2016-03-07 12:34:56'", query);

    // Square brackets can be used to escape Microsoft SQL Server column and table names.
    dbAdapter = new MSSQLDatabaseAdapter();
    processor.putColumnType(AbstractDatabaseFetchProcessor.getStateKey("[myTable]", "[DATE-CREATED]", dbAdapter), Types.TIMESTAMP);
    query = processor.getQuery(dbAdapter, "[myTable]", null, Arrays.asList("id", "[DATE-CREATED]"), null, stateManager.getState(Scope.CLUSTER).toMap());
    assertEquals("SELECT * FROM [myTable] WHERE id > 509 AND [DATE-CREATED] >= '2016-03-07 12:34:56'", query);

    // Test Oracle strategy
    dbAdapter = new OracleDatabaseAdapter();
    query = processor.getQuery(dbAdapter, "myTable", null, Arrays.asList("id", "DATE_CREATED"), "type = \"CUSTOMER\"", stateManager.getState(Scope.CLUSTER).toMap());
    assertEquals("SELECT * FROM myTable WHERE id > 509 AND DATE_CREATED >= timestamp '2016-03-07 12:34:56' AND (type = \"CUSTOMER\")", query);

    // Test time.
    processor.putColumnType("mytable" + AbstractDatabaseFetchProcessor.NAMESPACE_DELIMITER + "time_created", Types.TIME);
    maxValues.clear();
    maxValues.put("id", "509");
    maxValues.put("time_created", "12:34:57");
    maxValues.put("date_created", "2016-03-07 12:34:56");
    stateManager = runner.getStateManager();
    stateManager.clear(Scope.CLUSTER);
    stateManager.setState(maxValues, Scope.CLUSTER);
    query = processor.getQuery(dbAdapter, "myTable", null, Arrays.asList("id", "DATE_CREATED", "TIME_CREATED"), "type = \"CUSTOMER\"", stateManager.getState(Scope.CLUSTER).toMap());
    assertEquals("SELECT * FROM myTable WHERE id > 509 AND DATE_CREATED >= timestamp '2016-03-07 12:34:56' AND TIME_CREATED >= timestamp '12:34:57' AND (type = \"CUSTOMER\")", query);
    dbAdapter = new GenericDatabaseAdapter();
    query = processor.getQuery(dbAdapter, "myTable", null, Arrays.asList("id", "DATE_CREATED", "TIME_CREATED"), "type = \"CUSTOMER\"", stateManager.getState(Scope.CLUSTER).toMap());
    assertEquals("SELECT * FROM myTable WHERE id > 509 AND DATE_CREATED >= '2016-03-07 12:34:56' AND TIME_CREATED >= '12:34:57' AND (type = \"CUSTOMER\")", query);
}
 
Example 19
Source File: GetHBase.java    From nifi with Apache License 2.0 4 votes vote down vote up
private void storeState(final ScanResult scanResult, final StateManager stateManager) throws IOException {
    stateManager.setState(scanResult.toFlatMap(), Scope.CLUSTER);
}
 
Example 20
Source File: AbstractListProcessor.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
private void persist(final long listingTimestamp, final long processedTimestamp, final StateManager stateManager, final Scope scope) throws IOException {
    final Map<String, String> updatedState = new HashMap<>(1);
    updatedState.put(LISTING_TIMESTAMP_KEY, String.valueOf(listingTimestamp));
    updatedState.put(PROCESSED_TIMESTAMP_KEY, String.valueOf(processedTimestamp));
    stateManager.setState(updatedState, scope);
}