第一章 HBase 快速入門
1.1 HBase 安裝部署
1.1.1 Zookeeper 正常部署
首先保證 Zookeeper 集羣的正常部署,並啓動之:
https://www.cnblogs.com/wkfvawl/p/15539847.html#scroller-16
1.1.2 Hadoop 正常部署
Hadoop 集羣的正常部署並啓動:
https://www.cnblogs.com/wkfvawl/p/15369416.html#scroller-52
1.1.3 HBase 的解壓
解壓 Hbase 到指定目錄:
[atguigu@hadoop102 software]$ tar -zxvf hbase-1.3.1-bin.tar.gz -C /opt/module
1.1.4 HBase 的配置文件
修改 HBase 對應的配置文件。
1)hbase-env.sh 修改內容:
export JAVA_HOME=/opt/module/jdk1.8.0_212
export HBASE_MANAGES_ZK=false
註釋掉:
2)hbase-site.xml 修改內容:
<configuration>
<property>
<name>hbase.rootdir</name>
<value>hdfs://hadoop102:8020/HBase</value>
</property>
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
<!-- 0.98 後的新變動,之前版本沒有.port,默認端口爲 60000 -->
<property>
<name>hbase.master.port</name>
<value>16000</value>
</property>
<property>
<name>hbase.zookeeper.quorum</name>
<value>hadoop102,hadoop103,hadoop104</value>
</property>
<property>
<name>hbase.zookeeper.property.dataDir</name>
<value>/opt/module/zookeeper-3.5.7/zkData</value>
</property>
</configuration>
3)regionservers:
hadoop102
hadoop103
hadoop104
4)軟連接 hadoop 配置文件到 HBase:
ln -s /opt/module/hadoop-3.1.3/etc/hadoop/core-site.xml /opt/module/hbase/conf/core-site.xml
ln -s /opt/module/hadoop-3.1.3/etc/hadoop/hdfs-site.xml /opt/module/hbase/conf/hdfs-site.xml
1.1.5 HBase 遠程發送到其他集羣
[atguigu@hadoop102 module]$ xsync hbase/
1.1.6 HBase 服務的啓動
1.啓動方式
[atguigu@hadoop102 hbase]$ bin/hbase-daemon.sh start master
[atguigu@hadoop102 hbase]$ bin/hbase-daemon.sh start regionserver
提示:如果集羣之間的節點時間不同步,會導致 regionserver 無法啓動,拋出ClockOutOfSyncException 異常。
修復提示:
a、同步時間服務
https://www.cnblogs.com/wkfvawl/p/15369416.html#scroller-54
b、屬性:hbase.master.maxclockskew 設置更大的值
<property>
<name>hbase.master.maxclockskew</name>
<value>180000</value>
<description>Time difference of regionserver from
master</description>
</property>
2.啓動方式 2
[atguigu@hadoop102 hbase]$ bin/start-hbase.sh
對應的停止服務:
[atguigu@hadoop102 hbase]$ bin/stop-hbase.sh
1.1.7 查看 HBase 頁面
HBase的端口
HBase Master | 16000 | hbase-client-1.x.x.jar | RegionServer接入 | |
16010 | HTTP | http://namenode1:16010/ | 集羣監控 | |
HBase RegionServer | 16020 | N/A | 客戶端接入 | |
16030 | HTTP | http://datanode1:16030/ | 節點監控 |
啓動成功後,可以通過“host:port”的方式來訪問 HBase 管理頁面,例如:
http://hadoop102:16010
1.2 HBase Shell 操作
1.2.1 基本操作
1.進入 HBase 客戶端命令行
[atguigu@hadoop102 hbase]$ bin/hbase shell
2.查看幫助命令
hbase(main):001:0> help
3.查看當前數據庫中有哪些表
hbase(main):002:0> list
1.2.2 表的操作
1.創建表
hbase(main):002:0> create 'student','info'
2.插入數據到表
hbase(main):003:0> put 'student','1001','info:sex','male'
hbase(main):004:0> put 'student','1001','info:age','18'
hbase(main):005:0> put 'student','1002','info:name','Janna'
hbase(main):006:0> put 'student','1002','info:sex','female'
hbase(main):007:0> put 'student','1002','info:age','20'
3.掃描查看錶數據
hbase(main):008:0> scan 'student'
hbase(main):009:0> scan 'student',{STARTROW => '1001', STOPROW =>'1001'}
hbase(main):010:0> scan 'student',{STARTROW => '1001'}
4.查看錶結構
hbase(main):011:0> describe ‘student’
5.更新指定字段的數據
hbase(main):012:0> put 'student','1001','info:name','Nick'
hbase(main):013:0> put 'student','1001','info:age','100'
6.查看“指定行”或“指定列族:列”的數據
hbase(main):014:0> get 'student','1001'
hbase(main):015:0> get 'student','1001','info:name'
7.統計表數據行數
hbase(main):021:0> count 'student'
8.刪除數據
刪除某 rowkey 的全部數據:
hbase(main):016:0> deleteall 'student','1001'
刪除某 rowkey 的某一列數據:
hbase(main):017:0> delete 'student','1002','info:sex'
9.清空表數據
hbase(main):018:0> truncate 'student'
Truncating 'student' table (it may take a while):
- Disabling table...
- Truncating table...
0 row(s) in 3.8140 seconds
提示:清空表的操作順序爲先 disable,然後再 truncate。
10.刪除表
首先需要先讓該表爲 disable 狀態:
hbase(main):019:0> disable 'student'
然後才能 drop 這個表:
hbase(main):020:0> drop 'student'
提示:如果直接 drop 表,會報錯:ERROR: Table student is enabled. Disable it first.
11.變更表信息 將 info 列族中的數據存放 3 個版本:
hbase(main):022:0> alter 'student',{NAME=>'info',VERSIONS=>3}
hbase(main):022:0> get 'student','1001',{COLUMN=>'info:name',VERSIONS=>3}
第二章 HBase API 操作
2.1 環境準備
<dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>1.3.1</version> </dependency>
zookeeper和hadoop的依賴,與集羣上的版本號對應起來。
<dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.5.7</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>3.1.3</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>3.1.3</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-hdfs</artifactId> <version>3.1.3</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-auth</artifactId> <version>3.1.3</version> </dependency>
2.2 API
獲取 Configuration 對象
private static Configuration conf; private static Admin admin = null; private static Connection connection = null; // 獲取 Configuration 對象 static{ try{ //使用 HBaseConfiguration 的單例方法實例化 conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "hadoop102"); conf.set("hbase.zookeeper.property.clientPort", "2181"); connection = ConnectionFactory.createConnection(conf); // 獲取管理員對象 admin = connection.getAdmin(); } catch (IOException e){ e.printStackTrace(); } }
判斷表是否存在
// 判斷表是否存在 public static boolean isTableExist(String tableName) throws IOException { //在 HBase 中管理、訪問表需要先創建 HBaseAdmin 對象 System.out.println("method isTableExist"); return admin.tableExists(TableName.valueOf(tableName)); }
創建表
// 創建表 public static void createTable(String tableName, String... columnFamily) throws IOException{ //判斷表是否存在 if(isTableExist(tableName)){ System.out.println("表" + tableName + "已存在"); //System.exit(0); }else{ // 創建表屬性對象,表名需要轉字節 HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName)); //創建多個列族 for(String cf : columnFamily){ //創建列族描述器new HColumnDescriptor(cf) descriptor.addFamily(new HColumnDescriptor(cf)); } //根據對錶的配置,創建表 admin.createTable(descriptor); System.out.println("表" + tableName + "創建成功!"); } }
刪除表
// 刪除表 public static void dropTable(String tableName) throws IOException{ if(isTableExist(tableName)){ admin.disableTable(TableName.valueOf(tableName)); admin.deleteTable(TableName.valueOf(tableName)); System.out.println("表" + tableName + "刪除成功!"); }else{ System.out.println("表" + tableName + "不存在!"); } }
向表中插入數據
// 向表中插入數據 public static void addRowData(String tableName, String rowKey, String columnFamily, String column, String value) throws IOException{ //創建 HTable 對象 Table hTable = connection.getTable(TableName.valueOf(tableName)); //向表中插入數據 Put put = new Put(Bytes.toBytes(rowKey)); //向 Put 對象中組裝數據 put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value)); hTable.put(put); hTable.close(); System.out.println("插入數據成功"); }
獲取所有數據
// 獲取所有數據 public static void getAllRows(String tableName) throws IOException{ Table hTable = connection.getTable(TableName.valueOf(tableName)); //得到用於掃描 region 的對象 Scan scan = new Scan(); //使用 HTable 得到 resultcanner 實現類的對象 ResultScanner resultScanner = hTable.getScanner(scan); for(Result result : resultScanner){ Cell[] cells = result.rawCells(); for(Cell cell : cells){ //得到 rowkey System.out.println(" 行 鍵 :" + Bytes.toString(CellUtil.cloneRow(cell))); //得到列族 System.out.println(" 列 族 " + Bytes.toString(CellUtil.cloneFamily(cell))); System.out.println(" 列 :" + Bytes.toString(CellUtil.cloneQualifier(cell))); System.out.println(" 值 :" + Bytes.toString(CellUtil.cloneValue(cell))); } } }
獲取某一行數據
// 獲取某一行數據 public static void getRow(String tableName, String rowKey) throws IOException{ Table hTable = connection.getTable(TableName.valueOf(tableName)); Get get = new Get(Bytes.toBytes(rowKey)); //get.setMaxVersions();顯示所有版本 //get.setTimeStamp();顯示指定時間戳的版本 Result result = hTable.get(get); for(Cell cell : result.rawCells()){ System.out.println(" 行 鍵 :" +Bytes.toString(result.getRow())); System.out.println(" 列 族 " + Bytes.toString(CellUtil.cloneFamily(cell))); System.out.println(" 列 :" + Bytes.toString(CellUtil.cloneQualifier(cell))); System.out.println(" 值 :" + Bytes.toString(CellUtil.cloneValue(cell))); System.out.println("時間戳:" + cell.getTimestamp()); } }
獲取某一行指定“列族:列”的數據
// 獲取某一行指定“列族:列”的數據 public static void getRowQualifier(String tableName, String rowKey, String family, String qualifier) throws IOException{ Table hTable = connection.getTable(TableName.valueOf(tableName)); Get get = new Get(Bytes.toBytes(rowKey)); get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier)); Result result = hTable.get(get); for(Cell cell : result.rawCells()){ System.out.println(" 行 鍵 :" + Bytes.toString(result.getRow())); System.out.println(" 列 族 " + Bytes.toString(CellUtil.cloneFamily(cell))); System.out.println(" 列 :" + Bytes.toString(CellUtil.cloneQualifier(cell))); System.out.println(" 值 :" + Bytes.toString(CellUtil.cloneValue(cell))); } }
刪除指定行
public static void deleteData(String tableName, String rowKey, String columnFamily, String column ) throws IOException{ //創建 HTable 對象 Table table = connection.getTable(TableName.valueOf(tableName)); //構建刪除對象 Delete delete = new Delete(Bytes.toBytes(rowKey)); delete.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column)); //執行刪除操作 table.delete(delete); //關閉鏈接 table.close(); }
addColumn這個API要慎用
- addColumn() 只刪除等於給定時間戳的版本。
hbase存儲數據是按照版本存儲的,並且是按照時間戳決定的版本,只有時間戳最新的版本纔有效。
比如原有三個版本,都是表示數據類型的,時間戳假設是1,5,8三個。
而addColumn指定了時間戳是5,那麼hbas就會新增一個時間戳爲5的delete版本,
然後一共有4個版本,1,5,5(delete),8..
那麼5(delete)表示刪除的5這個版本。
當然,由於最新版的還是8,所以get和scan查詢出來的是8這個版本的數據。
如果沒有8這個版本,即添加了5(delete)之後的有1,5,5(delete)三個版本,那麼5(delete)還是表示刪除5這個版本,然後get或者scan查詢的數據將會是1這個版本的數據value。
- addColumns() 刪除給定列的所有小於等於給定的時間戳版本的數據。即添加一個版本,類型爲deleteColumn。
get或者scan時,小於該deleteColumn數據版本的所有數據都是不可見的
刪除多行數據
// 刪除多行數據
public static void deleteMultiRow(String tableName, String... rows) throws IOException{
Table hTable = connection.getTable(TableName.valueOf(tableName));
List<Delete> deleteList = new ArrayList<Delete>();
for(String row : rows){
Delete delete = new Delete(Bytes.toBytes(row));
deleteList.add(delete);
}
hTable.delete(deleteList);
hTable.close();
}
2.3 MapReduce
在Hadoop中MR使用HBase,需要將HBase的jar包添加到Hadoop的類路徑下,所以需要修改配置文件添加類路徑。這源於一個思想:
A要使用 B,那麼A要有B的jar包。例如:在 Hive的安裝中,Hive需要使用到MySQL數據庫,所以將jdbc驅動包放到lib文件夾中
通過 HBase 的相關 JavaAPI,我們可以實現伴隨 HBase 操作的 MapReduce 過程,比如使用MapReduce 將數據從本地文件系統導入到 HBase 的表中,比如我們從 HBase 中讀取一些原始數據後使用 MapReduce 做數據分析。
官方 HBase-MapReduce
1.查看 HBase 的 MapReduce 任務的執行
$ bin/hbase mapredcp
2.環境變量的導入
(1)執行環境變量的導入(臨時生效,在命令行執行下述操作)
$ export HBASE_HOME=/opt/module/hbase $ export HADOOP_HOME=/opt/module/hadoop-3.1.3 $ export HADOOP_CLASSPATH=`${HBASE_HOME}/bin/hbase mapredcp`
(2)永久生效:在/etc/profile 配置
export HBASE_HOME=/opt/module/hbase
export HADOOP_HOME=/opt/module/hadoop-3.1.3
並在 hadoop-env.sh 中配置:
export HADOOP_CLASSPATH=$HADOOP_CLASSPATH:/opt/module/hbase/lib/*
3.運行官方的 MapReduce 任務
案例一:統計 Student 表中有多少行數據
/opt/module/hadoop-3.1.3/bin/yarn jar lib/hbase-server-1.3.1.jar rowcounter stu
案例二:使用 MapReduce 將本地數據導入到 HBase
1)在本地創建一個 tsv 格式的文件:fruit.tsv
1001 Apple Red 1002 Pear Yellow 1003 Pineapple Yellow
注意這裏要用tab來分隔 否則會出錯誤
2)創建 Hbase 表
Hbase(main):001:0> create 'fruit','info'
3)在 HDFS 中上傳 fruit.tsv 文件到根目錄下
hadoop fs -put fruit.tsv /
在hdfs上查看http://hadoop102:9870/explorer.html#/
4)執行 MapReduce 到 HBase 的 fruit 表中
$ /opt/module/hadoop-3.1.3/bin/yarn jar lib/hbase-server-1.3.1.jar \
importtsv -Dimporttsv.columns=HBASE_ROW_KEY,info:name,info:color fruit \
hdfs://hadoop102:8020/fruit.tsv
這裏加 \ 將命令變成多行 可以方便閱讀。
5)使用 scan 命令查看導入後的結果
Hbase(main):001:0> scan ‘fruit’
自定義 HBase-MapReduce1
目標:將 fruit 表中的一部分數據,通過 MR 遷入到 fruit_mr 表中。
分步實現:
1.構建 ReadFruitMapper 類,用於讀取 fruit 表中的數據
import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; import java.io.IOException; public class FruitMapper extends Mapper<LongWritable, Text, LongWritable, Text> { @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { context.write(key, value); } }
2. 構建FruitReducer類,用於將讀取到的數據寫入到hbase中的fruit1表中
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.mapreduce.TableReducer; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.NullWritable; import org.apache.hadoop.io.Text; import java.io.IOException; public class FruitReducer extends TableReducer<LongWritable, Text, NullWritable> { @Override protected void reduce(LongWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException { // 1.遍歷values:1001 Apple Red for (Text value : values) { // 2.獲取每一行數據 String[] fields = value.toString().split("\t"); // 3.構建Put對象 Put put = new Put(Bytes.toBytes(fields[0])); // 4.給Put對象賦值 put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes(fields[1])); put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("color"), Bytes.toBytes(fields[2])); // 5.寫出 context.write(NullWritable.get(), put); } } }
3. 構建FruitDriver implements Tool用於組裝運行Job任務
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner; public class FruitDriver implements Tool { // 定義一個Configuration private Configuration configuration = null; public int run(String[] args) throws Exception { // 1.獲取Job對象 Job job = Job.getInstance(configuration); // 2.設置驅動類路徑 job.setJarByClass(FruitDriver.class); // 3.設置Mapper和Mapper輸出的KV類型 job.setMapperClass(FruitMapper.class); job.setMapOutputKeyClass(LongWritable.class); job.setMapOutputValueClass(Text.class); // 4.設置Reducer類 TableMapReduceUtil.initTableReducerJob(args[1], FruitReducer.class, job); // 5.設置輸入輸出參數 FileInputFormat.setInputPaths(job, new Path(args[0])); // 6.提交任務 boolean result = job.waitForCompletion(true); return result ? 0 : 1; } public void setConf(Configuration conf) { configuration = conf; } public Configuration getConf() { return configuration; } public static void main(String[] args) { try { Configuration configuration = new Configuration(); int run = ToolRunner.run(configuration, new FruitDriver(), args); System.exit(run); } catch (Exception e) { e.printStackTrace(); } } }
4. 打包運行任務
/opt/module/hadoop-3.1.3/bin/yarn jar HBaseExample-1.0-SNAPSHOT.jar com.stitch.hbase.mr1.FruitDriver /fruit.tsv fruit1
提示:運行任務前,如果待數據導入的表不存在,則需要提前創建。
提示:maven打包命令:-P local clean package或-P dev clean package install(將第三方jar包一同打包,需要插件:maven-shade-plugin)
自定義 HBase-MapReduce2
目標:將HBase中fruit1表中的一部分數據(name列),通過MR遷入到HBase的fruit2表中。
1. 構建Fruit2Mapper類,用於讀取fruit1表中的數據
import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.io.ImmutableBytesWritable; import org.apache.hadoop.hbase.mapreduce.TableMapper; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; public class Fruit2Mapper extends TableMapper<ImmutableBytesWritable, Put> { @Override protected void map(ImmutableBytesWritable key, Result value, Context context) throws IOException, InterruptedException { // 構建Put對象 Put put = new Put(key.get()); // 1.獲取數據 for (Cell cell : value.rawCells()) { // 2.判斷當前的cell是否爲"name"列 if ("name".equals(Bytes.toString(CellUtil.cloneQualifier(cell)))) { // 3.給Put對象賦值 put.add(cell); } } // 4.寫出 context.write(key, put); } }
2.構建Fruit2Reducer類,用於將讀取到的fruit1表中的數據寫入到fruit2表中
import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.io.ImmutableBytesWritable; import org.apache.hadoop.hbase.mapreduce.TableReducer; import org.apache.hadoop.io.NullWritable; import java.io.IOException; public class Fruit2Reducer extends TableReducer<ImmutableBytesWritable, Put, NullWritable> { @Override protected void reduce(ImmutableBytesWritable key, Iterable<Put> values, Context context) throws IOException, InterruptedException { // 遍歷寫出 for (Put put : values) { context.write(NullWritable.get(), put); } } }
3.構建Fruit2Driver implements Tool用於組裝運行Job任務
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.io.ImmutableBytesWritable; import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner; public class Fruit2Driver implements Tool { // 定義配置信息 private Configuration configuration = null; public int run(String[] args) throws Exception { // 1.獲取Job對象 Job job = Job.getInstance(configuration); // 2.設置主類路徑 job.setJarByClass(Fruit2Driver.class); // 3.設置Mapper和輸出KV類型 TableMapReduceUtil.initTableMapperJob("fruit1", new Scan(), Fruit2Mapper.class, ImmutableBytesWritable.class, Put.class, job); // 4.設置Reducer&輸出的表 TableMapReduceUtil.initTableReducerJob("fruit2", Fruit2Reducer.class, job); // 5.提交任務 boolean result = job.waitForCompletion(true); return result ? 0 : 1; } public void setConf(Configuration conf) { configuration = conf; } public Configuration getConf() { return configuration; } public static void main(String[] args) { try { Configuration configuration = HBaseConfiguration.create(); ToolRunner.run(configuration, new Fruit2Driver(), args); } catch (Exception e) { e.printStackTrace(); } } }
4.打包運行任務
/opt/module/hadoop-3.1.3/bin/yarn jar HBaseExample-1.0-SNAPSHOT.jar com.stitch.hbase.mr2.FruitDriver
提示:運行任務前,如果待數據導入的表不存在,則需要提前創建。
提示:maven打包命令:-P local clean package或-P dev clean package install(將第三方jar包一同打包,需要插件:maven-shade-plugin)
提示:運行任務前,如果待數據導入的表不存在,則需要提前創建。