Search in sources :

Example 1 with Insert

use of org.apache.kudu.client.Insert in project nifi by apache.

the class PutKudu method insertRecordToKudu.

@Override
protected Insert insertRecordToKudu(KuduTable kuduTable, Record record, List<String> fieldNames) throws IllegalStateException, Exception {
    Insert insert = kuduTable.newInsert();
    this.insert(kuduTable, insert, record, fieldNames);
    return insert;
}
Also used : Insert(org.apache.kudu.client.Insert)

Example 2 with Insert

use of org.apache.kudu.client.Insert in project drill by axbaretto.

the class TestKuduConnect method createKuduTable.

public static void createKuduTable(String tableName, int tablets, int replicas, int rows) throws Exception {
    try (KuduClient client = new KuduClient.KuduClientBuilder(KUDU_MASTER).build()) {
        ListTablesResponse tables = client.getTablesList(tableName);
        if (!tables.getTablesList().isEmpty()) {
            client.deleteTable(tableName);
        }
        List<ColumnSchema> columns = new ArrayList<>(5);
        columns.add(new ColumnSchema.ColumnSchemaBuilder("key", Type.INT32).key(true).build());
        columns.add(new ColumnSchema.ColumnSchemaBuilder("binary", Type.BINARY).nullable(false).build());
        columns.add(new ColumnSchema.ColumnSchemaBuilder("boolean", Type.BOOL).nullable(true).build());
        columns.add(new ColumnSchema.ColumnSchemaBuilder("float", Type.FLOAT).nullable(false).build());
        columns.add(new ColumnSchema.ColumnSchemaBuilder("string", Type.STRING).nullable(true).build());
        Schema schema = new Schema(columns);
        CreateTableOptions builder = new CreateTableOptions();
        builder.setNumReplicas(replicas);
        builder.setRangePartitionColumns(Arrays.asList("key"));
        for (int i = 1; i < tablets; i++) {
            PartialRow splitRow = schema.newPartialRow();
            splitRow.addInt("key", i * 1000);
            builder.addSplitRow(splitRow);
        }
        client.createTable(tableName, schema, builder);
        KuduTable table = client.openTable(tableName);
        KuduSession session = client.newSession();
        session.setFlushMode(SessionConfiguration.FlushMode.AUTO_FLUSH_SYNC);
        for (int i = 0; i < rows; i++) {
            Insert insert = table.newInsert();
            PartialRow row = insert.getRow();
            row.addInt(0, i);
            row.addBinary(1, ("Row " + i).getBytes());
            row.addBoolean(2, i % 2 == 0);
            row.addFloat(3, i + 0.01f);
            row.addString(4, ("Row " + i));
            session.apply(insert);
        }
        List<String> projectColumns = new ArrayList<>(1);
        projectColumns.add("float");
        KuduScanner scanner = client.newScannerBuilder(table).setProjectedColumnNames(projectColumns).build();
        while (scanner.hasMoreRows()) {
            RowResultIterator results = scanner.nextRows();
            while (results.hasNext()) {
                RowResult result = results.next();
                System.out.println(result.toStringLongFormat());
            }
        }
    }
}
Also used : KuduSession(org.apache.kudu.client.KuduSession) Schema(org.apache.kudu.Schema) ColumnSchema(org.apache.kudu.ColumnSchema) ArrayList(java.util.ArrayList) PartialRow(org.apache.kudu.client.PartialRow) ColumnSchema(org.apache.kudu.ColumnSchema) KuduTable(org.apache.kudu.client.KuduTable) Insert(org.apache.kudu.client.Insert) RowResultIterator(org.apache.kudu.client.RowResultIterator) RowResult(org.apache.kudu.client.RowResult) KuduScanner(org.apache.kudu.client.KuduScanner) KuduClient(org.apache.kudu.client.KuduClient) ListTablesResponse(org.apache.kudu.client.ListTablesResponse) CreateTableOptions(org.apache.kudu.client.CreateTableOptions)

Example 3 with Insert

use of org.apache.kudu.client.Insert in project drill by apache.

the class TestKuduConnect method createKuduTable.

