当前位置: 代码迷 >> 综合 >> HBase idea Java客户端操作
  详细解决方案

HBase idea Java客户端操作

热度:35   发布时间:2023-11-21 13:48:01.0

创建Maven项目
在这里插入图片描述
pom.xml

    <dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-client</artifactId><version>1.1.2</version></dependency><dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-common</artifactId><version>1.1.2</version></dependency>

在test文件夹下创建HBaseTest.java
在这里插入图片描述
配置连接

	Configuration conf = null;private Connection connection = null;private Table table = null;// 表管理员private Admin admin = null;@Beforepublic void init() throws IOException {
    conf = HBaseConfiguration.create();conf.set("hbase.zookeeper.quorum","192.168.133.151");conf.set("hbase.zookeeper.property.clientPort","2181");connection = ConnectionFactory.createConnection(conf);admin = connection.getAdmin();}

关闭连接

    @Afterpublic void close() throws IOException {
    if (table!=null){
    table.close();}if (connection!=null){
    connection.close();}}

测试连接

@Testpublic void test1(){
    System.out.println(connection);System.out.println(admin);}

创建表空间/删除表空间

    @Testpublic void createNameSpace(){
    NamespaceDescriptor mihaoyu= NamespaceDescriptor.create("mihaoyu").build();try {
    admin.createNamespace(mihaoyu);
// admin.deleteNamespace("mihaoyu");} catch (IOException e) {
    e.printStackTrace();}}

创建表

    @Testpublic void createTable() throws IOException {
    TableName tableName = TableName.valueOf("mihaoyu:student");HTableDescriptor desc = new HTableDescriptor(tableName);HColumnDescriptor family1 = new HColumnDescriptor("info1");HColumnDescriptor family2 = new HColumnDescriptor("info2");desc.addFamily(family1);desc.addFamily(family2);admin.createTable(desc);}

删除表

    @Testpublic void deleteTable() throws IOException {
    TableName tableName = TableName.valueOf("mihaoyu:student");admin.disableTable(tableName);admin.deleteTable(tableName);}

