处理很是庞大的表java
行存储:mysql oracle底层基于行存储数据的mysql
列存储:hbase底层基于列存储数据的sql
==安装hbase高可用集群以前首先要保证zookeeper和hadoop已经安装完成==shell
hbase-1.1.5-bin.tar.gz数据库
集群的规划apache
解压安装包vim
[root@uplooking01: /soft]: tar -zxvf hbase-1.1.5-bin.tar.gz -C /opt/
重命名数组
[root@uplooking01: /opt]: mv hbase-1.1.5/ hbase
配置环境变量缓存
[root@uplooking01: /opt]: #配置HBASE的环境变量 export HBASE_HOME=/opt/hbase export PATH=$PATH:$HBASE_HOME/bin
配置vim hbase-env.sh安全
[root@uplooking01: /opt/hbase/conf]: vim hbase-env.sh
export JAVA_HOME=/opt/jdk export HBASE_MANAGES_ZK=false #不使用hbase自带的zookeeper export HBASE_CLASSPATH=/opt/hadoop/etc/hadoop
配置hbase-site.xml
[root@uplooking01: /opt/hbase/conf]: vim hbase-site.xml
<configuration> <property> <name>hbase.rootdir</name> <value>hdfs://ns1/hbase</value> </property> <property> <name>hbase.tmp.dir</name> <value>/opt/hbase/tmp</value> </property> <property> <name>hbase.cluster.distributed</name> <value>true</value> </property> <property> <name>hbase.zookeeper.quorum</name> <value>uplooking03:2181,uplooking04:2181,uplooking05:2181</value> </property> </configuration>
配置 regionservers
[root@uplooking01: /opt/hbase/conf]: vim regionservers
uplooking03 uplooking04 uplooking05
分发文件
[root@uplooking01: /opt]: scp -r hbase uplooking02:/opt scp -r hbase uplooking03:/opt scp -r hbase uplooking04:/opt scp -r hbase uplooking05:/opt scp /etc/profile uplooking02:/etc/ scp /etc/profile uplooking03:/etc/ scp /etc/profile uplooking04:/etc/ scp /etc/profile uplooking05:/etc/
source /etc/profile(全部节点都作,要使环境变量生效)
启动hbase集群
start-hbase.sh
单独启动master
[root@uplooking02:/] hbase-daemon.sh start master
==启动hbase集群必定要保证整个集群的时间一致==
若是启动集群执行start-hbase.sh,master节点能够启动,可是regionserver节点不能启动,可是单独启动regionserver(hbase-daemon.sh start regionserver)是能够启动的,也没有问题,name就须要拷贝一个jar包,
将HADOOP_HOME/share/hadoop/common/lib下的htrace-core-3.0.4.jar 复制到$HBASE_HOME/lib下
表(table)
行键(rowKey)
列簇(列族)columnFamily
列限定符(column Qualifier)
时间戳(version)
单元格(cell)
三级定位
Zookeeper
Master
RegionServer
Hlog
Region
Hstore
MemStore
StoreFile
Hfile
zookeeper(寻找元数据信息)
列出全部的命名空间(至关于mysql中的show databases)
列出指定命名空间下的全部表
建立命名空间
建立表
禁用表,由于删除表以前首先须要禁用了
启用表
删除表
添加数据
查询数据
删除数据
删除一行数据
统计表的行数
建立Hbase表时指定列族的显示版本数
修改Hbase表中的列族的显示版本数
查询指定版本数的数据
根据显示的版本数,查询出来想要版本的时间戳,根据时间戳找出具体值
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <hbase-version>1.1.5</hbase-version> </properties> <dependencies> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>${hbase-version}</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>${hbase-version}</version> </dependency> <dependency> <groupId>org.apache.hive</groupId> <artifactId>hive-hbase-handler</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
public class HbaseTest { //添加数据 @Test public void testPut() throws IOException { Configuration conf = HBaseConfiguration.create(); //指定zk的地址 conf.set("hbase.zookeeper.quorum", "uplooking03:2181,uplooking04:2181,uplooking05:2181"); Connection conn = ConnectionFactory.createConnection(conf); Table table = conn.getTable(TableName.valueOf("ns1:t1")); Put put = new Put(Bytes.toBytes("row001")); put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("name"), Bytes.toBytes("admin02")); table.put(put); } //删除数据 @Test public void testDelete() throws IOException { Configuration conf = HBaseConfiguration.create(); //指定zk的地址 conf.set("hbase.zookeeper.quorum", "uplooking03:2181,uplooking04:2181,uplooking05:2181"); Connection conn = ConnectionFactory.createConnection(conf); Table table = conn.getTable(TableName.valueOf("ns1:t1")); Delete delete = new Delete(Bytes.toBytes("row001")); table.delete(delete); } //查询数据 @Test public void testGet() throws IOException { Configuration conf = HBaseConfiguration.create(); //指定zk的地址 conf.set("hbase.zookeeper.quorum", "uplooking03:2181,uplooking04:2181,uplooking05:2181"); Connection conn = ConnectionFactory.createConnection(conf); Table table = conn.getTable(TableName.valueOf("ns1:t1")); Get get = new Get(Bytes.toBytes("row001")); Result result = table.get(get); String s = Bytes.toString(result.getValue(Bytes.toBytes("f1"),Bytes.toBytes("name"))); System.out.println(s); } }
public class HbaseAdminTest { private Connection connection; @Before public void init() throws Exception { Configuration conf = new Configuration(); conf.set("hbase.zookeeper.quorum", "uplooking03:2181,uplooking04:2181,uplooking05:2181"); connection = ConnectionFactory.createConnection(conf); } /** * 建立表 * * @throws Exception */ @Test public void testCreateTable() throws Exception { //获取管理对象 Admin admin = connection.getAdmin(); HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("t2")); HColumnDescriptor hcd = new HColumnDescriptor(Bytes.toBytes("f1")); htd.addFamily(hcd); admin.createTable(htd); } /** * 列出全部的表 * @throws Exception */ @Test public void testListTableNames() throws Exception { //获取管理对象 Admin admin = connection.getAdmin(); TableName[] tableNames = admin.listTableNames("ns1:.*"); for (TableName tableName : tableNames) { System.out.println(tableName); } } }
//查询数据 @Test public void testScan() throws IOException { Configuration conf = HBaseConfiguration.create(); //指定zk的地址 conf.set("hbase.zookeeper.quorum", "uplooking03:2181,uplooking04:2181,uplooking05:2181"); Connection conn = ConnectionFactory.createConnection(conf); Table table = conn.getTable(TableName.valueOf("ns1:t1")); Scan scan = new Scan(); byte[] cf = Bytes.toBytes("f1"); byte[] column = Bytes.toBytes("name"); Filter filter = new SingleColumnValueFilter(cf, column, CompareFilter.CompareOp.EQUAL, Bytes.toBytes("admin123")); scan.setFilter(filter); //获取包含多行数据的对象 ResultScanner resultScanner = table.getScanner(scan); for (Result result : resultScanner) { System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("f1"), Bytes.toBytes("age")))); } }
耗时约20分钟
本身测试10分钟
8800000ms,插入15851742tiao数据
/** * 百万数据的插入 */ public class HbaseMiTest { private Connection connection; @Before public void init() throws Exception { Configuration conf = new Configuration(); conf.set("hbase.zookeeper.quorum", "uplooking03:2181,uplooking04:2181,uplooking05:2181"); connection = ConnectionFactory.createConnection(conf); } @Test public void test01() throws IOException { HTable table = (HTable) connection.getTable(TableName.valueOf("ns1:t1")); //不使用每一个put操做都刷出一次 table.setAutoFlush(false); long startTime = System.currentTimeMillis(); for (int i = 0; i < 1000000; i++) { Put put = new Put(Bytes.toBytes("row" + i)); //关闭预写日志,可是不建议使用,由于这样作不安全 put.setWriteToWAL(false); put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("name"), Bytes.toBytes("admin" + i)); table.put(put); if (i % 100000 == 0) { table.flushCommits(); } } table.flushCommits(); long endTime = System.currentTimeMillis(); System.out.println("总耗时:" + (endTime - startTime) + "ms"); } }
大约耗时27s
本身测试,1分20秒 590/80=7.4倍
查询一行是9秒
97602ms,插入15851742tiao数据 8800/175=50倍
split 'ns1:t1','row040'
move 'f6e6164514db53d660c5414df1f3864e','uplooking05,1602
**0,1539222350164'**
是因为行健类似、连续且数据量过大操做成单region的数据量过大,进而影响读写效率
行健应该尽可能的随机、不要出现连续行健。
常见的行健设计就是,好比手机号码倒置+时间戳,好比随机前缀+关系型数据库中的主键
由于hbase提供的查询内容很是很是low,可是全部关于hbase的查询只能经过rowkey,因此
在设计行健的时候,应该考虑将尽可能多的查询条件放到rowkey中去,造成的行健就成为复合键
列族的设计:
cf1----->"columnFamily"
cf2----->"cf"
建议hbase表是高表,不建议宽表,由于宽表拥有的列族不少,操做并跨越的文件(HFile)就不少,效率会有相应影响,
反之建议使用高表,列族不宜过多(列族通常使用一个)。
在设计表的时候,各个列/列族名称不宜过长,由于hbase须要对这些数据在内存中作缓存,作索引,进而影响内存容量,因此建议不易过长,以便可以在内存中容纳更多的数据。至于阅读性,有项目文档搞定。
HbaseExplorer