Hbase2.x java API

记一次 hbase 2.x java api
欢迎大佬 指导
环境准备resources 文件夹下添加
core-site.xml;habse-site.xml
代码:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.net.URISyntaxException;

public class Demo {
    private static Configuration conf;
    private static Connection connection;
    private static Admin admin;

    //创建hbaseAdmin
    static {
        conf = HBaseConfiguration.create();
        try {
            conf.addResource(new Path(ClassLoader.getSystemResource("hbase-site.xml").toURI()));
            conf.addResource(new Path(ClassLoader.getSystemResource("core-site.xml").toURI()));
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        try {
            connection = ConnectionFactory.createConnection(conf);
            admin = connection.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    public static void main(String[] args) throws IOException {
        String familyNames[] = {"nature", "social"};
        String newfamilyNames[] = {"dyll", "xxx"};
        try {
            //创建表
//            createTable("test_hbase_new", familyNames);
            //添加数据
//            insertData("test_hbase_new", "ceshi", "nature", "age", "12");
            //添加列族
            insertComnFaimly("test_hbase_new", newfamilyNames);
//            deleteTable("test_hbase");



        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            admin.close();
            connection.close();
        }

    }

    /*
    创建hbase表
    **/
    public static void createTable(String tableName, String familyNames[]) throws IOException {
        if (admin.tableExists(TableName.valueOf(tableName))) {
            System.out.println("table exist");
            return;

        }
        //创建表描述生成器
        TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
        for (String familyName : familyNames) {
            //创建列族描述器
            ColumnFamilyDescriptor of = ColumnFamilyDescriptorBuilder.of(familyName);
            //添加列族
            builder.setColumnFamily(of);
        }
        //构建表描述
        TableDescriptor build = builder.build();
        //创建表
        admin.createTable(build);

//        旧版本 通过HtableDescriptor类来描述一个表,hColumnDescription 描述一个列;
//                HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
//        for (String familyName : familyNames) {
//            tableDescriptor.addFamily(new HColumnDescriptor(familyName));
//        }
//        admin.createTable(tableDescriptor);
        System.out.println("chuangjain chnegg ");
    }

    //删除表
    public static void deleteTable(String tableName) throws IOException {
        if (admin.tableExists(TableName.valueOf(tableName))) {
            admin.disableTable(TableName.valueOf(tableName));
            admin.deleteTable(TableName.valueOf(tableName));
            System.out.println("表删除成功");

        } else {
            System.out.println("table is not exist");
        }
    }

    /*
     //增加列族
  **/
    public static void insertComnFaimly(String tableName, String familyNames[]) throws IOException {

        for (String familyName : familyNames) {
            //创建列族描述器(通过 of 转化为列族名)
            ColumnFamilyDescriptor of = ColumnFamilyDescriptorBuilder.of(familyName);
            //添加列族
            admin.addColumnFamily(TableName.valueOf(tableName), of);
        }

        System.out.println("insert  ok ");
    }

    /*
       插入数据
    **/
    public static void insertData(String tableName, String rowkey,String coumnFamily,String coumn,String value) throws IOException {
       //创建表连接对象
        Table table = connection.getTable(TableName.valueOf(tableName));
        //创建put对象
        Put put = new Put(Bytes.toBytes(rowkey));
        //添加属性
        put.addColumn(Bytes.toBytes(coumnFamily), Bytes.toBytes(coumn), Bytes.toBytes(value));
         //添加数据到表
        table.put(put);
        System.out.println("insert data  ok ");
        table.close();
        connection.close();
    }


}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值