Hbase学习(三)---hbase的API使用

版权声明:欢迎读者转载,如果有问题请给与评论。 https://blog.csdn.net/qq_41848006/article/details/86932687
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class  test{
    private static Configuration conf;
static {
        conf = HBaseConfiguration.create();
}

    //判断HBASE库里是否存在某表
private static  boolean isExist(String tablename) throws IOException {
    Connection connection = ConnectionFactory.createConnection(conf);
    HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
    return  admin.tableExists("tiger");
}
//创建表create 'table','ta','tb'
    public static  void createTable(String tablename,String... columnFamilly) throws IOException {
        Connection connection = ConnectionFactory.createConnection(conf);
        HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
        if(isExist(tablename)) {
            System.out.println("该表已经存在,请创建不一样的表名!");
        }
        else {
            HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tablename));
            for (String str : columnFamilly) {
                htd.addFamily(new HColumnDescriptor(str));
            }
            admin.createTable(htd);
            System.out.println(tablename + "表创建成功!");
        }
    }
    //删除HBASE中的表
    public  static  void delectTabel(String tablename) throws IOException {
        Connection connection = ConnectionFactory.createConnection(conf);
        HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
        if(isExist(tablename)){
            admin.disableTable(tablename);
            admin.deleteTable(tablename);
        }else {
            System.out.println("该表不存在!");
        }
    }
    //添加数据put 'tiger','cwx','cwxcwx'
    /*1.根据string类型的tablename得到一个table表
      2.根据rowkey进行数据的添加
     */
    public  static void putData(String tablename,String rowkey,String col,String coll,String value) throws IOException {
        Connection connection = ConnectionFactory.createConnection(conf);
        Table table = connection.getTable(TableName.valueOf(tablename));
        //用put的方式加入数据
        Put put = new Put(Bytes.toBytes(rowkey));
        //添加数据
        put.addColumn(Bytes.toBytes(col),Bytes.toBytes(coll),Bytes.toBytes(value));
        table.put(put);
    }
    //删除表中的一行数据
    public static  void deleteTable(String tablename,String rowkey) throws IOException {
        Connection connection = ConnectionFactory.createConnection(conf);
        Table table = connection.getTable(TableName.valueOf(tablename));
        Delete delete = new Delete(Bytes.toBytes(rowkey));
        table.delete(delete);
    }
   //删除表中的多行
   public  static  void deletemuchcol(String tablename,String... rowkey) throws IOException {
       Connection connection = ConnectionFactory.createConnection(conf);
       Table table = connection.getTable(TableName.valueOf(tablename));
       List<Delete>list=new ArrayList<Delete>();
       for (String k:rowkey){
           Delete delete = new Delete(Bytes.toBytes(k));
           list.add(delete);
       }
        table.delete(list);
   }
    //扫描全表
    public static  void scan(String tablename) throws IOException {
        Connection connection = ConnectionFactory.createConnection(conf);
        Table table = connection.getTable(TableName.valueOf(tablename));
        Scan scan = new Scan();
        ResultScanner scanner = table.getScanner(scan);
        for(Result res:scanner){
            Cell[] cells = res.rawCells();
            for(Cell c:cells){
                System.out.println("行键为:" + Bytes.toString(CellUtil.cloneRow(c)));
                System.out.println("列族为:" + Bytes.toString(CellUtil.cloneFamily(c)));
                System.out.println("值为:" + Bytes.toString(CellUtil.cloneValue(c)));
            }
        }
    }
    public static void getRow(String tablename,String rowkey) throws IOException {
        Connection connection = ConnectionFactory.createConnection(conf);
        Table table = connection.getTable(TableName.valueOf(tablename));
        //1.扫描指定数据需要get实例
        Get get = new Get(Bytes.toBytes(rowkey));
        get.addFamily(Bytes.toBytes("col"));
        Result result = table.get(get);
        Cell[] cells = result.rawCells();
        for(Cell c:cells){
            System.out.println("行键为:" + Bytes.toString(CellUtil.cloneRow(c)));
            System.out.println("列族为:" + Bytes.toString(CellUtil.cloneFamily(c)));
            System.out.println("值为:"+Bytes.toString(CellUtil.cloneValue(c)));
        }
    }
public static void main(String[]args) throws IOException {
    //System.out.println(isExist("tiger"));
    //createTable("test1156454","cwx","cwxcwx");
    //delectTabel("tiger");
    //putData("tiger","01","col","name","cwx");
    //deleteTabel("tiger");
    //deletemuchcol("tiget","01","02");
    // scan("tiger");
    getRow("tiger","2");



}
}

猜你喜欢

转载自blog.csdn.net/qq_41848006/article/details/86932687