Java Api 操作 Hbase

1.需求

主要实践建表、插入数据、删除数据、查询等功能。要求建立一个如下所示的表:

  • 表名:$your_name:student
  • 空白处自行填写, 姓名学号一律填写真实姓名和学号
    在这里插入图片描述

2.思路

2.1 建立远程Hbase集群连接 – HbaseInit.java

package com.jike.bigdata;
 
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
 
import java.io.IOException;
 
/**
 * @author: fz
 * @create: 2021-08-06 23:45
 * Hbase客户端的初始化
 **/
public class HbaseInit {
    public static Connection createConnection() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "jikehadoop01,jikehadoop02,jikehadoop03");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        return ConnectionFactory.createConnection(conf);
    }
}

2.2 创建表结构–CreateTable.java

package com.jike.bigdata;
 
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
import org.apache.hadoop.hbase.regionserver.BloomType;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import java.io.IOException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
/**
 * @author: fz
 * @create: 2021-08-06 23:49
 * 创建表
 **/
public class CreateTable {
    public static void create(Connection connection, String tableName, String... columnFamilies) throws IOException {
        Admin admin = connection.getAdmin();
        if (tableName == null || columnFamilies == null) {
            return;
        }
        HTableDescriptor table = new HTableDescriptor(TableName.valueOf(tableName));
        for (int i = 0; i < columnFamilies.length; i++) {
            if (columnFamilies[i] == null)
                continue;
            HColumnDescriptor columnDescriptor = new HColumnDescriptor(columnFamilies[i]);
            columnDescriptor.setMaxVersions(1);
            table.addFamily(columnDescriptor);
        }
        admin.createTable(table);
        System.out.println("成功创建表 " + table + ", column family: " + Arrays.toString(columnFamilies));
    }
}
 
 
//创建表空间 TableNameSpace:TableName--> xiaoyuer:student
public class CreateNameSpace {
    public static void createNamespace(Connection connection, String tablespace) throws IOException {
        HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
        admin.createNamespace(NamespaceDescriptor.create(tablespace).build());
        System.out.println("成功创建表空间 " + tablespace);
    }
}

2.3 插入数据 – PutRow.java

package com.jike.bigdata;
 
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
 
import java.io.IOException;
 
/**
 * @author: fz
 * @create: 2021-08-06 23:58
 * 插入行数据
 **/
public class PutRow {
    public static void insert(Connection connection, String tableName, String rowKey, String columnFamily, String column,
                              String value) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        Put put = new Put(rowKey.getBytes());
        put.addColumn(columnFamily.getBytes(), column.getBytes(), value.getBytes());
        table.put(put);
    }
}

2.4 查询浏览数据–ScanTable.java

package com.jike.bigdata;
 
import org.apache.hadoop.hbase.Cell;
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.util.List;
 
/**
 * @author: fz
 * @create: 2021-08-06 00:09
 * 打印表所有数据
 **/
public class ScanTable {
    public static void scan(Connection connection, String tableName) throws IOException {
        //Admin admin = connection.getAdmin();
        Table table = connection.getTable(TableName.valueOf(tableName));
        Scan scan = new Scan();
        ResultScanner scanner = table.getScanner(scan);
        Result tmp;
        System.out.println("Row\t\t\tColumn\tvalue");
        while ((tmp = scanner.next()) != null) {
            List<Cell> cells = tmp.listCells();
            for (Cell cell : cells) {
                String rk = Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
                String cf = Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
                String column = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
                String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
                System.out.println(rk + "\t\tcolumn:" + cf + ":" + column + ",value=" + value);
            }
        }
    }
}

2.5 删除数据–DeleteRow.java

package com.jike.bigdata;
 
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
 
import java.io.IOException;
 
/**
 * @author: fz
 * @create: 2021-08-06 00:34
 **/
public class DeleteRow {
 