public static void createKuduTable(String tableName, int tablets, int replicas, int rows) throws Exception {
    try (KuduClient client = new KuduClient.KuduClientBuilder(KUDU_MASTER).build()) {
        ListTablesResponse tables = client.getTablesList(tableName);
        if (!tables.getTablesList().isEmpty()) {
            client.deleteTable(tableName);
        }
        List<ColumnSchema> columns = new ArrayList<>(5);
        columns.add(new ColumnSchema.ColumnSchemaBuilder("key", Type.INT32).key(true).build());
        columns.add(new ColumnSchema.ColumnSchemaBuilder("binary", Type.BINARY).nullable(false).build());
        columns.add(new ColumnSchema.ColumnSchemaBuilder("boolean", Type.BOOL).nullable(true).build());
        columns.add(new ColumnSchema.ColumnSchemaBuilder("float", Type.FLOAT).nullable(false).build());
        columns.add(new ColumnSchema.ColumnSchemaBuilder("string", Type.STRING).nullable(true).build());
        Schema schema = new Schema(columns);
        CreateTableOptions builder = new CreateTableOptions();
        builder.setNumReplicas(replicas);
        builder.setRangePartitionColumns(Arrays.asList("key"));
        for (int i = 1; i < tablets; i++) {
            PartialRow splitRow = schema.newPartialRow();
            splitRow.addInt("key", i * 1000);
            builder.addSplitRow(splitRow);
        }
        client.createTable(tableName, schema, builder);
        KuduTable table = client.openTable(tableName);
        KuduSession session = client.newSession();
        session.setFlushMode(SessionConfiguration.FlushMode.AUTO_FLUSH_SYNC);
        for (int i = 0; i < rows; i++) {
            Insert insert = table.newInsert();
            PartialRow row = insert.getRow();
            row.addInt(0, i);
            row.addBinary(1, ("Row " + i).getBytes());
            row.addBoolean(2, i % 2 == 0);
            row.addFloat(3, i + 0.01f);
            row.addString(4, ("Row " + i));
            session.apply(insert);
        }
        List<String> projectColumns = new ArrayList<>(1);
        projectColumns.add("float");
        KuduScanner scanner = client.newScannerBuilder(table).setProjectedColumnNames(projectColumns).build();
        while (scanner.hasMoreRows()) {
            RowResultIterator results = scanner.nextRows();
            while (results.hasNext()) {
                logger.debug(results.next().toString());
            }
        }
    }
}
Also used : KuduSession(org.apache.kudu.client.KuduSession) Schema(org.apache.kudu.Schema) ColumnSchema(org.apache.kudu.ColumnSchema) ArrayList(java.util.ArrayList) PartialRow(org.apache.kudu.client.PartialRow) ColumnSchema(org.apache.kudu.ColumnSchema) KuduTable(org.apache.kudu.client.KuduTable) Insert(org.apache.kudu.client.Insert) RowResultIterator(org.apache.kudu.client.RowResultIterator) KuduScanner(org.apache.kudu.client.KuduScanner) KuduClient(org.apache.kudu.client.KuduClient) ListTablesResponse(org.apache.kudu.client.ListTablesResponse) CreateTableOptions(org.apache.kudu.client.CreateTableOptions)

Example 4 with Insert

use of org.apache.kudu.client.Insert in project hive by apache.

the class TestKuduInputFormat method testMultipleSplits.

@Test
public void testMultipleSplits() throws Exception {
    String tableName = "default.twoPartitionTable";
    Schema schema = new Schema(Arrays.asList(new ColumnSchema.ColumnSchemaBuilder("key", Type.INT32).key(true).build(), new ColumnSchema.ColumnSchemaBuilder("string", Type.STRING).build()));
    CreateTableOptions options = new CreateTableOptions().addHashPartitions(Collections.singletonList("key"), 2);
    harness.getClient().createTable(tableName, schema, options);
    // Insert multiple test rows.
    KuduTable table = harness.getClient().openTable(tableName);
    KuduSession session = harness.getClient().newSession();
    Insert insert1 = table.newInsert();
    PartialRow row1 = insert1.getRow();
    row1.addInt("key", 1);
    row1.addString("string", "one");
    session.apply(insert1);
    Insert insert2 = table.newInsert();
    PartialRow row2 = insert2.getRow();
    row2.addInt("key", 2);
    row2.addString("string", "two");
    session.apply(insert2);
    session.close();
    KuduInputFormat input = new KuduInputFormat();
    JobConf jobConf = new JobConf(BASE_CONF);
    jobConf.set(KUDU_TABLE_NAME_KEY, tableName);
    jobConf.set(serdeConstants.LIST_COLUMNS, "key");
    InputSplit[] splits = input.getSplits(jobConf, 1);
    assertEquals(2, splits.length);
}
Also used : KuduSession(org.apache.kudu.client.KuduSession) Schema(org.apache.kudu.Schema) ColumnSchema(org.apache.kudu.ColumnSchema) KuduTestUtils.getAllTypesSchema(org.apache.hadoop.hive.kudu.KuduTestUtils.getAllTypesSchema) PartialRow(org.apache.kudu.client.PartialRow) ColumnSchema(org.apache.kudu.ColumnSchema) KuduTable(org.apache.kudu.client.KuduTable) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Insert(org.apache.kudu.client.Insert) CreateTableOptions(org.apache.kudu.client.CreateTableOptions) JobConf(org.apache.hadoop.mapred.JobConf) KuduInputSplit(org.apache.hadoop.hive.kudu.KuduInputFormat.KuduInputSplit) InputSplit(org.apache.hadoop.mapred.InputSplit) Test(org.junit.Test)

