Java操作Kudu

    <dependency>
      <groupId>org.apache.kudu</groupId>
      <artifactId>kudu-client</artifactId>
      <version>1.9.0</version>
    </dependency>
import org.apache.kudu.ColumnSchema;
import org.apache.kudu.Schema;
import org.apache.kudu.Type;
import org.apache.kudu.client.*;

import java.util.LinkedList;
import java.util.List;

public class KuduDemo {

    //Kudu中三个master的hostname,master需要跟建表时指定的一样
    private static final String master = "master01,master02,master03";

	//表名可以使用Kudu自带的页面查看,因为是使用impala客户端创建的表,所以格式如下
    private static final String tablename = "impala::kudu_ods.saic_pay_order_t_pay_order";

    public static void main(String[] args) {
//        updateRow();
//        deleteRow();
        createTable();
    }

    //Kudu查询数据
    public static void scanTable()
    {
        try {
            KuduClient client = new KuduClient.KuduClientBuilder(master).defaultSocketReadTimeoutMs(10000).build();

            KuduTable table = client.openTable(tablename);

            KuduScanner scanner = client.newScannerBuilder(table).build();

            while (scanner.hasMoreRows())
            {
                RowResultIterator iterator = scanner.nextRows();
                while (iterator.hasNext()) {
                    RowResult result = iterator.next();
                    System.out.print("order_no:" + result.getString("order_no") +"   ");
                }
            }
            scanner.close();
            client.close();
        }catch (KuduException e){
            e.printStackTrace();
        }
    }

    //Kudu插入数据
    public static void insertRow()
    {
        KuduClient client = null;
        KuduSession session = null;

        try {
            client = new KuduClient.KuduClientBuilder(master).build();
            KuduTable table = client.openTable(tablename);

            session = client.newSession();
            session.setFlushMode(SessionConfiguration.FlushMode.MANUAL_FLUSH);
            session.setMutationBufferSpace(3000);

            Insert insert = table.newInsert();
            insert.getRow().addLong("id",22222222);
            insert.getRow().addInt("pay_channel", 1);

            session.apply(insert);
            session.flush();

//            session.close();
//            client.close();
        }catch (KuduException e){
            e.printStackTrace();
        }finally {
            if(session != null)
            {
                try {
                    session.close();
                }catch (KuduException e){
                    e.printStackTrace();
                }
            }

            if(client != null)
            {
                try {
                    client.close();
                }catch (KuduException e){
                    e.printStackTrace();
                }
            }
        }
    }

    //更新Kudu中的数据
    public static void updateRow()
    {
        KuduClient client = null;
        KuduSession session = null;

        try {
            client = new KuduClient.KuduClientBuilder(master).build();
            KuduTable table = client.openTable(tablename);

            session = client.newSession();
            session.setFlushMode(SessionConfiguration.FlushMode.AUTO_FLUSH_SYNC);

            Update update = table.newUpdate();
            PartialRow row = update.getRow();

            row.addLong("id", 22222222);
            row.addInt("pay_channel", 10);

            session.apply(update);
            session.flush();

        }catch (KuduException e){
            e.printStackTrace();
        }finally {
            if(session != null)
            {
                try {
                    session.close();
                }catch (KuduException e){
                    e.printStackTrace();
                }
            }

            if(client != null)
            {
                try {
                    client.close();
                }catch (KuduException e){
                    e.printStackTrace();
                }
            }
        }
    }

    //删除指定行
    public static void deleteRow()
    {
        KuduClient client = null;
        KuduSession session = null;

        try {
            client = new KuduClient.KuduClientBuilder(master).build();

            KuduTable table = client.openTable("kudu_ods.saic_coupon_t_coupon");
            session = client.newSession();
            session.setMutationBufferSpace(3000);
            session.setFlushMode(SessionConfiguration.FlushMode.MANUAL_FLUSH);

            Delete delete = table.newDelete();
            delete.getRow().addLong("id", 45132358);
            
            session.apply(delete);
            session.flush();

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(session != null)
            {
                try {
                    session.close();
                }catch (KuduException e){
                    e.printStackTrace();
                }
            }

            if(client != null)
            {
                try {
                    client.close();
                }catch (KuduException e){
                    e.printStackTrace();
                }
            }
        }

    }

    //创建表
    public static void createTable()
    {
        KuduClient client = null;
        try {
            // 创建kudu的数据库链接
            client = new KuduClient.KuduClientBuilder(master).defaultSocketReadTimeoutMs(6000).build();

            // 设置表的schema
            List<ColumnSchema> columns = new LinkedList<>();
            /**
             与 RDBMS 不同,Kudu 不提供自动递增列功能,因此应用程序必须始终在插入期间提供完整的主键
             */
            columns.add(newColumn("CompanyId", Type.INT32, true));
            columns.add(newColumn("WorkId", Type.INT32, false));
            columns.add(newColumn("Name", Type.STRING, false));
            columns.add(newColumn("Gender", Type.STRING, false));
            columns.add(newColumn("Photo", Type.STRING, false));
            Schema schema = new Schema(columns);
            //创建表时提供的所有选项
            CreateTableOptions options = new CreateTableOptions();
            // 设置表的replica备份和分区规则
            List<String> parcols = new LinkedList<String>();
            parcols.add("CompanyId");

            //设置表的备份数
            options.setNumReplicas(1);
            //设置range分区
            options.setRangePartitionColumns(parcols);
            //设置hash分区和数量
            options.addHashPartitions(parcols, 3);

            client.createTable("impala::kudu_ods.person", schema, options);
        } catch (KuduException e) {
            e.printStackTrace();
        }finally {
            try {
                client.close();
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }

    //删除表
    public static void dropTable()
    {

    }

    private static ColumnSchema newColumn(String name, Type type, boolean iskey) {
        ColumnSchema.ColumnSchemaBuilder column = new ColumnSchema.ColumnSchemaBuilder(name, type);
        column.key(iskey);
        return column.build();
    }
}

你可能感兴趣的:(Kudu)