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.批量插入(客户端缓 ...
随机推荐
- 如何设计一个分布式 ID 发号器?
大家好,我是树哥. 在复杂的分布式系统中,往往需要对大量的数据和消息进行唯一标识,例如:分库分表的 ID 主键.分布式追踪的请求 ID 等等.于是,设计「分布式 ID 发号器」就成为了一个非常常见的系 ...
- 精心整理16条MySQL使用规范,减少80%问题,推荐分享给团队
上篇文章介绍了如何创建合适的MySQL索引,今天再一块学一下如何更规范.更合理的使用MySQL? 合理规范的使用MySQL,可以大大减少开发工作量和线上问题,并提升SQL查询性能. 我精心总结了这16 ...
- C#反射跟特性
一.什么是反射? 了解反射之前我们必须知道一个概念--元数据.有关程序和程序类型的信息叫做元数据,通俗的解释就是类里面的方法.属性.字段等. 而程序在运行的时候去查看其它程序集的行为就叫做反射.在我们 ...
- 毕昇编译器优化:Lazy Code Motion
摘要:本文中,我们将介绍通过代码移动(插入)的方式消除冗余计算的一个典型方法. 本文分享自华为云社区<编译器优化那些事儿(3):Lazy Code Motion>,作者:毕昇小助手. 导语 ...
- RabbitMQ 入门系列:2、基础含义理解:链接、通道、队列、交换机
系列目录 RabbitMQ 入门系列:1.MQ的应用场景的选择与RabbitMQ安装. RabbitMQ 入门系列:2.基础含义:链接.通道.队列.交换机. RabbitMQ 入门系列:3.基础含义: ...
- vue.js及H5常见跨域问题解决方案
一.原生H5跨域问题解决方案 1.live-server 代理解决 首先在有node.js环境下,打开命令行工具,输入 npm install live-server -g 全局安装全局安装 live ...
- html页面嵌套其他网站页面的方法
直接上代码:html页面嵌套其他网站页面的方法 <div> <!--第一种:使用object标签--> <object type="text/html" ...
- RabbitMQ实现订单超时案例
前言 人间清醒 目录 前言 业务场景 JUC(DelayQueue)方案 DelayQueue简介 JUC DelayQueue实现订单超时案例代码 案例代码 Redis Key过期事件方案 简介 R ...
- APICloud 可视化编程 - 拖拉拽实现专业级源码
低代码开发平台是无需编码 (0 代码或⽆代码) 或通过少量代码就可以快速生成应用程序的开发平台.它的强⼤之处在于,允许终端⽤户使⽤易于理解的可视化⼯具开发自己的应用程序,而不是传统的编写代码⽅式.当遇 ...
- 2.窗口部件-对话框QDialog
1.模态和非模态 看代码 widget.cpp #include "widget.h" #include "ui_widget.h" #include<Q ...