HBase操作一
package Hbase; import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hdfs.protocol.proto.ClientNamenodeProtocolProtos.GetDataEncryptionKeyRequestProto;
import org.apache.hadoop.io.IOUtils; public class HbaseOperation { public static HTable getHTableByTableName(String tableName) throws IOException {
// get instance of default configuration
Configuration configuration = HBaseConfiguration.create();
// get table instance
HTable table = new HTable(configuration, tableName); return table;
} public static void getData(String tableName) throws IOException{
//String tableName = "user";
HTable table = getHTableByTableName(tableName);
//create get with rowkey
Get rowkey = new Get(Bytes.toBytes("10001"));
//****************************************************
//add column
rowkey.addColumn(//
Bytes.toBytes("info"),//
Bytes.toBytes("name")
); rowkey.addColumn(//
Bytes.toBytes("info"),//
Bytes.toBytes("age")
); //get data
Result result = table.get(rowkey);
//key : rewkey + cf +c +version
//value :value
for(Cell cell:result.rawCells()){
System.out.println(//
Bytes.toString(CellUtil.cloneFamily(cell)) + ":" //
+ Bytes.toString(CellUtil.cloneQualifier(cell)) + "-->" //
+ Bytes.toString(CellUtil.cloneValue(cell))
);
}
//table close
table.close(); } public static void putData(String tableName) throws IOException {
//String tableName = "user";
HTable table = getHTableByTableName(tableName); Put put = new Put(Bytes.toBytes("10003"));
//add a column with value
put.add(//
Bytes.toBytes("info"),//
Bytes.toBytes("name"),//
Bytes.toBytes("wangwu")//
); put.add(//
Bytes.toBytes("info"),//
Bytes.toBytes("age"),//
Bytes.toBytes("26")//
); put.add(//
Bytes.toBytes("info"),//
Bytes.toBytes("address"),//
Bytes.toBytes("tianjing")//
); table.put(put); table.close();
} public static void deleteData(String tableName) throws IOException {
HTable table = getHTableByTableName(tableName);
Delete delete = new Delete(Bytes.toBytes("10003"));
//delete a certain column
// delete.deleteColumn(Bytes.toBytes("info"), //
// Bytes.toBytes("address")); //delete a familycolumn
delete.deleteFamily(Bytes.toBytes("info")); table.delete(delete);
table.close();
} public static void main(String[] args) throws IOException {
String tableName = "user";
// HTable table = getHTableByTableName(tableName);
// getData(tableName);
// putData(tableName);
// deleteData(tableName); HTable table = null;
ResultScanner resultScanner = null;
try{
table = getHTableByTableName(tableName); Scan scan = new Scan();
//the range of scan
scan.setStartRow(Bytes.toBytes("10001"));
scan.setStartRow(Bytes.toBytes("10004")); //scan the certain column or familycolumn
// scan.addColumn(family, qualifier);
// scan.addFamily(family); //another way to scan
//Scan scan2 = new Scan(startRow, stopRow); //PrefixFilter
//PageFilter
// scan.setFilter(filter); // scan.setCacheBlocks(cacheBlocks);
// scan.setCaching(caching); resultScanner = table.getScanner(scan);
for (Result result:resultScanner) {
System.out.println(Bytes.toString(result.getRow()));
//System.out.println(result); for(Cell cell:result.rawCells()){
System.out.println(//
Bytes.toString(CellUtil.cloneFamily(cell)) + ":" //
+ Bytes.toString(CellUtil.cloneQualifier(cell)) + "-->" //
+ Bytes.toString(CellUtil.cloneValue(cell))
);
}
System.out.println("------------------------------");
}
}catch(Exception e){
e.printStackTrace();
}finally{
IOUtils.closeStream(resultScanner);
IOUtils.closeStream(table);
}
}
}
HBase操作一的更多相关文章
- HBase操作(Shell与Java API)
版权声明:本文为博主原创文章,未经博主允许不得转载. 转: http://blog.csdn.net/u013980127/article/details/52443155 下面代码在Hado ...
- Oracle数据迁移至HBase操作记录
Oracle数据迁移至HBase操作记录 @(HBase) 近期需要把Oracle数据库中的十几张表T级别的数据迁移至HBase中,过程中遇到了许多苦难和疑惑,在此记录一下希望能帮到一些有同样需求的兄 ...
- 实验3- 熟悉常用的 HBase 操作
石家庄铁道大学信息科学与技术学院 实验报告 2018年----2019年 第一学期 题目: 熟悉常用的 HBase ...
- hbase操作(shell 命令,如建表,清空表,增删改查)以及 hbase表存储结构和原理
两篇讲的不错文章 http://www.cnblogs.com/nexiyi/p/hbase_shell.html http://blog.csdn.net/u010967382/article/de ...
- HBase篇--HBase操作Api和Java操作Hbase相关Api
一.前述. Hbase shell启动命令窗口,然后再Hbase shell中对应的api命令如下. 二.说明 Hbase shell中删除键是空格+Ctrl键. 三.代码 1.封装所有的API pa ...
- 熟悉常用的HBase操作,编写MapReduce作业
1. 以下关系型数据库中的表和数据,要求将其转换为适合于HBase存储的表并插入数据: 学生表(Student) 学号(S_No) 姓名(S_Name) 性别(S_Sex) 年龄(S_Age) 201 ...
- Oracle数据导入Hbase操作步骤
——本文非本人原创,为公司同事整理,发布至此以便查阅 一.入库前数据准备 1.入hbase详细要求及rowkey生成规则,参考文档“_入HBase库要求 20190104.docx”. 2.根据标准库 ...
- Hbase操作table常见方法示例
首先上我的输出类: /** * 功能:电池历史数据数据结构 * Created by liuhuichao on 2016/12/5. */ public class ResBatteryDataHi ...
- 第9章 HBase操作
目录 9.1 集群环境搭建 1.上传解压HBase安装包 2.hbase-env.sh文件配置 3.hbase-site.xml文件配置 4.regionservers文件配置 5.拷贝hbase到其 ...
- spark 对hbase 操作
本文将分两部分介绍,第一部分讲解使用 HBase 新版 API 进行 CRUD 基本操作:第二部分讲解如何将 Spark 内的 RDDs 写入 HBase 的表中,反之,HBase 中的表又是如何以 ...
随机推荐
- django定义Model中的方法和属性
#定义一个Model class UserProfile(models.Model): user=models.OneToOneField(User,unique=True) phone=models ...
- mysql服务器硬件配置选择参考
这是在网上找的一个关于如何评估一个mysql服务器的硬件需求的文章,转载以备用 5 Minute DBA – Database Server Hardware Selection Posted on ...
- January 04 2017 Week 1st Wednesday
Nothing happens unless first a dream. 一切始于梦想. I have a dream, one day I can be the expert in this fi ...
- jquery cookie插件
jquery-cookie下载地址:http://www.bootcdn.cn/jquery-cookie/ 使用方法: 1.引入jQuery.Cookie.js插件. <script src= ...
- angularJs的$scope.$apply
<!DOCTYPE HTML> <html ng-app> <head> <meta http-equiv="Content-Type" ...
- BZOJ 1491 社交网络 Floyd 最短路的数目
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1491 题目大意: 见链接 思路: 直接用floyd算法求最短路,同时更新最短路的数目即 ...
- BZOJ4321:queue2(DP)
Description n 个沙茶,被编号 1~n.排完队之后,每个沙茶希望,自己的相邻的两人只要无一个人的编号和自己的编号相差为 1(+1 或-1)就行.现在想知道,存在多少方案满足沙茶们如此不苛刻 ...
- PHP------TP命名空间
命名空间: 相当于一个虚拟的目录 正常管理文件使用文件夹--物理区分 TP框架的初始命名空间是:ThinkPHP\Library 在TP框架下命名空间里面使用\代表的是初始命名空间(ThinkPHP\ ...
- Hadoop学习之路(十八)MapReduce框架Combiner分区
对combiner的理解 combiner其实属于优化方案,由于带宽限制,应该尽量map和reduce之间的数据传输数量.它在Map端把同一个key的键值对合并在一起并计算,计算规则与reduce一致 ...
- 5、RabbitMQ-订阅模式 Publish/Subscribe
http://www.rabbitmq.com/tutorials/tutorial-three-java.html 1.模型图 我们之前学习的都是一个消息只能被一个消费者消费,那么如果我想发一个消息 ...