文章目錄
- 前言
- Ubantu18.04 安裝Eclipse
- Ubantu18.04 安裝 Maven
- Eclipse 新建 Maven 工程
- 代碼
- 運行代碼操作 Hbase-JavaAPI
- 總結
前言
這是一個比較簡單的實驗,麻煩的地方在于需要在 Vmware + Ubantu18.04 中安裝好 Eclipse 和 Maven, 由于過于簡單,所以直接貼出我完成這個實驗的時候借鑒的實用博客!希望大家可以順利完成該實驗!💪

Ubantu18.04 安裝Eclipse
Ubantu18.04 安裝Eclipse
Ubantu18.04 安裝 Maven
Ubantu18.04 安裝 Maven
Eclipse 新建 Maven 工程
-
按照下文方式配置和創建
Eclipse中配置Maven -
配置 pom.xml 檔案,匯入 Hbase 的依賴包
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.3.5</version>
</dependency>
</dependencies>
代碼
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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.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;
import org.apache.hadoop.hbase.util.Bytes;
public class HbaseJavaApi {
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();
}
}
/**
* 建表,引數tableName為表的名稱,字串陣列fields為存盤記錄各個域名稱的陣列,
* 要求當HBase已經存在名為tableName的表時,先洗掉原有的表,然后再
* 創建新的表 field:列族
* @param tableName 表名
* @param fields 列族名
* @throws IOException
*/
public static void createTable(String tableName,String[] fields) throws IOException {
init();
TableName tablename = TableName.valueOf(tableName);
if(admin.tableExists(tablename)){
System.out.println("表已存在,將執行洗掉原表,重建新表!");
admin.disableTable(tablename);
admin.deleteTable(tablename);//洗掉原來的表
}
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
for(String str:fields){
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
System.out.println("表已創建成功");
close();
}
/**
* 向表 tableName、行 row(用 S_Name 表示)和字串陣列 fields 指定的單元格中
* 添加對應的資料 values,
* 其中,fields 中每個元素如果對應的列族下還有相應的列限定符的話,
* 用“columnFamily:column”表示,
* 例如,同時向“Math”、“Computer Science”、“English”三列添加成績時,
* 字串陣列 fields 為{“Score:Math”, ”Score:Computer Science”, ”Score:English”},
* 陣列values 存盤這三門課的成績,
*/
public static void addRecord(String tableName,String rowKey,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(rowKey.getBytes());
String [] cols = fields[i].split(":");
if(cols.length==1)
{
put.addColumn(cols[0].getBytes(), "".getBytes(), values[i].getBytes());//因為當輸入的是單列族,split僅讀出一個字符字串,即cols僅有一個元素
}
else {
put.addColumn(cols[0].getBytes(), cols[1].getBytes(), values[i].getBytes());
}
table.put(put);
}
table.close();
close();
}
/**
* 根據表名查找表資訊
*/
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(時間戳):"+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();
}
}
/**
* 瀏覽表 tableName 某一列的資料,如果某一行記錄中該列資料不存在,則回傳 null,
* 要求當引數 column 為某一列族名稱時,如果底下有若干個列限定符,則要列出每個列限定符代表的列的資料;
* 當引數 column 為某一列具體名稱(例如“Score:Math”)時,只需要列出該列的資料,
* @param tableName
* @param column
* @throws IOException
*/
public static void scanColumn (String tableName,String column) throws IOException
{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
String [] cols = column.split(":");
if(cols.length==1)
{
scan.addFamily(Bytes.toBytes(column));
}
else {
scan.addColumn(Bytes.toBytes(cols[0]),Bytes.toBytes(cols[1]));
}
ResultScanner scanner = table.getScanner(scan);
for (Result result = scanner.next(); result !=null;result = scanner.next()) {
showCell(result);
}
table.close();
close();
}
/**
* 修改表 tableName,行 row(可以用學生姓名 S_Name 表示),列 column 指定的單元格的資料,
* @throws IOException
*/
public static void modifyData(String tableName,String rowKey,String column,String value) throws IOException
{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(rowKey.getBytes());
String [] cols = column.split(":");
if(cols.length==1)
{
put.addColumn(column.getBytes(),"".getBytes() , value.getBytes());//qualifier:列族下的列名
}
else {
put.addColumn(cols[0].getBytes(),cols[1].getBytes() , value.getBytes());//qualifier:列族下的列名
}
table.put(put);
table.close();
close();
}
/**
* 洗掉表 tableName 中 row 指定的行的記錄,
* @throws IOException
*/
public static void deleteRow(String tableName,String rowKey) throws IOException
{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(rowKey.getBytes());
table.delete(delete);
table.close();
close();
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
HbaseJavaApi hbaseJavaApi = new HbaseJavaApi();
boolean flag =true;
while(flag)
{
System.out.println("------------------------------------------------提供以下功能----------------------------------------------");
System.out.println(" 1- createTable(創建表 ,提供表名、列族名) ");
System.out.println(" 2-addRecord (向已知表名、行鍵、列簇的表添加值) ");
System.out.println(" 3- ScanColumn(瀏覽表 某一列的資料) ");
System.out.println(" 4- modifyData(修改某表 某行,某一列,指定的單元格的資料) ");
System.out.println(" 5- deleteRow(洗掉 某表 某行的記錄) ");
System.out.println("------------------------------------------------------------------------------------------------------------------");
Scanner scan = new Scanner(System.in);
String choose1=scan.nextLine();
switch (choose1) {
case "1":
{
System.out.println("請輸入要創建的表名");
String tableName=scan.nextLine();
System.out.println("請輸入要創建的表的列族個數");
int Num=scan.nextInt();
String [] fields = new String[Num];
System.out.println("請輸入要創建的表的列族");
/* Scanner scanner = new Scanner(System.in); scanner.next 如不是全域,即會記得上一次輸出,相同地址讀入值時*/
for(int i=0;i< fields.length;i++)
{
/*BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
fields[i] = in.readLine();*/
/*fields[i]=scan.next(); 因為之前沒有輸入過,所以可以讀入新值*/
scan = new Scanner(System.in);
fields[i]=scan.nextLine();
}
System.out.println("正在執行創建表的操作");
hbaseJavaApi.createTable(tableName,fields);
break;
}
case "2":
{
System.out.println("請輸入要添加資料的表名");
String tableName=scan.nextLine();
System.out.println("請輸入要添加資料的表的行鍵");
String rowKey=scan.nextLine();
System.out.println("請輸入要添加資料的表的列的個數");
int num =scan.nextInt();
String fields[]=new String[num];
System.out.println("請輸入要添加資料的表的列資訊 共"+num+"條資訊");
for(int i=0;i< fields.length;i++)
{
BufferedReader in3= new BufferedReader(new InputStreamReader(System.in));
fields[i] = in3.readLine();
/*fields[i]=scan.next(); 因為之前沒有輸入過,所以可以讀入新值*/
}
System.out.println("請輸入要添加的資料資訊 共"+num+"條資訊");
String values[]=new String[num];
for(int i=0;i< values.length;i++)
{
BufferedReader in2 = new BufferedReader(new InputStreamReader(System.in));
values[i] = in2.readLine();
}
System.out.println("原表資訊");
hbaseJavaApi.getData(tableName);
System.out.println("正在執行向表中添加資料的操作........\n");
hbaseJavaApi.addRecord(tableName, rowKey, fields, values);
System.out.println("\n添加后的表的資訊........");
hbaseJavaApi.getData(tableName);
break;
}
case "3":
{
System.out.println("請輸入要查看資料的表名");
String tableName=scan.nextLine();
System.out.println("請輸入要查看資料的列名");
String column=scan.nextLine();
System.out.println("查看的資訊如下:........\n");
hbaseJavaApi.scanColumn(tableName, column);
break;
}
case "4":
{
System.out.println("請輸入要修改資料的表名");
String tableName=scan.nextLine();
System.out.println("請輸入要修改資料的表的行鍵");
String rowKey=scan.nextLine();
System.out.println("請輸入要修改資料的列名");
String column=scan.nextLine();
System.out.println("請輸入要修改的資料資訊 ");
String value=scan.nextLine();
System.out.println("原表資訊如下:........\n");
hbaseJavaApi.getData(tableName);
System.out.println("正在執行向表中修改資料的操作........\n");
hbaseJavaApi.modifyData(tableName, rowKey, column, value);
System.out.println("\n修改后的資訊如下:........\n");
hbaseJavaApi.getData(tableName);
break;
}
case "5":
{
System.out.println("請輸入要洗掉指定行的表名");
String tableName=scan.nextLine();
System.out.println("請輸入要洗掉指定行的行鍵");
String rowKey=scan.nextLine();
System.out.println("原表資訊如下:........\n");
hbaseJavaApi.getData(tableName);
System.out.println("正在執行向表中洗掉資料的操作........\n");
hbaseJavaApi.deleteRow(tableName, rowKey);
System.out.println("\n洗掉后的資訊如下:........\n");
hbaseJavaApi.getData(tableName);
break;
}
default:
{
System.out.println(" 你的操作有誤 !!! ");
break;
}
}
System.out.println(" 你要繼續操作嗎? 是-true 否-false ");
flag=scan.nextBoolean();
}
System.out.println(" 程式已退出! ");
}
}
運行代碼操作 Hbase-JavaAPI
如下圖按照控制臺列印輸出提示進行相關操作即可
- 運行 main 方法

- 根據提示進行操作







總結
Hbase 的Java API 操作都相對簡單,有點像操作JDBC,希望能夠幫助到大家完成實驗!學到的童鞋點個贊呀!👍👍!
堅持分享,堅持原創,喜歡博主的靚仔靚女們可以看看博主的首頁博客!
您的點贊與收藏是我分享博客的最大贊賞!
博主博客地址: https://blog.csdn.net/weixin_43967679
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/275757.html
標籤:其他
下一篇:堆排序(JAVA)
