HBase 单机版安装及使用,并用Java远程连接。

时间:2022-12-08 19:58:33


1.下载解压安装文件 到新建目录/home/HBase

wget http://mirror.bit.edu.cn/apache/hbase/stable/hbase-0.94.16.tar.gz

tar xvfz hbase-0.94.16.tar.gz

2.启动HBase

/home/HBase/hbase-1.2.6/bin/start-hbase.sh

如果成功启动会有如下提示信息:
starting master, logging to /usr/hbase-0.94.16/bin/…/logs/hbase-root-master-localhost.localdomain.out

关闭命令:/home/HBase/hbase-1.2.6/bin/stop-hbase.sh

4.使用HBase Shell命令行

可以通过HBase Shell和HBase交互

/home/HBase/hbase-1.2.6/bin/hbase

/home/HBase/hbase-1.2.6/bin/hbase shell

这说明Java和HBase函数库已经安装成功,列出HBase中的所有表可以使用list命令。

6.使用REST访问HBase
启动REST服务

/home/HBase/hbase-1.2.6/bin/hbase rest

启动REST服务,监听9999端口

/home/HBase/hbase-1.2.6/bin/hbase rest start -p 9999

在浏览器以及可以访问HBase的REST了
​​​ http://localhost:9999/​

​http://localhost:9999/version​

10、退出HBase Shell。要退出HBase Shell并从群集断开连接,请使用以下quit命令。HBase仍在后台运行。
命令:quit

配置环境变量,jdk已安装的情况下:

查看jdk安装路径
which java

打开配置文件目录:
cd /home/HBase/hbase-1.2.6/conf

编辑环境变量
vim hbase-env.sh
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.262.b10-0.el7_8.x86_64/jre

查看Hbase 版本:hbase shell ,Hbase-shell

Java连接Hbase:

package com.hessianhealth.common.core.HBase;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.FamilyFilter;
import org.apache.hadoop.hbase.util.Bytes;
/**
* Created by liangguannan on 2020/10/27.
*/
public class HbseApiDemo11 {


public static Configuration conf;

static {
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "192.168.8.144:2181");
}

/**
* 判断是表是否存在
*
* @param tableName
* @return
* @throws MasterNotRunningException
* @throws ZooKeeperConnectionException
* @throws IOException
*/
public static boolean isTableExist(String tableName)
throws MasterNotRunningException, ZooKeeperConnectionException, IOException {

// 在 HBase 中管理、访问表需要先创建 HBaseAdmin 对象
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
return admin.tableExists(TableName.valueOf(tableName));
}

/**
* create table
*
* @param tableName
* @param columnFamily
* @throws MasterNotRunningException
* @throws ZooKeeperConnectionException
* @throws IOException
*/
public static void createTable(String tableName, String... columnFamily) throws IOException {
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
// 判断表是否存在
if (isTableExist(tableName)) {
System.out.println("表" + tableName + "已存在");
// System.exit(0);
} else {
// 创建表属性对象,表名需要转字节
HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
// 创建多个列族
for (String cf : columnFamily) {
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
hColumnDescriptor.setMinVersions(1);
hColumnDescriptor.setMaxVersions(1);
descriptor.addFamily(hColumnDescriptor);
}
// 根据对表的配置,创建表
admin.createTable(descriptor);
System.out.println("表" + tableName + "创建成功!");

}
}

/**
* 删除表
* @param tableName
* @throws IOException
*/
public static void dropTable(String tableName) throws IOException {
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
// 判断表是否存在
if (isTableExist(tableName)) {
// delete this table
admin.disableTable(TableName.valueOf(tableName));
admin.deleteTable(TableName.valueOf(tableName));
System.out.println("表" + tableName + "删除成功!");
} else {
System.out.println("表" + tableName + "不存在!");

}
}

/**
* 删除多行数据
* @param tableName
* @param rows
* @throws IOException
*/
public static void deleteMutilRows(String tableName ,String... rowkeys )throws IOException {
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf(tableName));
ArrayList<Delete> deletes = new ArrayList<Delete>();
for (String rowkey : rowkeys)
{
deletes.add(new Delete(Bytes.toBytes(rowkey)));
}
table.delete(deletes);
}

/**
* 删除指定列族的数据
* @param tableName
* @param rows
* @throws IOException
*/
public static void deleteColumnFamily(String tableName ,String rowkey ,String columnFamilys,String column)throws IOException {
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(Bytes.toBytes(rowkey));
delete.addColumn(Bytes.toBytes(columnFamilys),Bytes.toBytes(column));
table.delete(delete);
}





