HBase的编程实践(实验3_熟悉常用的HBase操作)
HBase的編程實(shí)踐
|
? 準(zhǔn)備工作:啟動(dòng)、關(guān)閉hbase之類的(以及該過程可能遇到的bug) 一、Hbase中使用Shell命令: ① HBase中創(chuàng)建表:(create 命令:第一個(gè)變量是表名,然后是列族名) ? 增加數(shù)據(jù) ② put:(put 命令:第一個(gè)變量是表名,第二個(gè)變量是行鍵,第三個(gè)變量開始就是添加列族情況啦(列限定符可選) ? 刪除數(shù)據(jù) ③ delete命令: □ put 的反向操作: □ 刪除 一行中的所有數(shù)據(jù): □ 刪除 表: ? 查看數(shù)據(jù): □ get: 查看的是一行中的數(shù)據(jù) □ scan: 查看的是表中的全部數(shù)據(jù) ?查詢表歷史數(shù)據(jù): ④ 查詢表的歷史版本,需要兩步。 ? 退出HBase數(shù)據(jù)庫操作:
二、Hbase編程實(shí)踐:? 準(zhǔn)備工作:導(dǎo)入jar包: 例子:創(chuàng)建表,插入數(shù)據(jù)、查看表中數(shù)據(jù) 三、實(shí)驗(yàn):熟悉常用的HBase 操作: (一)編程實(shí)現(xiàn)以下指定功能,并用 Hadoop 提供的 HBase Shell 命令完成相同任務(wù): 列出 HBase所有的表的相關(guān)信息,例如表名; (二)現(xiàn)有以下關(guān)系型數(shù)據(jù)庫中的表和數(shù)據(jù),要求將其轉(zhuǎn)換為適合于HBase存儲(chǔ)的表并插入數(shù)據(jù): 學(xué)生表(Student)、課程表(Course)、選課表(SC):同時(shí),請(qǐng)編程完成以下指定功能: .... |
? 準(zhǔn)備工作:
■ 先啟動(dòng)hadoop,再啟動(dòng)hbase;(關(guān)閉:關(guān)閉先關(guān)hbase,再關(guān)閉hadoop)
□ 啟動(dòng)hadoop:
ssh localhost cd /usr/local/hadoop ./sbin/start-dfs.sh
□ 啟動(dòng)hbase:(因?yàn)閔base 咱將其bin 目錄配置了環(huán)境變量,相當(dāng)于全局變量了,在終端命令可以直接使用, 而hadoop 沒有配置系統(tǒng)的全局變量,所以需要切換到其安裝目錄下的sbin 目錄)
start-hbase.sh
□ 進(jìn)入shell界面:
hbase shell
□關(guān)閉hbase:
stop-hbase.sh
□關(guān)閉hadoop:
cd /usr/local/hadoop ./sbin/stop-dfs.sh
可能遇到bug:org.apache.hadoop.hbase.PleaseHoldException:Maste is initializing
logs中報(bào)錯(cuò):master.HMaster: Master failed to complete initialization after 900000ms. Please consider submitting a bug report including a thread dump of this process.
master.SplitLogManger:error while splitting logs in [hdfs:// localhost:9000/hbase/WALS...]
屋漏偏逢連夜雨:又卡住。。。。。
解決:ctr+c,關(guān)閉hbase、關(guān)閉hadoop,然后,關(guān)掉shell,重啟,然后重新登錄。。。(ssh localhost 又發(fā)現(xiàn)。。。,直覺,這個(gè)問題無傷大雅,跳過它)
Failed to connect to https://changelogs.ubuntu.com/meta-relese-lts.Check your Internet connection or proxy settings.
詭異的事。。。(前一秒成功創(chuàng)建了一張表,下一秒卡死)
日志顯示:【詭異的地方是在,我創(chuàng)建的表格名為user 就卡住,創(chuàng)建叫其他名字的表格就沒事】
警告:WARN[ProcExecTimeout] assignment.AssignmentManger:STUCK Region-In-Transition rit=OPENING, location=null,table=user,region=d7e...
然后又:ERROR:master.HMasterCommandLine:Master exiting。。。
至此越搞越多bug,我選擇重新安裝(因?yàn)橹匦掳惭b對(duì)于解決問題的是一個(gè)優(yōu)解,為什么不重新安裝呢,辦法沒有高低貴賤,高效即最好。)
也許一開始的原因在于刪除表的命令輸入先后順序有問題,我是先輸入:truncate 'user' (‘user’ 是表名) 然后輸入 drop 'user' #停用表之后才能刪除
(好像輸入 drop命令前已經(jīng)停表了哈哈哈)問題不大,重新裝一下即可
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
一、Hbase中使用Shell命令:
① HBase中創(chuàng)建表:(create 命令:第一個(gè)變量是表名,然后是列族名)
語法:create '表名稱','列族名稱1','列族名稱2','列族名稱N'
create 'student','Sname','Ssex','Sage','Sdept','course'
通過命令 describe 'student' 進(jìn)行查看表的結(jié)構(gòu):(desc ‘表名’,查看表的結(jié)構(gòu))
接下來是hbase 常規(guī)操作(增刪改查)
? 增加數(shù)據(jù)
② put:(put 命令:第一個(gè)變量是表名,第二個(gè)變量是行鍵,第三個(gè)變量開始就是添加列族情況啦(列限定符可選)
注意 put 命令:一次只能為一個(gè)表的一行數(shù)據(jù)的一個(gè)列,也就是一次只能給一個(gè)單元格添加一個(gè)數(shù)據(jù),
所以直接用shell命令插入數(shù)據(jù)效率很低,在實(shí)際應(yīng)用中,一般都是利用編程操作數(shù)據(jù)。
語法:put '表名稱','行名稱','列名稱:','值'
例子:student表添加了學(xué)號(hào)為95001,名字為L(zhǎng)iYing的一行數(shù)據(jù),其行鍵為95001。
put 'student', '95001','Sname','LiYing'
例子:為95001行下的course列族的math列添加了一個(gè)數(shù)據(jù):
put 'student','95001','course:math','80'
在表格中的樣子(大概如此,變量名不一定對(duì)得上哦,我只是為了展示表格的形式):
? 刪除數(shù)據(jù)
③ delete命令:
□ put 的反向操作:
□ 刪除 一行中的所有數(shù)據(jù):
□ 刪除 表:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
□ put 的反向操作:
delete 'student','95001','Sname:firstName'
□ 刪除 一行中的所有數(shù)據(jù):
deleteall 'student','95001'
□ 刪除表 :
disable 'student' #讓表不可用 drop 'student' #刪除表
? 查看數(shù)據(jù):
□ get: 查看的是一行中的數(shù)據(jù)
□ scan: 查看的是表中的全部數(shù)據(jù)
get 'student','95001'
scan 'student'
還可以查詢部分細(xì)節(jié)的數(shù)據(jù)等等細(xì)節(jié)的數(shù)據(jù),例如:查詢 某個(gè)列族的數(shù)據(jù):
?查詢表歷史數(shù)據(jù):
④ 查詢表的歷史版本,需要兩步。
1、在創(chuàng)建表的時(shí)候,指定保存的版本數(shù)(假設(shè)指定為5)
create 'teacher',{NAME=>'username',VERSIONS=>5}
2、插入數(shù)據(jù)然后更新數(shù)據(jù),使其產(chǎn)生歷史版本數(shù)據(jù),注意:這里插入數(shù)據(jù)和更新數(shù)據(jù)都是用put命令
put 'teacher','91001','username','Mary' put 'teacher','91001','username','Mary1' put 'teacher','91001','username','Mary2' put 'teacher','91001','username','Mary3' put 'teacher','91001','username','Mary4' put 'teacher','91001','username','Mary5'
3、查詢時(shí),指定查詢的歷史版本數(shù)。默認(rèn)會(huì)查詢出最新的數(shù)據(jù)
get 'teacher','91001',{COLUMN=>'username',VERSIONS=>3}
? 退出HBase數(shù)據(jù)庫操作
⑤ 命令: exit
注意:這里退出HBase數(shù)據(jù)庫是退出對(duì)數(shù)據(jù)庫表的操作,而不是停止啟動(dòng)HBase數(shù)據(jù)庫后臺(tái)運(yùn)行。
二、Hbase編程實(shí)踐:
? 準(zhǔn)備工作:導(dǎo)入jar包:
導(dǎo)包步驟:File -》 Project Structure -》Libraries -》+ -》選擇需要導(dǎo)入的包,然后記得導(dǎo)入完成后,點(diǎn)擊一下 Apply,再點(diǎn) Ok
(1) 進(jìn)入到“/usr/local/hbase/lib”目錄,選中該目錄下的所有jar文件(注意,不要選中client-facing-thirdparty、ruby、shaded-clients和zkcli這四個(gè)目錄)
(2) 進(jìn)入到“/usr/local/hbase/lib/client-facing-thirdparty”目錄, 選中該目錄下的所有jar文件。
例子:創(chuàng)建表,插入數(shù)據(jù),查看表中數(shù)據(jù)
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class ExampleForHBase {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void main(String[] args)throws IOException{
init(); //主要操作就是為了連接到數(shù)據(jù)庫hbase
createTable("student",new String[]{"score"}); //創(chuàng)建表,shell命令:create '表名','列族名1','列族名2','列族名3' ...
insertData("student","zhangsan","score","English","69"); //shell命令: put 'student','張三','score:English','69'
insertData("student","zhangsan","score","Math","86");
insertData("student","zhangsan","score","Computer","77");
getData("student", "zhangsan", "score","English");
close();
}
public static void init(){
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
try{
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
}catch (IOException e){
e.printStackTrace();
}
}
public static void close(){
try{
if(admin != null){
admin.close();
}
if(null != connection){
connection.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
public static void createTable(String myTableName,String[] colFamily) throws IOException {
TableName tableName = TableName.valueOf(myTableName);
if(admin.tableExists(tableName)){
System.out.println("talbe is exists!");
}else {
TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tableName);
for(String str:colFamily){
ColumnFamilyDescriptor family =
ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(str)).build();
tableDescriptor.setColumnFamily(family);
}
admin.createTable(tableDescriptor.build());
}
}
public static void insertData(String tableName,String rowKey,String colFamily,String col,String val) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(rowKey.getBytes());
put.addColumn(colFamily.getBytes(),col.getBytes(), val.getBytes());
table.put(put);
table.close();
}
public static void getData(String tableName,String rowKey,String colFamily, String col)throws IOException{
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(rowKey.getBytes());
get.addColumn(colFamily.getBytes(),col.getBytes());
Result result = table.get(get);
System.out.println(new String(result.getValue(colFamily.getBytes(),col==null?null:col.getBytes())));
table.close();
}
}
運(yùn)行程序后,到終端輸入:scan ‘student’ 查看一下
三、實(shí)驗(yàn):熟悉常用的HBase 操作:
(一)編程實(shí)現(xiàn)以下指定功能,并用 Hadoop 提供的 HBase Shell 命令完成相同任務(wù):
列出 HBase所有的表的相關(guān)信息,例如表名;
在終端打印出指定的表的所有記錄數(shù)據(jù);
向已經(jīng)創(chuàng)建好的表添加和刪除指定的列族或列;
清空指定的表的所有記錄數(shù)據(jù);
統(tǒng)計(jì)表的行數(shù)。
1.列出 HBase所有的表的相關(guān)信息,例如表名:
■ HBase Shell:List
■Java Api:
/**
* 同樣是正常的建立 數(shù)據(jù)庫連接,執(zhí)行操作,然后最后關(guān)閉連接
* 重點(diǎn)是:HTableDescriptor hTableDescriptors[] = admin.listTables(); 獲取到 表格列表,然后遍歷
*/
import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; import java.io.IOException; public class Test_1 { public static Configuration configuration; public static Connection connection; public static Admin admin; /** * 建立連接 */ public static void init() { configuration = HBaseConfiguration.create(); configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase"); try { connection = ConnectionFactory.createConnection(configuration); admin = connection.getAdmin(); } catch (IOException e) { e.printStackTrace(); } } /** * 關(guān)閉連接 */ public static void close() { try { if (admin != null) { admin.close(); } if (null != connection) { connection.close(); } } catch (IOException e) { e.printStackTrace(); } } /** * * 查看已有表,通過方法listTables() * * @throws IOException * */ public static void listTables() throws IOException { init(); HTableDescriptor hTableDescriptors[] = admin.listTables(); for (HTableDescriptor hTableDescriptor : hTableDescriptors) { System.out.println(hTableDescriptor.getNameAsString()); } close(); } public static void main(String[] args) { Test_1 t = new Test_1(); try { System.out.println("以下為Hbase 數(shù)據(jù)庫中所存的表信息"); t.listTables(); } catch (IOException e) { e.printStackTrace(); } } }
2.在終端打印出指定的表的所有記錄數(shù)據(jù);
■ HBase Shell:scan 'student'
■ Java Api:
/**
* 同樣是正常的建立 數(shù)據(jù)庫連接,執(zhí)行操作,然后最后關(guān)閉連接
* 重點(diǎn)是:
* Table table = connection.getTable(TableName.valueOf(tableName));獲取到表格對(duì)象
* Scan scan = new Scan(); ResultScanner scanner = table.getScanner(scan); 然后通過Scanner對(duì)象,獲取到ResultScanner掃描結(jié)果對(duì)象,遍歷輸出
*/
import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; import java.io.IOException; import java.util.Scanner; public class Test_2 { public static Configuration configuration; public static Connection connection; public static Admin admin; // 建立連接 public static void init() { configuration = HBaseConfiguration.create(); configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase"); try { connection = ConnectionFactory.createConnection(configuration); admin = connection.getAdmin(); } catch (IOException e) { e.printStackTrace(); } } // 關(guān)閉連接 public static void close() { try { if (admin != null) { admin.close(); } if (null != connection) { connection.close(); } } catch (IOException e) { e.printStackTrace(); } } /** * * 根據(jù)表名查找表信息 * */ public static void getData(String tableName) throws IOException { init(); Table table = connection.getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); ResultScanner scanner = table.getScanner(scan); for (Result result : scanner) { showCell((result)); } close(); } /** * * 格式化輸出 * * @param result * */ public static void showCell(Result result) { Cell[] cells = result.rawCells(); for (Cell cell : cells) { System.out.println("RowName(行鍵):" + new String(CellUtil.cloneRow(cell)) + " "); System.out.println("Timetamp(時(shí)間戳):" + cell.getTimestamp() + " "); System.out.println("column Family(列簇):" + new String(CellUtil.cloneFamily(cell)) + " "); System.out.println("column Name(列名):" + new String(CellUtil.cloneQualifier(cell)) + " "); System.out.println("value:(值)" + new String(CellUtil.cloneValue(cell)) + " "); System.out.println(); } } public static void main(String[] args) throws IOException { // TODO Auto-generated method stub Test_2 t = new Test_2(); System.out.println("請(qǐng)輸入要查看的表名"); Scanner scan = new Scanner(System.in); String tableName = scan.nextLine(); System.out.println("信息如下:"); t.getData(tableName); } }
3,向已經(jīng)創(chuàng)建好的表添加和刪除指定的列族或列:
■ HBase Shell:
put 'student','95003','Sname','wangjinxuan' (添加列)
put 'student','95003','Sname:nickName','wang' (添加列族)
put 'student','95003','Sname:firstName','jinxuan' (添加列族)
put的反向操作的delete:
delete 'student' ,’95003’,’Sname’
delete 'student' ,’95003’,’Sname:nickName’
deleteall 'student' ,’95003’ (刪除整個(gè)行記錄)
■ Java Api:
/**
* hbase只關(guān)注rowkey,column Family(列族),并沒有說在創(chuàng)建表的時(shí)候指定cq(列限定修飾符)有多少,這也是hbase列式存儲(chǔ)的特點(diǎn),
* 所以在hbase API中是沒有提供delete 一個(gè)列下的所有數(shù)據(jù)的
*
* 同樣是正常的建立 數(shù)據(jù)庫連接,執(zhí)行操作,然后最后關(guān)閉連接
* 1,Table table = connection.getTable(TableName.valueOf(tableName)); 先獲取到表
* 2,插入:(① 創(chuàng)建Put對(duì)象,② 然后通過方法 addColumn將列、列限定符、值 放到put對(duì)象,③ 最后將put對(duì)象put到表格)
* Put put = new Put(rowKey.getBytes());
* put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
* table.put(put);
* 3,刪除:
* Table table = connection.getTable(TableName.valueOf(tableName)); 同樣首先獲取到表
* Delete delete = new Delete(rowKey.getBytes()); //通過傳入行鍵,new一個(gè)刪除對(duì)象
* //刪除對(duì)象添加要被刪除的列或列族
* ① 刪除指定列族的所有數(shù)據(jù)(此情況是列族下無列限定符時(shí)的情況):delete.addFamily(colFamily.getBytes());
* ② 刪除指定列的數(shù)據(jù)(此列主要說的是列限定修飾符):delete.addColumn(colFamily.getBytes(), col.getBytes());
* table.delete(delete); //最后就是表格delete掉 delete對(duì)象
*/
import java.io.IOException;
import java.util.Scanner;
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.TableName;
import org.apache.hadoop.hbase.client.Admin;
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.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;
public class Test_3 {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
// 建立連接
public static void init() {
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
try {
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
// 關(guān)閉連接
public static void close() {
try {
if (admin != null) {
admin.close();
}
if (null != connection) {
connection.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 向某一行的某一列插入數(shù)據(jù)
*
* @param tableName 表名
* @param rowKey 行鍵
* @param colFamily 列族名
* @param col 列名(如果其列族下沒有子列,此參數(shù)可為空)
* @param val 值
* @throws IOException
*/
public static void insertRow(String tableName, String rowKey, String colFamily, String col, String val)throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(rowKey.getBytes());
put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
table.put(put);
table.close();
close();
}
/**
* 根據(jù)表名查找表信息
*/
public static void getData(String tableName) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
showCell((result));
}
close();
}
/**
*
* 格式化輸出
*
* @param result
*
*/
public static void showCell(Result result) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println("RowName(行鍵):" + new String(CellUtil.cloneRow(cell)) + " ");
System.out.println("Timetamp(時(shí)間戳):" + cell.getTimestamp() + " ");
System.out.println("column Family(列簇):" + new String(CellUtil.cloneFamily(cell)) + " ");
System.out.println("column Name(列名):" + new String(CellUtil.cloneQualifier(cell)) + " ");
System.out.println("value:(值)" + new String(CellUtil.cloneValue(cell)) + " ");
System.out.println();
}
}
/**
*
* 刪除數(shù)據(jù)
*
* @param tableName 表名
*
* @param rowKey 行鍵
*
* @param colFamily 列族名
*
* @param col 列名
*
* @throws IOException
*
*/
public static void deleteRow(String tableName, String rowKey, String colFamily, String col) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(rowKey.getBytes());
if(col == null) {
//刪除指定列族的所有數(shù)據(jù)(此情況是列族下無列限定符時(shí)的情況)
delete.addFamily(colFamily.getBytes());
table.delete(delete);
table.close();
}else {
//刪除指定列的數(shù)據(jù)(此列主要說的是列限定修飾符)
delete.addColumn(colFamily.getBytes(), col.getBytes());
table.delete(delete);
table.close();
}
close();
}
public static void main(String[] args) {
Test_3 t = new Test_3();
boolean flag = true;
while (flag){
System.out.println("------------向已經(jīng)創(chuàng)建好的表中添加和刪除指定的列簇或列--------------------");
System.out.println(" 請(qǐng)輸入您要進(jìn)行的操作 1- 添加 2-刪除 ");
Scanner scan = new Scanner(System.in);
String choose1 = scan.nextLine();
switch (choose1) {
case "1":
try {
//put 'student','95003','Sname','wangjinxuan' (添加列)
//put 'student','95003','Sname:nickName','wang' (添加列族)
//put 'student','95003','Sname:firstName','jinxuan' (添加列族)
// t.insertRow(tableName, rowKey, colFamily, col, val);
t.insertRow("student", "95003", "Sname",null, "wangjingxuan");
t.insertRow("student", "95003", "Sname", "nickName", "wang");
t.insertRow("student", "95003", "Sname", "firstName", "jingxuan");
System.out.println("插入成功:");
t.getData(tableName);
} catch (IOException e) {
e.getMessage();
}
break;
case "2":
try {
System.out.println("----------------------刪除前,表的原本信息如下---------------------");
t.getData(tableName);
//delete 'student' ,’95003’,’Sname’
//delete 'student' ,’95003’,’Sname:nickName’
// t.deleteRow(tableName, rowKey, colFamily, col);
t.deleteRow("student", "95003", "Sname", "firstName");
System.out.println("-----------------------刪除成功-----------------------------
");
System.out.println("---------------------刪除后,表的信息如下---------------------");
t.getData(tableName);
} catch (IOException e) {
e.getMessage();
}
break;
}
System.out.println(" 你要繼續(xù)操作嗎? 是-true 否-false ");
flag = scan.nextBoolean();
}
System.out.println(" 程序已退出! ");
}
}
4,清空指定的表的所有記錄數(shù)據(jù):
■ HBase Shell:truncate 'student'
■ Java Api:
import java.io.IOException;
import java.util.Scanner;
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.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.HBaseAdmin;
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.util.Bytes;
public class Test_4 {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
// 建立連接
public static void init() {
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
try {
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
// 關(guān)閉連接
public static void close() {
try {
if (admin != null) {
admin.close();
}
if (null != connection) {
connection.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*
* 清空制定的表的所有記錄數(shù)據(jù)
*
* @param args
*
* @throws IOException
*
*/
public static void clearRows(String tableName) throws IOException {
init();
HBaseAdmin admin1 = new HBaseAdmin(configuration);
// 讀取了之前表的表名 列簇等信息,然后再進(jìn)行刪除操作。
HTableDescriptor tDescriptor = admin1.getTableDescriptor(Bytes.toBytes(tableName));
// 總思想是先將原表結(jié)構(gòu)保留下來,然后進(jìn)行刪除,再重新依據(jù)保存的信息重新創(chuàng)建表。
TableName tablename = TableName.valueOf(tableName);
// 刪除表
admin.disableTable(tablename);
admin.deleteTable(tablename);
// 重新建表
admin.createTable(tDescriptor);
close();
}
/**
*
* 根據(jù)表名查找表信息
*
*/
public static void getData(String tableName) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner){
showCell((result));
}
close();
}
/**
*
* 格式化輸出
*
* @param result
*
*/
public static void showCell(Result result) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println("RowName(行鍵):" + new String(CellUtil.cloneRow(cell)) + " ");
System.out.println("Timetamp(時(shí)間戳):" + cell.getTimestamp() + " ");
System.out.println("column Family(列簇):" + new String(CellUtil.cloneFamily(cell)) + " ");
System.out.println("column Name(列名):" + new String(CellUtil.cloneQualifier(cell)) + " ");
System.out.println("value:(值)" + new String(CellUtil.cloneValue(cell)) + " ");
System.out.println();
}
}
public static void main(String[] args) {
Test_4 test_4 = new Test_4();
Scanner scan = new Scanner(System.in);
System.out.println("請(qǐng)輸入要清空的表名");
String tableName = scan.nextLine();
try {
System.out.println("表原來的信息:");
test_4.getData(tableName);
test_4.clearRows(tableName);
System.out.println("表已清空:");
} catch (IOException e) {
e.printStackTrace();
}
}
}
5,統(tǒng)計(jì)表的行數(shù):
■ HBase Shell:count 'student'
■ Java Api:
import java.io.IOException;
import java.util.Scanner;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
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;
public class Test_5 {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
//建立連接
public static void init() {
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
try {
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
// 關(guān)閉連接
public static void close() {
try {
if (admin != null) {
admin.close();
}
if (null != connection) {
connection.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void countRows(String tableName) throws IOException{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
int num = 0;
for (Result result = scanner.next(); result != null; result = scanner.next()){
num++;
}
System.out.println("行數(shù):" + num);
scanner.close();
close();
}
public static void main(String[] args) throws IOException {
Test_5 test_5 = new Test_5();
Scanner scan = new Scanner(System.in);
System.out.println("請(qǐng)輸入要統(tǒng)計(jì)行數(shù)的表名");
String tableName = scan.nextLine();
test_5.countRows(tableName);
}
}
(二)現(xiàn)有以下關(guān)系型數(shù)據(jù)庫中的表和數(shù)據(jù),要求將其轉(zhuǎn)換為適合于HBase存儲(chǔ)的表并插入數(shù)據(jù):
學(xué)生表(Student)、課程表(Course)、選課表(SC):同時(shí),請(qǐng)編程完成以下指定功能:
create 'Student','S_No','S_Name','S_Sex','S_Age' put 'Student','s001','S_No','2015001' put 'Student','s001','S_Name','Zhangsan' put 'Student','s001','S_Sex','male' put 'Student','s001','S_Age','23' put 'Student','s002','S_No','2015002' put 'Student','s002','S_Name','Mary' put 'Student','s002','S_Sex','female' put 'Student','s002','S_Age','22' put 'Student','s003','S_No','2015003' put 'Student','s003','S_Name','Lisi' put 'Student','s003','S_Sex','male' put 'Student','s003','S_Age','24' ————————————————————————————————————————————————————————————————————————————— create 'Course','C_No','C_Name','C_Credit' put 'Course','c001','C_No','123001' put 'Course','c001','C_Name','Math' put 'Course','c001','C_Credit','2.0' put 'Course','c002','C_No','123002' put 'Course','c002','C_Name','Computer' put 'Course','c002','C_Credit','5.0' put 'Course','c003','C_No','123003' put 'Course','c003','C_Name','English' put 'Course','c003','C_Credit','3.0' ———————————————————————————————————————————————————————————————————————————————— put 'SC','sc001','SC_Sno','2015001' put 'SC','sc001','SC_Cno','123001' put 'SC','sc001','SC_Score','86' put 'SC','sc002','SC_Sno','2015001' put 'SC','sc002','SC_Cno','123003' put 'SC','sc002','SC_Score','69' put 'SC','sc003','SC_Sno','2015002' put 'SC','sc003','SC_Cno','123002' put 'SC','sc003','SC_Score','77' put 'SC','sc004','SC_Sno','2015002' put 'SC','sc004','SC_Cno','123003' put 'SC','sc004','SC_Score','99' put 'SC','sc005','SC_Sno','2015003' put 'SC','sc005','SC_Cno','123001' put 'SC','sc005','SC_Score','98' put 'SC','sc006','SC_Sno','2015003' put 'SC','sc006','SC_Cno','123002' put 'SC','sc006','SC_Score','95'
同時(shí),請(qǐng)編程完成以下指定功能:
① createTable(String tableName, String[] fields):創(chuàng)建表,參數(shù)tableName為表的名稱,字符串?dāng)?shù)組fields為存儲(chǔ)記錄各個(gè)域名稱的數(shù)組。
(域名稱即列族名稱啦)要求當(dāng)HBase已經(jīng)存在名為tableName的表的時(shí)候,先刪除原有的表,然后再創(chuàng)建新的表。
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import java.io.IOException;
public class CreateTable {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void createTable(String tableName, String[] fields) throws IOException {
init();
TableName tablename = TableName.valueOf(tableName);
if (admin.tableExists(tablename)) {
System.out.println("table is exists!");
admin.disableTable(tablename);
admin.deleteTable(tablename);
}
HTableDescriptor hTableDescriptor = new HTableDescriptor(tablename);
for (String str : fields) {
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
close();
}
public static void init() {
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
try {
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void close() {
try {
if (admin != null) {
admin.close();
}
if (null != connection) {
connection.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String[] fields = {"Score"};
try {
createTable("person", fields);
} catch (IOException e) {
e.printStackTrace();
}
}
}
②addRecord(String tableName, String row, String[] fields, String[] values): 向表tableName、行row(用S_Name表示)和字符串?dāng)?shù)組files指定的單元格中添加對(duì)應(yīng)的數(shù)據(jù)values。其中fields中每個(gè)元素如果對(duì)應(yīng)的列族下還有相應(yīng)的列限定符的話,用"columnFamily:column"表示。例如,同時(shí)向"Math"、“Computer Science”、"English"三列添加成績(jī)時(shí),字符串?dāng)?shù)組fields為{“Score:Math”, “Score: Computer Science”, “Score:English”},數(shù)組values存儲(chǔ)這三門課的成績(jī)。
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
public class AddRecord {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void addRecord(String tableName, String row, String[] fields, String[] values) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
for (int i = 0; i != fields.length; i++) {
Put put = new Put(row.getBytes());
String[] cols = fields[i].split(":");
put.addColumn(cols[0].getBytes(), cols[1].getBytes(), values[i].getBytes());
table.put(put);
}
table.close();
close();
}
public static void init() {
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
try {
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void close() {
try {
if (admin != null) {
admin.close();
}
if (null != connection) {
connection.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String[] fields = {"Score:Math", "Score:Computer Science", "Score:English"};
String[] values = {"99", "80", "100"};
try {
addRecord("person", "Score", fields, values);
} catch (IOException e) {
e.printStackTrace();
}
}
}
③ scanColumn(String tableName, String column): 瀏覽表tableName某一列的數(shù)據(jù),如果某一行記錄中該列數(shù)據(jù)不存在,則返回null。要求當(dāng)參數(shù)column為某一列族名稱時(shí),如果底下有若干個(gè)列限定符,則要列出每個(gè)列限定符代表的列的數(shù)據(jù);當(dāng)參數(shù)column為某一列具體名稱(例如"Score:Math")時(shí),只需要列出該列的數(shù)據(jù)。
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.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class ScanColumn {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void scanColumn(String tableName, String column) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
scan.addFamily(Bytes.toBytes(column));
ResultScanner scanner = table.getScanner(scan);
for (Result result = scanner.next(); result != null; result = scanner.next()) {
showCell(result);
}
table.close();
close();
}
public static void showCell(Result result) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println("RowName:" + new String(CellUtil.cloneRow(cell)) + " ");
System.out.println("Timetamp:" + cell.getTimestamp() + " ");
System.out.println("column Family:" + new String(CellUtil.cloneFamily(cell)) + " ");
System.out.println("row Name:" + new String(CellUtil.cloneQualifier(cell)) + " ");
System.out.println("value:" + new String(CellUtil.cloneValue(cell)) + " ");
}
}
public static void init() {
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
try {
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
// 關(guān)閉連接
public static void close() {
try {
if (admin != null) {
admin.close();
}
if (null != connection) {
connection.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
scanColumn("person", "Score");
} catch (IOException e) {
e.printStackTrace();
}
}
}
④ modifyData(String tableName, String row, String column):修改表tableName,行row(可以用學(xué)生姓名S_Name表示),列column指定的單元格的數(shù)據(jù)。
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
public class ModifyData {
public static long ts;
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void modifyData(String tableName, String row, String column, String val) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(row.getBytes());
Scan scan = new Scan();
ResultScanner resultScanner = table.getScanner(scan);
for (Result r : resultScanner) {
for (Cell cell : r.getColumnCells(row.getBytes(), column.getBytes())) {
ts = cell.getTimestamp();
}
}
put.addColumn(row.getBytes(), column.getBytes(), ts, val.getBytes());
table.put(put);
table.close();
close();
}
public static void init() {
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
try {
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void close() {
try {
if (admin != null) {
admin.close();
}
if (null != connection) {
connection.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
modifyData("person", "Score", "Math", "100");
} catch (IOException e) {
e.printStackTrace();
}
}
}
⑤ deleteRow(String tableName, String row):刪除表tableName中row指定的行的記錄。
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
public class ModifyData {
public static long ts;
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void modifyData(String tableName, String row, String column, String val) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(row.getBytes());
Scan scan = new Scan();
ResultScanner resultScanner = table.getScanner(scan);
for (Result r : resultScanner) {
for (Cell cell : r.getColumnCells(row.getBytes(), column.getBytes())) {
ts = cell.getTimestamp();
}
}
put.addColumn(row.getBytes(), column.getBytes(), ts, val.getBytes());
table.put(put);
table.close();
close();
}
public static void init() {
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
try {
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void close() {
try {
if (admin != null) {
admin.close();
}
if (null != connection) {
connection.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
modifyData("person", "Score", "Math", "100");
} catch (IOException e) {
e.printStackTrace();
}
}
}
參考:
《HBase2.2.2安裝和編程實(shí)踐指南_廈大數(shù)據(jù)庫實(shí)驗(yàn)室博客 (xmu.edu.cn)》
《實(shí)驗(yàn)3 熟悉常用的 HBase 操作》https://blog.csdn.net/qq_38648558/article/details/83033050
《實(shí)驗(yàn)3_熟悉常用的HBase操作》https://blog.csdn.net/qq_50596778/article/details/120552574
總結(jié)
以上是生活随笔為你收集整理的HBase的编程实践(实验3_熟悉常用的HBase操作)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 缓冲区溢出原理及防御(转)
- 下一篇: weka 学习总结(持续)