Java Code Examples for org.apache.kudu.client.KuduScanner#forEach()

The following examples show how to use org.apache.kudu.client.KuduScanner#forEach() . 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: KuduTestBase.java    From bahir-flink with Apache License 2.0 6 votes vote down vote up
protected void validateSingleKey(String tableName) throws Exception {
    KuduTable kuduTable = harness.getClient().openTable(tableName);
    Schema schema = kuduTable.getSchema();

    assertEquals(1, schema.getPrimaryKeyColumnCount());
    assertEquals(2, schema.getColumnCount());

    assertTrue(schema.getColumn("first").isKey());
    assertFalse(schema.getColumn("second").isKey());

    KuduScanner scanner = harness.getClient().newScannerBuilder(kuduTable).build();
    List<RowResult> rows = new ArrayList<>();
    scanner.forEach(rows::add);

    assertEquals(1, rows.size());
    assertEquals("f", rows.get(0).getString("first"));
    assertEquals("s", rows.get(0).getString("second"));
}
 
Example 2
Source File: KuduCatalogTest.java    From bahir-flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testTimestamp() throws Exception {
    tableEnv.sqlUpdate("CREATE TABLE TestTableTsC (`first` STRING, `second` TIMESTAMP(3)) " +
            "WITH ('kudu.hash-columns'='first', 'kudu.primary-key-columns'='first')");
    tableEnv.sqlUpdate("INSERT INTO TestTableTsC values ('f', TIMESTAMP '2020-01-01 12:12:12.123456')");
    tableEnv.execute("test");

    KuduTable kuduTable = harness.getClient().openTable("TestTableTsC");
    assertEquals(Type.UNIXTIME_MICROS, kuduTable.getSchema().getColumn("second").getType());

    KuduScanner scanner = harness.getClient().newScannerBuilder(kuduTable).build();
    List<RowResult> rows = new ArrayList<>();
    scanner.forEach(rows::add);

    assertEquals(1, rows.size());
    assertEquals("f", rows.get(0).getString(0));
    assertEquals(Timestamp.valueOf("2020-01-01 12:12:12.123"), rows.get(0).getTimestamp(1));
}
 
Example 3
Source File: KuduCatalogTest.java    From bahir-flink with Apache License 2.0 6 votes vote down vote up
private void validateMultiKey(String tableName) throws Exception {
    KuduTable kuduTable = harness.getClient().openTable(tableName);
    Schema schema = kuduTable.getSchema();

    assertEquals(2, schema.getPrimaryKeyColumnCount());
    assertEquals(3, schema.getColumnCount());

    assertTrue(schema.getColumn("first").isKey());
    assertTrue(schema.getColumn("second").isKey());

    assertFalse(schema.getColumn("third").isKey());

    KuduScanner scanner = harness.getClient().newScannerBuilder(kuduTable).build();
    List<RowResult> rows = new ArrayList<>();
    scanner.forEach(rows::add);

    assertEquals(1, rows.size());
    assertEquals("f", rows.get(0).getString("first"));
    assertEquals(2, rows.get(0).getInt("second"));
    assertEquals("t", rows.get(0).getString("third"));
}
 
Example 4
Source File: KuduCatalogTest.java    From bahir-flink with Apache License 2.0 5 votes vote down vote up
private void validateManyTypes(String tableName) throws Exception {
    KuduTable kuduTable = harness.getClient().openTable(tableName);
    Schema schema = kuduTable.getSchema();

    assertEquals(Type.STRING, schema.getColumn("first").getType());
    assertEquals(Type.BOOL, schema.getColumn("second").getType());
    assertEquals(Type.BINARY, schema.getColumn("third").getType());
    assertEquals(Type.INT8, schema.getColumn("fourth").getType());
    assertEquals(Type.INT16, schema.getColumn("fifth").getType());
    assertEquals(Type.INT32, schema.getColumn("sixth").getType());
    assertEquals(Type.INT64, schema.getColumn("seventh").getType());
    assertEquals(Type.FLOAT, schema.getColumn("eighth").getType());
    assertEquals(Type.DOUBLE, schema.getColumn("ninth").getType());
    assertEquals(Type.UNIXTIME_MICROS, schema.getColumn("tenth").getType());

    KuduScanner scanner = harness.getClient().newScannerBuilder(kuduTable).build();
    List<RowResult> rows = new ArrayList<>();
    scanner.forEach(rows::add);

    assertEquals(1, rows.size());
    assertEquals("f", rows.get(0).getString(0));
    assertEquals(false, rows.get(0).getBoolean(1));
    assertEquals(ByteBuffer.wrap("bbbb".getBytes()), rows.get(0).getBinary(2));
    assertEquals(12, rows.get(0).getByte(3));
    assertEquals(34, rows.get(0).getShort(4));
    assertEquals(56, rows.get(0).getInt(5));
    assertEquals(78, rows.get(0).getLong(6));
    assertEquals(3.14, rows.get(0).getFloat(7), 0.01);
    assertEquals(1.2345, rows.get(0).getDouble(8), 0.0001);
    assertEquals(Timestamp.valueOf("2020-04-15 12:34:56.123"), rows.get(0).getTimestamp(9));
}
 
