IDEA+Hadoop2.10.1+Zookeeper3.4.10+Hbase 2.3.5 操作JavaAPI
在此之前要配置好三节点的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 部分结果截图


仅供参考,有错误还请指出!
有什么想法,评论区留言,互相指教指教。
IDEA+Hadoop2.10.1+Zookeeper3.4.10+Hbase 2.3.5 操作JavaAPI的更多相关文章
- ZooKeeper-3.4.10分布式安装指南
目录 目录 1 1. 前言 1 2. 约定 1 3. 安装步骤 2 3.1. 配置/etc/hosts 2 3.2. 设置myid 2 3.3. 修改conf/zoo.cfg 2 3.4. 修改/bi ...
- Ubuntu16.4 zookeeper-3.4.10 单机多实例部署
上传 zookeeper-3.4.10.tar.gz 到服务器 root@temple-:/usr/local/temple/jar# ll total drwxr-xr-x root root 8月 ...
- linux安装配置zookeeper-3.4.10
此文是基于上一篇文章:hadoop集群搭建 安装zookeeper: [在各个slave节点安装zookeeper] 下载地址:http://mirror.bit.edu.cn/apache/zook ...
- Linux上安装ZooKeeper并设置开机启动(CentOS7+ZooKeeper3.4.10)
1下载Zookeeper 2安装启动测试 2.1上载压缩文件并解压 2.2新建 zookeeper配置文件 2.3安装JDK 2.4启动zookeeper 2.5查看zookeeper的状态 3将Zo ...
- Linux CentOS7下安装Zookeeper-3.4.10服务(最新)
Linux CentOS7下安装Zookeeper-3.4.10服务(最新) 2017年10月27日 01:25:26 极速-蜗牛 阅读数:1933 版权声明:本文为博主原创文章,未经博主允许不得 ...
- shell脚本部署zookeeper-3.4.10 [含注释]
文章目录 zk_install.sh conf/config conf/zoo_template.cfg package zk_install.sh #!/bin/bash base_path=$(c ...
- Hadoop-2.6.0 + Zookeeper-3.4.6 + HBase-0.98.9-hadoop2环境搭建示例
1 基本信息 1.1 软件信息 hadoop-2.6.0 zookeeper-3.4.6 hbase-0.98.9-hadoop2 (以下示例中使用的操作系统是Centos 6.5,请将 ...
- ubuntu12.04+hadoop2.2.0+zookeeper3.4.5+hbase0.96.2+hive0.13.1伪分布式环境部署
目录: 一.hadoop2.2.0.zookeeper3.4.5.hbase0.96.2.hive0.13.1都是什么? 二.这些软件在哪里下载? 三.如何安装 1.安装JDK 2.用parallel ...
- SQL 2008升级SQL 2008 R2完全教程或者10.00.1600升级10.50.1600
http://blog.csdn.net/feng19821209/article/details/8571571 SQL 2008升级SQL 2008 R2完全教程或者10.00.1600升级10. ...
随机推荐
- [源码解析] 深度学习分布式训练框架 Horovod (1) --- 基础知识
[源码解析] 深度学习分布式训练框架 Horovod --- (1) 基础知识 目录 [源码解析] 深度学习分布式训练框架 Horovod --- (1) 基础知识 0x00 摘要 0x01 分布式并 ...
- Python+Selenium学习笔记3 - 二维码生成
用qrcode模块生成二维码 # coding = utf-8 import qrcode qr = qrcode.QRCode( version=1, error_correction=qrcode ...
- 针对Spring MVC的Interceptor内存马
针对Spring MVC的Interceptor内存马 目录 针对Spring MVC的Interceptor内存马 1 基础拦截器和调用流程的探索 1.1 基础拦截器 1.2 探索拦截器的调用链 1 ...
- 插件 ExcelWrite 导出Excel格式数据/获取图层
使用ExcelWrite 插件可以导出Excel格式的数据: ExcelFile端口接 文件数据路径 最后面的是文件名,不用写格式 如果存储树形数据,需要 勾选 List To Row 选项: 附: ...
- 错误码:events.js:183 throw er; // Unhandled 'error' event ^ Error: listen EADDRINUSE :::8081
错误的产生: 错误的原因: 端口被占用 修改访问端口就可以了 https://blog.csdn.net/qq_25479327/article/details/79824742
- ICCV2019论文点评:3D Object Detect疏密度点云三维目标检测
ICCV2019论文点评:3D Object Detect疏密度点云三维目标检测 STD: Sparse-to-Dense 3D Object Detector for Point Cloud 论文链 ...
- VS Code 调试 OneFlow
VS Code 调试 OneFlow 本文介绍如何配置 VS Code,搭建 OneFlow 的 GUI 开发环境. 如果对于 VS Code 及其插件系统还不熟悉,可以参阅官方文档. 本文包括: 如 ...
- Yolov4性能分析(下)
Yolov4性能分析(下) 六. 权重更新 "darknet/src/detector.c"--train_detector()函数中: ...... /* 开始训练网络 */ f ...
- Node.js使用本地依赖
最近项目组写了几个模块,打算采用依赖的方式来使用它们. 因为敏感性问题,不打算发布到npm官方仓库:自行搭建一个npm仓库成本也大. 所以最后决定采用本地安装. 即在package.json中填写如下 ...
- MySQL零散知识点(01)
内容概要 --- 表字段操作补充(掌握) --- python操作MySQL(掌握) --- 视图(了解) --- 触发器(了解) --- 存储过程(了解) --- 事务(掌握) --- 内置函数(了 ...