/**
* 向表中插入数据
* @param tableName
* @param rowKey
* @param columnFamily
* @param column
* @param value
* @throws IOException
*/
public static void addRowData(String tableName,String rowkey,String columnFamily ,String colName,String value) throws IOException {

Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf(tableName));
Put put=new Put(Bytes.toBytes(rowkey));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(colName), Bytes.toBytes(value));
table.put(put);

}




/**
* 得到所有的数据
* @param tableName
* @throws IOException
*/
public static void getAllRows(String tableName) throws IOException {

Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan=new Scan();
ResultScanner scanResult = table.getScanner(scan);
for (Result result : scanResult) {
// byte[] row = result.getRow();
Cell[] rawCell = result.rawCells();
for (Cell cell : rawCell)
{
System.err.println("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
System.err.println(Bytes.toString(CellUtil.cloneRow(cell)));
System.err.println(Bytes.toString(CellUtil.cloneFamily(cell)));
System.err.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
System.err.println(Bytes.toString( CellUtil.cloneValue(cell)));
}
}
}
/**
* 得到所有的数据
* @param tableName
* @throws IOException
*/
public static void getByRowkeys(String tableName,String rowkey) throws IOException {

Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf(tableName));

Get get=new Get(Bytes.toBytes(rowkey));
Result result = table.get(get);
Cell[] rawCell = result.rawCells();
for (Cell cell : rawCell)
{
System.err.println("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
System.err.println(Bytes.toString(CellUtil.cloneRow(cell)));
System.err.println(Bytes.toString(CellUtil.cloneFamily(cell)));
System.err.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
System.err.println(Bytes.toString( CellUtil.cloneValue(cell)));
}
}







/**
* test filter
* @param args
* @throws IOException
* 比较器 ByteArrayComparable
通过比较器可以实现多样化目标匹配效果,比较器有以下子类可以使用:
BinaryComparator 匹配完整字节数组
BinaryPrefixComparator 匹配字节数组前缀
BitComparator
NullComparator
RegexStringComparator 正则表达式匹配
SubstringComparator 子串匹配
*/
public static ResultScanner getDataFamilyFilter(String tableName,String family) throws IOException{
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf(tableName));
FamilyFilter ff = new FamilyFilter(CompareOp.EQUAL ,
new BinaryComparator(Bytes.toBytes(family))); //表中不存在account列族,过滤结果为空
// new BinaryPrefixComparator(value) //匹配字节数组前缀
// new RegexStringComparator(expr) // 正则表达式匹配
// new SubstringComparator(substr)// 子字符串匹配
Scan scan = new Scan();
// 通过scan.addFamily(family) 也可以实现此操作
scan.setFilter(ff);
ResultScanner resultScanner = table.getScanner(scan);
return resultScanner;
}





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

boolean tableExist = isTableExist("student");
System.err.println(tableExist);
System.err.println("############################");

System.err.println("#########create staff1 ###################");

createTable("staff1", "info1","info2");
addRowData("staff1","0101","info1","sea","test");
addRowData("staff1","0102","info2","sea1","test1");
addRowData("staff1","0102","info2","name","test1");
addRowData("staff1","0102","info2","addr","test1");
boolean isExitStaff = isTableExist("staff1");
System.err.println("exit table isExitStaff"+isExitStaff);
System.err.println(isExitStaff);
// dropTable("staff1");
System.err.println("############################");

// deleteMutilRows("student","1002","10023");
// deleteColumnFamily("student","10025","info","sex");
//
// addRowData("student","0101","info","sea","test");
getAllRows("staff1");
}

}

运行报错无法打开 127.0.0.1:
关掉所有的zookeeper,使用外部的ZK,导致冲突。单机版会使用自带的zookeeper启动,避免端口被占用,这个地方很容易进坑
查看zk端口是否被占用,被占用的一律关掉,只有在分布式情况下使用外部zk需要配置才可以使用
netstat -antp | fgrep 2181

HBase 单机版安装及使用,并用Java远程连接。

现HBase绑定的是本地IP:127.0.0.1,这当然访问不了

解决:
hotname最好提前设置好,否则远程客户端(API链接)会找不到服务,其内部路径命名全部为hotname方式,这是个大坑

hostname master
vi /etc/hosts
192.168.1.101 master

这里配置的hostname要Linux重启才生效,为了不重启就生效,我们可以执行:hostname master命令,暂时设置hostname

vi /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=master

客户端如果要连接,也需要在host中设置,windows在C:\Windows\System32\drivers\etc\hosts设置
192.168.1.101 master

再次运行,成功:

HBase 单机版安装及使用,并用Java远程连接。