插入数据

    @Testpublic void insertData() throws IOException {
    table = connection.getTable(TableName.valueOf("mihaoyu:student"));// Put rowkey1 = new Put(Bytes.toBytes("rowkey1"));
 rowkey1.add(Bytes.toBytes("info1"),Bytes.toBytes("name"),Bytes.toBytes("zs")); 过时
//
// rowkey1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("name"),Bytes.toBytes("zs"));
// rowkey1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("age"),Bytes.toBytes("zs"));
// rowkey1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("gender"),Bytes.toBytes("zs"));
// rowkey1.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("name"),Bytes.toBytes("andemen"));
//
// table.put(rowkey1);List<Put> list = new ArrayList<>();Put put1 = new Put(Bytes.toBytes("rowkey2"));put1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("name"),Bytes.toBytes("zs1"));put1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("age"),Bytes.toBytes("zs1"));put1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("gender"),Bytes.toBytes("zs1"));put1.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("name"),Bytes.toBytes("andemen1"));Put put2 = new Put(Bytes.toBytes("rowkey3"));put2.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("name"),Bytes.toBytes("zs2"));put2.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("age"),Bytes.toBytes("zs2"));put2.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("gender"),Bytes.toBytes("zs2"));put2.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("name"),Bytes.toBytes("andemen2"));list.add(put1);list.add(put2);table.put(list);}

删除表数据

    @Testpublic void delete() throws IOException {
    table = connection.getTable(TableName.valueOf("mihaoyu:student"));// 删除一行 rowkey1全部删除Delete rowkey1 = new Delete(Bytes.toBytes("rowkey1"));table.delete(rowkey1);// 删除 rowkey2 的info2列族Delete rowkey2 = new Delete(Bytes.toBytes("rowkey2"));rowkey2.addFamily(Bytes.toBytes("info2"));table.delete(rowkey2);// 删除 rowkey3 列族为 info1 下的 gender列Delete rowkey3 = new Delete(Bytes.toBytes("rowkey3"));rowkey3.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("gender"));table.delete(rowkey3);}

查询数据

    @Testpublic void getData() throws IOException {
    table = connection.getTable(TableName.valueOf("kb15:student"));Get get = new Get(Bytes.toBytes("rowkey1"));
// get.addFamily(Bytes.toBytes("baseInfo"));Result result = table.get(get);byte[] name = result.getValue(Bytes.toBytes("baseInfo"), Bytes.toBytes("name"));byte[] age = result.getValue(Bytes.toBytes("baseInfo"), Bytes.toBytes("age"));System.out.println(Bytes.toString(name));System.out.println(Integer.valueOf(Bytes.toString(age)));}

全表扫描

    @Testpublic void scanData() throws IOException {
    table = connection.getTable(TableName.valueOf("kb15:student"));Scan scan = new Scan();scan.addColumn(Bytes.toBytes("baseInfo"),Bytes.toBytes("age"));scan.addColumn(Bytes.toBytes("baseInfo"),Bytes.toBytes("name"));scan.setStartRow(Bytes.toBytes("rowkey1"));scan.setStopRow(Bytes.toBytes("rowkey3"));ResultScanner scanner = table.getScanner(scan);for (Result result :scanner) {
    byte[] name = result.getValue(Bytes.toBytes("baseInfo"), Bytes.toBytes("name"));byte[] age = result.getValue(Bytes.toBytes("baseInfo"), Bytes.toBytes("age"));byte[] row = result.getRow();System.out.println(Bytes.toString(name));System.out.println(Bytes.toString(age));System.out.println(Bytes.toString(row));}}

过滤查询

    @Testpublic void getFilterRowKey() throws IOException {
    table = connection.getTable(TableName.valueOf("kb15:student"));Scan scan = new Scan();FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes("baseInfo")));scan.setFilter(familyFilter);ResultScanner scanner = table.getScanner(scan);for (Result result :scanner) {
    byte[] name = result.getValue(Bytes.toBytes("baseInfo"), Bytes.toBytes("name"));byte[] age = result.getValue(Bytes.toBytes("baseInfo"), Bytes.toBytes("age"));byte[] row = result.getRow();System.out.println(Bytes.toString(name));System.out.println(Bytes.toString(age));System.out.println(Bytes.toString(row));}}

全部代码

package nj.zb.kb15;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.FamilyFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;public class HBaseTest {
    Configuration conf = null;private Connection connection = null;private Table table = null;// 表管理员private Admin admin = null;@Beforepublic void init() throws IOException {
    conf = HBaseConfiguration.create();conf.set("hbase.zookeeper.quorum","192.168.133.151");conf.set("hbase.zookeeper.property.clientPort","2181");connection = ConnectionFactory.createConnection(conf);admin = connection.getAdmin();}@Testpublic void test1(){
    System.out.println(connection);System.out.println(admin);}@Testpublic void createNameSpace(){
    NamespaceDescriptor mihaoyu = NamespaceDescriptor.create("mihaoyu").build();try {
    admin.createNamespace(mihaoyu);
// admin.deleteNamespace("mihaoyu");} catch (IOException e) {
    e.printStackTrace();}}@Testpublic void createTable() throws IOException {
    TableName tableName = TableName.valueOf("mihaoyu:student");HTableDescriptor desc = new HTableDescriptor(tableName);HColumnDescriptor family1 = new HColumnDescriptor("info1");HColumnDescriptor family2 = new HColumnDescriptor("info2");desc.addFamily(family1);desc.addFamily(family2);admin.createTable(desc);}@Testpublic void deleteTable() throws IOException {
    TableName tableName = TableName.valueOf("mihaoyu:student");admin.disableTable(tableName);admin.deleteTable(tableName);}@Testpublic void insertData() throws IOException {
    table = connection.getTable(TableName.valueOf("mihaoyu:student"));// Put rowkey1 = new Put(Bytes.toBytes("rowkey1"));
 rowkey1.add(Bytes.toBytes("info1"),Bytes.toBytes("name"),Bytes.toBytes("zs")); 过时
//
// rowkey1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("name"),Bytes.toBytes("zs"));
// rowkey1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("age"),Bytes.toBytes("zs"));
// rowkey1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("gender"),Bytes.toBytes("zs"));
// rowkey1.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("name"),Bytes.toBytes("andemen"));
//
// table.put(rowkey1);List<Put> list = new ArrayList<>();Put put1 = new Put(Bytes.toBytes("rowkey2"));put1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("name"),Bytes.toBytes("zs1"));put1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("age"),Bytes.toBytes("zs1"));put1.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("gender"),Bytes.toBytes("zs1"));put1.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("name"),Bytes.toBytes("andemen1"));Put put2 = new Put(Bytes.toBytes("rowkey3"));put2.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("name"),Bytes.toBytes("zs2"));put2.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("age"),Bytes.toBytes("zs2"));put2.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("gender"),Bytes.toBytes("zs2"));put2.addColumn(Bytes.toBytes("info2"),Bytes.toBytes("name"),Bytes.toBytes("andemen2"));list.add(put1);list.add(put2);table.put(list);}@Testpublic void delete() throws IOException {
    table = connection.getTable(TableName.valueOf("mihaoyu:student"));// 删除一行 rowkey1全部删除
// Delete rowkey1 = new Delete(Bytes.toBytes("rowkey1"));
// table.delete(rowkey1);// 删除 rowkey2 的info2列族
// Delete rowkey2 = new Delete(Bytes.toBytes("rowkey2"));
// rowkey2.addFamily(Bytes.toBytes("info2"));
// table.delete(rowkey2);// 删除 rowkey3 列族为 info1 下的 gender 列dDelete rowkey3 = new Delete(Bytes.toBytes("rowkey3"));rowkey3.addColumn(Bytes.toBytes("info1"),Bytes.toBytes("gender"));table.delete(rowkey3);}@Testpublic void getData() throws IOException {
    table = connection.getTable(TableName.valueOf("kb15:student"));Get get = new Get(Bytes.toBytes("rowkey1"));
// get.addFamily(Bytes.toBytes("baseInfo"));Result result = table.get(get);byte[] name = result.getValue(Bytes.toBytes("baseInfo"), Bytes.toBytes("name"));byte[] age = result.getValue(Bytes.toBytes("baseInfo"), Bytes.toBytes("age"));System.out.println(Bytes.toString(name));System.out.println(Integer.valueOf(Bytes.toString(age)));}@Testpublic void scanData() throws IOException {
    table = connection.getTable(TableName.valueOf("kb15:student"));Scan scan = new Scan();scan.addColumn(Bytes.toBytes("baseInfo"),Bytes.toBytes("age"));scan.addColumn(Bytes.toBytes("baseInfo"),Bytes.toBytes("name"));scan.setStartRow(Bytes.toBytes("rowkey1"));scan.setStopRow(Bytes.toBytes("rowkey3"));ResultScanner scanner = table.getScanner(scan);for (Result result :scanner) {
    byte[] name = result.getValue(Bytes.toBytes("baseInfo"), Bytes.toBytes("name"));byte[] age = result.getValue(Bytes.toBytes("baseInfo"), Bytes.toBytes("age"));byte[] row = result.getRow();System.out.println(Bytes.toString(name));System.out.println(Bytes.toString(age));System.out.println(Bytes.toString(row));}}@Testpublic void getFilterRowKey() throws IOException {
    table = connection.getTable(TableName.valueOf("kb15:student"));Scan scan = new Scan();FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes("baseInfo")));scan.setFilter(familyFilter);ResultScanner scanner = table.getScanner(scan);for (Result result :scanner) {
    byte[] name = result.getValue(Bytes.toBytes("baseInfo"), Bytes.toBytes("name"));byte[] age = result.getValue(Bytes.toBytes("baseInfo"), Bytes.toBytes("age"));byte[] row = result.getRow();System.out.println(Bytes.toString(name));System.out.println(Bytes.toString(age));System.out.println(Bytes.toString(row));}}@Afterpublic void close() throws IOException {
    if (table!=null){
    table.close();}if (connection!=null){
    connection.close();}}}
  相关解决方案