大數據hadoop學習【9】-----通過JAVA語言編程,實現對Hbase數據庫表及數據的相關操作目錄
上次的hadoop學習,我們瞭解瞭如何通過Hbase的shell界面相關shell命令,對數據庫中表及表中數據進行操作!
但其實在很多時候,我們需要的是通過程序編程,完成與用戶的界面交互功能,肯定不能讓用戶來進行shell命令進行相關數據的操作吧,這時候,我們需要通過Java語言來進行對Hbase的數據庫的訪問操作,本次博客,林君學長主要帶大家瞭解,如何通過JAVA編程,來對HBase數據庫進行訪問並且操作
- 操作系統:ubuntuKylin-16.04
- hadoop版本:hadoop-2.7.7
- hbase版本:hbase-1.4.13
- java版本:jdk-1.7
一、Hbase數據庫的運行
1、運行SSH
1)、新建終端,輸入以下命令切換hadoop賬戶,然後運行ssh
su - hadoop
ssh localhost
2、運行hadoop
1)、終端輸入以下命令,運行hadoop
start-dfs.sh
jps
3、運行Hbase
1)、當前終端輸入如下命令,運行Hbase
start-hbase.sh
jps
上面三步執行之後,便可以隱藏終端,不要關閉哦!
二、eclipse整體Java項目介紹
1、創建屬於hbase的Java Project
記住上面的JSE的選擇,一定要選擇1.5版本,因爲後面的代碼需要這個版本的,高於版本的不支持有些代碼,需要自己重新編寫!
2、導入Hbase所需要的基本庫
1)、找到安裝hbase路徑的地方,然後導入路徑 /usr/local/hbase/lib文件中的所有的庫:
3、創建相關的包、類
三、通過JAVA編程,實現對Hbase數據庫的連接與關閉 ,已經菜單函數的編寫
1、通過Java編程實現對Hbase數據庫的連接
1)、java代碼如下所示:
//建立連接
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();
}
}
2、通過Java編程實現對Hbase數據庫的關閉
1)、java代碼如下所示:
//關閉連接
public static void close(){
try{
if(admin != null){
admin.close();
}
if(null != connection){
connection.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
3、菜單交互界面主函數
1)、java代碼如下所示:
public static void main(String[] args) throws IOException{
HDataBase h1=new HDataBase();
@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
while(true){
System.out.println("**********************基於JAVA的Hbase數據庫表及表的基本操作**********************");
System.out.println("1、向Hbase數據庫創建一張表");
System.out.println("2、查看數據庫中已存在的表");
System.out.println("3、爲創建的表的某一行的某一列插入數據");
System.out.println("4、根據行鍵rowkey查找數據");
System.out.println("5、刪除表中的數據");
System.out.println("6、刪除指定表");
System.out.println("0、退出!");
System.out.print("請輸入你的選擇:");
int a=in.nextInt();
switch(a){
case 1: h1.createTable("Score",new String[]{"sname","course"});break;
case 2: h1.listTables();break;
case 3: h1.insertRow("Score", "10001", "sname"," ","chenYiYue");break;
case 4: h1.getData("Score", "10001", "sname", " ");break;
case 5: h1.deleteRow("Score", "10001", "sname", "chenYiYue");break;
case 6: h1.deleteTable("Score");break;
case 0:break;
}
}
}
2)、運行結果如下:
四、Java編程實現對Hbase數據庫表操作
1、通過JAVA編程,實現對Hbase數據庫表的創建
1)、java程序如下所示:
public void createTable(String myTableName,String[] colFamily) throws IOException {
init();
TableName tableName = TableName.valueOf(myTableName);
if(admin.tableExists(tableName)){
System.out.println("該表已經存在!");
}else {
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
for(String str:colFamily){
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
System.out.println("表創建成功!");
}
close();
}
通過輸入需要穿件的表名,以及包括的列名組,來進行hbase表的創建
如:createTable(“Score”,new String[]{“sname”,“course”})
2)、運行結果如下所示:
3)、在終端進入hbase的shell界面,通過shell命令查詢該表是否完成創建
hbase shell
list
可以看出,上表Score已經完成創建!
2、通過JAVA編程,實現對Hbase數據庫表的查詢
1)、java代碼如下所示:
public void listTables() throws IOException {
init();
HTableDescriptor hTableDescriptors[] = admin.listTables();
for(HTableDescriptor hTableDescriptor :hTableDescriptors){
System.out.println(hTableDescriptor.getNameAsString());
}
close();
}
上面函數的主要功能就是查詢出Hbase數據庫中的所有表,並在終端顯示出來
2)、運行結果如下所示:
3)、終端查詢是否一致
上圖可以看出,完全一致!
3、通過JAVA編程,實現對Hbase數據庫表的刪除
1)、java代碼如下所示:
public void deleteTable(String tableName) throws IOException {
init();
TableName tn = TableName.valueOf(tableName);
if (admin.tableExists(tn)) {
admin.disableTable(tn);
admin.deleteTable(tn);
System.out.println("表:"+tableName+"已成功刪除");
}
close();
}
通過輸入Hbase的表名,完成對數據庫表的刪除,如:deleteTable(“Score”)
2)、運行結果如下所示:
3)、在shell界面查詢是否刪除成功
終端已經沒有該表了!以上步驟建議在完成以下數據操作步驟後執行!
五、Java實現對數據庫表中數據的操作
1、通過JAVA編程,實現對Hbase數據庫表中數據的插入
1)、java代碼如下所示:
public 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());
System.out.println("數據插入成功");
table.put(put);
table.close();
close();
}
通過輸入表名、行鍵(可以理解爲主鍵,主鍵唯一)、列族名、列族名下的列名、已經插入的數據進行表中數據的插入,如:insertRow(“Score”, “10001”, “sname”," ",“chenYiYue”),由於sname下面沒有其他的列名,所以我們輸入空就行
2)、運行結果如下所示:
3)、shell界面查詢是否成功插入數據
get 'Score','10001'
數據已成功插入!
2、通過JAVA編程,實現對Hbase數據庫表中數據的查詢
1)、java代碼如下所示:
public void getData(String tableName,String rowKey,String colFamily,String col)throws IOException{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(rowKey.getBytes());
get.addColumn(colFamily.getBytes(),col.getBytes());
Result result = table.get(get);
showCell(result);
table.close();
close();
}
public 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))+" ");
}
}
通過輸入表名、行鍵(主鍵)、以及列族名,和列族名下的列名,如:
getData(“Score”, “10001”, “sname”," "),由於該sname列名下沒有列名,所以我們輸入空
2)、查詢的結果如下所示:
3)、在shell界面查看查詢的內容是否一致
get 'Score','10001','sname'
可以看出,完全一致!
3、通過JAVA編程,實現對Hbase數據庫表中數據的刪除
1)、java代碼如下所示:
public 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());
//刪除指定列族的所有數據
//delete.addFamily(colFamily.getBytes());
//刪除指定列的數據
//delete.addColumn(colFamily.getBytes(), col.getBytes());
table.delete(delete);
System.out.println("信息已經刪除");
table.close();
close();
}
通過指定表名、行鍵(主鍵唯一)、列族名、和列族名下的數據進行數據的刪除,如:deleteRow(“Score”, “10001”, “sname”, “chenYiYue”)
2)、運行結果如下所示:
3)、在shell界面查詢該條數據是否刪除成功:
get 'Score','10001','sname'
可以看出,sname下的數據chenyiyue已經被成功刪除,該列下已經沒有數據了!
六、項目整體代碼
1、菜單類
package view;
import hbaseFile.HDataBase;
import java.io.IOException;
import java.util.Scanner;
public class Menu {
public static void main(String[] args) throws IOException{
HDataBase h1=new HDataBase();
@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
while(true){
System.out.println("**********************基於JAVA的Hbase數據庫表及表的基本操作**********************");
System.out.println("1、向Hbase數據庫創建一張表");
System.out.println("2、查看數據庫中已存在的表");
System.out.println("3、爲創建的表的某一行的某一列插入數據");
System.out.println("4、根據行鍵rowkey查找數據");
System.out.println("5、刪除表中的數據");
System.out.println("6、刪除指定表");
System.out.println("0、退出!");
System.out.print("請輸入你的選擇:");
int a=in.nextInt();
switch(a){
case 1: h1.createTable("Score",new String[]{"sname","course"});break;
case 2: h1.listTables();break;
case 3: h1.insertRow("Score", "10001", "sname"," ","chenYiYue");break;
case 4: h1.getData("Score", "10001", "sname", "chenYiYue");break;
case 5: h1.deleteRow("Score", "10001", "sname", "chenYiYue");break;
case 6: h1.deleteTable("Score");break;
case 0:break;
}
}
}
}
2、Hbase數據庫操作類
package hbaseFile;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
public class HDataBase{
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();
}
}
//關閉連接
public static void close(){
try{
if(admin != null){
admin.close();
}
if(null != connection){
connection.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
/**
* 建表。HBase的表中會有一個系統默認的屬性作爲主鍵,主鍵無需自行創建,默認爲put命令操作中表名後第一個數據,因此此處無需創建id列
* @param myTableName 表名
* @param colFamily 列族名
* @throws IOException
*/
public void createTable(String myTableName,String[] colFamily) throws IOException {
init();
TableName tableName = TableName.valueOf(myTableName);
if(admin.tableExists(tableName)){
System.out.println("該表已經存在!");
}else {
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
for(String str:colFamily){
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
System.out.println("表創建成功!");
}
close();
}
/**
* 刪除指定表
* @param tableName 表名
* @throws IOException
*/
public void deleteTable(String tableName) throws IOException {
init();
TableName tn = TableName.valueOf(tableName);
if (admin.tableExists(tn)) {
admin.disableTable(tn);
admin.deleteTable(tn);
System.out.println("表:"+tableName+"已成功刪除");
}
close();
}
/**
* 查看已有表
* @throws IOException
*/
public void listTables() throws IOException {
init();
HTableDescriptor hTableDescriptors[] = admin.listTables();
for(HTableDescriptor hTableDescriptor :hTableDescriptors){
System.out.println(hTableDescriptor.getNameAsString());
}
close();
}
/**
* 向某一行的某一列插入數據
* @param tableName 表名
* @param rowKey 行鍵
* @param colFamily 列族名
* @param col 列名(如果其列族下沒有子列,此參數可爲空)
* @param val 值
* @throws IOException
*/
public 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());
System.out.println("數據插入成功");
table.put(put);
table.close();
close();
}
/**
* 刪除數據
* @param tableName 表名
* @param rowKey 行鍵
* @param colFamily 列族名
* @param col 列名
* @throws IOException
*/
public 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());
//刪除指定列族的所有數據
//delete.addFamily(colFamily.getBytes());
//刪除指定列的數據
//delete.addColumn(colFamily.getBytes(), col.getBytes());
table.delete(delete);
System.out.println("信息已經刪除");
table.close();
close();
}
/**
* 根據行鍵rowkey查找數據
* @param tableName 表名
* @param rowKey 行鍵
* @param colFamily 列族名
* @param col 列名
* @throws IOException
*/
public void getData(String tableName,String rowKey,String colFamily,String col)throws IOException{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(rowKey.getBytes());
get.addColumn(colFamily.getBytes(),col.getBytes());
Result result = table.get(get);
showCell(result);
table.close();
close();
}
/**
* 格式化輸出
* @param result
*/
public 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))+" ");
}
}
}
3、程序關閉
每次操作之後,記得關閉自己的hadoop已經hbase,養成良好的操作習慣!
stop-hbase.sh
stop-dfs.sh
exit
以上就是本次博客的全部內容啦,希望閱讀的小夥伴可以懂得如何通過java代碼實現對Hbase數據庫的操作,代碼不要照搬,理解代碼纔是硬道理,一定要熟悉後理解哦!
遇到問題的小夥伴記得評論區留言,林君學長看到會爲大家解答的,這個學長不太冷!
陳一月的又一天編程歲月^ _ ^