hbase单节点安装
启动一个单节点的hbase方便我们测试,学习。在官网下载hbase.tar.gz安装包 https://hbase.apache.org/修改配置文件${hbase}/conf/hbase-env.sh,指定jdk位置
【hbase-env.sh】
export JAVA_HOME=/usr/local/jdk
【hbase-site.xml】
指定hbase的data路径,zookeeper的data路径
<configuration>
<property>
<name>hbase.rootdir</name>
<value>file:///home/hadoop/hbase/data</value>
</property>
<property>
<name>hbase.zookeeper.property.dataDir</name>
<value>/home/hadoop/hbase/zkData</value>
</property>
<property>
<name>hbase.unsafe.stream.capability.enforce</name>
<value>false</value>
<description>
Controls whether HBase will check for stream capabilities (hflush/hsync).
Disable this if you intend to run on LocalFileSystem, denoted by a rootdir
with the 'file://' scheme, but be mindful of the NOTE below.
WARNING: Setting this to false blinds you to potential data loss and
inconsistent system state in the event of process and/or node failures. If
HBase is complaining of an inability to use hsync or hflush it's most
likely not a false positive.
</description>
</property>
</configuration>
hbase单节点启动
//start-hbase.sh
>start-hbase.sh
//查看webUI,“localhost:16010”
hbase_shell操作
|
|
java api操作
需要把“hbase-site.xml”放到类路径下
1.put get
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;
public class TestCRUD {
@Test
public void testPut() throws Exception {
//创建配置文件
Configuration conf = HBaseConfiguration.create();
//创建连接
Connection conn = ConnectionFactory.createConnection(conf);
TableName tableName = TableName.valueOf("default:test");
//根据表名获得表对象
Table table = conn.getTable(tableName);
byte[] row1 = Bytes.toBytes("row2");
//利用put对象向表中插入数据,new的时候需要指定rowkey
Put put = new Put(row1);
byte[] cf1 = Bytes.toBytes("cf1");
byte[] col = Bytes.toBytes("id");
byte[] value = Bytes.toBytes("101");
//put对象添加一列,需要参数列簇,列,值
put.addColumn(cf1,col,value);
table.put(put);
conn.close();
}
/**
* 根据rowkey,列簇,列,查询value
* @throws Exception
*/
@Test
public void testGet() throws Exception{
Configuration conf = HBaseConfiguration.create();
Connection conn = ConnectionFactory.createConnection(conf);
TableName tableName = TableName.valueOf("default:test");
Table table = conn.getTable(tableName);
byte[] row2 = Bytes.toBytes("row1");
//通过get对象,执行对表中一行的get操作
Get get_row2 = new Get(row2);
Result r = table.get(get_row2);
byte[] cf1 = Bytes.toBytes("cf1");
byte[] col = Bytes.toBytes("id");
byte[] idvalue = r.getValue(cf1,col);
System.out.println(Bytes.toString(idvalue));
}
}
hbase中的一些概念
写前日志
WAL,"write ahead log",写前日志。位于、hbase/meta下,表的操作先写WAL里
如果插入大量数据
table.setAutoFlush(false);//关闭表的自动flush
put.setWriteToWAL(false);//关闭写前日志
table.flushCommits();//提交日志
hbase基于hdfs
相同列簇的数据存放在一个文件中。hdfs://ip:port/hbase/data/${名称空间}/${表名}/${区域名称}/${列簇名称}/${文件名}
[WAL目录结构构成]
hdfs://s201:8020/hbase/WALs/${区域服务器名称,主机名,端口号,时间戳}/
client端交互过程
0.hbase集群启动时,master负责分配区域到指定区域服务器。1.联系zk,找出meta表所在rs(regionserver)
/hbase/meta-region-server
2.定位row key,找到对应region server
3.缓存信息在本地。
4.联系RegionServer
5.HRegionServer负责open HRegion对象,为每个列族创建Store对象,Store包含多个StoreFile实例,
他们是对HFile的轻量级封装。每个Store还对应了一个MemStore,用于内存存储数据。