IDEA+Hadoop2.10.1+Zookeeper3.4.10+Hbase 2.3.5 操作JavaAPI
2024-10-19 10:52:49
在此之前要配置好三节点的hadoop集群,zookeeper集群,并启动它们,然后再配置好HBase环境
本文只是HBase2.3.5API操作作相应说明,如果前面环境还没有配置好,可以翻看我之前的博客,欢迎留言交流
节点hadoop01
节点hadoop02
节点hadoop03
1 maven依赖
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>2.3.5</version>
</dependency> <dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.3.5</version>
</dependency> <dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>C:/Program Files/Java/jdk1.8.0_261/lib/tools.jar</systemPath>
</dependency>
2 API操作
package com.hbase; 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 org.stringtemplate.v4.ST;
import scala.util.control.Exception; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; public class ConfigulationHBase { private static Configuration configuration; private static Connection connection; private static Admin admin; static {
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "192.168.161.141");
configuration.set("hbase.zookeeper.property.clientPort", "2181");
try {
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 判斷表是否存在
* <br>存在則返回true
* @param tableName
* @return
* @throws IOException
*/
public static boolean isTableExist(String tableName) throws IOException {
boolean b = admin.tableExists(TableName.valueOf(tableName));
return b;
} /**
* 創建表
*
* 参数tableName为表的名称,字符串数组fields为存储记录各个域名称的数组。<br>
* 要求当HBase已经存在名为tableName的表时,先删除原有的表,然后再<br>
* 创建新的表 field:列族<br>
* @param tableName 表名
* @param fields 列族名
* @throws IOException
*/
public static void createTable(String tableName,String[] fields) throws IOException {
if(isTableExist(tableName)){
System.out.println(tableName + " table is alreadly exist...");
admin.disableTable(TableName.valueOf(tableName));
admin.deleteTable(TableName.valueOf(tableName));
System.out.println(tableName + " table is deleted...");
}
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(tableName + " table is created!");
admin.close();
} /**
* 添加数据
*
* 向表tableName,行键rowKey和fields字段指定的单元格中添加对应的值values<br>
* 例如:表名:student,行键:1001,添加的字段:info:name,添加的值:Janna<br>
* put 'student','1001','info:name','Janna'<br>
*
* @param tableName 表名
* @param rowKey 行键
* @param family 列族
* @param qualifier 列族值
* @param value 值
* @throws IOException
*/
public static void addRow(String tableName, String rowKey, String family, String qualifier, String value) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
byte[] rowKeyAsBytes = rowKey.getBytes();
Put put = new Put(rowKeyAsBytes);
put.addColumn(family.getBytes(), qualifier.getBytes(), value.getBytes());
table.put(put);
table.close();
admin.close();
} /**
* 删除一行或者多行数据
* @param tableName
* @param rows
* @throws IOException
*/
public static void deleteMultiRow(String tableName, String... rows) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
List<Delete> deleteList = new ArrayList<Delete>();
for (int i = 0; i < rows.length; i++) {
Delete delete = new Delete(rows[i].getBytes());
deleteList.add(delete);
}
table.delete(deleteList);
table.close();
admin.close();
} /**
*
* 查询某tableName所有的数据
* @param tableName 表名
* @throws IOException
*/
public static void getAllRows(String tableName) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result:scanner){
Cell[] cells = result.rawCells();
for (Cell cell:cells){
System.out.println("RowKey:" + Bytes.toString(CellUtil.cloneRow(cell)));
System.out.println("Famliy:" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println("--------------------------------------");
}
System.out.println("=======================================");
}
table.close();
} /**
* 查询tableName表的rowKey行键的数据
* get 'student','1001'
* @param tableName 表名
* @param rowKey 行键
*/
public static void getRow(String tableName, String rowKey) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(rowKey.getBytes());
Result result = table.get(get);
for (Cell cell:result.rawCells()){
System.out.println("RowKey:" + Bytes.toString(CellUtil.cloneRow(cell)));
System.out.println("Famliy:" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println("--------------------------------------");
}
} /**
* 获取某表某行键某列族的值
*
* @param tableName 表名
* @param rowKey 行键
* @param famliy 列族
* @param qualifier 列族值
* @throws IOException
*/
public static void getRowQualifier(String tableName, String rowKey, String famliy, String qualifier) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(rowKey.getBytes());
get.addColumn(famliy.getBytes(), qualifier.getBytes());
Result result = table.get(get);
for (Cell cell:result.rawCells()){
System.out.println("RowKey:" + Bytes.toString(CellUtil.cloneRow(cell)));
System.out.println("Famliy:" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println("timestamp:" + cell.getTimestamp());
System.out.println("--------------------------------------");
}
} /**
* main 程序入口
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// 創建表
// String[] fileds = {"base_info","other_info"};
// createTable("football", fileds);
// 添加数据
// addRow("football", "11002", "base_info", "name", "lin");
// addRow("football", "11002", "base_info", "sex", "female");
// addRow("football", "11003", "base_info", "sex", "male");
// 删除数据
// deleteMultiRow("football", new String[]{"11002"});
// 得到所有的数据
// getAllRows("student");
// 查询某表某行键数据
// getRow("student","1001");
// 查询某表某行键某列族数据
getRowQualifier("student", "1001", "info", "name"); }
}
3 部分结果截图
仅供参考,有错误还请指出!
有什么想法,评论区留言,互相指教指教。
最新文章
- linux 运维必备150个命令
- UIButton修改文字大小问题
- HDU 1540 Tunnel Warfare(线段树+区间合并)
- 第三章 深入 ZAB 协议
- [转]popwindow用法
- 跪求 EF缓存问题 也不知道是不是缓存 问题
- 项目中经常用到的reset.css文件
- CSS中关于margin的理解误区
- Python输出hello world(各行命令详解)
- leetcode — unique-binary-search-trees-ii
- mumu模拟器安装xposed--如何在android模拟器上进行root
- 正则表达式验证input文本框
- hdu-2072(字典树)
- 二十五、Linux 进程与信号---exec函数
- IntelliJ IDEA 配置
- ELF格式探析之三:sections
- 多分类评价指标python代码
- anu - reactIE
- 记录php漏洞--宇宙最强语言 PHP 爆出 DoS 漏洞,可以直接灌满 CPU
- java 学习网站
热门文章
- Jmeter(四十六) - 从入门到精通高级篇 - Jmeter之网页图片爬虫-下篇(详解教程)
- Qt 设置窗体透明
- 使用指定源安装python包
- MongoDB学习笔记:Python 操作MongoDB
- js动态加载HTML元素时出现的无效的点击事件
- Win7 64 + mysql5.6.24(.zip) 不知道root密码的情况下重设密码
- 大型图像数据聚类匹配:ICCV2019论文解析
- CloudHub概述
- JavaFx 创建快捷方式及设置开机启动
- 菜鸟刷题路:剑指 Offer 06. 从尾到头打印链表