Java API 操作HBase Shell
HBase Shell API 操作
创建工程
本实验的环境实在ubuntu18.04下完成,首先在改虚拟机中安装开发工具eclipse。
然后创建Java项目名字叫hbase-test








配置运行环境
在src下创建HBaseDemo类

然后编写init方法和close方法,一个创建与HBASE的连接,一个关闭连接。
/**
* 创建连接返回admin
*/
public static void init() {
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir", "file:///usr/local/hbase/hbase-tmp");
// configuration.set("hbase.zookeeper.quorum", "hadoop02");
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();
}
}
建表操作
/**
* 创建表方法
*
* @param myTableName
* @param colFamily
* @throws IOException
*/
public static void createTable(String myTableName, String[] colFamily) throws IOException {
TableName tableName = TableName.valueOf(myTableName);
if (admin.tableExists(tableName)) {
System.out.println("talbe is exists!");
} else {
TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tableName);
for (String str : colFamily) {
ColumnFamilyDescriptor family = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(str)).build();
tableDescriptor.setColumnFamily(family);
}
admin.createTable(tableDescriptor.build());
}
}
测试:创建student表,列族有score
public static void main(String[] args) throws IOException {
init();
System.out.print("==================分割线===================");
//创建student表
createTable("student",new String[]{"score"});
close();
}
查看执行结果

查看现有表的名称
/**
* 查看已有表
* @throws IOException
*/
public static void listTables() throws IOException {
init();
HTableDescriptor hTableDescriptors[] = admin.listTables();
for(HTableDescriptor hTableDescriptor :hTableDescriptors){
System.out.println(hTableDescriptor.getNameAsString());
}
close();
}
测试:
public static void main(String[] args) throws IOException {
init();
System.out.println("==================分割线===================");
listTables();
System.out.println("==================分割线===================");
close();
}
删除表操作
代码:
/**
* 删除指定表
* @param tableName 表名
* @throws IOException
*/
public static void deleteTable(String tableName) throws IOException {
init();
TableName tn = TableName.valueOf(tableName);
if (admin.tableExists(tn)) {
admin.disableTable(tn);
admin.deleteTable(tn);
}
close();
}
测试:
public static void main(String[] args) throws IOException {
init();
System.out.println("==================分割线===================");
deleteTable("student");
System.out.println("==================分割线===================");
close();
}
结果

删除指定列操作
代码
/**
* 删除指定列数据
* @param tableName 表名
* @param rowKey 行键
* @param colFamily 列族名
* @param col 列名
* @throws IOException
*/
public static 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);
table.close();
close();
}
测试
public static void main(String[] args) throws IOException {
init();
System.out.println("==================分割线===================");
deleteRow("student", "zhangsan", "score", "Math");
System.out.println("==================分割线===================");
close();
}
结果 zhangsan行,score列族中的Math列被删除
添加数据
/**
* 向指定表中插入数据
*
* @param tableName
* @param rowKey
* @param colFamily
* @param col
* @param val
* @throws IOException
*/
public static void insertData(String tableName, String rowKey, String colFamily, String col, String val)
throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(rowKey.getBytes());
put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
table.put(put);
table.close();
}
向student表中添加三条数据,分别是zhangsan的英语成绩,数学成绩和计算机成绩。
public static void main(String[] args) throws IOException {
init();
System.out.print("==================分割线===================");
//插入三条数据
insertData("student","zhangsan","score","English","69");
insertData("student","zhangsan","score","Math","86");
insertData("student","zhangsan","score","Computer","77");
close();
}
测试结果

查看数据
/**
* 获取指定表中ceil数据
*
* @param tableName
* @param rowKey
* @param colFamily
* @param col
* @throws IOException
*/
public static void getData(String tableName, String rowKey, String colFamily, String col) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(rowKey.getBytes());
get.addColumn(colFamily.getBytes(), col.getBytes());
Result result = table.get(get);
System.out.println(tableName+"表,"+colFamily+"列族,"+col+"的值是:"+new String(result.getValue(colFamily.getBytes(), col == null ? null : col.getBytes())));
table.close();
}
查看张三英语的 成绩
public static void main(String[] args) throws IOException {
init();
System.out.println("==================分割线===================");
getData("student","zhangsan", "score","English");
close();
}
控制台输出结果:

