HBase API操作
|的ascII最大
ctrl+shift+t查找类 ctrl+p显示提示
HBase API操作
依赖的jar包
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.3.</version>
</dependency> <dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.3.</version>
</dependency> </dependencies>
public class TestHbase {
//1.构建Configuration, Connection, Admin
//Configuration 持有了zk的信息,进而hbase集群的信息可以间接获得
public static Configuration conf;
//Connection hbase连接 借助配置信息 获得连接
public static Connection connection;
public static Admin admin;
static{ //为静态属性初始化,或者说辅助类初始化
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "hadoop101,hadoop102,hadoop103");
try {
connection = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
}
//admin
try {
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
//1.创建库
public static void createNS(String namespace) throws IOException {
//①构建 ns的描述器 声明库名
NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(namespace).build();
//②创建库
try{
admin.createNamespace(namespaceDescriptor);
}catch (NamespaceExistException e){
System.out.println("该库已经存在!");
}
//③关资源
admin.close();
}
//2.判断表是否存在
public static boolean isExists(String tableName) throws IOException {
boolean exists = admin.tableExists(TableName.valueOf(tableName));
System.out.println("exits:" + exists);
admin.close();
return exists;
}
//3.创建表
public static void createTable(String tableName, String... info) throws IOException {
//①HTableDescriptor
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
//②添加columnFamily 列族
for (String cf : info) {
hTableDescriptor.addFamily(new HColumnDescriptor(cf));
}
//③建表
admin.createTable(hTableDescriptor);
//④释放资源
admin.close();
}
public static void deleteTable(String tableName) throws IOException {
//禁用并删除表
admin.disableTable(TableName.valueOf(tableName));
admin.deleteTable(TableName.valueOf(tableName));
admin.close();
}
//5.插入数据 put 'student','1001','cf1:name','kris'
public static void insertData(String tableName, String rowkey, String column, String value) throws IOException {
//①获取table
Table table = connection.getTable(TableName.valueOf(tableName));
//②获得put
Put put = new Put(Bytes.toBytes(rowkey));//把String类型转成bytes类型
put.addColumn(Bytes.toBytes(column.split(":")[0]), Bytes.toBytes(column.split(":")[1]),
Bytes.toBytes(value));
table.put(put); //③添加数据
table.close();//④释放资源
}
//6.删除数据
public static void deleteData(String tableName, String... rowkey) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
for (String rk : rowkey) {
Delete del = new Delete(Bytes.toBytes(rk));//获得delete对象,其中持有要删除行的rowkey
table.delete(del);
}
table.close();
}
//7.查询
public static void queryAll(String tableName) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner results = table.getScanner(scan);
for (Result result : results) { //result对应一行数据
Cell[] cells = result.rawCells(); //获取一行的所有cells
for (Cell cell : cells) {
String rowkey = Bytes.toString(CellUtil.cloneRow(cell));//
String family = Bytes.toString(CellUtil.cloneFamily(cell));
String column = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println("rowkey:" + rowkey + "\t" + family + ":" + column
+"\t" + value);
}
}
}
//8.查询单行
public static void getRow(String tableName, String rowkey) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowkey));
get.addColumn(Bytes.toBytes("cf2"), Bytes.toBytes("name"));
//get.addFamily(Bytes.toBytes("cf1")); //如果不追加列族,则查询所有列族
Result result = table.get(get);
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println("查询单行");
String row = Bytes.toString(CellUtil.cloneRow(cell));
String family = Bytes.toString(CellUtil.cloneFamily(cell));
String column = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println("row:" + row +"\t" + family + ":" + column + "\t" + value);
}
}
HBase API操作的更多相关文章
- 5.Hbase API 操作开发
Hbase API 操作开发需要连接Zookeeper进行节点的管理控制 1.配置 HBaseConfiguration: 包:org.apache.hadoop.hbase.HBaseConfigu ...
- HBASE API操作问题总结
org.apache.hadoop.hbase.MasterNotRunningException 在centos中查看,发现没有HMaster进程 解决方法: 1.启动hadoop后,需要等一段时间 ...
- Hbase——API操作
1.判断表是否存在 public static boolean isTableExit(String tableName) throws IOException { // //获取配置文件信息 // ...
- Hbase Shell命令详解+API操作
HBase Shell 操作 3.1 基本操作1.进入 HBase 客户端命令行,在hbase-2.1.3目录下 bin/hbase shell 2.查看帮助命令 hbase(main):001:0& ...
- 大数据技术之_11_HBase学习_02_HBase API 操作 + HBase 与 Hive 集成 + HBase 优化
第6章 HBase API 操作6.1 环境准备6.2 HBase API6.2.1 判断表是否存在6.2.2 抽取获取 Configuration.Connection.Admin 对象的方法以及关 ...
- HBase 6、用Phoenix Java api操作HBase
开发环境准备:eclipse3.5.jdk1.7.window8.hadoop2.2.0.hbase0.98.0.2.phoenix4.3.0 1.从集群拷贝以下文件:core-site.xml.hb ...
- HBase API 基础操作
对于数据操作,HBase支持四类主要的数据操作,分别是: Put :增加一行,修改一行 Delete :删除一行,删除指定列族,删除指定column的多个版本,删除指定column的制定版本等 Get ...
- 使用IDEA操作Hbase API 报错:org.apache.hadoop.hbase.client.RetriesExhaustedException的解决方法:
使用IDEA操作Hbase API 报错:org.apache.hadoop.hbase.client.RetriesExhaustedException的解决方法: 1.错误详情: Excepti ...
- Java API 操作HBase Shell
HBase Shell API 操作 创建工程 本实验的环境实在ubuntu18.04下完成,首先在改虚拟机中安装开发工具eclipse. 然后创建Java项目名字叫hbase-test 配置运行环境 ...
随机推荐
- 修改oracle数据库允许连接的数
当前连接数:select count(*) from v$process;查询数据库允许的最大连接数: select value from v$parameter where name = 'proc ...
- 修复ogg source端意外宕机造成的数据不同步
修复ogg source端意外宕机造成的数据不同步 分类: Oracle2016-04-28 11:50:40原文地址:修复ogg source端意外宕机造成的数据不同步 作者:十字螺丝钉 ogg s ...
- ios 调整 label 的字体行间距
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 200) ...
- Connection reset by [server_ip] port 22 (hexo d 部署博客出错)
问题 在使用 hexo d 部署博客和使用 Git/Github 进行 git push -u origin master 时遇到了以下问题: git -c diff.mnemonicprefix=f ...
- Confluence 6 基本性能问题诊断步骤
基本性能问题诊断步骤 开始下面的程序: 进入 Troubleshooting Confluence hanging or crashing页面找到已知的主要性能问题. 进行页面 Performance ...
- Confluence 6 编辑一个站点装饰文件
希望编辑一个站点的 decorator 文件: 进入 > 基本配置(General Configuration) > 布局(Layouts )(在Look and Feel 菜单下面) ...
- Mybaits动态Sql
什么是动态SQL? MyBatis的强大之处便是它的动态SQL,如果你使用JDBC那么在根据不同条件查询时,拼接SQL语句是多么的痛苦. 比如查询一个学生信息,可以根据学生的姓名,性别,班级,年龄,学 ...
- Max Sum (dp)
Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. F ...
- 微信浏览器发送ajax请求执行多次解决方法
http://www.cnblogs.com/whatlonelytear/p/8934738.html
- poj3417lca+树上差分
/* 给定n个点的树,在其中加入m条新边(称为非树边) 现在可以割断一条树边,一条非树边,使图分裂成两个联通块,请问有几种切割方式 对树边进行分情况讨论 如果树边不处在环中,则割断这条树边后可以割断任 ...