Search in sources :

Example 6 with Delete

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

the class KuduTemplate method delete.

/**
 * 删除行
 *
 * @param tableName
 * @param dataList
 * @throws KuduException
 */
public void delete(String tableName, List<Map<String, Object>> dataList) throws KuduException {
    this.checkClient();
    KuduTable kuduTable = kuduClient.openTable(tableName);
    KuduSession session = kuduClient.newSession();
    try {
        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) {
            Delete delete = kuduTable.newDelete();
            PartialRow row = delete.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(delete);
            // 对于手工提交, 需要buffer在未满的时候flush,这里采用了buffer一半时即提交
            uncommit = uncommit + 1;
            if (uncommit > OPERATION_BATCH / 3 * 2) {
                List<OperationResponse> delete_option = session.flush();
                if (delete_option.size() > 0) {
                    OperationResponse response = delete_option.get(0);
                    if (response.hasRowError()) {
                        logger.error("delete row fail table name is :{} ", tableName);
                        logger.error("error list is :{}", response.getRowError().getMessage());
                    }
                }
                uncommit = 0;
            }
        }
        List<OperationResponse> delete_option = session.flush();
        if (delete_option.size() > 0) {
            OperationResponse response = delete_option.get(0);
            if (response.hasRowError()) {
                logger.error("delete row fail table name is :{} ", tableName);
                logger.error("error 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 : Delete(org.apache.kudu.client.Delete) 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) 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

Delete (org.apache.kudu.client.Delete)6 KuduException (org.apache.kudu.client.KuduException)4 KuduSession (org.apache.kudu.client.KuduSession)4 KuduTable (org.apache.kudu.client.KuduTable)3 PartialRow (org.apache.kudu.client.PartialRow)3 Schema (org.apache.kudu.Schema)2 OperationResponse (org.apache.kudu.client.OperationResponse)2 PrestoException (com.facebook.presto.spi.PrestoException)1 Slice (io.airlift.slice.Slice)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Column (org.apache.gora.kudu.mapping.Column)1 GoraException (org.apache.gora.util.GoraException)1 ColumnSchema (org.apache.kudu.ColumnSchema)1 Type (org.apache.kudu.Type)1 KuduScanToken (org.apache.kudu.client.KuduScanToken)1 KuduScanner (org.apache.kudu.client.KuduScanner)1 RowResult (org.apache.kudu.client.RowResult)1 RowResultIterator (org.apache.kudu.client.RowResultIterator)1