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操作一的更多相关文章

  1. HBase操作(Shell与Java API)

    版权声明:本文为博主原创文章,未经博主允许不得转载.     转: http://blog.csdn.net/u013980127/article/details/52443155 下面代码在Hado ...

  2. Oracle数据迁移至HBase操作记录

    Oracle数据迁移至HBase操作记录 @(HBase) 近期需要把Oracle数据库中的十几张表T级别的数据迁移至HBase中,过程中遇到了许多苦难和疑惑,在此记录一下希望能帮到一些有同样需求的兄 ...

  3. 实验3- 熟悉常用的 HBase 操作

        石家庄铁道大学信息科学与技术学院               实验报告 2018年----2019年  第一学期                       题目:  熟悉常用的 HBase ...

  4. hbase操作(shell 命令,如建表,清空表,增删改查)以及 hbase表存储结构和原理

    两篇讲的不错文章 http://www.cnblogs.com/nexiyi/p/hbase_shell.html http://blog.csdn.net/u010967382/article/de ...

  5. HBase篇--HBase操作Api和Java操作Hbase相关Api

    一.前述. Hbase shell启动命令窗口,然后再Hbase shell中对应的api命令如下. 二.说明 Hbase shell中删除键是空格+Ctrl键. 三.代码 1.封装所有的API pa ...

  6. 熟悉常用的HBase操作,编写MapReduce作业

    1. 以下关系型数据库中的表和数据,要求将其转换为适合于HBase存储的表并插入数据: 学生表(Student) 学号(S_No) 姓名(S_Name) 性别(S_Sex) 年龄(S_Age) 201 ...

  7. Oracle数据导入Hbase操作步骤

    ——本文非本人原创,为公司同事整理,发布至此以便查阅 一.入库前数据准备 1.入hbase详细要求及rowkey生成规则,参考文档“_入HBase库要求 20190104.docx”. 2.根据标准库 ...

  8. Hbase操作table常见方法示例

    首先上我的输出类: /** * 功能:电池历史数据数据结构 * Created by liuhuichao on 2016/12/5. */ public class ResBatteryDataHi ...

  9. 第9章 HBase操作

    目录 9.1 集群环境搭建 1.上传解压HBase安装包 2.hbase-env.sh文件配置 3.hbase-site.xml文件配置 4.regionservers文件配置 5.拷贝hbase到其 ...

  10. spark 对hbase 操作

    本文将分两部分介绍,第一部分讲解使用 HBase 新版 API 进行 CRUD 基本操作:第二部分讲解如何将 Spark 内的 RDDs 写入 HBase 的表中,反之,HBase 中的表又是如何以 ...

随机推荐

  1. [翻译] MJParallaxCollectionView

    MJParallaxCollectionView https://github.com/mayuur/MJParallaxCollectionView This is a parallax for t ...

  2. linux邮件客户端mutt日志文件,发不出邮件

    linux上面使用很方便的收发邮件客户端(即MUA),还有一个是mail,大家也可以试试. 在网上找了很多,都说需要msmtp配合使用,其实mutt里面也内建了smtp支持的. mutt and SM ...

  3. java String 常用方法集合

    String a = "abc";String b = "abc";a==b ;//返回true,因为a,b指向的是同一个地址 String a = new S ...

  4. 【LGP5162】WD与积木

    题目 场面过度玄学,容易引起不适 我们发现我们要求的这个期望由分母和分子两部分构成 易发现 \[Ans=\frac{\sum_{i=1}^nS_2(n,i)\times i\times i!}{\su ...

  5. chrome下载离线安装包的方法

    https://www.google.com/chrome/browser/desktop/index.html?system=true&standalone=1,一般默认下载稳定版,如果需要 ...

  6. 一款不错的网站压力测试工具webbench

    webbench最多可以模拟3万个并发连接去测试网站的负载能力,个人感觉要比Apache自带的ab压力测试工具好,安装使用也特别方便. 1.适用系统:Linux 2.编译安装: 引用 wget htt ...

  7. C#ref和out的区别-ref是有进有出,out是只出不进

    之前学习C#时候就遇到了这个问题,不过当时没有深究.昨晚想到这个问题时候自己尝试敲了敲代码,结果从运行的结果来看,越看越乱.在查看了一些资料的基础上,自己总结了一下. 可能会有点乱,但是自己总结出来的 ...

  8. webpack4配置

    一.安装webpack 需要先在项目中npm init初始化一下,生成package.json 建议node版本安装到8.2以上 // webpack4中除了正常安装webpack之外,需要再单独安一 ...

  9. QTP基本方法3-----截屏

    1.桌面截屏 Desktop.captureBitMap  path[,bolean] path:保存路径,可选择绝对路径或相对路径 相对路径是保存在脚本保存的目录下编号最大的res目录下. bole ...

  10. DPDK+Pktgen 高速发包测试

    参考博客 Pktgen概述 Pktgen,(Packet Gen-erator)是一个基于DPDK的软件框架,发包速率可达线速. 提供运行时管理,端口实时测量. 可以控制 UDP, TCP, ARP, ...