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

The following examples show how to use org.apache.nifi.components.state.StateManager#clear() . 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: 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 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: MonitorActivity.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@OnStopped
public void onStopped(final ProcessContext context) {
    if (getNodeTypeProvider().isPrimary()) {
        final StateManager stateManager = context.getStateManager();
        try {
            stateManager.clear(Scope.CLUSTER);
        } catch (IOException e) {
            getLogger().error("Failed to clear cluster state due to " + e, e);
        }
    }
}
 
Example 4
Source File: StandardComponentStateDAO.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private void clearState(final String componentId) {
    try {
        final StateManager manager = stateManagerProvider.getStateManager(componentId);
        if (manager == null) {
            throw new ResourceNotFoundException(String.format("State for the specified component %s could not be found.", componentId));
        }

        // clear both state's at the same time
        manager.clear(Scope.CLUSTER);
        manager.clear(Scope.LOCAL);
    } catch (final IOException ioe) {
        throw new IllegalStateException(String.format("Unable to clear the state for the specified component %s: %s", componentId, ioe), ioe);
    }
}
 
Example 5
Source File: MonitorActivity.java    From nifi with Apache License 2.0 5 votes vote down vote up
@OnStopped
public void onStopped(final ProcessContext context) {
    if (getNodeTypeProvider().isPrimary()) {
        final StateManager stateManager = context.getStateManager();
        try {
            stateManager.clear(Scope.CLUSTER);
        } catch (IOException e) {
            getLogger().error("Failed to clear cluster state due to " + e, e);
        }
    }
}
 
Example 6
Source File: StandardComponentStateDAO.java    From nifi with Apache License 2.0 5 votes vote down vote up
private void clearState(final String componentId) {
    try {
        final StateManager manager = stateManagerProvider.getStateManager(componentId);
        if (manager == null) {
            throw new ResourceNotFoundException(String.format("State for the specified component %s could not be found.", componentId));
        }

        // clear both state's at the same time
        manager.clear(Scope.CLUSTER);
        manager.clear(Scope.LOCAL);
    } catch (final IOException ioe) {
        throw new IllegalStateException(String.format("Unable to clear the state for the specified component %s: %s", componentId, ioe), ioe);
    }
}
 
Example 7
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 8
Source File: QueryDatabaseTableTest.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(processor.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(processor.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(processor.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(processor.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(processor.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);
}