Java API 操作HBase Shell的更多相关文章
- HBase 6、用Phoenix Java api操作HBase
开发环境准备:eclipse3.5.jdk1.7.window8.hadoop2.2.0.hbase0.98.0.2.phoenix4.3.0 1.从集群拷贝以下文件:core-site.xml.hb ...
- Hbase框架原理及相关的知识点理解、Hbase访问MapReduce、Hbase访问Java API、Hbase shell及Hbase性能优化总结
转自:http://blog.csdn.net/zhongwen7710/article/details/39577431 本blog的内容包含: 第一部分:Hbase框架原理理解 第二部分:Hbas ...
- linux 下通过过 hbase 的Java api 操作hbase
hbase版本:0.98.5 hadoop版本:1.2.1 使用自带的zk 本文的内容是在集群中创建java项目调用api来操作hbase,主要涉及对hbase的创建表格,删除表格,插入数据,删除数据 ...
- hadoop2-HBase的Java API操作
Hbase提供了丰富的Java API,以及线程池操作,下面我用线程池来展示一下使用Java API操作Hbase. 项目结构如下: 我使用的Hbase的版本是 hbase-0.98.9-hadoop ...
- 使用Java API操作HDFS文件系统
使用Junit封装HFDS import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import org ...
- 【Hbase三】Java,python操作Hbase
Java,python操作Hbase 操作Hbase python操作Hbase 安装Thrift之前所需准备 安装Thrift 产生针对Python的Hbase的API 启动Thrift服务 执行p ...
- java api操作
java api操作 导入开发包 将hbase安装包中lib下包导入java项目 创建表 Configuration conf = HBaseConfiguration.create(); c ...
- hive-通过Java API操作
通过Java API操作hive,算是测试hive第三种对外接口 测试hive 服务启动 package org.admln.hive; import java.sql.SQLException; i ...
- Kafka系列三 java API操作
使用java API操作kafka 1.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xs ...
随机推荐
- mac系统下用ssh方式连接git仓库
1.应用程序-终端,键入命令 ssh-keygen -t rsa -C "xxxxx@xxxxx.com" ,后面是你的邮箱地址.一直回车,生成密钥. 2.键入 open ~ ...
- Android开源项目分类汇总-转载
太长了,还是转载吧...今天在看博客的时候,无意中发现了@Trinea在GitHub上的一个项目Android开源项目分类汇总,由于类容太多了,我没有一个个完整地看完,但是里面介绍的开源项目都非常有参 ...
- tengine下载和安装
tengine简介: Tengine所基于开发的Nginx的意思是Engine-X,Tengine在淘宝开发,所以我们把Engine-X中的X替换成Taobao.Tengine即Taobao-Engi ...
- 一、eclipse配置TestNG
eclipse配置TestNG可以通过eclipse直接下载,但我没有vpn,所以使用线下配置. 1-下载TestNG的配置文件,有两个文件 features 和 plugins 2-eclipse配 ...
- Leetcode——练习
平时没事刷刷Leetcode,还办了个年会员.为了自己150刀.为了自己的大脑投资,从不差钱儿.刷刷题能练习coding,此外看一些别人的优秀的答案,能增长见解.大家共同努力,共勉. 十.Google ...
- 【Electron Playground 系列】文件下载篇
作者:long.woo 文件下载是我们开发中比较常见的业务需求,比如:导出 excel. web 应用文件下载存在一些局限性,通常是让后端将响应的头信息改成 Content-Disposition: ...
- python 字典常用操作
字典键是唯一的,但值则不是 一个简单的字典 dict = {"guo":"1106","tang":"0809",&qu ...
- [日常摸鱼]JSOI2008最大数
校运会的时候随手抽的题- 一句话题意 维护一个序列,初始为空,要求滋兹: 1.查询这个序列末尾$x$个数的最大值 2.设上一次查询的答案为$t$(如果还没查询$t=0$),在末尾插入一个数$(x+t) ...
- 图的建立以及应用(BFS,DFS,Prim)
关于带权无向图的一些操作 题目:根据图来建立它的邻接矩阵,通过邻接矩阵转化为邻接表,对邻接表进行深度优先访问和广度优先访问,最后用邻接矩阵生成它的最小生成树: 1.输入一个带权无向图(如下面图1和图2 ...
- MybatisPlus_01
目录 1.1 简介 1.1.1 特性 1.1.2 框架结构 2.1 快速开始 2.1.1 2.1.2 yaml文件配置 2.1.3 编码 2.1.4 测试 3.1 思考 1.1 简介 MyBatis- ...