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使用的更多相关文章

  1. HBase详解(05) - HBase优化 整合Phoenix 集成Hive

    HBase详解(05) - HBase优化 整合Phoenix 集成Hive HBase优化 预分区 每一个region维护着startRow与endRowKey,如果加入的数据符合某个region维 ...

  2. kafka详解(03) - kafka JAVA API

    kafka详解(03) - kafka JAVA API Producer (生产者)API 消息发送流程 Kafka的Producer发送消息采用的是异步发送的方式.在消息发送的过程中,涉及到了两个 ...

  3. HBase详解(01) - Hbase简介

    HBase简介 定义:HBase是一种分布式.可扩展.支持海量数据存储的NoSQL数据库. 数据模型:逻辑上,HBase的数据模型同关系型数据库很类似,数据存储在一张表中,有行有列.但从HBase的底 ...

  4. HBase详解(03) - HBase架构和数据读写流程

    RegionServer 架构 每个RegionServer可以服务于多个Region 每个RegionServer中有多个Store, 1个WAL和1个BlockCache 每个Store对应一个列 ...

  5. [转帖]HBase详解(很全面)

    HBase详解(很全面) very long story 简单看了一遍 很多不明白的地方.. 2018-06-08 16:12:32 卢子墨 阅读数 34857更多 分类专栏: HBase   [转自 ...

  6. 图解大数据 | 海量数据库查询-Hive与HBase详解

    作者:韩信子@ShowMeAI 教程地址:http://www.showmeai.tech/tutorials/84 本文地址:http://www.showmeai.tech/article-det ...

  7. HBase详解(02) - HBase-2.0.5安装

    HBase详解(02) - HBase-2.0.5安装 HBase安装环境准备 Zookeeper安装 Zookeeper安装参考<Zookeeper详解(02) - zookeeper安装部署 ...

  8. Java网络编程和NIO详解开篇:Java网络编程基础

    Java网络编程和NIO详解开篇:Java网络编程基础 计算机网络编程基础 转自:https://mp.weixin.qq.com/s/XXMz5uAFSsPdg38bth2jAA 我们是幸运的,因为 ...

  9. Hadoop详解(04)-Hdfs

    Hadoop详解(04)-Hdfs HDFS概述 HDFS产出背景及定义 背景:随着数据量越来越大,在一个操作系统存不下所有的数据,那么就分配到更多的操作系统管理的磁盘中,但是不方便管理和维护,迫切需 ...

随机推荐

  1. Python地图栅格化实例

    Python地图栅格化实例 引言 shapefile是GIS中的一种非常重要的数据类型,由ESRI开发的空间数据开放格式,目前该数据格式已经成为了GIS领域的开放标准.目前绝大多数开源以及收费的GIS ...

  2. vulnhub靶场|NAPPING: 1.0.1

    准备: 攻击机:虚拟机kali.本机win10. 靶机:NAPPING: 1.0.1,地址我这里设置的桥接,,下载地址:https://download.vulnhub.com/napping/nap ...

  3. 齐博x1模型里边钩子的创建与使用

    在模型里边的钩子创建与使用方法跟在控制器里边的钩子创建及使用方法是有所区别的在模型里边创建的钩子,你可以理解为执行一个函数,是无法调用模型里边的类的方法及属性的.比如系统文件\application\ ...

  4. 靶机: easy_cloudantivirus

    靶机: easy_cloudantivirus 准备 下载靶机(Target):https://www.vulnhub.com/entry/boredhackerblog-cloud-av,453/ ...

  5. mycat搭建

    搭建mycat 一.准备工作 1.确保jdk已安装成功,并且jdk版本选用1.7以上版本 2.准备一台新的主机mysql_mycat放到master的前面做代理 mycat ip 192.168.23 ...

  6. 【题解】CF374C Inna and Dima

    题面传送门 解决思路 本题是找最长路的图上问题,所以先考虑如何建图. 首先把每一个字母转化为数字,然后对于每一个点枚举四个方向,如果有下一个字母,就向那个点建一条边,可以用 \(vector\) 存图 ...

  7. Codeforces Round #817 (Div. 4)

    CF传送门 因为洛谷题库未更新,所以给出的题面都是CF的. 现场打真是太卡了(梯子挂了,codeforc.es也崩了),所以五六分钟才打开题目 \(qwq\) A. Spell Check 萌萌题,把 ...

  8. CentOS 8 离线安装 podman 解决方法

    CentOS 8 系统中如果没有安装Podman的话,想要离线安装会比较麻烦,因为podman依赖的包比较多,从网上一个一个下载会很繁琐,也容易出错. 这里介绍一种曲线救国的方式来离线安装. 首先分享 ...

  9. 【Docker】容器使用规范--安全挂载建议

    容器挂载过程和安全挂载建议 绑定挂载 本文所提到的挂载主要指绑定挂载(bind mount),即通过-v /xx/xx:/xx/xx 和 --mount type=bind,xxx,xxx两种方式设置 ...

  10. PHP 正在“杀死”Python

    最近,我突然发现自己好像又在逆潮流而动.可能我的想法与很多朋友不同,我认为 PHP 这个编程语言界的"混蛋"比以往任何时候都更受欢迎. 或许你会质疑--PHP 不是已经完蛋了吗?市 ...