HBase详解(04) - HBase Java API使用
HBase详解(04) - HBase Java API使用
环境准备
新建Maven项目,在pom.xml中添加依赖
|
<dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>2.0.5</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>2.0.5</version> </dependency> |
DDL 库表的操作
- 创建HBase_DDL类
package com.zhangjk.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.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.junit.Test;
import java.io.IOException;
public
class HBase_DDL {
}
- 判断表是否存在
//判断表是否存在
public
static
boolean isTableExist(String tableName)
throws IOException {
//1.创建配置信息并配置
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum",
"hadoop102,hadoop103,hadoop104");
//2.获取与HBase的连接
Connection connection = ConnectionFactory.createConnection(configuration);
//3.获取DDL操作对象
Admin admin = connection.getAdmin();
//4.判断表是否存在操作
boolean exists = admin.tableExists(TableName.valueOf(tableName));
//5.关闭连接
admin.close();
connection.close();
//6.返回结果
return exists;
}
@Test
public
void isTableExist()
throws IOException {
boolean b = isTableExist("student");
System.out.println(b);
}
- 创建表
//创建表
public
static
void createTable(String tableName, String... cfs)
throws IOException {
//1.判断是否存在列族信息
if
(cfs.length <=
0)
{
System.out.println("请设置列族信息!");
return;
}
//2.判断表是否存在
if
(isTableExist(tableName))
{
System.out.println("需要创建的表已存在!");
return;
}
//3.创建配置信息并配置
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum",
"hadoop102,hadoop103,hadoop104");
//4.获取与HBase的连接
Connection connection = ConnectionFactory.createConnection(configuration);
//5.获取DDL操作对象
Admin admin = connection.getAdmin();
//6.创建表描述器构造器
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
//7.循环添加列族信息
for
(String cf : cfs)
{
ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(cf));
tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build());
}
//8.执行创建表的操作
admin.createTable(tableDescriptorBuilder.build());
//9.关闭资源
admin.close();
connection.close();
}
@Test
public
void createTable()
throws IOException {
createTable("student1","info1",
"info2");
}
- 删除表
//删除表
public
static
void dropTable(String tableName)
throws IOException {
//1.判断表是否存在
if
(!isTableExist(tableName))
{
System.out.println("需要删除的表不存在!");
return;
}
//2.创建配置信息并配置
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum",
"hadoop102,hadoop103,hadoop104");
//3.获取与HBase的连接
Connection connection = ConnectionFactory.createConnection(configuration);
//4.获取DDL操作对象
Admin admin = connection.getAdmin();
//5.使表下线
TableName name = TableName.valueOf(tableName);
admin.disableTable(name);
//6.执行删除表操作
admin.deleteTable(name);
//7.关闭资源
admin.close();
connection.close();
}
@Test
public
void dropTable()
throws IOException {
dropTable("student1");
}
- 创建命名空间
//创建命名空间
public
static
void createNameSpace(String ns)
throws IOException {
//1.创建配置信息并配置
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum",
"hadoop102,hadoop103,hadoop104");
//2.获取与HBase的连接
Connection connection = ConnectionFactory.createConnection(configuration);
//3.获取DDL操作对象
Admin admin = connection.getAdmin();
//4.创建命名空间描述器
NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(ns).build();
//5.执行创建命名空间操作
try
{
admin.createNamespace(namespaceDescriptor);
}
catch
(NamespaceExistException e)
{
System.out.println("命名空间已存在!");
}
catch
(Exception e)
{
e.printStackTrace();
}
//6.关闭连接
admin.close();
connection.close();
}
@Test
public
void createNameSpace()
throws IOException {
createNameSpace("test1");
}
DML 增删改查
- 创建类HBase_DML
- 插入数据
//插入数据
//tableName 表名
//rowKey 主键
//cf 列族名
//字段名称
//需要添加到值
public
static
void putData(String tableName, String rowKey, String cf, String cn, String value)
throws IOException {
//1.获取配置信息并设置连接参数
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum",
"hadoop102,hadoop103,hadoop104");
//2.获取连接
Connection connection = ConnectionFactory.createConnection(configuration);
//3.获取表的连接
Table table = connection.getTable(TableName.valueOf(tableName));
//4.创建Put对象
Put put =
new Put(Bytes.toBytes(rowKey));
//5.放入数据
put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value));
//6.执行插入数据操作
table.put(put);
//7.关闭连接
table.close();
connection.close();
}
@Test
public
void putData()
throws IOException {
String tableName =
"student1";
String rowKey =
"001";
String cf =
"info1";
String cn =
"name";
String value =
"zhangshan";
putData(tableName, rowKey, cf, cn, value);
}
- 单条数据查询(GET)
//单条数据查询(GET)
public
static
void getDate(String tableName, String rowKey, String cf, String cn)
throws IOException {
//1.获取配置信息并设置连接参数
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum",
"hadoop102,hadoop103,hadoop104");
//2.获取连接
Connection connection = ConnectionFactory.createConnection(configuration);
//3.获取表的连接
Table table = connection.getTable(TableName.valueOf(tableName));
//4.创建Get对象
Get get =
new Get(Bytes.toBytes(rowKey));
// 指定列族查询
// get.addFamily(Bytes.toBytes(cf));
// 指定列族:列查询
// get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));
//5.查询数据
Result result = table.get(get);
//6.解析result
for
(Cell cell : result.rawCells())
{
System.out.println("ROW:"
+ Bytes.toString(CellUtil.cloneRow(cell))
+
" CF:"
+ Bytes.toString(CellUtil.cloneFamily(cell))
+
" CL:"
+ Bytes.toString(CellUtil.cloneQualifier(cell))
+
" VALUE:"
+ Bytes.toString(CellUtil.cloneValue(cell)));
}
//7.关闭连接
table.close();
connection.close();
}
@Test
public
void getDate()
throws IOException {
String tableName =
"student1";
String rowKey =
"001";
String cf =
"info1";
String cn =
"name";
getDate(tableName, rowKey, cf, cn);
}
- 扫描数据(Scan)
//扫描数据(Scan)
public
static
void scanTable(String tableName)
throws IOException {
//1.获取配置信息并设置连接参数
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum",
"hadoop102,hadoop103,hadoop104");
//2.获取连接
Connection connection = ConnectionFactory.createConnection(configuration);
//3.获取表的连接
Table table = connection.getTable(TableName.valueOf(tableName));
//4.创建Scan对象
Scan scan =
new Scan();
//5.扫描数据
ResultScanner results = table.getScanner(scan);
//6.解析results
for
(Result result : results)
{
for
(Cell cell : result.rawCells())
{
System.out.println(
Bytes.toString(CellUtil.cloneRow(cell))+":"+
Bytes.toString(CellUtil.cloneFamily(cell))+":"
+
Bytes.toString(CellUtil.cloneQualifier(cell))
+":"
+
Bytes.toString(CellUtil.cloneValue(cell))
);
}
}
//7.关闭资源
table.close();
connection.close();
}
@Test
public
void scanTable()
throws IOException {
scanTable("student1");
}
- 删除数据
//删除数据
public
static
void deletaData(String tableName, String rowKey, String cf, String cn)
throws IOException {
//1.获取配置信息并设置连接参数
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum",
"hadoop102,hadoop103,hadoop104");
//2.获取连接
Connection connection = ConnectionFactory.createConnection(configuration);
//3.获取表的连接
Table table = connection.getTable(TableName.valueOf(tableName));
//4.创建Delete对象
Delete delete =
new Delete(Bytes.toBytes(rowKey));
// 指定列族删除数据
// delete.addFamily(Bytes.toBytes(cf));
// 指定列族:列删除数据(所有版本)
// delete.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));
// 指定列族:列删除数据(指定版本)
// delete.addColumns(Bytes.toBytes(cf), Bytes.toBytes(cn));
//5.执行删除数据操作
table.delete(delete);
//6.关闭资源
table.close();
connection.close();
}
@Test
public
void deletaData()
throws IOException {
String tableName =
"student1";
String rowKey =
"001";
String cf =
"info1";
String cn =
"name";
deletaData(tableName, rowKey, cf, cn);
}
HBase详解(04) - HBase Java API使用的更多相关文章
- HBase详解(05) - HBase优化 整合Phoenix 集成Hive
HBase详解(05) - HBase优化 整合Phoenix 集成Hive HBase优化 预分区 每一个region维护着startRow与endRowKey,如果加入的数据符合某个region维 ...
- kafka详解(03) - kafka JAVA API
kafka详解(03) - kafka JAVA API Producer (生产者)API 消息发送流程 Kafka的Producer发送消息采用的是异步发送的方式.在消息发送的过程中,涉及到了两个 ...
- HBase详解(01) - Hbase简介
HBase简介 定义:HBase是一种分布式.可扩展.支持海量数据存储的NoSQL数据库. 数据模型:逻辑上,HBase的数据模型同关系型数据库很类似,数据存储在一张表中,有行有列.但从HBase的底 ...
- HBase详解(03) - HBase架构和数据读写流程
RegionServer 架构 每个RegionServer可以服务于多个Region 每个RegionServer中有多个Store, 1个WAL和1个BlockCache 每个Store对应一个列 ...
- [转帖]HBase详解(很全面)
HBase详解(很全面) very long story 简单看了一遍 很多不明白的地方.. 2018-06-08 16:12:32 卢子墨 阅读数 34857更多 分类专栏: HBase [转自 ...
- 图解大数据 | 海量数据库查询-Hive与HBase详解
作者:韩信子@ShowMeAI 教程地址:http://www.showmeai.tech/tutorials/84 本文地址:http://www.showmeai.tech/article-det ...
- HBase详解(02) - HBase-2.0.5安装
HBase详解(02) - HBase-2.0.5安装 HBase安装环境准备 Zookeeper安装 Zookeeper安装参考<Zookeeper详解(02) - zookeeper安装部署 ...
- Java网络编程和NIO详解开篇:Java网络编程基础
Java网络编程和NIO详解开篇:Java网络编程基础 计算机网络编程基础 转自:https://mp.weixin.qq.com/s/XXMz5uAFSsPdg38bth2jAA 我们是幸运的,因为 ...
- Hadoop详解(04)-Hdfs
Hadoop详解(04)-Hdfs HDFS概述 HDFS产出背景及定义 背景:随着数据量越来越大,在一个操作系统存不下所有的数据,那么就分配到更多的操作系统管理的磁盘中,但是不方便管理和维护,迫切需 ...
随机推荐
- CentOS6/7开机启动配置
最近在配置Linux系统的ntp校时,涉及到开机启动问题,总结一下 两个环境: CentOS release 6.5 (Final) CentOS Linux release 7.9.2009 (Co ...
- Docker | 制作tomcat镜像并部署项目
本文讲解如何制作自己的tomcat镜像,并使用tomcat部署项目 原料准备: tomcat.jdk安装包,dockerfile文件 步骤 1.准备压缩包 jdk-8u301-linux-x64.ta ...
- ssh端口映射 解决服务器使用tensorboard的问题
有时会在服务器上使用tensorboard,然而本地无法直接访问tensorboard结果网页.这时候使用端口映射即可.比如tensorboard上占用的是 6006 端口,也就是说结果在服务器的 l ...
- 9-模型层及ORM介绍
一.模型层 模型层负责和数据库之间进行通信 二.Django配置mysql数据库 1.Django默认的数据库是sqllite3,将其更改为mysql数据库需要进行对应配置 1.安装mysql ...
- 关于引用JS和CSS文件刷新浏览器缓存问题,部署服务器后客户端样式不刷新
问题描述 对样式的css文件进行了修改,部署到服务器后访问发现页面展示不正常,但是刷新之后就会展示正常. 问题分析 研究之后发现可能的原因有 css文件过大,加载缓慢 本地缓存问题,虽然服务器修改了c ...
- Aspose.Cell篇章3,设置写入到Excel文件的各种样式及输出
Aspose.Cell的Style.Number设置全部设置 /// <summary> /// 单元格样式编号 /// 0 General General /// 1 Decimal 0 ...
- Linux备份文件加“时间”命令
好记性不如烂笔头,好用. date命令用于显示及设置系统的时间或日期,如何设置时间此处不再多说. date命令非常强大,可以将数据备份命令与date命令结合在一起使用,可以便捷的分辨出每个文件的备份时 ...
- 要写文档了,emmm,先写个文档工具吧——DocMarkdown
前言 之前想用Markdown来写框架文档,找来找去发现还是Jekyll的多,但又感觉不是很合我的需求 于是打算自己简单弄一个展示Markdown文档的网站工具,要支持多版本.多语言.导航.页内导航等 ...
- freeswitch的mod_curl模块
概述 有时候,我们需要在呼叫的过程中,或过程后调用web api接口. freeswitch的mod_curl模块可以很方便的实现web api的接口调用. mod_curl模块默认不安装,需要进入模 ...
- 《吐血整理》高级系列教程-吃透Fiddler抓包教程(34)-Fiddler如何抓取微信小程序的包-上篇
1.简介 有些小伙伴或者是童鞋们说小程序抓不到包,该怎么办了???其实苹果手机如果按照宏哥前边的抓取APP包的设置方式设置好了,应该可以轻松就抓到包了.那么安卓手机小程序就比较困难,不是那么友好了.所 ...