Hbase之API基本操作
API之框架

private static Admin admin = null;
private static Connection connection = null;
private static Configuration conf = null; static {
// hbase的配置文件
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "192.168.11.11");
conf.set("hbase.zookeeper.property.clientPort", "2181"); // 获取hbase的管理员
try {
connection = ConnectionFactory.createConnection(conf);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
} // 进行核心代码编写 // 释放hbase的资源
private void close(Connection connection, Admin admin) {
if (connection != null) {
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (admin != null) {
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
判断表是否存在

// 判断表是否存在
public static boolean tableExists(String tableName) throws IOException {
// 执行操作
boolean tableExists = admin.tableExists(TableName.valueOf(tableName));
return tableExists;
} public static void main(String[] args) throws IOException {
// 查看表是否存在
System.out.println(tableExists("student"));
}
创建表

// 创建表
public static void createTable(String tableName, String... cfs) throws IOException {
if (tableExists(tableName)) {
System.out.println(tableName + "已存在");
return;
}
// 表需要表描述器
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName)); // 添加列族,列族可以有多个,so for{ }
for (String cf : cfs) {
// 列族需要列描述器
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
hColumnDescriptor.setMaxVersions(1);// 添加版本号
hTableDescriptor.addFamily(hColumnDescriptor);
}
//创建表
admin.createTable(hTableDescriptor);
System.out.println(tableName + "创建成功!!!");
} public static void main(String[] args) throws IOException {
/*// 创建表*/
createTable("student1", "info");
System.out.println(tableExists("student1"));
}
删除表

// 删除表
public static void deleteTable(String tableName) throws IOException {
if (tableExists(tableName)) {
// 使得表下线(使表不可用)
admin.disableTable(TableName.valueOf(tableName));
// 删除表
admin.deleteTable(TableName.valueOf(tableName));
System.out.println(tableName + "删除成功!!!");
} else System.out.println(tableName + "不存在!!!");
} public static void main(String[] args) throws IOException {
/*// 删除表*/
deleteTable("student1");
System.out.println(tableExists("student1"));
}
添加数据

// 增加数据//修改数据
public static void putData(String tableName, String rowKey, String cf, String cn, String value) throws IOException {
if (tableExists(tableName)) {
// 获取table对象
Table table = connection.getTable(TableName.valueOf(tableName));
ArrayList<Put> list = new ArrayList<Put>();
Put put = null;
for (int i = 0; i <= rowKey.length(); i++) {
put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value));
list.add(put);
if (i % 100 == 0) {
// 开始执行添加数据操作
table.put(list);
list.clear();
}
}
// 关闭table
table.close();
} else System.out.println(tableName + "不存在!!!");
} public static void main(String[] args) throws IOException {
/*// 添加数据*/
for (int i = 0; i <= 1000; i++) {
putData("student", "100" + i, "info", "email", "tao" + i + "qq.com");
} }
删除数据

// 删除表
public static void delete(String tableName, String rowkey, String cf, String cn) throws IOException {
if (tableExists(tableName)) {
// 获取table对象
Table table = connection.getTable(TableName.valueOf(tableName));
// 创建delete对象
Delete delete = new Delete(Bytes.toBytes(rowkey)); // 删除所有版本的数据,公司常用的
delete.addColumns(Bytes.toBytes(cf), Bytes.toBytes(cn));
/**
* 如果是多个版本的数据,删除最新版本的数据,之前的版本会出来顶替值
* 在公司慎用
* 可以更细粒度是控制版本 timestamp
*/
// delete.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn)); // 开始执行删除操作
table.delete(delete);
// 关闭table
table.close();
} else System.out.println(tableName + "不存在!!!");
} public static void main(String[] args) throws IOException {
/*// 删除数据*/
delete("student", "1001", "info", "name");
}
全表扫描

// 全表扫描
public static void scan(String tableName, String startRow) throws IOException {
// 创建表对象
Table table = connection.getTable(TableName.valueOf(tableName));
// 创建scan对象 可以设置多条件 eg:startRow,stopRow
Scan scan = new Scan();
// scan.getStartRow(Bytes.toBytes(startRow));
// scan.getStopRow(); // 开始扫描数据
ResultScanner resultScanner = table.getScanner(scan);
// 表有多个rowkey
for (Result result : resultScanner) {
Cell[] cells = result.rawCells();
// 每个rowkey有多个单元格数据
for (Cell cell : cells) {
System.out.println("RK-->" + Bytes.toString(CellUtil.cloneRow(cell))
+ ",CF-->" + Bytes.toString(CellUtil.cloneFamily(cell))
+ ",CN-->" + Bytes.toString(CellUtil.cloneQualifier(cell))
+ ",VALUE-->" + Bytes.toString(CellUtil.cloneValue(cell)));
}
}
} public static void main(String[] args) throws IOException {
/*// 全表扫描数据*/
scan("student", "1002");
}
根据rowkey获取数据