    public static boolean deleteRow(Connection connection, String tableName, String rowkey) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        Delete delete = new Delete(Bytes.toBytes(rowkey));
        table.delete(delete);
        System.out.println("成功删除 " + tableName + " " + rowkey);
        return true;
    }
}

2.6 删除表–DropTable.java

package com.jike.bigdata;
 
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
 
import java.io.IOException;
 
/**
 * @author: fz
 * @create: 2021-08-06 00:11
 **/
public class DropTable {
    public static void drop(Connection connection, String tableName) throws IOException {
        Admin admin = connection.getAdmin();
        if (admin.isTableDisabled(TableName.valueOf(tableName))) {
            System.out.println("table不存在");
            return;
        }
        if (!admin.isTableDisabled(TableName.valueOf(tableName))) {
            admin.disableTable(TableName.valueOf(tableName));
        }
        admin.deleteTable(TableName.valueOf(tableName));
        System.out.println("成功删除表 " + tableName);
    }
}

3.测试并查看执行结果

3.1 创建表 xiaoyuer:student

package com.jike.bigdata;
 
import org.apache.hadoop.hbase.client.Connection;
import org.junit.Test;
 
import java.io.IOException;
 
public class CreateNameSpaceTest {
    @Test
    public void create() {
        try (Connection connection = HbaseInit.createConnection()) {
            CreateNameSpace.createNamespace(connection,"xiaoyuer");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
package com.jike.bigdata;
 
import org.apache.hadoop.hbase.client.Connection;
import org.junit.Test;
import java.io.IOException;
 
public class CreateTableTest {
 
    @Test
    public void create() {
        try (Connection connection = HbaseInit.createConnection()) {
            CreateTable.create(connection,"xiaoyuer:student", "name", "info", "score");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

3.2 插入数据

package com.jike.bigdata;
 
import org.apache.hadoop.hbase.client.Connection;
import org.junit.Test;
 
import java.io.IOException;
 
public class PutRowTest {
 
    @Test
    public void insert() {
        try (Connection connection = HbaseInit.createConnection()) {
            PutRow.insert(connection, "xiaoyuer:student", "row1", "name", "", "Tom");
            PutRow.insert(connection, "xiaoyuer:student", "row1", "info", "student_id", "20210000000001");
            PutRow.insert(connection, "xiaoyuer:student", "row1", "info", "class", "1");
            PutRow.insert(connection, "xiaoyuer:student", "row1", "score", "understanding", "75");
            PutRow.insert(connection, "xiaoyuer:student", "row1", "score", "programming", "82");
 
            PutRow.insert(connection, "xiaoyuer:student", "row2", "name", "", "Jerry");
            PutRow.insert(connection, "xiaoyuer:student", "row2", "info", "student_id", "20210000000002");
            PutRow.insert(connection, "xiaoyuer:student", "row2", "info", "class", "1");
            PutRow.insert(connection, "xiaoyuer:student", "row2", "score", "understanding", "85");
            PutRow.insert(connection, "xiaoyuer:student", "row2", "score", "programming", "67");
 
 
            PutRow.insert(connection, "xiaoyuer:student", "row3", "name", "", "Jack");
            PutRow.insert(connection, "xiaoyuer:student", "row3", "info", "student_id", "20210000000003");
            PutRow.insert(connection, "xiaoyuer:student", "row3", "info", "class", "2");
            PutRow.insert(connection, "xiaoyuer:student", "row3", "score", "understanding", "80");
            PutRow.insert(connection, "xiaoyuer:student", "row3", "score", "programming", "80");
 
            PutRow.insert(connection, "xiaoyuer:student", "row4", "name", "", "Rose");
            PutRow.insert(connection, "xiaoyuer:student", "row4", "info", "student_id", "20210000000004");
            PutRow.insert(connection, "xiaoyuer:student", "row4", "info", "class", "2");
            PutRow.insert(connection, "xiaoyuer:student", "row4", "score", "understanding", "60");
            PutRow.insert(connection, "xiaoyuer:student", "row4", "score", "programming", "61");
 
            PutRow.insert(connection, "xiaoyuer:student", "row5", "name", "", "FuZhuang");
            PutRow.insert(connection, "xiaoyuer:student", "row5", "info", "student_id", "G20210735010249");
            PutRow.insert(connection, "xiaoyuer:student", "row5", "info", "class", "3");
            PutRow.insert(connection, "xiaoyuer:student", "row5", "score", "understanding", "70");
            PutRow.insert(connection, "xiaoyuer:student", "row5", "score", "programming", "60");
 
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

3.3 浏览数据

package com.jike.bigdata;
 
import org.apache.hadoop.hbase.client.Connection;
import org.junit.Test;
 
public class ScanTableTest {
 
    @Test
    public void scan() {
        try (Connection connection = HbaseInit.createConnection()){
            ScanTable.scan(connection,"xiaoyuer:student");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 执行结果

3.4 删除某一行数据

package com.jike.bigdata;
 
import org.apache.hadoop.hbase.client.Connection;
import org.junit.Test;
 
import java.io.IOException;
 
public class DeleteRowTest {
 
    @Test
    public void deleteRow() {
        try (Connection connection = HbaseInit.createConnection()){
            DeleteRow.deleteRow(connection, "xiaoyuer:student", "row1");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

3.5 获取某一行数据

package com.jike.bigdata;
 
import org.apache.hadoop.hbase.client.Connection;
import org.junit.Test;
 
public class GetRowTest {
 
    @Test
    public void printRowValue() {
        try (Connection connection = HbaseInit.createConnection()) {
            GetRow.printRowValue(connection, "xiaoyuer:student", "row2", "name");
            GetRow.printRowValue(connection, "xiaoyuer:student", "row2", "info", "class");
            GetRow.printRowValue(connection, "xiaoyuer:student", "row2", "info", "student_id");
            GetRow.printRowValue(connection, "xiaoyuer:student", "row2", "score", "programming");
            GetRow.printRowValue(connection, "xiaoyuer:student", "row2", "score", "understanding");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

3.6 删除表 xiaoyuer:student

package com.jike.bigdata;
 
import org.apache.hadoop.hbase.client.Connection;
import org.junit.Test;
 
import java.io.IOException;
 
public class DropTableTest {
 
    @Test
    public void drop() {
        try (Connection connection = HbaseInit.createConnection()) {
            DropTable.drop(connection, "xiaoyuer:student");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

4.参考资料

1.https://github.com/DeanVincent/boldness

2.http://c.biancheng.net/view/3599.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Java API操作HBase非常简单和方便。HBase提供了一个Java库,可以使用它来连接和与HBase进行交互。下面是使用Java API操作HBase的步骤: 1. 首先,需要导入HBaseJava库。可以在项目的构建文件(例如pom.xml)中添加HBase相关依赖项,或者手动将HBase库添加到项目的类路径中。 2. 创建HBase的配置对象,并设置必要的配置参数。配置对象可以指定HBase的连接地址、端口号等信息。 3. 使用HBase的配置对象创建一个HBase的连接对象。连接对象允许与HBase进行通信。 4. 通过连接对象创建一个HBase的管理员对象。管理员对象用于对HBase的表进行管理,如创建表、删除表等操作。 5. 创建HBase表的描述符对象,并指定表的名称、列族等信息。 6. 使用管理员对象创建HBase表。可以使用表的描述符对象来定义表的结构。 7. 使用HBase表的描述符对象创建一个表对象。表对象用于与HBase的表进行交互。 8. 使用表对象执行各种操作,如插入数据、更新数据、删除数据等。可以使用行键(row key)和列族名(column family)来定位和操作特定的数据。 9. 关闭与HBase的连接,释放资源。 通过以上步骤,可以使用Java API来连接和操作HBase。在实际应用中,还可以根据具体需求来添加其他操作,如查询数据、扫描表等。使用Java API操作HBase可以灵活地控制和管理HBase中的数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值