Example 5
Source File: KuduTableFactoryTest.java    From bahir-flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testTimestamp() throws Exception {
    // Timestamp should be bridged to sql.Timestamp
    // Test it when creating the table...
    tableEnv.sqlUpdate("CREATE TABLE TestTableTs (`first` STRING, `second` TIMESTAMP(3)) " +
            "WITH ('connector.type'='kudu', 'kudu.table'='TestTableTs', 'kudu.masters'='" + kuduMasters + "', " +
            "'kudu.hash-columns'='first', 'kudu.primary-key-columns'='first')");
    tableEnv.sqlUpdate("INSERT INTO TestTableTs values ('f', TIMESTAMP '2020-01-01 12:12:12.123456')");
    tableEnv.execute("test");

    // And also when inserting into existing table
    tableEnv.sqlUpdate("CREATE TABLE TestTableTsE (`first` STRING, `second` TIMESTAMP(3)) " +
            "WITH ('connector.type'='kudu', 'kudu.table'='TestTableTs', 'kudu.masters'='" + kuduMasters + "')");

    tableEnv.sqlUpdate("INSERT INTO TestTableTsE values ('s', TIMESTAMP '2020-02-02 23:23:23')");
    tableEnv.execute("test");

    KuduTable kuduTable = harness.getClient().openTable("TestTableTs");
    assertEquals(Type.UNIXTIME_MICROS, kuduTable.getSchema().getColumn("second").getType());

    KuduScanner scanner = harness.getClient().newScannerBuilder(kuduTable).build();
    HashSet<Timestamp> results = new HashSet<>();
    scanner.forEach(sc -> results.add(sc.getTimestamp("second")));

    assertEquals(2, results.size());
    List<Timestamp> expected = Lists.newArrayList(
            Timestamp.valueOf("2020-01-01 12:12:12.123"),
            Timestamp.valueOf("2020-02-02 23:23:23"));
    assertEquals(new HashSet<>(expected), results);
}
 
Example 6
Source File: KuduTableFactoryTest.java    From bahir-flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testExistingTable() throws Exception {
    // Creating a table
    tableEnv.sqlUpdate("CREATE TABLE TestTable12 (`first` STRING, `second` STRING) " +
            "WITH ('connector.type'='kudu', 'kudu.table'='TestTable12', 'kudu.masters'='" + kuduMasters + "', " +
            "'kudu.hash-columns'='first', 'kudu.primary-key-columns'='first')");

    tableEnv.sqlUpdate("INSERT INTO TestTable12 values ('f', 's')");
    tableEnv.execute("test");

    // Then another one in SQL that refers to the previously created one
    tableEnv.sqlUpdate("CREATE TABLE TestTable12b (`first` STRING, `second` STRING) " +
            "WITH ('connector.type'='kudu', 'kudu.table'='TestTable12', 'kudu.masters'='" + kuduMasters + "')");
    tableEnv.sqlUpdate("INSERT INTO TestTable12b values ('f2','s2')");
    tableEnv.execute("test2");

    // Validate that both insertions were into the same table
    KuduTable kuduTable = harness.getClient().openTable("TestTable12");
    KuduScanner scanner = harness.getClient().newScannerBuilder(kuduTable).build();
    List<RowResult> rows = new ArrayList<>();
    scanner.forEach(rows::add);

    assertEquals(2, rows.size());
    assertEquals("f", rows.get(0).getString("first"));
    assertEquals("s", rows.get(0).getString("second"));
    assertEquals("f2", rows.get(1).getString("first"));
    assertEquals("s2", rows.get(1).getString("second"));
}