Kudu之java操作

參考:kudu原理與使用

java操作kudu數據庫

導入依賴

<!--
<dependency>
  <groupId>org.apache.kudu</groupId>
  <artifactId>kudu-client</artifactId>
  <version>${kudu.version}</version>
  <scope>test</scope>
</dependency>
-->

<!--導入kudu的客戶端工具-->
<dependency>
  <groupId>org.apache.kudu</groupId>
  <artifactId>kudu-client-tools</artifactId>
  <version>${kudu.version}</version>
</dependency>

方案一(use)

KuduUtils

package org.fiend.kudutest;

import com.alibaba.fastjson.JSONObject;
import org.apache.kudu.ColumnSchema;
import org.apache.kudu.Schema;
import org.apache.kudu.Type;
import org.apache.kudu.client.*;

/**
 * @author langpf 2020-05-07 21:57:47
 */
public class KuduUtils {
    private static final ThreadLocal<KuduSession> threadLocal = new ThreadLocal<>();

    public static KuduTable table(String name) throws KuduException {
        return Kudu.INSTANCE.table(name);
    }

    public static Insert emptyInsert(String table) throws KuduException {
        KuduTable ktable = Kudu.INSTANCE.table(table);
        return ktable.newInsert();
    }

    public static Update emptyUpdate(String table) throws KuduException {
        KuduTable ktable = Kudu.INSTANCE.table(table);
        return ktable.newUpdate();
    }

    public static Upsert emptyUpsert(String table) throws KuduException {
        KuduTable ktable = Kudu.INSTANCE.table(table);
        return ktable.newUpsert();
    }

    /**
     * Only columns which are part of the key can be set
     *
     * @param table
     * @return
     */
    public static Delete emptyDelete(String table) throws KuduException {
        KuduTable ktable = Kudu.INSTANCE.table(table);
        return ktable.newDelete();
    }

    public static Upsert createUpsert(String table, JSONObject data) throws KuduException {
        KuduTable ktable = Kudu.INSTANCE.table(table);
        Upsert upsert = ktable.newUpsert();
        PartialRow row = upsert.getRow();
        Schema schema = ktable.getSchema();
        for (String colName : data.keySet()) {
            ColumnSchema colSchema = schema.getColumn(colName);
            fillRow(row, colSchema, data);
        }
        return upsert;
    }

    public static Insert createInsert(String table, JSONObject data) throws KuduException {
        KuduTable ktable = Kudu.INSTANCE.table(table);
        Insert insert = ktable.newInsert();
        PartialRow row = insert.getRow();
        Schema schema = ktable.getSchema();
        for (String colName : data.keySet()) {
            ColumnSchema colSchema = schema.getColumn(colName);
            fillRow(row, colSchema, data);
        }
        return insert;
    }
    public static void insert(String table, JSONObject data) throws KuduException {
        Insert insert = createInsert(table, data);
        KuduSession session = getSession();
        session.apply(insert);
        session.flush();
        closeSession();
    }
    private static void fillRow(PartialRow row, ColumnSchema colSchema, JSONObject data) {
        String name = colSchema.getName();
        if (data.get(name) == null) {
            return;
        }
        Type type = colSchema.getType();
        switch (type) {
            case STRING:
                row.addString(name, data.getString(name));
                break;
            case INT64:
            case UNIXTIME_MICROS:
                row.addLong(name, data.getLongValue(name));
                break;
            case DOUBLE:
                row.addDouble(name, data.getDoubleValue(name));
                break;
            case INT32:
                row.addInt(name, data.getIntValue(name));
                break;
            case INT16:
                row.addShort(name, data.getShortValue(name));
                break;
            case INT8:
                row.addByte(name, data.getByteValue(name));
                break;
            case BOOL:
                row.addBoolean(name, data.getBooleanValue(name));
                break;
            case BINARY:
                row.addBinary(name, data.getBytes(name));
                break;
            case FLOAT:
                row.addFloat(name, data.getFloatValue(name));
                break;
            default:
                break;
        }
    }

    public static KuduSession getSession() throws KuduException {
        KuduSession session = threadLocal.get();
        if (session == null) {
            session = Kudu.INSTANCE.newAsyncSession();
            threadLocal.set(session);
        }
        return session;
    }

    public static KuduSession getAsyncSession() throws KuduException {
        KuduSession session = threadLocal.get();
        if (session == null) {
            session = Kudu.INSTANCE.newAsyncSession();
            threadLocal.set(session);
        }
        return session;
    }

    public static void closeSession() {
        KuduSession session = threadLocal.get();
        threadLocal.set(null);
        Kudu.INSTANCE.closeSession(session);
    }

}

Kudu

package org.fiend.kudutest;

