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 中的表又是如何以 ...
随机推荐
- 在datagridview中添加button按钮
.Net的DataGridView控件中,提供了一种列的类型,叫 DataGridViewButtonColumn ,这种列类型是展示为一个 按钮,可以给button赋予相应的text,并且,此but ...
- 基于双下划线的跨表查询 (join查询)
因为你的数据库中的查询就是重点 那么你的django提供的orm也是查询语句最重点 ,也提供的查询方法比较的多,下面我们学习下类似于MYSQL的连表(join)查询 Django 还提供了一种直观而 ...
- mysql 的增删改查
数据库的基本流程就是先看你的数据库中的库都是哪些:show databases; 然后再进入相应的库进行操作 : use+进入的库/表 切换路径 查看这个库内的所有的表: show tabales ...
- Mysql学习---索引的学习 180101
索引:约束 + 快速查找 索引是数据库中用于帮助用户快速查询数据的一种数据结构.类似于字典中的目录,查找字典内容时可以根据目录查找到数据的存放位置,然后直接获取即可. 问:为什么索引可以这么快?[类似 ...
- 如何在Code First、Database First和Model First之间选择
Code First.Database First和Model First基本图解: 1)Database First: 如果数据库已经存在,可以使用VS自动生成数据模型,已经相关的edmx信息 2) ...
- java冒泡排序-选择排序-插入排序-使用API中文文档直接调用函数
import java.util.Arrays; public class ArrayDemo2_3 { public static void main(String []args) { //---- ...
- VNC Viewer
首先需要明确,什么事VNC , Virtual Network Computing ,VNC允许Linux系统可以类似实现像Windows中的远程桌面访问那样访问Linux桌面. 首先试试服务器装了V ...
- 【OpenCV】三种方式操作图像像素
OpenCV中,有3种访问每个像素的方法:使用at方法.使用迭代器方法.使用指针 运行如下程序后可以发现使用at方法速度最快. 代码如下: //操作图像像素 #include <opencv2/ ...
- Web项目打成war包部署到tomcat时报MySQL Access denied for user 'root'@'localhost' (using password: YES)错误解决方案
Web项目使用使用root账号root密码进行部署,通过Eclipse加载到Tomcat服务器可以发布成功,打成war包放到tomcat的webapps目录无法发布成功,报错: jdbc.proper ...
- [19/05/02-星期四] GOF23_行为型模式(状态模式、观察者模式、备忘录模式)
一.状态模式 [状态接口] /*** * 房间"状态"接口 */ package cn.sxt.state; public interface State { void handl ...