HBase
单机环境搭建Red Hat 4.8.5-36
JDK1.8
HBase2.1.1
1、下载 hbase-2.1.1-bin.tar.gz
2、解压文件 tar -zxvf hbase-2.1.1-bin.tar.gz
3、修改配置文件$HBASE_HOME/conf/hbase-site.xml
添加如下参数
<configuration>
<property>
<name>hbase.rootdirname>
<value>PATHvalue>
property>
configuration>
1、进入$HBASE_HOME/bin
路径下./start-hbase.sh
2、./hbase shell
进入hbase shell
形式,进行命令行操作
Java
连接HBase
单机服务1、因为hbase
默认通过hostname
去找ip
然后将这个ip
注册到zookeeper
中作为hbase
单机服务的ip
地址。所以我们需要修改/etc/hosts
文件。我本地虚拟机环境的hostname
为localhost.localdomain
,原本指向127.0.0.1
,这里修改成虚拟机网卡
ip
地址。并设置本地域名为hbaseserver
。
#127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
#::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.2.52.138 hbaseserver localhost.localdomain
2、在windows
端修改hosts
文件 文件路径: C:\Windows\System32\drivers\etc
添加上这一行:
10.2.52.138 hbaseserver
hbase-site.xml
配置<configuration>
<property>
<name>hbase.zookeeper.property.clientPortname>
<value>2181value>
property>
<property>
<name>hbase.master.info.portname>
<value>16010value>
property>
<property>
<name>hbase.zookeeper.quorumname>
<value>hbaseservervalue>
property>
<property>
<name>hbase.regionserver.portname>
<value>16020value>
property>
<property>
<name>hbase.regionserver.info.portname>
<value>16030value>
property>
configuration>
pom.xml
<dependency>
<groupId>org.apache.hbasegroupId>
<artifactId>hbase-clientartifactId>
<version>2.1.1version>
dependency>
Java
连接代码demo
package com.example.demo.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.log4j.Appender;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggingEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.NavigableMap;
public class HBaseClientDemo {
private HBaseClientDemo() {}
private static Connection connection;
//我这里将异常捕获住,将本来是因为用户配置错误导致的IO异常 改为空指针异常,并打印异常信息 正常这种异常应该抛出
private static void initConnectionInstance() {
if(connection == null) {
synchronized (HBaseClientDemo.class) {
if(connection == null) {
try {
Configuration config = HBaseConfiguration.create();
System.out.println("hbase.regionserver.lease.period: " + config.getLong(HConstants.HBASE_REGIONSERVER_LEASE_PERIOD_KEY,-1));
config.set("hbase.zookeeper.quorum", "hbaseserver");
config.set("hbase.zookeeper.property.clientPort", "2181");
//config.set("hbase.regionserver.dns.nameserver","10.2.52.138");
connection = ConnectionFactory.createConnection(config);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/**
* 创建HTable
* @param tableNameStr
* @param columnFamilies
* @throws Exception
*/
public static void createTable(String tableNameStr, List<String> columnFamilies) throws Exception {
if(tableNameStr == null || tableNameStr.equals("") || columnFamilies ==null || columnFamilies.size() == 0) {
throw new RuntimeException("parameter is empty or illegal");
}
if(connection == null) {
initConnectionInstance();
}
Admin admin = null;
try {
admin = connection.getAdmin();
TableName tableName = TableName.valueOf(tableNameStr);
List<ColumnFamilyDescriptor> familyDescriptors = new ArrayList<>(columnFamilies.size());
columnFamilies.forEach(cf -> {
familyDescriptors.add(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(cf)).build());
});
TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableName)
.setColumnFamilies(familyDescriptors)
.build();
if(admin.tableExists(tableName)) {
return;
}
admin.createTable(tableDescriptor);
} catch(Exception e) {
throw e;
} finally {
admin.close();
}
}
public static void putExample(String tableName, String primaryRow, String columnFamily, String qualify, String value) throws Exception {
Table table = null;
try {
table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(primaryRow));
put.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(qualify),Bytes.toBytes(value));
table.put(put);
} catch(Exception e) {
throw e;
} finally {
if(table != null) {
table.close();
}
}
}
public static void getExample(String tableName) throws Exception {
Table table = null;
try {
table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes("primaryRow1"));
get.addColumn(Bytes.toBytes("testColumnFamily"),Bytes.toBytes("qualityOne"));
Result result = table.get(get);
byte[] value = result.getValue(Bytes.toBytes("testColumnFamily"),Bytes.toBytes("qualityOne"));
System.out.println("value: " + Bytes.toString(value));
NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> resultMap = result.getMap();
System.out.println(result);
} catch(Exception e) {
e.printStackTrace();
} finally {
if(table != null) {
table.close();
}
}
}
public static void deleteExample(String tableName, String rowKey, String columnFamily, String qualify) {
Table table = null;
Delete delete = null;
try {
table = connection.getTable(TableName.valueOf(tableName));
delete = new Delete(Bytes.toBytes(rowKey));
//只删除当前列的最新版本数据
//delete.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(qualify));
//删除当前列全部版本数据
delete.addColumns(Bytes.toBytes(columnFamily),Bytes.toBytes(qualify));
RowMutations mutations = new RowMutations(Bytes.toBytes("primaryRow2"));
mutations.add(delete);
//Mutate 1 successful: false
//checkAndMutate (行键,列族,列分隔) =>比对操作<= (期望值) ====> T mutations F not mutations
//(china mobile 1)<(val1) 为假没有进行更新
boolean res1 = table.checkAndMutate(Bytes.toBytes("primaryRow2"), Bytes.toBytes("testColumnFamily"), Bytes.toBytes("qualityTwo"), CompareFilter.CompareOp.EQUAL, Bytes.toBytes("value"), mutations);
System.out.println("Mutate 1 result: " + res1);
//table.delete(delete);
//Boolean flag = table.checkAndMutate(Bytes.toBytes("primaryRow1"),Bytes.toBytes("testColumnFamily")).qualifier(Bytes.toBytes("qualityTwo")).ifEquals(Bytes.toBytes("value2")).ifMatches(CompareOperator.EQUAL,Bytes.toBytes("")).thenDelete(delete);
//System.out.println("delete result: " + flag);
} catch (Exception e) {
e.printStackTrace();
if(delete != null) {
System.out.println("failed delete: " + delete);
}
} finally {
if(table != null) {
try {
table.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void batch(String tableName) {
//在一个批处理事务中 当table没有调用close方法时,get请求是获取不到put的数据,除非使用getBufferWriter
Table table = null;
String columnFamily = "testColumnFamily";
String primaryKey = "primaryRow3";
String qualify = "qualifyThree";
try {
table = connection.getTable(TableName.valueOf(tableName));
List<Row> batch = new ArrayList<>();
for (int i = 0; i< 200; i++) {
Put put = new Put(Bytes.toBytes(primaryKey+i));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(qualify),Bytes.toBytes("batchValue"+i));
batch.add(put);
}
/*Put put2 = new Put(Bytes.toBytes(primaryKey));
put2.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(qualify),Bytes.toBytes("newBatchValue"));
batch.add(put2);*/
/*Get get = new Get(Bytes.toBytes(primaryKey));
get.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(qualify));
batch.add(get);
Delete delete = new Delete(Bytes.toBytes(primaryKey));
delete.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(qualify));
batch.add(delete);*/
Object[] results = new Object[batch.size()];
table.batch(batch,results);
for (int i = 0; i < results.length; i++) {
System.out.println("result " + i +": " + results[i]);
}
//table.put(put);
} catch(Exception e) {
e.printStackTrace();
} finally {
if(table != null) {
try {
table.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
}
public static void scan(String tableName) {
Logger log = Logger.getLogger("hbase");
final int[] counters = new int[2];
Appender appender = new AppenderSkeleton() {
@Override
protected void append(LoggingEvent loggingEvent) {
String msg = loggingEvent.getMessage().toString();
if(msg != null && msg.contains("Call: next")) {
counters[0]++;
}
}
@Override
public void close() {}
@Override
public boolean requiresLayout() { return false; }
};
log.removeAllAppenders();
log.setAdditivity(false);
log.addAppender(appender);
log.setLevel(Level.DEBUG);
log.info("Call: next");
String family = "testColumnFamily";
Table table = null;
try {
table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
scan.setCaching(10);
scan.setBatch(10);
scan.addFamily(Bytes.toBytes(family));
ResultScanner rs = table.getScanner(scan);
for(Result result : rs) {
counters[1]++;
System.out.println(result);
}
rs.close();
System.out.println("rpcTimes: " + counters[0]);
System.out.println("resultTimes: " + counters[1]);
} catch(Exception e) {
e.printStackTrace();
} finally {
if(table != null) {
try {
table.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws Exception {
String tableNameStr = "testTable";
/*List columnFamilies = new ArrayList(){
{
add("testColumnFamily");
}
};
createTable(tableNameStr,columnFamilies);
;*/
initConnectionInstance();
//putExample(tableNameStr,"primaryRow1","testColumnFamily","qualityOne","value2");
//getExample(tableNameStr);
//deleteExample(tableNameStr,"primaryRow2","testColumnFamily","qualityTwo");
scan(tableNameStr);
}
}
bug
场景与解决方案java.net.ConnectException: Call to localhost/127.0.0.1:16000 failed on connection exception:
org.apache.hbase.thirdparty.io.netty.channel.AbstractChannel$AnnotatedConnectException:
Connection refused: no further information: localhost/127.0.0.1:16000
产生如上报错,是因为没有修改HBase
服务器中hosts
文件,没有将hostname
与网卡ip
对应,解决方案已在环境部署中说明