【HBase】通过Java代码实现HBase数据库中数据的增删改查
目录
创建maven工程,导入jar包
<repositories>
<repository>
<id>cloudera</id>
<url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.0-mr1-cdh5.14.0</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.0-cdh5.14.0</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.2.0-cdh5.14.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<!-- <verbal>true</verbal>-->
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*/RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
java代码实现创建hbase表
package cn.itcast.HBaseStudy;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
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.jupiter.api.Test;
import java.io.IOException;
public class HBaseFirst {
/**
* 需求1:创建myuser表,包含f1和f2两个列族
*/
@Test
public void createTable() throws IOException {
//连接HBase服务端
Configuration configuration = HBaseConfiguration.create();
/*
通信三要素:IP地址、端口号、传输协议
设置HBase连接ZK的地址
*/
configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
//获取HBase数据库连接对象
Connection connection = ConnectionFactory.createConnection(configuration);
//获取管理员对象,用于创建表、删除表等
Admin admin = connection.getAdmin();
//获取HTableDescriptor对象
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf("myuser"));
//给表添加列族名需要创建HColumnDescriptor对象
HColumnDescriptor f1 = new HColumnDescriptor("f1");
HColumnDescriptor f2 = new HColumnDescriptor("f2");
//先给表添加列族名
hTableDescriptor.addFamily(f1);
hTableDescriptor.addFamily(f2);
//用管理员创建表
admin.createTable(hTableDescriptor);
//关闭admin
admin.close();
//关闭connection
connection.close();
}
}
java代码实现向hbase表中插入数据
/**
* 创建连接HBase服务器的方法
*/
private Connection connection;
private Table table;
@BeforeTest
public void init() throws IOException {
//获取连接
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");
connection = ConnectionFactory.createConnection(configuration);
//获取表
table = connection.getTable(TableName.valueOf("myuser"));
}
/**
* 需求2:向表中添加数据
*/
@Test
public void addData() throws IOException {
//创建put对象,并指定rowkey
Put put = new Put("0001".getBytes());
put.addColumn("f1".getBytes(),"id".getBytes(), Bytes.toBytes(1));
put.addColumn("f1".getBytes(),"name".getBytes(), Bytes.toBytes("张三"));
put.addColumn("f1".getBytes(),"age".getBytes(), Bytes.toBytes(18));
put.addColumn("f2".getBytes(),"address".getBytes(), Bytes.toBytes("地球人"));
put.addColumn("f2".getBytes(),"phone".getBytes(), Bytes.toBytes("15874102589"));
//插入数据
table.put(put);
}
/**
* 关闭系统连接和表连接
*/
@AfterTest
public void close() throws IOException {
//关闭表
table.close();
connection.close();
}
java代码查询hbase数据
/**
* 查询rowKey为0003的数据
*/
@Test
public void searchData() throws IOException {
//获取get对象
Get get = new Get(Bytes.toBytes("0003"));
//通过get获取数据 result封装了所有结果数据
Result result = table.get(get);
//打印结果数据,或者这条数据所有的cell
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
//获取所在列族
byte[] family = cell.getFamily();
//获取所在列名
byte[] qualifier = cell.getQualifier();
//获取值
byte[] value = cell.getValue();
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(qualifier))) {
System.out.println("列族名称为" + Bytes.toString(family) + "列名称为" + Bytes.toString(qualifier) + "列值为" + Bytes.toInt(value));
} else {
System.out.println("列族名称为" + Bytes.toString(family) + "列名称为" + Bytes.toString(qualifier) + "列值为" + Bytes.toString(value));
}
}
}
使用rowKey查询指定列族指定列的值
/**
* 使用rowKey查询0003,f1列族,name列的值
**/
@Test
public void getColumn() throws IOException {
//获取get对象
Get get = new Get("0003".getBytes());
//拿到f1列族和name列
get.addColumn("f1".getBytes(),"name".getBytes());
//拿到以上要求的所有数据
Result result = table.get(get);
//将数据值放到一个数组中
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] family = cell.getFamily();
byte[] qualifier = cell.getQualifier();
byte[] value = cell.getValue();
System.out.println("rk为0003,在f1列族name列下的数据值为"+Bytes.toString(value));
}
}
通过startRowKey和endRowKey进行扫描
/**
* 扫描rowKey为0004-0006的范围值
*/
@Test
public void rangeRowKey() throws IOException {
//创建Scan对象,获取到rk的范围,如果不指定范围就是全表扫描
Scan scan = new Scan("0004".getBytes(),"0006".getBytes());
//拿到了多条数据的结果
ResultScanner scanner = table.getScanner(scan);
//循环遍历ResultScanner,将多条数据分成一条条数据
for (Result result : scanner) {
byte[] row = result.getRow();
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] family = cell.getFamily();
byte[] qualifier = cell.getQualifier();
byte[] value = cell.getValue();
/**
* 扫描rowKey为0004-0006的范围值
*/
@Test
public void rangeRowKey() throws IOException {
//创建Scan对象,获取到rk的范围,如果不指定范围就是全表扫描
Scan scan = new Scan("0004".getBytes(),"0006".getBytes());
//拿到了多条数据的结果
ResultScanner scanner = table.getScanner(scan);
//循环遍历ResultScanner,将多条数据分成一条条数据
for (Result result : scanner) {
byte[] row = result.getRow();
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] family = cell.getFamily();
byte[] qualifier = cell.getQualifier();
byte[] value = cell.getValue();
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(qualifier))) {
System.out.println("列族名称为" + Bytes.toString(family) + "列名称为" + Bytes.toString(qualifier) + "列值为" + Bytes.toInt(value));
} else {
System.out.println("列族名称为" + Bytes.toString(family) + "列名称为" + Bytes.toString(qualifier) + "列值为" + Bytes.toString(value));
}
}
}
}
}
}
}
【HBase】通过Java代码实现HBase数据库中数据的增删改查的更多相关文章
- MVC模式:实现数据库中数据的增删改查功能
*.数据库连接池c3p0,连接mysql数据库: *.Jquery使用,删除时跳出框,确定是否要删除: *.使用EL和JSTL,简化在jsp页面中插入的java语言 1.连接数据库 (1)导入连接数据 ...
- 控制台程序实现利用CRM组织服务和SqlConnection对数据库中数据的增删改查操作
一.首先新建一个控制台程序.命名为TestCol. 二.打开App.config在里面加入,数据库和CRM连接字符串 <connectionStrings> <add name=&q ...
- Delphi - cxGrid连接Oracle数据库 实现数据的增删改查
cxGrid连接Oracle数据库 实现数据的增删改查 cxGrid连接Oracle数据库 1:通过OraSession连接数据库.OraDataSet实现OraSession和OraDataSour ...
- 数据库中简单的增删改查(CRUD)
一切都是基于数据,而对数据的管理都离不开数据库.最近学到数据库的简单操作,所以写下这篇文章,总结一下学习到的知识.浅陋之处,多多见谅. 补充一下:一直弄不清SQL Server,Mysql ,以及Or ...
- mysql基础之mariadb对表中数据的增删改查
复习: 查看表:show tables; 创建表:create table 表名(字符类型); 删除表:drop table 表名; 对表的结构进行增删改查: 查看表结构:desc 表名; 修改表-添 ...
- mysql--对行(表中数据)的增删改查
一.插入数据(增加)insert 1.插入数据(顺序插入) 语法一: INSERT INTO 表名(字段1,字段2,字段3…字段n) VALUES(值1,值2,值3…值n); #指定字段来插入数据,插 ...
- JAVA 操作远程mysql数据库实现单表增删改查操作
package MysqlTest; import java.sql.DriverManager; import java.sql.ResultSet; import com.mysql.jdbc.C ...
- HDFS只支持文件append操作, 而依赖HDFS的HBase如何完成数据的增删改查
转:http://www.th7.cn/db/nosql/201510/135382.shtml 1. HDFS的文件append功能 早期版本的HDFS不支持任何的文件更新操作,一旦一个文件创建.写 ...
- MySQL数据库之表的增删改查
目录 MySQL数据库之表的增删改查 1 引言 2 创建表 3 删除表 4 修改表 5 查看表 6 复制表 MySQL数据库之表的增删改查 1 引言 1.MySQL数据库中,数据库database就是 ...
随机推荐
- python2.7安装numpy和pandas
扩展官网安装numpy,use [v][p][n]下载得会比较快 然后在CMD命令行下进入该文件夹然后输入pip install +numpy的路径+文件名.比如我的是:pip install num ...
- 植物大战僵尸的代码如何使用python来实现
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:程序IT圈 PS:如有需要Python学习资料的小伙伴可以加点击下方链 ...
- C - Sweets Eating
规律题 前缀和+规律 先求前缀和...答案为c[i]=arr[i]+c[i-m]//i>m时. #include<bits/stdc++.h> using namespace std ...
- Android内存优化—dumpsys meminfo详解
原创置顶 不死鸟JGC 最后发布于2018-12-24 14:19:28 阅读数 3960 收藏展开dumpsys 介绍Dumpsys用户系统诊断,它运行在设备上,并提供系统服务状态信息 命令格式: ...
- BUUOJ [CISCN2019 华北赛区 Day2 Web1]Hack World
补一下这道题,顺便发篇博客 不知道今年国赛是什么时候,菜鸡还是来刷刷题好了 0X01 考点 SQL注入.盲注.数字型 0X02自己尝试 尝试输入1 赵师傅需要女朋友吗???随便都能有好吧 输入2 ?? ...
- [Laravel框架学习一]:Laravel框架的安装以及 Composer的安装
1.先下载Composer-Setup.exe,下载地址:下载Composer .会自动搜索PHP.exe的安装路径,如果没有,就手动找到php路径下的php.exe. 2.在PHP目录下,打开php ...
- MySQL笔记总结-TCL语言
TCL语言 事务 一.含义 事务控制语言 Transaction Control Language 事务:一条或多条sql语句组成一个执行单位,一组sql语句要么都执行要么都不执行 二.特点(ACID ...
- kubernetes的headless service介绍
headless service是一个特殊的ClusterIP类service,这种service创建时不指定clusterIP(--cluster-ip=None),因为这点,kube-proxy不 ...
- 如何快速地恢复你的win10
win10清单 这份List不会介绍如何安装系统,而是当面对一个新系统,如何最快的搭建,或者说恢复到一个生产力环境. 必备习惯 备份软件安装包和常用内容上云是高效恢复的两点关键. 备份软件安装包 我的 ...
- Web中间件常见漏洞
IIS Internet Information Services--windows 解析漏洞 IIS 6.x 基于文件名:该版本默认会将 *.asp;.jpg 此种格式的文件名,当成 Asp 解析, ...