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. 已经不再使用的表为什么数据页还在SQLServer的内存缓存中

    1. 问题发现 在学习内存调优时,使用如下代码,查询目前内存缓冲区中生产数据库的每个对象缓存页计数 SELECT count(*)AS cached_pages_count ,name ,index_ ...

  2. asp.net mvc4 小问题

    最近在学习mvc4中间出现一些问题.留作记录.. 1.新建立的项目在vs2013中运行后会出现一个长轮询..这个叫browserLink 是vs2013中新加入的东西.至于更多解释.直接百度.. 关闭 ...

  3. python----------闭包 、装饰器

    闭包: 就是内层函数对外层函数(非全局变量的)非全局变量的引用 def func(): name = '老人家' def func1(): print(name)#局部变量对全局变量的引用 因为并没有 ...

  4. 定义类/实例(Class)

    # -*- coding: UTF-8 -*- class pp(): '''Description'''   def __init__(self,name): #初始化函数     self.nam ...

  5. HTTP协议图--HTTP 协议基础

    1.通过请求和响应的交换达成通信 应用 HTTP 协议时,必定是一端担任客户端角色,另一端担任服务器端角色.仅从一条通信线路来说,服务器端和客服端的角色是确定的.HTTP 协议规定,请求从客户端发出, ...

  6. 原生js模仿jq fadeIn fadeOut效果 兼容IE低版本

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  7. 解决python编码问题报错:'ascii' codec can't encode characters in position 0-15: ordinal not in range(128)

    这个问题很奇怪,在服务器上执行一个写数据库的python文件,正常执行,但是使用java的ssh进行调用脚本,发现就是不执行数据库的写入,然后使用了try except的方式,打印了错误信息,发现报错 ...

  8. Spark Streaming和Kafka集成深入浅出

    写在前面 本文主要介绍Spark Streaming基本概念.kafka集成.Offset管理 本文主要介绍Spark Streaming基本概念.kafka集成.Offset管理 一.概述 Spar ...

  9. 6、RabbitMQ-路由模式

    Exchange(交换机 转换器) Exchange分发消息时根据类型的不同分发策略有区别, 目前共四种类型:direct.fanout.topic.headers .   一方面是接受生产者的消息, ...

  10. No.4 - 3D 空间的卡片翻转动效

    参考 ①张鑫旭http://www.zhangxinxu.com/wordpress/2012/09/css3-3d-transform-perspective-animate-transition/ ...