目錄
這篇文章的源碼比較多,需要的私信我
需求與數(shù)據(jù)集
準(zhǔn)備工作
下載安裝maven
下載安裝idea
配置國內(nèi)的maven鏡像庫
創(chuàng)建一個maven工程
修改pom文件,導(dǎo)入相關(guān)的依賴
復(fù)制Hadoop的配置文件core-site.xml和HBase的配置文件hbase-site.xml到resources目錄中
創(chuàng)建包結(jié)構(gòu)
創(chuàng)建hbase連接類及管理對象
測試
?創(chuàng)建
案例一到案例八的源碼
案例一、使用java api創(chuàng)建hbase的表
編寫代碼
運行
查看創(chuàng)建的表
案例二、使用java api刪除表
編寫刪除表的方法代碼
調(diào)用方法
案例三、往創(chuàng)建的表中插入數(shù)據(jù)
編寫插入列數(shù)據(jù)的方法
在main方法中調(diào)用
查看執(zhí)行結(jié)果
案例四、查看一條數(shù)據(jù)
編寫方法
獲取某列的值
獲取某行的數(shù)據(jù)
調(diào)用方法
查看結(jié)果
案例五、刪除一條數(shù)據(jù)
編寫方法
調(diào)用方法
查看結(jié)果
案例六、導(dǎo)入數(shù)據(jù)
需求
Import JOB導(dǎo)入大量的數(shù)據(jù)
上傳數(shù)據(jù)文件到hdfs上
導(dǎo)入數(shù)據(jù)
運行導(dǎo)入命令
查看數(shù)據(jù)
count計數(shù)
mapreduce計數(shù)
案例七、查詢2020年6月份所有用戶的用水量
需求分析
編寫代碼(源碼也在上面總的)
調(diào)用方法
查看結(jié)果
解決數(shù)值型數(shù)據(jù)顯示亂碼的問題
案例八:Export Job導(dǎo)出數(shù)據(jù)
這篇文章的源碼比較多,需要的私信我
需求與數(shù)據(jù)集
某自來水公司,需要存儲大量的繳費明細數(shù)據(jù),以下截取了繳費明細的一部分內(nèi)容:
用戶id |
姓名 |
地址 |
性別 |
繳費時間 |
表示數(shù)(本次) |
表示數(shù)(上次) |
用量(立方) |
合計金額 |
查表日期 |
最遲繳費日期 |
4944191 |
張三 |
河北省石家莊市裕華區(qū)萬達校區(qū)2-1-401 |
男 |
2022-3-27 |
308.1 |
283.1 |
25 |
150 |
2022-2-25 |
2022-4-24 |
因為繳費明細的數(shù)據(jù)記錄非常龐大,該公司的信息部門決定使用hbase來存儲這些數(shù)據(jù),并且可以使用java來訪問這些數(shù)據(jù)。
準(zhǔn)備工作
idea:社區(qū)版免費,企業(yè)版收費
eclipse:開源免費
下載安裝maven
下載安裝idea
配置國內(nèi)的maven鏡像庫
創(chuàng)建一個maven工程
修改pom文件,導(dǎo)入相關(guān)的依賴
復(fù)制Hadoop的配置文件core-site.xml和HBase的配置文件hbase-site.xml到resources目錄中
先導(dǎo)出到本地計算機
?再添加一個日志log4j的配置文件
創(chuàng)建包結(jié)構(gòu)
創(chuàng)建hbase連接類及管理對象
測試
這個是一個小測試,來測試環(huán)境,下面那個才是真正的連接器
package cn.edu.hgu.dashuju19.hbase;
import org.apache.hadoop.conf.Configuration;
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;
/**
* @description: 創(chuàng)建hbase的連接及管理對象
* @author:
* @date: 2022-3-28
*/
public class HbaseConnect {
public static void main(String[] args){
//1、創(chuàng)建hbase的配置
Configuration configuration = new Configuration();
//2、創(chuàng)建hbase的連接
Connection connection;
{
try {
connection = ConnectionFactory.createConnection(configuration);
System.out.println(connection);
//3、創(chuàng)建admin對象
Admin admin = connection.getAdmin();
System.out.println(admin);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
?創(chuàng)建
package cn.edu.hgu.dashuju19.hbase;
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 org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
/**
* Hbase_connect
* @author
* @date 2022-03-30 11:58
*/
public class HbaseConnect {
private static Connection connection;
private static Admin admin;
public static void main(String[] args) throws IOException {
// 1. 使用HbaseConfiguration.create()創(chuàng)建Hbase配置
Configuration configuration = HBaseConfiguration.create();
// 2. 使用ConnectionFactory.createConnection()創(chuàng)建Hbase連接
connection = ConnectionFactory.createConnection(configuration);
// 3. 要創(chuàng)建表,需要基于Hbase連接獲取admin管理對象
// 要創(chuàng)建表、刪除表需要和HMaster連接,所以需要有一個admin對象
admin = connection.getAdmin();
TableName tableName = TableName.valueOf("WATER_BILL2");
// 4. 判斷表是否存在
if(admin.tableExists(tableName)) {
// a) 存在,則退出
return;
}
// 構(gòu)建表
// 5. 使用TableDescriptorBuilder.newBuilder構(gòu)建表描述構(gòu)建器
// TableDescriptor: 表描述器,描述這個表有幾個列蔟、其他的屬性都是在這里可以配置
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);
// 6. 使用ColumnFamilyDescriptorBuilder.newBuilder構(gòu)建列蔟描述構(gòu)建器
// 創(chuàng)建列蔟也需要有列蔟的描述器,需要用一個構(gòu)建起來構(gòu)建ColumnFamilyDescriptor
// 經(jīng)常會使用到一個工具類:Bytes(hbase包下的Bytes工具類)
// 這個工具類可以將字符串、long、double類型轉(zhuǎn)換成byte[]數(shù)組
// 也可以將byte[]數(shù)組轉(zhuǎn)換為指定類型
ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("C1"));
// 7. 構(gòu)建列蔟描述,構(gòu)建表描述
ColumnFamilyDescriptor cfDes = columnFamilyDescriptorBuilder.build();
// 建立表和列蔟的關(guān)聯(lián)
tableDescriptorBuilder.setColumnFamily(cfDes);
TableDescriptor tableDescriptor = tableDescriptorBuilder.build();
// 8. 創(chuàng)建表
admin.createTable(tableDescriptor);
// 9. 使用admin.close、connection.close關(guān)閉連接
admin.close();
connection.close();
}
}
這下準(zhǔn)備工作就做好了
接下來就是八個案例了
還是那句話,需要源碼包的私信我,當(dāng)然這里面也有源碼可以復(fù)制
案例一到案例八的源碼
這幾個案例放在一個java文件里了
源碼放在這里
大家在做案例的時候需要根據(jù)介紹來打開或者取消注釋
package cn.edu.hgu.dashuju19.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CompareOperator;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.List;
/**
* @description 創(chuàng)建hbase的連接以及管理對象
* @date 2022-3-30
*/
public class HbaseJavaAPIMain {
public static void main(String[] args) throws IOException {
Admin admin = getAdmin();
// //調(diào)用刪除表的方法
// boolean result = deleteTable(admin, "WATER_BILL");
// if (result) {
// System.out.println("刪除成功");
// }else {
// System.out.println("表不存在");
// }
//
//
// 調(diào)用插入數(shù)據(jù)的方法
// putTable(admin.getConnection(),"water_bill","4944191","info","addr","石家莊市裕華區(qū)");
// //獲取name值
// String name = getValue(admin.getConnection(),"water_bill","4944191","info","name");
// System.out.println(name);
// //輸出rowkey
// getOne(admin.getConnection(),"water_bill","4944191");
刪除某行數(shù)據(jù)
// deleteOne(admin.getConnection(),"water_bill","4944191");
// //查詢六月份數(shù)據(jù)
queryDate(admin.getConnection(), "WATER_BILL","C1","RECORD_DATE","2020-06-01", "2020-06-30");
// // 關(guān)閉admin
// admin.close();
}
/**
* 獲取admin對象
*
* @return
* @throws IOException
*/
public static Admin getAdmin() throws IOException {
//1.創(chuàng)建hbase的配置
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorom", "192.168.153.100");
configuration.set("hbase.zookeeper.property.clientPort", "2181");
configuration.set("hbase.master", "192.168.153.100:16010");
//2.創(chuàng)建hbase連接
Connection connection = null;
Admin admin = null;
try {
//通過工廠模式,根據(jù)配置來創(chuàng)建連接
connection = ConnectionFactory.createConnection(configuration);
System.out.println(connection);
//3.創(chuàng)建admin對象
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
return admin;
}
/**
* 刪除表
*
* @param admin
* @param name
* @throws IOException
* @return
*/
public static boolean deleteTable(Admin admin, String name) throws IOException {
//定義表名
TableName tableName = TableName.valueOf(name);
//判斷表是否存在
if (admin.tableExists(tableName)) {
//禁用表名
admin.disableTable(tableName);
//刪除表
admin.deleteTable(tableName);
return true;
} else {
return false;
}
}
/**
* 往表中插入數(shù)據(jù)
* @param connection
* @param tableName
* @param rowkey
* @param columnFamily
* @param column
* @param value
* @throws IOException
*/
public static void putTable(Connection connection,String tableName,String rowkey,String columnFamily,String column,String value) throws IOException {
//獲取table對象
Table table = connection.getTable(TableName.valueOf(tableName));
//根據(jù)rowkey獲取put對象
Put put = new Put(Bytes.toBytes(rowkey));
//添加姓名列
put.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column),Bytes.toBytes(value));
//插入數(shù)據(jù)
table.put(put);
//關(guān)閉table
table.close();
}
/**
* 獲取某列的值
* @param connection
* @param tableName
* @param rowkey
* @param columnFamily
* @param column
* @return
* @throws IOException
*/
public static String getValue(Connection connection, String tableName, String rowkey, String columnFamily, String column) throws IOException {
//1.獲取htable
Table table = connection.getTable(TableName.valueOf(tableName));
//2.使用rowkey構(gòu)建get對象
Get get = new Get(Bytes.toBytes(rowkey));
//3.執(zhí)行g(shù)et請求。獲取result對象
Result result = table.get(get);//result對象創(chuàng)建快捷鍵ctrl+alt+v
//4.某列的值
String name = Bytes.toString(result.getValue(columnFamily.getBytes(),column.getBytes()));
//System.out.println(name);
//System.out.println(result.toString());
// byte[] row = result.getRow();
//5.關(guān)閉表
table.close();
return name;
// System.out.println("rowkey=>" + Bytes.toString(row));
//獲取所有單元格
//List<Cell> cells = result.listCells();
}
/**
* 獲取并顯示某行的數(shù)據(jù)
*/
public static void getOne(Connection connection,String tableName,String rowkey) throws IOException {
//1.獲取htable
Table table = connection.getTable(TableName.valueOf(tableName));
//2.使用rowkey構(gòu)建get對象
Get get = new Get(Bytes.toBytes(rowkey));
//3.執(zhí)行g(shù)et請求。獲取result對象
Result result = table.get(get);//result對象創(chuàng)建快捷鍵ctrl+alt+v
//4.獲取rowkey
byte[] row = result.getRow();
System.out.println("rowkey=>" + Bytes.toString(row));
//5.獲取所有的單元格
List<Cell> cells = result.listCells();
//6.迭代處理每個單元格
for (Cell cell:cells) {
System.out.print(Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength()));
System.out.println("=>" + Bytes.toString(cell.getValueArray(),cell.getValueOffset(),cell.getValueLength()));
}
//7.關(guān)閉表
table.close();
}
/**
* 刪除指定行
*/
public static void deleteOne(Connection connection,String tableName,String rowkey) throws IOException {
//1.獲取htable
Table table = connection.getTable(TableName.valueOf(tableName));
//2.使用rowkey構(gòu)建delete對象
Delete delete = new Delete(Bytes.toBytes(rowkey));
//3.執(zhí)行delete請求
table.delete(delete);
//4.關(guān)閉表
table.close();
}
/**
* 查詢某列某個日期范圍的數(shù)據(jù)
* @param connection
* @param tableName
* @param columnFamily
* @param column
* @param startValue
* @param endValue
* @throws IOException
*/
public static void queryDate(Connection connection,String tableName,String columnFamily,String column,String startValue,String endValue) throws IOException {
//1.獲取htable
Table table = connection.getTable(TableName.valueOf(tableName));
//2.構(gòu)建scan對象
Scan scan = new Scan();
//3.構(gòu)建兩個過濾器
//構(gòu)建日期范圍的過濾器
//構(gòu)建開始日期的過濾器
SingleColumnValueFilter startDateFilter = new SingleColumnValueFilter(Bytes.toBytes(columnFamily),Bytes.toBytes(column),
CompareOperator.GREATER_OR_EQUAL,Bytes.toBytes(startValue));
//構(gòu)建結(jié)束日期的過濾器
SingleColumnValueFilter endDateFilter = new SingleColumnValueFilter(Bytes.toBytes(columnFamily),Bytes.toBytes(column),
CompareOperator.LESS_OR_EQUAL,Bytes.toBytes(endValue));
//3.2 構(gòu)建過濾器列表
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL,startDateFilter,endDateFilter);
//4.構(gòu)建掃描器
scan.setFilter(filterList);
//5.執(zhí)行scan掃描操作
ResultScanner resultScanner = table.getScanner(scan);
//6.迭代打印result
for (Result result:resultScanner) {
//6.1 打印rowkey
System.out.println("rowkey=>" + Bytes.toString(result.getRow()));
System.out.println("-------------------------------------------");
//6.2 迭代單元格列表
List<Cell> cells = result.listCells();
for (Cell cell:cells) {
//6.3打印數(shù)據(jù)
// 打印列簇名
System.out.print(Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength()));
//打印列名
//解決數(shù)值型數(shù)據(jù)亂碼
String columnName = Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength());
//System.out.print(":" + Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength()));
System.out.println(":" + columnName);
//判斷是否為數(shù)值型的列
if (columnName.equals("NUM_CURRENT") || columnName.equals("NUM_PREVIOUS")
|| columnName.equals("NUM_USAGE") || columnName.equals("TOTAL_MONEY")) {
//打印數(shù)值型值
System.out.println("=>" + Bytes.toDouble(cell.getValueArray(),cell.getValueOffset()));
}else{
//打印字符串值
System.out.println("=>" + Bytes.toString(cell.getValueArray(),cell.getValueOffset(),cell.getValueLength()));}
}
System.out.println("--------------------------------------------");
}
//7.關(guān)閉資源
resultScanner.close();
table.close();
}
}
案例一、使用java api創(chuàng)建hbase的表
源碼在上邊總的
創(chuàng)建一個名為water_bill的表,包含一個列簇info。
編寫代碼
- 定義表名,判斷表是否存在
- 表描述構(gòu)建器,建立表描述對象
- 列簇描述構(gòu)建器,建立列簇描述對象
- 表描述對象和列簇描述對象建立關(guān)系
- 創(chuàng)建表
?
?
運行
?
查看創(chuàng)建的表
?
案例二、使用java api刪除表
源碼在上邊總的
刪除剛剛創(chuàng)建的表
編寫刪除表的方法代碼
- 定義表名,判斷表是否存在
- 禁用表
- 刪除表
?
?
調(diào)用方法
?查看結(jié)果
?
案例三、往創(chuàng)建的表中插入數(shù)據(jù)
往water_bill中插入姓名列的數(shù)據(jù)
源碼在上邊總的
這里需要先把案例一在運行一遍把表創(chuàng)建好
編寫插入列數(shù)據(jù)的方法
- 使用hbase的連接獲取Htable
- 構(gòu)建rowkey、列簇名、列名、值
- 構(gòu)建Put對象(對應(yīng)put命令)
- 添加某列(列簇、列名、值)
- Htable對象執(zhí)行put操作
- 關(guān)閉htable對象
?
?
在main方法中調(diào)用
?
查看執(zhí)行結(jié)果
?
出錯了不知道為啥
重新運行一下
?
?運行成功
案例四、查看一條數(shù)據(jù)
查詢顯示rowkey為4944191的某列或者所有列的數(shù)據(jù),
編寫方法
獲取某列的值
?
獲取某行的數(shù)據(jù)
?
調(diào)用方法
?
?
?
查看結(jié)果
?
?
案例五、刪除一條數(shù)據(jù)
刪除rowkey為“4944191”的數(shù)據(jù)
編寫方法
?
調(diào)用方法
?
查看結(jié)果
?
?
?
案例六、導(dǎo)入數(shù)據(jù)
需求
有一份10W條記錄的抄表數(shù)據(jù)文件,需求將其導(dǎo)入hbase中
網(wǎng)盤鏈接
https://pan.baidu.com/s/1UEewxFODFPa2aREa-YjM2w?pwd=1234 ? 提取碼:1234
Import JOB導(dǎo)入大量的數(shù)據(jù)
在hbase中,有一個import的MR作業(yè),可以專門用來將數(shù)據(jù)導(dǎo)入到hbase中
用法:
hbase org.apache.hadoop.hbase.mapreduce.Import 表名 hdfs數(shù)據(jù)文件路徑
上傳數(shù)據(jù)文件到hdfs上
?
?
?
導(dǎo)入數(shù)據(jù)
- 啟動yarn
- 創(chuàng)建表
?
?
?
運行導(dǎo)入命令
hbase org.apache.hadoop.hbase.mapreduce.Import WATER_BILL /water_bill/
?運行結(jié)果
?
查看數(shù)據(jù)
?
count計數(shù)
?
?
mapreduce計數(shù)
hbase org.apache.hadoop.hbase.mapreduce.RowCounter "WATER_BILL"
?
案例七、查詢2020年6月份所有用戶的用水量
需求分析
在hbase中用scan+filter實現(xiàn)過濾查詢。2020年6月份其實就是從2020年6月1號到2020年6月30日的所有抄表數(shù)據(jù)
編寫代碼(源碼也在上面總的)
?
調(diào)用方法
?
查看結(jié)果
?
輸出結(jié)果代碼改進
?
?
解決數(shù)值型數(shù)據(jù)顯示亂碼的問題
打印顯示字符串?dāng)?shù)據(jù)是正常,但是如果HBase存儲的是int、double、float等數(shù)值型數(shù)據(jù)時,顯示就會亂碼,解決的方法就是判斷是否是數(shù)值型數(shù)據(jù),如果是則進行相應(yīng)的轉(zhuǎn)換
?顯示結(jié)果
?
案例八:Export Job導(dǎo)出數(shù)據(jù)
用法:
hbase org.apache.hadoop.hbase.mapreduce.Export 表名 hdfs路徑
?
?
????????這篇hbase的java aip實例在這里也算是告一段落了,下一章(4)是關(guān)于hbase高可用的相關(guān)介紹和實現(xiàn),希望大家一起學(xué)習(xí),一起進步。文章來源:http://www.zghlxwxcb.cn/news/detail-411385.html
如遇侵權(quán),請聯(lián)系刪除。文章來源地址http://www.zghlxwxcb.cn/news/detail-411385.html
到了這里,關(guān)于【Hbase】hbase的java api操作(3)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!