import org.apache.kudu.client.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public enum Kudu {
    INSTANCE;

    private Kudu() {
        init();
        addShutdownHook();
    }

    private KuduClient client = null;
    private Map<String, KuduTable> tables = new HashMap<>();
    private Logger logger = LoggerFactory.getLogger(Kudu.class);

    private void init() {
        client = new KuduClient.KuduClientBuilder("192.168.1.132").defaultOperationTimeoutMs(60000)
                .defaultSocketReadTimeoutMs(30000).defaultAdminOperationTimeoutMs(60000).build();
    }

    private void addShutdownHook() {
        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {
                if (client != null) {
                    try {
                        client.close();
                    } catch (Exception e) {
                        logger.error("ShutdownHook Close KuduClient Error!", e);
                    }
                }
            }
        });
    }

    public KuduClient client() {
        return client;
    }

    public KuduTable table(String name) throws KuduException {
        KuduTable table = tables.get(name);
        if (table == null) {
            table = client.openTable(name);
            tables.put(name, table);
        }
        return table;
    }

    /**
     * FlushMode:AUTO_FLUSH_BACKGROUND
     *
     * @return
     * @throws KuduException
     */
    public KuduSession newAsyncSession() throws KuduException {
        KuduSession session = client.newSession();
        session.setFlushMode(SessionConfiguration.FlushMode.AUTO_FLUSH_BACKGROUND);
        session.setFlushInterval(500);
        session.setMutationBufferSpace(5000);
        return session;
    }

    /**
     * FlushMode:AUTO_FLUSH_SYNC
     *
     * @return
     * @throws KuduException
     */
    public KuduSession newSession() throws KuduException {
        KuduSession session = client.newSession();
        session.setFlushMode(SessionConfiguration.FlushMode.AUTO_FLUSH_SYNC);
        session.setMutationBufferSpace(5000);
        return session;
    }

    public void closeSession(KuduSession session) {
        if (session != null && !session.isClosed()) {
            try {
                session.close();
            } catch (KuduException e) {
                logger.error("Close KuduSession Error!", e);
            }
        }
    }

    public KuduScanner.KuduScannerBuilder scannerBuilder(String table) {
        return client.newScannerBuilder(tables.get(table));
    }
}

方案二

表創建

/**
 * 創建表
 * @throws Exception
 */
public static void createTable() throws Exception{
  //1、創建一個client
  KuduClient client = new KuduClientBuilder(KUDU_MASTER).build();
  
  //2、創建schema信息
  List<ColumnSchema> columns = new ArrayList<ColumnSchema>();
  columns.add(new ColumnSchema.ColumnSchemaBuilder("id", Type.INT32).key(true).nullable(false).build());
  columns.add(new ColumnSchema.ColumnSchemaBuilder("name", Type.STRING).key(false).nullable(false).build());
  columns.add(new ColumnSchema.ColumnSchemaBuilder("age", Type.INT32).key(false).nullable(false).build());
  Schema schema = new Schema(columns);
  
  //3、指定分區字段
  List<String> partions = new ArrayList<String>();
  partions.add("id");
  
  //4、指定分區方式爲hash分區、6個分區,一個副本
  CreateTableOptions options = new CreateTableOptions().addHashPartitions(partions, 6).setNumReplicas(1);
  
  //5、創建表,
  client.createTable("person",schema,options);
  
  client.close();
}

數據插入(Insert)

/**
 * 插入數據
 * @throws Exception
 */
public static void add() throws Exception{
  //1、創建一個client
  KuduClient client = new KuduClientBuilder(KUDU_MASTER).build();
  
  //2、打開表
  KuduTable table = client.openTable("person");
  
  //3、創建一個session會話
  KuduSession session = client.newSession();
  
  //4、創建插入
  Insert insert = table.newInsert();
  
  //5、指定插入數據
  insert.getRow().addInt("id",1);
  insert.getRow().addInt("age",18);
  insert.getRow().addString("name","張三");
  
  //6、應用插入
  session.apply(insert);
  
  session.close();
  
  client.close();
}

數據更新(update)

/**
 * 更新數據
 * @throws Exception
 */
public static void update() throws Exception{
  //1、創建kudu client
  KuduClient client = new KuduClientBuilder(KUDU_MASTER).build();
  
  //2、打開表
  KuduTable table = client.openTable("person");
  
  KuduSession session = client.newSession();
  
  Update update = table.newUpdate();
  update.getRow().addInt("id",1);
  update.getRow().addString("name","李四");
  
  session.apply(update);
  session.flush();
  session.close();
  
  client.close();
}

刪除(delete)

/**
 * 刪除數據
 * @throws Exception
 */
public static void delete() throws Exception{
  //1、創建kudu client
  KuduClient client = new KuduClientBuilder(KUDU_MASTER).build();
  //2、打開表
  KuduTable table = client.openTable("person");
  
  KuduSession session = client.newSession();
  
  Delete delete = table.newDelete();
  delete.getRow().addInt("id",1);
  
  
  session.apply(delete);
  session.flush();
  session.close();
  
  client.close();
}

查詢(query)

/**
 * 條件查詢 select * from person where id=1
 * @throws Exception
 */
public static void query() throws Exception{
  //1、創建kudu client
  KuduClient client = new KuduClientBuilder(KUDU_MASTER).build();
  
  //2、打開表
  KuduTable table = client.openTable("person");
  
  //3、創建scanner掃描器
  KuduScanner.KuduScannerBuilder kuduScannerBuilder = client.newScannerBuilder(table);
  
  //4、創建查詢條件
  KuduPredicate filter = KuduPredicate.newComparisonPredicate(table.getSchema().getColumn("id"), 
                                                  KuduPredicate.ComparisonOp.EQUAL, 1);
  
  //5、將查詢條件加入到scanner中
  KuduScanner scanner = kuduScannerBuilder.addPredicate(filter).build();
  
  //6、獲取查詢結果
  while (scanner.hasMoreRows()){
    RowResultIterator rows = scanner.nextRows();
    while (rows.hasNext()){
  	  RowResult row = rows.next();
  	  Integer id = row.getInt("id");
  	  String name = row.getString("name");
  	  int age = row.getInt("age");
  	  System.out.println(id+"---"+name+"---"+age);
    }
  }
  
  //7、關閉client
  client.close();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章