您好,登錄后才能下訂單哦!
一、概述:
1、定義:HBase是Google Bigtable的開源山寨版本。是建立的HDFS之上,提供高可靠性、高性能、列存儲、可伸縮、實時、隨機讀寫的數(shù)據(jù)庫系統(tǒng)。
它介于nosql和RDBMS之間,僅能通過主鍵(row key)和主鍵的range來檢索數(shù)據(jù),僅支持單行事務(wù)(可通過hive支持來實現(xiàn)多表join等復(fù)雜操作)。主要用來存儲非結(jié)構(gòu)化和半結(jié)構(gòu)化的松散數(shù)據(jù)。與hadoop一樣,Hbase目標(biāo)主要依靠橫向擴展,通過不斷增加廉價的商用服務(wù)器,來增加計算和存儲能力。
2、特點:
HBase中的表一般有這樣的特點:
(1) 、大:一個表可以有上億行,上百萬列
(2)、 面向列:面向列(族)的存儲和權(quán)限控制,列(族)獨立檢索。
(3)、稀疏:對于為空(null)的列,并不占用存儲空間,因此,表可以設(shè)計的非常稀疏。
二、hbase命令行:
1、進入hbase命令行 ./hbase shell
2、顯示hbase中的表 list
3、創(chuàng)建user表,包含info、data兩個列族
create 'user', {NAME => 'info', VERSIONS => '3'},{NAME => 'data'}
4、向user表中插入信息:
(1)、插入row key為rk0001,列族info中添加name列標(biāo)示符,值為zhangsan
put 'user', 'rk0001', 'info:name', 'zhangsan'
(2)、插入row key為rk0001,列族info中添加gender列標(biāo)示符,值為female
put 'user', 'rk0001', 'info:gender', 'female'
(3)、插入row key為rk0001,列族info中添加age列標(biāo)示符,值為20
put 'user', 'rk0001', 'info:age', 20
(4)、插入row key為rk0001,列族data中添加pic列標(biāo)示符,值為picture
put 'user', 'rk0001', 'data:pic', 'picture'
5、get獲取數(shù)據(jù):
(1)、獲取user表中row key為rk0001的所有信息
get 'user', 'rk0001'
(2)、獲取user表中row key為rk0001,info列族的所有信息
get 'user', 'rk0001', 'info'
(3)、獲取user表中row key為rk0001,info列族的name、age列標(biāo)示符的信息
get 'user', 'rk0001', 'info:name', 'info:age'
(4)、獲取user表中row key為rk0001,info、data列族的信息
get 'user', 'rk0001', 'info', 'data'
get 'user', 'rk0001', {COLUMN => ['info', 'data']}
(5)、獲取user表中row key為rk0001,列族為info,版本號最新5個的信息
get 'user', 'rk0001', {COLUMN => 'info:name', VERSIONS => 5}
6、scan獲取數(shù)據(jù):
(1)、查詢user表中的所有信息
scan 'user'
(2)、查詢user表中row key以rk字符開頭的
scan 'user',{FILTER=>"PrefixFilter('rk')"}
(3)、查詢user表中列族為info,rk范圍是[rk0001, rk0003)的數(shù)據(jù)
scan 'people', {COLUMNS => 'info', STARTROW => 'rk0001', ENDROW => 'rk0003'}
(4)、查詢user表中列族為info和data且列標(biāo)示符中含有a字符的信息
scan 'user', {COLUMNS => ['info', 'data'], FILTER => "(QualifierFilter(=,'substring:a'))"}
(5)、查詢user表中指定范圍的數(shù)據(jù)
scan 'user', {TIMERANGE => [1392368783980, 1392380169184]}
7、刪除數(shù)據(jù)
(1)、刪除user表row key為rk0001,列標(biāo)示符為info:name的數(shù)據(jù)
delete 'user', 'rk0001', 'info:name'
(2)、刪除user表row key為rk0001,列標(biāo)示符為info:name,timestamp為1392383705316的數(shù)據(jù)
delete 'user', 'rk0001', 'info:name', 1392383705316
8、刪除表
disable 'user'
drop 'user'
三、HBase的java api:
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
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.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.BinaryPrefixComparator;
import org.apache.hadoop.hbase.filter.ByteArrayComparable;
import org.apache.hadoop.hbase.filter.ColumnPrefixFilter;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.FamilyFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.MultipleColumnPrefixFilter;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.filter.QualifierFilter;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.master.TableNamespaceManager;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Before;
import org.junit.Test;
public class HbaseDemo {
private Configuration conf = null;
@Before
public void init(){
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "node1,node2,node3");
}
@Test
public void testDrop() throws Exception{
HBaseAdmin admin = new HBaseAdmin(conf);
admin.disableTable("account");
admin.deleteTable("account");
admin.close();
}
@Test
public void testPut() throws Exception{
HTable table = new HTable(conf, "person_info");
Put p = new Put(Bytes.toBytes("person_rk_bj_zhang_000002"));
p.add("base_info".getBytes(), "name".getBytes(), "zhangwuji".getBytes());
table.put(p);
table.close();
}
@Test
public void testGet() throws Exception{
HTable table = new HTable(conf, "person_info");
Get get = new Get(Bytes.toBytes("person_rk_bj_zhang_000001"));
get.setMaxVersions(5);
Result result = table.get(get);
List<Cell> cells = result.listCells();
//result.getValue(family, qualifier); 可以從result中直接取出一個特定的value
//遍歷出result中所有的鍵值對
for(KeyValue kv : result.list()){
String family = new String(kv.getFamily());
System.out.println(family);
String qualifier = new String(kv.getQualifier());
System.out.println(qualifier);
System.out.println(new String(kv.getValue()));
}
table.close();
}
/**
* 多種過濾條件的使用方法
* @throws Exception
*/
@Test
public void testScan() throws Exception{
HTable table = new HTable(conf, "person_info".getBytes());
Scan scan = new Scan(Bytes.toBytes("person_rk_bj_zhang_000001"), Bytes.toBytes("person_rk_bj_zhang_000002"));
//前綴過濾器----針對行鍵
Filter filter = new PrefixFilter(Bytes.toBytes("rk"));
//行過濾器
ByteArrayComparable rowComparator = new BinaryComparator(Bytes.toBytes("person_rk_bj_zhang_000001"));
RowFilter rf = new RowFilter(CompareOp.LESS_OR_EQUAL, rowComparator);
/**
* 假設(shè)rowkey格式為:創(chuàng)建日期_發(fā)布日期_ID_TITLE
* 目標(biāo):查找 發(fā)布日期 為 2014-12-21 的數(shù)據(jù)
*/
rf = new RowFilter(CompareOp.EQUAL , new SubstringComparator("_2014-12-21_"));
//單值過濾器 1 完整匹配字節(jié)數(shù)組
new SingleColumnValueFilter("base_info".getBytes(), "name".getBytes(), CompareOp.EQUAL, "zhangsan".getBytes());
//單值過濾器2 匹配正則表達(dá)式
ByteArrayComparable comparator = new RegexStringComparator("zhang.");
new SingleColumnValueFilter("info".getBytes(), "NAME".getBytes(), CompareOp.EQUAL, comparator);
//單值過濾器2 匹配是否包含子串,大小寫不敏感
comparator = new SubstringComparator("wu");
new SingleColumnValueFilter("info".getBytes(), "NAME".getBytes(), CompareOp.EQUAL, comparator);
//鍵值對元數(shù)據(jù)過濾-----family過濾----字節(jié)數(shù)組完整匹配
FamilyFilter ff = new FamilyFilter(
CompareOp.EQUAL ,
new BinaryComparator(Bytes.toBytes("base_info")) //表中不存在inf列族,過濾結(jié)果為空
);
//鍵值對元數(shù)據(jù)過濾-----family過濾----字節(jié)數(shù)組前綴匹配
ff = new FamilyFilter(
CompareOp.EQUAL ,
new BinaryPrefixComparator(Bytes.toBytes("inf")) //表中存在以inf打頭的列族info,過濾結(jié)果為該列族所有行
);
//鍵值對元數(shù)據(jù)過濾-----qualifier過濾----字節(jié)數(shù)組完整匹配
filter = new QualifierFilter(
CompareOp.EQUAL ,
new BinaryComparator(Bytes.toBytes("na")) //表中不存在na列,過濾結(jié)果為空
);
filter = new QualifierFilter(
CompareOp.EQUAL ,
new BinaryPrefixComparator(Bytes.toBytes("na")) //表中存在以na打頭的列name,過濾結(jié)果為所有行的該列數(shù)據(jù)
);
//基于列名(即Qualifier)前綴過濾數(shù)據(jù)的ColumnPrefixFilter
filter = new ColumnPrefixFilter("na".getBytes());
//基于列名(即Qualifier)多個前綴過濾數(shù)據(jù)的MultipleColumnPrefixFilter
byte[][] prefixes = new byte[][] {Bytes.toBytes("na"), Bytes.toBytes("me")};
filter = new MultipleColumnPrefixFilter(prefixes);
//為查詢設(shè)置過濾條件
scan.setFilter(filter);
scan.addFamily(Bytes.toBytes("base_info"));
ResultScanner scanner = table.getScanner(scan);
for(Result r : scanner){
/**
for(KeyValue kv : r.list()){
String family = new String(kv.getFamily());
System.out.println(family);
String qualifier = new String(kv.getQualifier());
System.out.println(qualifier);
System.out.println(new String(kv.getValue()));
}
*/
//直接從result中取到某個特定的value
byte[] value = r.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("name"));
System.out.println(new String(value));
}
table.close();
}
@Test
public void testDel() throws Exception{
HTable table = new HTable(conf, "user");
Delete del = new Delete(Bytes.toBytes("rk0001"));
del.deleteColumn(Bytes.toBytes("data"), Bytes.toBytes("pic"));
table.delete(del);
table.close();
}
public static void main(String[] args) throws Exception {
Configuration conf = HBaseConfiguration.create();
//conf.set("hbase.zookeeper.quorum", "weekend05:2181,weekend06:2181,weekend07:2181");
HBaseAdmin admin = new HBaseAdmin(conf);
TableName tableName = TableName.valueOf("person_info");
HTableDescriptor td = new HTableDescriptor(tableName);
HColumnDescriptor cd = new HColumnDescriptor("base_info");
cd.setMaxVersions(10);
td.addFamily(cd);
admin.createTable(td);
admin.close();
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。