// 获取指定数据 可以指定cf cn
public static void getData(String tableName, String rowkey, String cf, String cn) throws IOException {
// 获取表对象
Table table = connection.getTable(TableName.valueOf(tableName));
// 获取get对象
Get get = new Get(Bytes.toBytes(rowkey));
get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));
get.addFamily(Bytes.toBytes(cf));
get.setMaxVersions();// 默认是1版本
// 执行查找数据操作
Result result = table.get(get); // 开始遍历操作
Cell[] cells = result.rawCells(); // 每个rowkey下都有很多数据
for (Cell cell : cells) {
System.out.println("RK-->" + Bytes.toString(CellUtil.cloneRow(cell))
+ ",CF-->" + Bytes.toString(CellUtil.cloneFamily(cell))
+ ",CN-->" + Bytes.toString(CellUtil.cloneQualifier(cell))
+ ",VALUE-->" + Bytes.toString(CellUtil.cloneValue(cell)));
}
} public static void main(String[] args) throws IOException {
// 根据rowkey获取数据
getData("student", "1002", "info", "name");
}
Hbase之API基本操作的更多相关文章
- Hbase客户端API基础小结笔记(未完)
客户端API:基础 HBase的主要客户端接口是由org.apache.hadoop.hbase.client包中的HTable类提供的,通过这个类,用户可以完成向HBase存储和检索数据,以及删除无 ...
- HBase伪分布式环境下,HBase的API操作,遇到的问题
在hadoop2.5.2伪分布式上,安装了hbase1.0.1.1的伪分布式 利用HBase的API创建个testapi的表时,提示 Exception in thread "main&q ...
- 使用hbase的api创建表时出现的异常
/usr/lib/jvm/java-7-openjdk-amd64/bin/java -Didea.launcher.port=7538 -Didea.launcher.bin.path=/usr/l ...
- hbase rest api接口链接管理【golang语言版】
# go-hbase-resthbase rest api接口链接管理[golang语言版]关于hbase的rest接口的详细信息可以到官网查看[http://hbase.apache.org/boo ...
- HBase Python API
HBase Python API HBase通过thrift机制可以实现多语言编程,信息通过端口传递,因此Python是个不错的选择 吐槽 博主在Mac上配置HBase,奈何Zoomkeeper一直报 ...
- Hadoop生态圈-Hbase的API常见操作
Hadoop生态圈-Hbase的API常见操作 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.
- HBase编程 API入门系列之create(管理端而言)(8)
大家,若是看过我前期的这篇博客的话,则 HBase编程 API入门系列之put(客户端而言)(1) 就知道,在这篇博文里,我是在HBase Shell里创建HBase表的. 这里,我带领大家,学习更高 ...
- HDFS API基本操作
对HDFS API基本操作都是通过 org.apache.hadoop.fs.FileSystem类进行的,以下是一些常见的操作: package HdfsAPI; import java.io.Bu ...
- hbase java api样例(版本1.3.1,新API)
hbase版本:1.3.1 目的:HBase新API的使用方法. 尝试并验证了如下几种java api的使用方法. 1.创建表 2.创建表(预分区) 3.单条插入 4.批量插入 5.批量插入(客户端缓 ...
随机推荐
- 编码GBK的不可映射字符,最新版sublime
最近开始学java了,跟着老师写一个hello world,刚执行javac就报错:编码GBK的不可映射字符 然后去网上找了一堆,总结来说的就是编码不对,最新版的sublime只要设置utf-8保存即 ...
- Redis 15 主从复制
参考源 https://www.bilibili.com/video/BV1S54y1R7SB?spm_id_from=333.999.0.0 版本 本文章基于 Redis 6.2.6 概述 主从复制 ...
- CM311-1a(S905L3系列)玩转桌面
那安装了ambian后,玩转桌面(安装GUI桌面环境)有没有可能呢?那肯定啊!那桌面有什么用?当然有用,多一种玩法,可以写写代码,上网冲浪,学习linux语法什么的.而且单主机只要40左右,想想看可以 ...
- 如何使能512个virtio_blk设备
一例virtio_blk设备中断占用分析 背景:这个是在客户的centos8.4的环境上复现的,dpu是目前很多 云服务器上的网卡标配了,在云豹的dpu产品测试中,dpu实现的virtio_blk 设 ...
- hotspot算法实现 <<深入理解Java虚拟机>>
1.枚举根节点 解决何时枚举,不需要实时的枚举,oopMap数据结构对象存储枚举信息 对象引用发生变化,需要存储每一条指令到OOPMap吗,,几百M的对象耗时需要很大的内存.GC空间成本 2.安全点: ...
- SpringMVC完整版详解
1.回顾MVC 1.1什么是MVC MVC是模型(Model).视图(View).控制器(Controller)的简写,是一种软件设计规范. 是将业务逻辑.数据.显示分离的方法来组织代码. MVC主要 ...
- .NET 开源工作流: Slickflow流程引擎高级开发(十) -- BpmnJS流程设计器集成
前言: 在Slickflow产品开发过程中,前端流程设计器经历了几个不同的版本(jsPlumb, mxGraph等),目的是为了在设计流程时的用户体验更加良好,得到客户的好评和认可.BpmnJS流程设 ...
- 二 代理模式【Proxy Pattern】 来自CBF4LIFE 的设计模式
什么是代理模式呢?我很忙,忙的没空理你,那你要找我呢就先找我的代理人吧,那代理人总要知道被代理人能做哪些事情不能做哪些事情吧,那就是两个人具备同一个接口,代理人虽然不能干活,但是被代理的人能干活呀. ...
- Android下的IPC通信方式
一.Bundle Android的Activity.Service.Receiver都支持在Intent传递Bundle数据,Bundle实现了Parcelable接口, 所以能很方便的在不同进程之间 ...
- Altium Designer 18学习
目录 目录 快捷键 通孔 敷铜 修改铜皮与导线之间的间隔 去除指定敷铜区域 DRC设计规则检查问题: 快捷键 EJC 快速跳转到器件 M 移动 CTRL+M 测量距离 通孔 敷铜 放置多边形平面 -- ...