|NO.Z.00004|——————————|BigDataEnd|——|Hadoop&OLAP_Kudu.V04|——|kudu.v04|常用API|创建删除表|

一、KuDu常用Api(java):创建maven工程:kudu_project
### --- 在pom.xml下添加依赖

    <dependencies>

    <dependency>
        <groupId>org.apache.kudu</groupId>
        <artifactId>kudu-client</artifactId>
        <version>1.4.0</version>
    </dependency>

    </dependencies>
二、KuDuApi常用操作:创建表
### --- 创建表

~~~     # 思路:kudu建表几个必须:
~~~     必须指定表连接到的master节点主机名
~~~     必须定义schema
~~~     必须指定副本数量、分区策略和数量
### --- 编程代码实现
### --- 编译打印:通过Chrome访问http://hadoop01:8051/tables查看创建的表

package com.yanqi.kudu.api;

import org.apache.kudu.ColumnSchema;
import org.apache.kudu.Schema;
import org.apache.kudu.Type;
import org.apache.kudu.client.CreateTableOptions;
import org.apache.kudu.client.KuduClient;
import org.apache.kudu.client.KuduException;

import java.util.ArrayList;

public class createTableDemo {
    /**
     * (1)必须指定表连接到的master节点主机名
     *
     * (2)必须定义schema,声明的每一个字段必须显式的说明是否是主键
     *
     * (3)必须指定副本数量、分区策略和数量
     */
    public static void main(String[] args) {
        //client
        String masterAddresses = "hadoop01";
        KuduClient.KuduClientBuilder kuduClientBuilder = new KuduClient.KuduClientBuilder(masterAddresses);
        KuduClient client = kuduClientBuilder.build();

        String tableName = "student";
        //id int pkey    name string

        //指定每一列的信息
        ArrayList<ColumnSchema> columnSchemas = new ArrayList<ColumnSchema>();
        ColumnSchema id = new ColumnSchema.ColumnSchemaBuilder("id", Type.INT32).key(true).build();
        ColumnSchema name = new ColumnSchema.ColumnSchemaBuilder("name", Type.STRING).key(false).build();
        columnSchemas.add(id);
        columnSchemas.add(name);
        Schema schema = new Schema(columnSchemas);

        CreateTableOptions options = new CreateTableOptions();
        //设定当前的副本数量为1
        options.setNumReplicas(1);
        ArrayList<String> colrule = new ArrayList<String>();
        colrule.add("id");
        options.addHashPartitions(colrule,3);


        try {
            client.createTable(tableName,schema,options);
        } catch (KuduException e) {
            e.printStackTrace();
        } finally {
            try {
                client.close();
            } catch (KuduException e) {
                e.printStackTrace();
            }
        }
    }

}
三、KuDuApi常用操作:删除表
### --- 编程代码实现:删除表:client.deleteTable
### --- 编译打印

package com.yanqi.kudu.api;

import org.apache.kudu.client.KuduClient;
import org.apache.kudu.client.KuduException;

public class deleteTableDemo {
    public static void main(String[] args) {
        KuduClient client = new KuduClient.KuduClientBuilder("hadoop01").build();
        try {
            client.deleteTable("student");
        } catch (KuduException e) {
            e.printStackTrace();
        }finally {
            try {
                client.close();
            } catch (KuduException e) {
                e.printStackTrace();
            }
        }
    }
}

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

posted on   yanqi_vip  阅读(16)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示