使用Java Api 对HBase进行简单操作
/**
* 功能:测试Hbase基本的增删改查操作
* Created by liuhuichao on 2016/12/5.
*/
public class HbaseCRUDTest {
public static Configuration configuration;
static{
configuration= HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","lhc-centos");
}
/**
* 测试创建student表:测试已通过
* @throws IOException
*/
@Test
public void createTable() throws IOException {
HBaseAdmin admin=new HBaseAdmin(configuration); //HBaseAdmin负责管理HBase集群,添加和丢弃表
if(admin.tableExists("studentInfo")){
System.out.println("student表已经存在");
return;
}
HTableDescriptor descriptor=new HTableDescriptor("studentInfo");
descriptor.addFamily(new HColumnDescriptor("Name"));//创建列族,名字是Name
descriptor.addFamily(new HColumnDescriptor("Address"));//创建列族,名字是Address
admin.createTable(descriptor); //创建表
System.out.println("student表创建成功!!!");
}
/**
* 功能:想hbase中插入一行记录 --测试已通过
* @throws IOException
*/
@Test
public void insertHbaseStudentTable() throws IOException {
HTable table=new HTable(configuration, Bytes.toBytes("studentInfo"));
Put put=new Put(Bytes.toBytes("1"));
put.addColumn(Bytes.toBytes("Name"),Bytes.toBytes("firstName"),Bytes.toBytes("liu"));
put.addColumn(Bytes.toBytes("Name"),Bytes.toBytes("secondName"),Bytes.toBytes("huichao"));
put.addColumn(Bytes.toBytes("Address"),Bytes.toBytes("province"),Bytes.toBytes("hebei"));
put.addColumn(Bytes.toBytes("Address"),Bytes.toBytes("city"),Bytes.toBytes("baoding"));
put.addColumn(Bytes.toBytes("Address"),Bytes.toBytes("area"),Bytes.toBytes("qingyuan"));
table.put(put);
}
/**
* 功能:根据行健获取数据
* @throws IOException
*/
@Test
public void getDataByRowKey() throws IOException {
HTable table=new HTable(configuration, Bytes.toBytes("studentInfo"));
Get get=new Get(Bytes.toBytes("1"));
Result result=table.get(get);
for(KeyValue kv :result.list()){
System.out.println("family:"+Bytes.toString(kv.getFamilyArray()));//所属列族名称
System.out.println("qualifier:"+Bytes.toString(kv.getQualifier()));//列名称
System.out.println("value:"+Bytes.toString(kv.getValue()));//存储的值
System.out.println("Timestamp:"+kv.getTimestamp());//获取时间戳
}
}
/**
* 功能:测试全表扫描
* @throws IOException
*/
@Test
public void selectHBaseScan() throws IOException {
HTable table=new HTable(configuration, Bytes.toBytes("studentInfo"));
/*遍历查询*/
Scan scan=new Scan();
ResultScanner rs=null;
try {
rs=table.getScanner(scan);
for(Result result : rs){
for(KeyValue kv : result.list()){
System.out.println("family:"+Bytes.toString(kv.getFamilyArray()));//所属列族名称
System.out.println("qualifier:"+Bytes.toString(kv.getQualifier()));//列名称
System.out.println("value:"+Bytes.toString(kv.getValue()));//存储的值
System.out.println("Timestamp:"+kv.getTimestamp());//获取时间戳
}
}
}finally {
rs.close();
}
}
/**
* 更新
* @throws Exception
*/
@Test
public void updateHBase() throws Exception{
HTable table=new HTable(configuration,Bytes.toBytes("studentInfo"));
Put put=new Put(Bytes.toBytes("1")); //设置行健
put.add(Bytes.toBytes("Address"),Bytes.toBytes("city"),Bytes.toBytes("beijing"));///更新的时候找对族名和列名,再给定新的value值就可以了
table.put(put);
}
/**
* 功能:查询nickname的多个(本示例为2个)版本值.
* @throws Exception
*/
@Test
public void selectSomeVersion() throws Exception{
HTable table=new HTable(configuration,Bytes.toBytes("studentInfo"));
Get get=new Get(Bytes.toBytes("1"));
get.addColumn(Bytes.toBytes("Address"),Bytes.toBytes("city"));
// get.setMaxVersions(3);
List<KeyValue> results=table.get(get).list();
int total=results.size();
System.out.println("Address列族中city列的各个版本值");
for(int i=0;i<total;i++){
System.out.println(Bytes.toString(results.get(i).getValue()));
}
}
/**
* 功能:删除指定的某一行
* @throws Exception
*/
@Test
public void deleteColumn() throws Exception{
HTable table = new HTable(configuration, Bytes.toBytes("studentInfo"));//HTabel负责跟记录相关的操作如增删改查等
Delete deleteAll = new Delete(Bytes.toBytes("1"));
table.delete(deleteAll);
}
/**
* 功能:删除表
* @throws Exception
*/
@Test
public void deleteTable() throws Exception{
HBaseAdmin admin=new HBaseAdmin(configuration); //HBaseAdmin负责管理HBase集群,添加和丢弃表
admin.disableTable("student");
admin.deleteTable("student");
}
}
使用Java Api 对HBase进行简单操作的更多相关文章
- Hbase框架原理及相关的知识点理解、Hbase访问MapReduce、Hbase访问Java API、Hbase shell及Hbase性能优化总结
转自:http://blog.csdn.net/zhongwen7710/article/details/39577431 本blog的内容包含: 第一部分:Hbase框架原理理解 第二部分:Hbas ...
- 通过Java Api与HBase交互(转)
HBase提供了Java Api的访问接口,掌握这个就跟Java应用使用RDBMS时需要JDBC一样重要,本文将继续前两篇文章中blog表的示例,介绍常用的Api. import java.io.IO ...
- Java使用poi对Execl简单操作_总结
poi是Apache组织给开发者提供一套操作office(Execl,Word,PowerPoint)等Java API,开发者通过Poi API可以快速的操作office办公软件,以上3篇博文只是一 ...
- HBase总结(十二)Java API 与HBase交互实例
HBase提供了Java Api的訪问接口,掌握这个就跟Java应用使用RDBMS时须要JDBC一样重要 import java.io.IOException; import org.apache.h ...
- 通过Java Api与HBase交互
HBase提供了Java Api的访问接口,掌握这个就跟Java应用使用RDBMS时需要JDBC一样重要,本文将继续前两篇文章中blog表的示例,介绍常用的Api. import java.io.IO ...
- JAVA API访问Hbase org.apache.hadoop.hbase.client.RetriesExhaustedException: Failed after attempts=32
Java使用API访问Hbase报错: 我的hbase主节点是spark1 java代码访问hbase的时候写的是ip 结果运行程序报错 不能够识别主机名 修改主机名 修改主机hosts文 ...
- 使用JAVA API编程实现简易Habse操作
使用JAVA API编程实现下面内容: 1.创建<王者荣耀>游戏玩家信息表gamer,包含列族personalInfo(个人信息).recordInfo(战绩信息).assetsInfo( ...
- Java Api与HBase交互实例
import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hb ...
- Java中对session的简单操作
1.jsp中操作session <% String name=(String)request.getSession().getAttribute("username"); / ...
随机推荐
- linux服务器部署nodejs项目
一.安装nodejs 1.去官网下载和自己系统匹配的文件: 英文网址:https://nodejs.org/en/download/ 中文网址:http://nodejs.cn/download/ 通 ...
- 微服务框架SpringCloud(Dalston版)学习 (一):Eureka服务注册与发现
eureka-server eureka服务端,提供服务的注册与发现,类似于zookeeper 新建spring-boot工程,pom依赖: <dependency> <groupI ...
- 2763. [JLOI2011]飞行路线【分层图最短路】
Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并 ...
- @property 装饰器
property() 函数作用于新式类,返回属性值. class C(object): def __init__(self): self._x = None def getx(self): print ...
- FireFox新标签页打开搜索和书签
FireFox更新,发现界面完全不适应,虽然他是越做越丑,但是也没办法,一直用FireFox,许多书签,保存的密码现在都记不住了,只能靠保存的自动填充.... 进入正题,FireFox更新了之后,搜索 ...
- Vue实现购物小球抛物线
.shop{ position: fixed; top: 300px; left: 40px; } .ball{ position: fixed; left: 32px; bottom: 22px; ...
- PAT——1030. 完美数列
给定一个正整数数列,和正整数p,设这个数列中的最大值是M,最小值是m,如果M <= m * p,则称这个数列是完美数列. 现在给定参数p和一些正整数,请你从中选择尽可能多的数构成一个完美数列. ...
- 系统构架篇之基于SSDB的二级缓存
1.什么是ssdb 你可以把ssdb理解成redis.不同之处在于redis缓存的数据是在内存中的,所能缓存的数据大小受内存大小的限制,一般不适合缓存大量的数据.而ssdb将数据保存在磁盘中,数据量大 ...
- ora.ctssd OBSERVER
[grid@ydb1 ~]$ crsctl status res -t -init ora.ctssd 1 ONLINE ONLINE ydb1 ...
- 截屏状态监听 - iOS
既接到电话状态监听的需求之后再次添加了截屏状态的监听,当使用 App 时若用户执行截屏操作需要对当前状态进行监听操作,下面有两种方法,其中可以替换截屏的图片内容(Plan A),也可以弹出提示框(Pl ...