hbase,部分 Java API 操作

2021-03-09  本文已影响0人  mayuee

package com.mzb.hbase;

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 HbaseOperate {

public static void main(String[] args) throws IOException {


    listTableNames();
    createTable("student","info");
    createTable("studentTest","base_info","extra_info");
    listTableNames();

    System.out.println("-------------------------------------");
    deleteTable("studentTest");
    listTableNames();

    scanTable("student");

    System.out.println("-------------------------------------");
    System.out.println("插入数据-----------");
    putData("student", "1001", "info", "name", "王大锤");
    putData("student", "1001", "info", "age", "29");
    putData("student", "1001", "info", "class", "一班");
    putData("student", "1001", "info", "grade", "三年级");
    putData("student", "1002", "info", "name", "李大锯");
    putData("student", "1003", "info", "age", "16");
    putData("student", "1003", "info", "class", "二班");

    scanTable("student");

    System.out.println("-------------------------------------");
    System.out.println("获取rowKey为1001的学生数据--------");
    getRowData("student", "1001");

    System.out.println("-------------------------------------");
    System.out.println("查询 rowKey为1001的学生的年级------");
    getRowColumnData("student", "1001", "info", "grade");


    System.out.println("-------------------------------------");
    System.out.println("修改数据------------");
    putData("student", "1001", "info", "name", "赵大钢");
    putData("student", "1003", "info", "age", "10086");
    scanTable("student");

    System.out.println("-------------------------------------");
    System.out.println("删除数据------------");
    deleteTable("student", "1003", "1002");
    deleteQualifierTable("student", "1001", "info", "age");
    scanTable("student");

    close(connection, admin);
}

//获取Configuration对象
public static Configuration configuration = null;
public static Connection connection = null;
public static Admin admin = null;

static {
    configuration = HBaseConfiguration.create();
    configuration.set("hbase.zookeeper.quorum", "hadoop01:2181,hadoop02:2181,hadoop03:2181,hadoop04:2181");

    try {
        connection = ConnectionFactory.createConnection(configuration);
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        admin = connection.getAdmin();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

//判断表是否存在
public static boolean tableIsExists(String tableName) throws IOException {

    boolean isExists = admin.tableExists(TableName.valueOf(tableName));

    if (isExists){

// System.out.println("表"+tableName+"存在!");
}else {
System.out.println("表"+tableName+"不存在!");

    }

    return isExists;
}

//打印所有表名
public static void listTableNames() throws IOException {
    TableName[] tableNames = admin.listTableNames();
    System.out.println("打印所有表名:");
    for (TableName tableName : tableNames) {
        System.out.println(tableName.toString());
    }
    System.out.println();
}

//1、创建表
public static void createTable(String tableName,String... columnFamily) throws IOException {
    if (tableIsExists(tableName)){
        return;
    }

    //创建表描述器
    HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));

    //添加列簇
    for (String s : columnFamily) {
        //创建列描述器
        HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(s);

// hColumnDescriptor.setVersions()
hTableDescriptor.addFamily(hColumnDescriptor);
}

    //创建表
    admin.createTable(hTableDescriptor);
    System.out.println("表"+tableName+"创建成功!");

}

//2、Put:增加或更新一条数据
public static void putData(String tableName, String rowKey, String columnFamily, String column, String value) throws IOException {
    if (tableIsExists(tableName)) {
        //获取表对象
        Table table = connection.getTable(TableName.valueOf(tableName));

        //创建put对象
        Put put = new Put(Bytes.toBytes(rowKey));
        //添加数据
        put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));

        //执行添加操作
        table.put(put);
    }
}

//3、删除表
public static void deleteTable(String tableName) throws IOException {
    if (tableIsExists(tableName)){
        TableName tn = TableName.valueOf(tableName);
        //删除表之前要先disable表(下线)
        admin.disableTable(tn);
        //删除表
        admin.deleteTable(tn);
        System.out.println("表"+tableName+"已删除!");
    }
}
//4、Delete:删除一条数据
public static void deleteQualifierTable(String tableName, String rowKey, String columnFamily, String column) throws IOException {
    if (tableIsExists(tableName)){
        //获取表对象
        Table table = connection.getTable(TableName.valueOf(tableName));

        //创建delete对象
        Delete delete = new Delete(Bytes.toBytes(rowKey));
        //添加数据
        delete.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column));
        delete.addColumns(Bytes.toBytes(columnFamily), Bytes.toBytes(column));

        //执行添加操作
        table.delete(delete);
    }
}
//5、删除单行或多行数据
public static void deleteTable(String tableName, String... rowKeys) throws IOException {
    if (tableIsExists(tableName)){
        //获取表对象
        Table table = connection.getTable(TableName.valueOf(tableName));

        List<Delete> list =  new ArrayList<>();
        for (String rowKey : rowKeys) {
            //创建delete对象
            Delete delete = new Delete(Bytes.toBytes(rowKey));
            list.add(delete);
        }
        //执行删除操作
        table.delete(list);
    }
}

//6、全表扫描
public static void scanTable(String tableName) throws IOException {
    if (tableIsExists(tableName)){
        //获取表对象
        Table table = connection.getTable(TableName.valueOf(tableName));

        //得到用于扫描region的对象scan
        //Scan: 封装查询信息,很get有一点不同,Scan可以设置Filter
        Scan scan = new Scan();

// scan.setStartRow();
// scan.setStopRow();
//使用HTable得到resultcanner实现类的对象
ResultScanner resultScanner = table.getScanner(scan);
for(Result result : resultScanner){
//Cell:封装了Column的所有的信息:Rowkey、column qualifier、value、时间戳
Cell[] cells = result.rawCells();
for(Cell cell : cells){
printRowKeyAndCell(Bytes.toString(result.getRow()), cell);
}
}
}
}

//7、Get:查看 获取指定行
public static void getRowData(String tableName, String rowKey) throws IOException {
    if (tableIsExists(tableName)){
        //获取表对象
        Table table = connection.getTable(TableName.valueOf(tableName));

        //创建Get对象
        Get get = new Get(Bytes.toBytes(rowKey));

        //执行Get操作
        Result result = table.get(get);

        Cell[] cells = result.rawCells();
        for(Cell cell : cells){
            printRowKeyAndCell(Bytes.toString(result.getRow()), cell);
        }
    }
}

//8、获取某行指定的数据,比如指定某个列簇的某个列限定符
public static void getRowColumnData(String tableName, String rowKey, String columnFamily, String column) throws IOException{
    if (tableIsExists(tableName)){
        //获取表对象
        Table table = connection.getTable(TableName.valueOf(tableName));
        Get get = new Get(Bytes.toBytes(rowKey));
        get.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column));
        Result result = table.get(get);
        //循环获取所有信息,也可以单独打印自己需要的字段即可,这个一般根据业务需求修改。
        Cell[] cells = result.rawCells();
        for(Cell cell : cells){
            printRowKeyAndCell(Bytes.toString(result.getRow()), cell);
        }
    }
}

public static void printRowKeyAndCell(String rowKey, Cell cell){
    System.out.println("RK: " + rowKey
            + " CF: " + Bytes.toString(CellUtil.cloneFamily(cell))
            + " CN: " + Bytes.toString(CellUtil.cloneQualifier(cell))
            + " V: " + Bytes.toString(CellUtil.cloneValue(cell))
            + " T: " + cell.getTimestamp());
}


private static void close(Connection connection, Admin admin) throws IOException {
    if (connection != null){
        connection.close();
    }

    if (admin != null){
        admin.close();
    }
}

}

上一篇 下一篇

猜你喜欢

热点阅读