一、HBase安裝
1、上傳解壓
2、修改環境變量
vi /etc/profile
export HBASE_HOME=/home/hadoop/hbase export PATH=$PATH:$HBASE_HOME/bin
3、修改配置文件
vi hbase-env.sh
export JAVA_HOME=/usr/jdk/ export JAVA_CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export HBASE_OPTS="-XX:+UseConcMarkSweepGC" export HBASE_MANAGES_ZK=false
vi hbase-site.xml
<configuration> <property> <name>hbase.master</name> <value>master1:60000</value> </property> <property> <name>hbase.master.maxclockskew</name> <value>180000</value> </property> <property> <name>hbase.rootdir</name> <value>hdfs://hadoop-cluster1/hbase</value> </property> <property> <name>hbase.cluster.distributed</name> <value>true</value> </property> <property> <name>hbase.zookeeper.quorum</name> <value>master1ha,master2,master2ha</value> </property> <property> <name>hbase.zookeeper.property.dataDir</name> <value>/home/hadoop/hbase/tmp/zookeeper</value> </property> </configuration>
vi regionservers
h2slave1 h2slave2 h2slave3
4、發送到其他機器
scp –r /home/hadoop/hbase hadoop@slave1:/home/hadoop scp –r /home/hadoop/hbase hadoop@slave2:/home/hadoop scp –r /home/hadoop/hbase hadoop@slave3:/home/hadoop
5、啟動
start-hbase.sh
6、訪問
http://master:60010/
二、HBase數據模型
Row Key(行健): 用來檢索記錄的主鍵。
訪問HBase table中的行,只有三種方式:
1.通過單個row key訪問
2.通過row key的range(正則)
3.全表掃描
Columns Family(列簇):
HBase表中的每個列,都歸屬於某個列族。
三、HBase命令
創建表
create '表名', '列族名1','列族名2','列族名N'
查看所有表
list
描述表
describe ‘表名’
判斷表存在
exists '表名'
判斷是否禁用啟用表
is_enabled '表名' is_disabled ‘表名’
添加記錄
put ‘表名’, ‘rowKey’, ‘列族 : 列‘ , '值'
查看記錄rowkey下的所有數據
get '表名' , 'rowKey'
查看表中的記錄總數
count '表名'
獲取某個列族
get '表名','rowkey','列族'
獲取某個列族的某個列
get '表名','rowkey','列族:列’
刪除記錄
delete ‘表名’ ,‘行名’ , ‘列族:列'
刪除整行
deleteall '表名','rowkey'
刪除一張表
先要屏蔽該表,才能對該表進行刪除 第一步 disable ‘表名’ ,第二步 drop '表名'
清空表
truncate '表名'
查看所有記錄
scan "表名"
查看某個表某個列中所有數據
scan "表名" , {COLUMNS=>'列族名:列名'}
四、Java API操作HBase
package cn.itcast_01_hbase; 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.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.HBaseAdmin; import org.apache.hadoop.hbase.client.HConnection; import org.apache.hadoop.hbase.client.HConnectionManager; 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.ColumnPrefixFilter; import org.apache.hadoop.hbase.filter.CompareFilter; import org.apache.hadoop.hbase.filter.FilterList; import org.apache.hadoop.hbase.filter.FilterList.Operator; 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.util.Bytes; import org.junit.After; import org.junit.Before; import org.junit.Test; public class HbaseTest { /** * 配置ss */ static Configuration config = null; private Connection connection = null; private Table table = null; @Before Hbase依賴zookeeper public void init() throws Exception { config = HBaseConfiguration.create();// 配置 config.set("hbase.zookeeper.quorum", "master,work1,work2");// zookeeper地址 config.set("hbase.zookeeper.property.clientPort", "2181");// zookeeper端口 connection = ConnectionFactory.createConnection(config); table = connection.getTable(TableName.valueOf("user")); } /** * 創建一個表 */ @Test public void createTable() throws Exception { // 創建表管理類 HBaseAdmin admin = new HBaseAdmin(config); // hbase表管理 // 創建表描述類 TableName tableName = TableName.valueOf("test3"); // 表名稱 HTableDescriptor desc = new HTableDescriptor(tableName); // 創建列族的描述類 HColumnDescriptor family = new HColumnDescriptor("info"); // 列族 // 將列族添加到表中 desc.addFamily(family); HColumnDescriptor family2 = new HColumnDescriptor("info2"); // 列族 // 將列族添加到表中 desc.addFamily(family2); // 創建表 admin.createTable(desc); // 創建表 } @Test @SuppressWarnings("deprecation") public void deleteTable() throws MasterNotRunningException, ZooKeeperConnectionException, Exception { HBaseAdmin admin = new HBaseAdmin(config); admin.disableTable("test3"); admin.deleteTable("test3"); admin.close(); } /** * 向hbase中增加數據 */ @SuppressWarnings({ "deprecation", "resource" }) @Test public void insertData() throws Exception { table.setAutoFlushTo(false); table.setWriteBufferSize(534534534); ArrayList<Put> arrayList = new ArrayList<Put>(); for (int i = 21; i < 50; i++) { Put put = new Put(Bytes.toBytes("1234"+i)); put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("wangwu"+i)); put.add(Bytes.toBytes("info"), Bytes.toBytes("password"), Bytes.toBytes(1234+i)); arrayList.add(put); } //插入數據 table.put(arrayList); //提交 table.flushCommits(); } /** * 修改數據 */ @Test public void uodateData() throws Exception { Put put = new Put(Bytes.toBytes("1234")); put.add(Bytes.toBytes("info"), Bytes.toBytes("namessss"), Bytes.toBytes("lisi1234")); put.add(Bytes.toBytes("info"), Bytes.toBytes("password"), Bytes.toBytes(1234)); //插入數據 table.put(put); //提交 table.flushCommits(); } /** * 刪除數據 * * @throws Exception */ @Test public void deleteDate() throws Exception { Delete delete = new Delete(Bytes.toBytes("1234")); table.delete(delete); table.flushCommits(); } /** * 單條查詢 */ @Test public void queryData() throws Exception { Get get = new Get(Bytes.toBytes("1234")); Result result = table.get(get); System.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("password")))); System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("namessss")))); System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("sex")))); } /** * 全表掃描 */ @Test public void scanData() throws Exception { Scan scan = new Scan(); //scan.addFamily(Bytes.toBytes("info")); //scan.addColumn(Bytes.toBytes("info"), Bytes.toBytes("password")); scan.setStartRow(Bytes.toBytes("wangsf_0")); scan.setStopRow(Bytes.toBytes("wangwu")); ResultScanner scanner = table.getScanner(scan); for (Result result : scanner) { System.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("password")))); System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")))); //System.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("password")))); //System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("name")))); } } /** * 全表掃描的過濾器 * 列值過濾器 */ @Test public void scanDataByFilter1() throws Exception { // 創建全表掃描的scan Scan scan = new Scan(); //過濾器:列值過濾器 SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("name"), CompareFilter.CompareOp.EQUAL, Bytes.toBytes("zhangsan2")); // 設置過濾器 scan.setFilter(filter); // 打印結果集 ResultScanner scanner = table.getScanner(scan); for (Result result : scanner) { System.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("password")))); System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")))); //System.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("password")))); //System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("name")))); } } /** * rowkey過濾器 */ @Test public void scanDataByFilter2() throws Exception { // 創建全表掃描的scan Scan scan = new Scan(); //匹配rowkey以wangsenfeng開頭的 RowFilter filter = new RowFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("^12341")); // 設置過濾器 scan.setFilter(filter); // 打印結果集 ResultScanner scanner = table.getScanner(scan); for (Result result : scanner) { System.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("password")))); System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")))); //System.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("password")))); //System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("name")))); } } /** * 匹配列名前綴 */ @Test public void scanDataByFilter3() throws Exception { // 創建全表掃描的scan Scan scan = new Scan(); //匹配rowkey以wangsenfeng開頭的 ColumnPrefixFilter filter = new ColumnPrefixFilter(Bytes.toBytes("na")); // 設置過濾器 scan.setFilter(filter); // 打印結果集 ResultScanner scanner = table.getScanner(scan); for (Result result : scanner) { System.out.println("rowkey:" + Bytes.toString(result.getRow())); System.out.println("info:name:" + Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")))); // 判斷取出來的值是否為空 if (result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age")) != null) { System.out.println("info:age:" + Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age")))); } // 判斷取出來的值是否為空 if (result.getValue(Bytes.toBytes("info"), Bytes.toBytes("sex")) != null) { System.out.println("infi:sex:" + Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("sex")))); } // 判斷取出來的值是否為空 if (result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("name")) != null) { System.out .println("info2:name:" + Bytes.toString(result.getValue( Bytes.toBytes("info2"), Bytes.toBytes("name")))); } // 判斷取出來的值是否為空 if (result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("age")) != null) { System.out.println("info2:age:" + Bytes.toInt(result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("age")))); } // 判斷取出來的值是否為空 if (result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("sex")) != null) { System.out.println("info2:sex:" + Bytes.toInt(result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("sex")))); } } } /** * 過濾器集合 */ @Test public void scanDataByFilter4() throws Exception { // 創建全表掃描的scan Scan scan = new Scan(); //過濾器集合:MUST_PASS_ALL(and),MUST_PASS_ONE(or) FilterList filterList = new FilterList(Operator.MUST_PASS_ONE); //匹配rowkey以wangsenfeng開頭的 RowFilter filter = new RowFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("^wangsenfeng")); //匹配name的值等於wangsenfeng SingleColumnValueFilter filter2 = new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("name"), CompareFilter.CompareOp.EQUAL, Bytes.toBytes("zhangsan")); filterList.addFilter(filter); filterList.addFilter(filter2); // 設置過濾器 scan.setFilter(filterList); // 打印結果集 ResultScanner scanner = table.getScanner(scan); for (Result result : scanner) { System.out.println("rowkey:" + Bytes.toString(result.getRow())); System.out.println("info:name:" + Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")))); // 判斷取出來的值是否為空 if (result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age")) != null) { System.out.println("info:age:" + Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age")))); } // 判斷取出來的值是否為空 if (result.getValue(Bytes.toBytes("info"), Bytes.toBytes("sex")) != null) { System.out.println("infi:sex:" + Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("sex")))); } // 判斷取出來的值是否為空 if (result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("name")) != null) { System.out .println("info2:name:" + Bytes.toString(result.getValue( Bytes.toBytes("info2"), Bytes.toBytes("name")))); } // 判斷取出來的值是否為空 if (result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("age")) != null) { System.out.println("info2:age:" + Bytes.toInt(result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("age")))); } // 判斷取出來的值是否為空 if (result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("sex")) != null) { System.out.println("info2:sex:" + Bytes.toInt(result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("sex")))); } } } @After public void close() throws Exception { table.close(); connection.close(); } }
更多java、大數據學習面試資料,請掃碼關注我的公眾號: