美文网首页
hbase,部分 Java API 操作

hbase,部分 Java API 操作

作者: mayuee | 来源:发表于2021-03-09 11:15 被阅读0次

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();
    }
}

}

相关文章

  • HBase的API操作

    HBase的API操作 Java API 1、HBaseConfiguration:封装了hbase集群所有的配置...

  • hbase,部分 Java API 操作

    package com.mzb.hbase; import org.apache.hadoop.conf.Conf...

  • Spark项目案例实战和分布式部署

    前面讲到Hbase的时候可以通过Java API的方式操作Hbase数据库,由于Java和Scala可以互相调用,...

  • Spark&Hbase操作

    HBase 新版 API 进行 CRUD 基本操作配置环境 Hbase基本操作新版 API 中加入了 Connec...

  • hbase java api 的使用

    [TOC] 1 hbase java api的使用 hbase的java api在windows中使用,第一次连接...

  • 四、HBase Java API 操作

    1、Java代码测试是否连接成功连接以及简单的增删改查操作:

  • HBASE操作(2)-JAVA API

    安装 0.安装 Eclipse1.打开 Eclipse,新建 JAVA 项目 2.在项目中点属性,选择JAVA B...

  • HBase Java API 批量操作

    之前我们是这样获取数据的: 那么问题来了,我们想要获取多条数据,比如说查询1万条数据怎么办呢? 可能我们第一时间就...

  • HBase系列4-常用API

    一. Hbase常用API介绍 二. Java API实操 备注:我使用的CDH 6.3.1版本,Hbase版本 ...

  • HBase客户端API-Batch操作

    上一篇博客说了使用 HBase 的客户端 API 来操作操作 HBase 表中记录,今天我们看看怎样通过 API ...

网友评论

      本文标题:hbase,部分 Java API 操作

      本文链接:https://www.haomeiwen.com/subject/yoivqltx.html