[TOC]
依賴的maven
~~~
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase</artifactId>
<version>1.4.3</version>
<type>pom</type>
</dependency>
~~~
代碼
~~~
package com.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.regionserver.BloomType;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
public class HbaseDemo {
private Configuration conf = null;
private Connection conn = null;
@Before
public void init() throws IOException {
//構建個配置
conf = HBaseConfiguration.create();
//對于hbase的客戶端來說,只需要知道hbase所使用的zookeeper集群就可以了
//因為hbase的客戶端找hbase讀寫數據完全不用經過hmaster
conf.set("hbase.zookeeper.quorum", "master:2181,slave:2181");
conn = ConnectionFactory.createConnection(conf);
}
//建表
@Test
public void testCreate() throws IOException {
//獲取一個表管理器
Admin admin = conn.getAdmin();
//構造一個表描述器,并指定表名
HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("t_user_info"));
//構造一個列族描述器,并指定列族名
HColumnDescriptor hcd1 = new HColumnDescriptor("base_info");
//為該列族設定一個布隆過濾器類型參數/版本數量
hcd1.setBloomFilterType(BloomType.ROW).setVersions(1, 3);
//構造第二個列族描述器,并指定列族名
HColumnDescriptor hcd2 = new HColumnDescriptor("extra_info");
//為該列族設定一個布隆過濾器類型參數/版本數量
hcd2.setBloomFilterType(BloomType.ROW).setVersions(1, 3);
//將列族描述器添加到表描述器中
htd.addFamily(hcd1).addFamily(hcd2);
admin.createTable(htd);
admin.close();
conn.close();
}
//刪除表
@Test
public void testDrop() throws IOException {
Admin admin = conn.getAdmin();
admin.disableTable(TableName.valueOf("t_user_info"));
admin.deleteTable(TableName.valueOf("t_user_info"));
admin.close();
conn.close();
}
//修改表定義
@Test
public void testModify() throws IOException {
//這
Admin admin = conn.getAdmin();
// admin.disableTable(TableName.valueOf("t_user_info"));
// 修改已有的ColumnFamily
HTableDescriptor table = admin.getTableDescriptor(TableName.valueOf("t_user_info"));
HColumnDescriptor f2 = table.getFamily("extra_info".getBytes());
f2.setBloomFilterType(BloomType.ROWCOL);
f2.setVersions(1, 5);
// 添加新的ColumnFamily111
table.addFamily(new HColumnDescriptor("other_info"));
admin.modifyTable(TableName.valueOf("t_user_info"), table);
admin.close();
conn.close();
}
@Test
public void testPut() throws IOException {
Table table = conn.getTable(TableName.valueOf("t_user_info"));
ArrayList<Put> puts = new ArrayList<Put>();
//構建一個put對象(kv),指定行鍵
Put put01 = new Put(Bytes.toBytes("user001"));
put01.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("zhangsan"));
Put put02 = new Put("user001".getBytes());
put02.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("password"), Bytes.toBytes("123456"));
Put put03 = new Put("user002".getBytes());
put03.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("lisi"));
put03.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("married"), Bytes.toBytes("false"));
Put put04 = new Put("zhang_sh_01".getBytes());
put04.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("zhang01"));
put04.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("married"), Bytes.toBytes("false"));
Put put05 = new Put("zhang_sh_02".getBytes());
put05.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("zhang02"));
put05.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("married"), Bytes.toBytes("false"));
Put put06 = new Put("liu_sh_01".getBytes());
put06.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("liu01"));
put06.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("married"), Bytes.toBytes("false"));
Put put07 = new Put("zhang_bj_01".getBytes());
put07.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("zhang03"));
put07.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("married"), Bytes.toBytes("false"));
Put put08 = new Put("zhang_bj_01".getBytes());
put08.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("zhang04"));
put08.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("married"), Bytes.toBytes("false"));
puts.add(put01);
puts.add(put02);
puts.add(put03);
puts.add(put04);
puts.add(put05);
puts.add(put06);
puts.add(put07);
puts.add(put08);
table.put(puts);
table.close();
conn.close();
}
//讀取,get一次讀取一行
@Test
public void testGet() throws IOException {
Table table = conn.getTable(TableName.valueOf("t_user_info"));
//構造一個get查詢對象.指定要get的是那一行
Get get = new Get("user001".getBytes());
Result result = table.get(get);
CellScanner cellScanner = result.cellScanner();
//迭代
while (cellScanner.advance()) {
Cell current = cellScanner.current();
//列族名
byte[] familyArray = current.getFamilyArray();
//列標識符的名稱
byte[] qualifierArray = current.getQualifierArray();
//具體的值
byte[] valueArray = current.getValueArray();
//獲取有用字符
System.out.printf(new String(familyArray, current.getFamilyOffset(), current.getFamilyLength()));
System.out.printf(":" + new String(qualifierArray, current.getQualifierOffset(), current.getQualifierLength()));
System.out.printf(" " + new String(valueArray, current.getValueOffset(), current.getValueLength()));
System.out.println();
}
table.close();
conn.close();
}
//刪除表中的數據
@Test
public void testDel() throws IOException {
Table t_user_info = conn.getTable(TableName.valueOf("t_user_info"));
//row key為user001
Delete delete = new Delete("user001".getBytes());
//column=base_info:password 刪除這一列
delete.addColumn("base_info".getBytes(), "password".getBytes());
t_user_info.delete(delete);
t_user_info.close();
conn.close();
}
//批量查詢數據
@Test
public void testScan() throws IOException {
Table t_user_info = conn.getTable(TableName.valueOf("t_user_info"));
//表是liu_sh_01,row key是zhang_bj_01
//數據(字典排序,從liu_sh_01到zhang_bj_01之間的row key全部遍歷)("\000"不加這個是包頭不包尾,加了是全部包,原因是這個字段排序是排在zhang_bj_01后面)
Scan scan = new Scan(Bytes.toBytes("liu_sh_01"), Bytes.toBytes("zhang_bj_01" + "\000"));
ResultScanner scanner = t_user_info.getScanner(scan);
//迭代器
Iterator<Result> iter = scanner.iterator();
while (iter.hasNext()) {
//獲取一行記錄
Result result = iter.next();
//獲取到每一個cell
CellScanner cellScanner = result.cellScanner();
//遍歷cell
while (cellScanner.advance()) {
Cell current = cellScanner.current();
byte[] familyArray = current.getFamilyArray();
byte[] valueArray = current.getValueArray();
byte[] qualifierArray = current.getQualifierArray();
byte[] rowArray = current.getRowArray();
System.out.print(new String(rowArray, current.getRowOffset(), current.getRowLength())+" ");
System.out.print(new String(familyArray, current.getFamilyOffset(), current.getFamilyLength()));
System.out.print(":" + new String(qualifierArray, current.getQualifierOffset(), current.getQualifierLength()));
System.out.print(" " + new String(valueArray, current.getValueOffset(), current.getValueLength()));
System.out.println();
}
System.out.println("-----------------------------");
}
}
}
~~~
- linux
- 常用命令
- 高級文本命令
- 面試題
- redis
- String
- list
- hash
- set
- sortedSet
- 案例-推薦
- java高級特性
- 多線程
- 實現線程的三種方式
- 同步關鍵詞
- 讀寫鎖
- 鎖的相關概念
- 多線程的join
- 有三個線程T1 T2 T3,保證順序執行
- java五種線程池
- 守護線程與普通線程
- ThreadLocal
- BlockingQueue消息隊列
- JMS
- 反射
- volatile
- jvm
- IO
- nio
- netty
- netty簡介
- 案例一發送字符串
- 案例二發送對象
- 輕量級RPC開發
- 簡介
- spring(IOC/AOP)
- spring初始化順序
- 通過ApplicationContextAware加載Spring上下文
- InitializingBean的作用
- 結論
- 自定義注解
- zk在框架中的應用
- hadoop
- 簡介
- hadoop集群搭建
- hadoop單機安裝
- HDFS簡介
- hdfs基本操作
- hdfs環境搭建
- 常見問題匯總
- hdfs客戶端操作
- mapreduce工作機制
- 案列-單詞統計
- 局部聚合Combiner
- 案列-流量統計(分區,排序,比較)
- 案列-倒排索引
- 案例-共同好友
- 案列-join算法實現
- 案例-求topN(分組)
- 自定義inputFormat
- 自定義outputFormat
- 框架運算全流程
- mapreduce的優化方案
- HA機制
- Hive
- 安裝
- DDL操作
- 創建表
- 修改表
- DML操作
- Load
- insert
- select
- join操作
- 嚴格模式
- 數據類型
- shell參數
- 函數
- 內置運算符
- 內置函數
- 自定義函數
- Transform實現
- 特殊分割符處理
- 案例
- 級聯求和accumulate
- flume
- 簡介
- 安裝
- 常用的組件
- 攔截器
- 案例
- 采集目錄到HDFS
- 采集文件到HDFS
- 多個agent串聯
- 日志采集和匯總
- 自定義攔截器
- 高可用配置
- 使用注意
- sqoop
- 安裝
- 數據導入
- 導入數據到HDFS
- 導入關系表到HIVE
- 導入表數據子集
- 增量導入
- 數據導出
- 作業
- 原理
- azkaban
- 簡介
- 安裝
- 案例
- 簡介
- command類型單一job
- command類型多job工作流flow
- HDFS操作任務
- mapreduce任務
- hive腳本任務
- hbase
- 簡介
- 安裝
- 命令行
- 基本CURD
- 過濾器查詢
- 系統架構
- 物理存儲
- 尋址機制
- 讀寫過程
- Region管理
- master工作機制
- 建表高級屬性
- 與mapreduce結合
- 協處理器
- 點擊流平臺開發
- 簡介
- storm
- 簡介
- 安裝
- 集群啟動及任務過程分析
- 單詞統計
- 并行度
- ACK容錯機制
- ACK簡介