Java Code Examples for org.apache.flume.Channel#stop()

The following examples show how to use org.apache.flume.Channel#stop() . 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: JDBCSinkTest.java    From ingestion with Apache License 2.0 4 votes vote down vote up
@Test
public void mappedWithH2() throws Exception {
    Class.forName("org.h2.Driver").newInstance();
    Connection conn = DriverManager.getConnection("jdbc:h2:/tmp/jdbcsink_test");
    conn.prepareStatement("DROP TABLE public.test IF EXISTS;").execute();
    conn.prepareStatement("CREATE TABLE public.test (myInteger INTEGER, myString VARCHAR, myId BIGINT AUTO_INCREMENT PRIMARY KEY);").execute();
    conn.commit();
    conn.close();

    Context ctx = new Context();
    ctx.put("driver", "org.h2.Driver");
    ctx.put("connectionString", "jdbc:h2:/tmp/jdbcsink_test");
    ctx.put("table", "test");
    ctx.put("sqlDialect", "H2");
    ctx.put("batchSize", "1");

    JDBCSink jdbcSink = new JDBCSink();

    Configurables.configure(jdbcSink, ctx);

    Context channelContext = new Context();
    channelContext.put("capacity", "10000");
    channelContext.put("transactionCapacity", "200");

    Channel channel = new MemoryChannel();
    channel.setName("junitChannel");
    Configurables.configure(channel, channelContext);

    jdbcSink.setChannel(channel);

    channel.start();
    jdbcSink.start();

    Transaction tx = channel.getTransaction();
    tx.begin();

    Map<String, String> headers = new HashMap<String, String>();
    headers.put("myString", "bar"); // Overwrites the value defined in JSON body
    headers.put("myInteger", "64");
    headers.put("myBoolean", "true");
    headers.put("myDouble", "1.0");
    headers.put("myNull", "foobar");

    Date myDate = new Date();
    headers.put("myDate", Long.toString(myDate.getTime()));

    headers.put("myString2", "baz");

    Event event = EventBuilder.withBody(new byte[0], headers);
    channel.put(event);

    tx.commit();
    tx.close();

    jdbcSink.process();

    jdbcSink.stop();
    channel.stop();

    conn = DriverManager.getConnection("jdbc:h2:/tmp/jdbcsink_test");
    ResultSet resultSet = conn.prepareStatement("SELECT * FROM public.test").executeQuery();
    resultSet.next();
    assertThat(resultSet.getInt("myInteger")).isEqualTo(64);
    assertThat(resultSet.getString("myString")).isEqualTo("bar");
    conn.close();
}
 
Example 2
Source File: JDBCSinkTest.java    From ingestion with Apache License 2.0 4 votes vote down vote up
@Test
public void mappedWithDerby() throws Exception {
    FileUtils.deleteDirectory(new File("test_derby_db"));
    Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
    Connection conn = DriverManager.getConnection("jdbc:derby:test_derby_db;create=true");
    conn.prepareStatement("CREATE TABLE TEST (myInteger INT, myString VARCHAR(255), myId INT NOT NULL GENERATED ALWAYS AS IDENTITY \n" +
            "\t(START WITH 1, INCREMENT BY 1), PRIMARY KEY(myId))").execute();
    conn.commit();
    conn.close();

    Context ctx = new Context();
    ctx.put("driver", "org.apache.derby.jdbc.EmbeddedDriver");
    ctx.put("connectionString", "jdbc:derby:test_derby_db");
    ctx.put("table", "test");
    ctx.put("sqlDialect", "DERBY");
    ctx.put("batchSize", "1");

    JDBCSink jdbcSink = new JDBCSink();

    Configurables.configure(jdbcSink, ctx);

    Context channelContext = new Context();
    channelContext.put("capacity", "10000");
    channelContext.put("transactionCapacity", "200");

    Channel channel = new MemoryChannel();
    channel.setName("junitChannel");
    Configurables.configure(channel, channelContext);

    jdbcSink.setChannel(channel);

    channel.start();
    jdbcSink.start();

    Transaction tx = channel.getTransaction();
    tx.begin();

    Map<String, String> headers = new HashMap<String, String>();
    headers.put("myString", "bar"); // Overwrites the value defined in JSON body
    headers.put("myInteger", "64");
    headers.put("myBoolean", "true");
    headers.put("myDouble", "1.0");
    headers.put("myNull", "foobar");

    Date myDate = new Date();
    headers.put("myDate", Long.toString(myDate.getTime()));

    headers.put("myString2", "baz");

    Event event = EventBuilder.withBody(new byte[0], headers);
    channel.put(event);

    tx.commit();
    tx.close();

    jdbcSink.process();

    jdbcSink.stop();
    channel.stop();

    conn = DriverManager.getConnection("jdbc:derby:test_derby_db");
    ResultSet resultSet = conn.prepareStatement("SELECT * FROM TEST").executeQuery();
    resultSet.next();
    assertThat(resultSet.getInt("myInteger")).isEqualTo(64);
    assertThat(resultSet.getString("myString")).isEqualTo("bar");
    conn.close();

    try {
        DriverManager.getConnection("jdbc:derby:;shutdown=true");
    } catch (SQLException ex) {

    }
}
 
Example 3
Source File: JDBCSinkTest.java    From ingestion with Apache License 2.0 4 votes vote down vote up
@Test
    public void templateWithH2() throws Exception {
        Class.forName("org.h2.Driver").newInstance();
        Connection conn = DriverManager.getConnection("jdbc:h2:/tmp/jdbcsink_test");
        conn.prepareStatement("DROP TABLE public.test IF EXISTS;").execute();
        conn.prepareStatement("CREATE TABLE public.test (myInteger INTEGER, myString VARCHAR, myId BIGINT AUTO_INCREMENT PRIMARY KEY);").execute();
        conn.commit();
        conn.close();

        Context ctx = new Context();
        ctx.put("driver", "org.h2.Driver");
        ctx.put("connectionString", "jdbc:h2:/tmp/jdbcsink_test");
        ctx.put("sqlDialect", "H2");
        ctx.put("batchSize", "1");
        ctx.put("sql", "INSERT INTO \"PUBLIC\".\"TEST\" (\"MYINTEGER\", \"MYSTRING\") VALUES (cast(${header.myInteger:int} as integer), cast(${header.myString:varchar} as varchar))");

        JDBCSink jdbcSink = new JDBCSink();

        Configurables.configure(jdbcSink, ctx);

        Context channelContext = new Context();
        channelContext.put("capacity", "10000");
        channelContext.put("transactionCapacity", "200");

        Channel channel = new MemoryChannel();
        channel.setName("junitChannel");
        Configurables.configure(channel, channelContext);

        jdbcSink.setChannel(channel);

        channel.start();
        jdbcSink.start();

        Transaction tx = channel.getTransaction();
        tx.begin();

        Map<String, String> headers = new HashMap<String, String>();
        headers.put("myString", "bar"); // Overwrites the value defined in JSON body
        headers.put("myInteger", "64");
        headers.put("myBoolean", "true");
        headers.put("myDouble", "1.0");
        headers.put("myNull", "foobar");

        Date myDate = new Date();
        headers.put("myDate", Long.toString(myDate.getTime()));

        headers.put("myString2", "baz");

        Event event = EventBuilder.withBody(new byte[0], headers);
        channel.put(event);

        tx.commit();
        tx.close();

        jdbcSink.process();

        jdbcSink.stop();
        channel.stop();

        conn = DriverManager.getConnection("jdbc:h2:/tmp/jdbcsink_test");

        ResultSet rs = conn.prepareStatement("SELECT count(*) AS count FROM public.test").executeQuery();
        rs.next();
        assertThat(rs.getInt("count")).isEqualTo(1);

        rs = conn.prepareStatement("SELECT * FROM public.test").executeQuery();
        rs.next();
//        for (int i = 1; i <= 3; i++) {
//            System.out.println(rs.getString(i));
//        }
        assertThat(rs.getInt("myInteger")).isEqualTo(64);
        assertThat(rs.getString("myString")).isEqualTo("bar");
        conn.close();

    }
 
Example 4
Source File: JDBCSinkTest.java    From ingestion with Apache License 2.0 4 votes vote down vote up
/**
 * Sample code to validate postgresql integration with JDBC sink
 * Ignored until we get a Docker Postgresql and move to integration test
 * @throws Exception
 */
@Test
@Ignore
public void templateWithPostgres() throws Exception {

    Class.forName("org.postgresql.Driver").newInstance();


    Connection connTruncate = DriverManager.getConnection("jdbc:postgresql://10.200.0.126:5432/test_erige?user=postgres&password=");
    connTruncate.prepareStatement("TRUNCATE tc40;").execute();
    //conn.prepareStatement("CREATE TABLE tc40 (myInteger INTEGER, myString VARCHAR, myId BIGINT AUTO_INCREMENT "
    //        + "PRIMARY KEY);").execute();
    //connTruncate.commit();
    connTruncate.close();


    Context ctx = new Context();
    ctx.put("driver", "org.postgresql.Driver");
    ctx.put("connectionString", "jdbc:postgresql://10.200.0.126:5432/test_erige?user=postgres&password=");
    ctx.put("sqlDialect", "POSTGRES");
    ctx.put("table", "tc40");
    ctx.put("username", "postgres");
    ctx.put("batchSize", "1");
    ctx.put("sql", "INSERT INTO \"tc40\" (\"arn\", \"account_number\") VALUES "
            + "(${header.arn:varchar}, ${header.account_number:varchar})");

    JDBCSink jdbcSink = new JDBCSink();

    Configurables.configure(jdbcSink, ctx);

    Context channelContext = new Context();
    channelContext.put("capacity", "10000");
    channelContext.put("transactionCapacity", "200");

    Channel channel = new MemoryChannel();
    channel.setName("junitChannel");
    Configurables.configure(channel, channelContext);

    jdbcSink.setChannel(channel);

    channel.start();
    jdbcSink.start();

    Transaction tx = channel.getTransaction();
    tx.begin();

    Map<String, String> headers = new HashMap<String, String>();
    headers.put("arn", "bar"); // Overwrites the value defined in JSON body
    headers.put("account_number", "account number");
    headers.put("dsadas", "dsadasdas");

    Event event = EventBuilder.withBody(new byte[0], headers);
    channel.put(event);

    tx.commit();
    tx.close();

    jdbcSink.process();

    jdbcSink.stop();
    channel.stop();

    //Connection conn = DriverManager.getConnection("jdbc:h2:/tmp/jdbcsink_test");
    Connection conn = DriverManager.getConnection("jdbc:postgresql://10.200.0"
            + ".126:5432/test_erige?user=postgres&password=");

    ResultSet rs = conn.prepareStatement("SELECT count(*) AS count FROM tc40").executeQuery();
    rs.next();
    assertThat(rs.getInt("count")).isEqualTo(1);

    rs = conn.prepareStatement("SELECT * FROM tc40").executeQuery();
    rs.next();
    //        for (int i = 1; i <= 3; i++) {
    //            System.out.println(rs.getString(i));
    //        }
    assertThat(rs.getString("arn")).isEqualTo("bar");
    conn.close();

}