Example 5 with Insert

use of org.apache.kudu.client.Insert in project canal by alibaba.

the class KuduTemplate method insert.

/**
 * 插入数据
 *
 * @param tableName
 * @param dataList
 * @throws KuduException
 */
public void insert(String tableName, List<Map<String, Object>> dataList) throws KuduException {
    this.checkClient();
    // 打开表
    KuduTable kuduTable = kuduClient.openTable(tableName);
    // 创建写session,kudu必须通过session写入
    KuduSession session = kuduClient.newSession();
    try {
        // 采取Flush方式
        session.setFlushMode(SessionConfiguration.FlushMode.MANUAL_FLUSH);
        // 手动刷新
        session.setMutationBufferSpace(OPERATION_BATCH);
        // 获取元数据结构
        Map<String, Type> metaMap = new HashMap<>();
        Schema schema = kuduTable.getSchema();
        for (ColumnSchema columnSchema : schema.getColumns()) {
            String colName = columnSchema.getName().toLowerCase();
            Type type = columnSchema.getType();
            metaMap.put(colName, type);
        }
        int uncommit = 0;
        for (Map<String, Object> data : dataList) {
            Insert insert = kuduTable.newInsert();
            PartialRow row = insert.getRow();
            for (Map.Entry<String, Object> entry : data.entrySet()) {
                String name = entry.getKey().toLowerCase();
                Type type = metaMap.get(name);
                Object value = entry.getValue();
                // 填充行数据
                fillRow(row, name, value, type);
            }
            session.apply(insert);
            // 对于手工提交, 需要buffer在未满的时候flush,这里采用了buffer一半时即提交
            uncommit = uncommit + 1;
            if (uncommit > OPERATION_BATCH / 3 * 2) {
                List<OperationResponse> insert_option = session.flush();
                if (insert_option.size() > 0) {
                    OperationResponse response = insert_option.get(0);
                    if (response.hasRowError()) {
                        logger.error("insert row fail table name is :{} ", tableName);
                        logger.error("insert list is :{}", response.getRowError().getMessage());
                    }
                }
                uncommit = 0;
            }
        }
        List<OperationResponse> insert_option = session.flush();
        if (insert_option.size() > 0) {
            OperationResponse response = insert_option.get(0);
            if (response.hasRowError()) {
                logger.error("insert row fail table name is :{} ", tableName);
                logger.error("insert list is :{}", response.getRowError().getMessage());
            }
        }
    } catch (KuduException e) {
        logger.error("error message is :{}", dataList.toString());
        throw e;
    } finally {
        if (!session.isClosed()) {
            session.close();
        }
    }
}
Also used : KuduSession(org.apache.kudu.client.KuduSession) HashMap(java.util.HashMap) Schema(org.apache.kudu.Schema) ColumnSchema(org.apache.kudu.ColumnSchema) PartialRow(org.apache.kudu.client.PartialRow) KuduTable(org.apache.kudu.client.KuduTable) ColumnSchema(org.apache.kudu.ColumnSchema) Insert(org.apache.kudu.client.Insert) KuduException(org.apache.kudu.client.KuduException) Type(org.apache.kudu.Type) OperationResponse(org.apache.kudu.client.OperationResponse) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Insert (org.apache.kudu.client.Insert)9 KuduSession (org.apache.kudu.client.KuduSession)7 KuduTable (org.apache.kudu.client.KuduTable)7 ColumnSchema (org.apache.kudu.ColumnSchema)6 PartialRow (org.apache.kudu.client.PartialRow)6 Schema (org.apache.kudu.Schema)5 CreateTableOptions (org.apache.kudu.client.CreateTableOptions)5 ArrayList (java.util.ArrayList)2 KuduInputSplit (org.apache.hadoop.hive.kudu.KuduInputFormat.KuduInputSplit)2 InputSplit (org.apache.hadoop.mapred.InputSplit)2 JobConf (org.apache.hadoop.mapred.JobConf)2 KuduClient (org.apache.kudu.client.KuduClient)2 KuduScanner (org.apache.kudu.client.KuduScanner)2 ListTablesResponse (org.apache.kudu.client.ListTablesResponse)2 RowResult (org.apache.kudu.client.RowResult)2 RowResultIterator (org.apache.kudu.client.RowResultIterator)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 Test (org.junit.Test)2 BigDecimal (java.math.BigDecimal)1 Timestamp (java.sql.Timestamp)1