一、get 、put、delete、scan

1、代码

package com.beifeng.senior.hadoop.hbase;

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.io.IOUtils; /**
* CRUD operations
*
* @author root
*
*/ public class HBaseOperation { public static HTable getHTableByTableName(String tableName) throws Exception {
// get instance of default configuration
Configuration configuration = HBaseConfiguration.create(); // get table instance
HTable table = new HTable(configuration, tableName); return table;
} public void getData() throws Exception {
String tableName = "user"; // default.user HTable table = getHTableByTableName(tableName); //create get with rowkey
Get get = new Get(Bytes.toBytes("10002")); //add column
get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));
get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age")); //get data
Result result = table.get(get); //key: rowkey + 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();
} /**
* 建议:
* tablename & column family ->常量, HbaseTableContent
*
* Map<String, Object>
*
* @throws Exception
*/ public void putData() throws Exception {
String tableName = "user"; // default.user HTable table = getHTableByTableName(tableName); Put put = new Put(Bytes.toBytes("10004")); //add a column with value
put.add(Bytes.toBytes("info"),
Bytes.toBytes("name"),
Bytes.toBytes("zhaoliu")); put.add(Bytes.toBytes("info"),
Bytes.toBytes("age"),
Bytes.toBytes("25")); put.add(Bytes.toBytes("info"),
Bytes.toBytes("address"),
Bytes.toBytes("shanghai")); table.put(put); table.close();
} public void deleteData() throws Exception {
String tableName = "user"; // default.user HTable table = getHTableByTableName(tableName); Delete delete = new Delete(Bytes.toBytes("10004")); /*删除单个数据
delete.deleteColumn(Bytes.toBytes("info"),
Bytes.toBytes("address"));
*/ /*删除整个列簇*/
delete.deleteFamily(Bytes.toBytes("info")); table.delete(delete); table.close();
} public static void main(String[] args) throws Exception {
String tableName = "user"; // default.user HTable table = null;
ResultScanner resultScanner = null; try {
table = getHTableByTableName(tableName); /*全表扫描
Scan scan = new Scan();
*/ //指定Rowkey范围
Scan scan = new Scan(); scan.setStartRow(Bytes.toBytes("10002"));
scan.setStopRow(Bytes.toBytes("10004")); 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(main):006:0> flush 'table name'        //刷数据

hbase(main):007:0> compact 'table name'    //合并数据

2.3-2.6 HBase java API的更多相关文章

  1. 【Hbase学习之三】Hbase Java API

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-2.6.5 hbase-0.98.12.1-h ...

  2. hbase java api样例(版本1.3.1,新API)

    hbase版本:1.3.1 目的:HBase新API的使用方法. 尝试并验证了如下几种java api的使用方法. 1.创建表 2.创建表(预分区) 3.单条插入 4.批量插入 5.批量插入(客户端缓 ...

  3. hbase java API跟新数据,创建表

    package hbaseCURD; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import o ...

  4. HBase 学习之路(六)——HBase Java API 的基本使用

    一.简述 截至到目前(2019.04),HBase 有两个主要的版本,分别是1.x 和 2.x ,两个版本的Java API有所不同,1.x 中某些方法在2.x中被标识为@deprecated过时.所 ...

  5. HBase 系列(六)——HBase Java API 的基本使用

    一.简述 截至到目前 (2019.04),HBase 有两个主要的版本,分别是 1.x 和 2.x ,两个版本的 Java API 有所不同,1.x 中某些方法在 2.x 中被标识为 @depreca ...

  6. Hbase Java API详解

    HBase是Hadoop的数据库,能够对大数据提供随机.实时读写访问.他是开源的,分布式的,多版本的,面向列的,存储模型. 在讲解的时候我首先给大家讲解一下HBase的整体结构,如下图: HBase ...

  7. Hbase Java API程序设计步骤

    http://www.it165.net/admin/html/201407/3390.html 步骤1:创建一个Configuration对象 包含了客户端链接Hbase服务所需的全部信息: zoo ...

  8. HBase Java API使用(一)

    前言 1. 创建表:(由master完成) 首先需要获取master地址(master启动时会将地址告诉zookeeper)因而客户端首先会访问zookeeper获取master的地址 client和 ...

  9. Hbase(六) hbase Java API

    一. 几个主要 Hbase API 类和数据模型之间的对应关系: 1. HBaseAdmin关系: org.apache.hadoop.hbase.client.HBaseAdmin作用:提供了一个接 ...

  10. Hbase Java API包括协处理器统计行数

    package com.zy; import java.io.IOException; import org.apache.commons.lang.time.StopWatch; import or ...

随机推荐

  1. C---指针篇

    指针变量:专门存放内存地址的一种变量 听说C因为指针而强大 一段代码来解释 指针 *指针 &指针 &指向变量 的关系 /* * 返回指针所指向内存地址中存放的值 它是单目运算符 也称作 ...

  2. git mirror的创建与使用

    please donwload repo mirro as follow steps, thanks 1.mirror server,server IP:192.168.0.123 1.1 -- de ...

  3. Spring Boot中使用RSocket

    1. 概述 RSocket应用层协议支持 Reactive Streams语义, 例如:用RSocket作为HTTP的一种替代方案.在本教程中, 我们将看到RSocket用在spring boot中, ...

  4. asp.net mvc4 之Webapi之客户端或服务器端安全控制

    一.WebAPI的工作方式 WebAPI的工作方式:HTTP的请求最先是被传递到HOST中的,如果WebAPI是被寄宿在IIS上的,这个HOST就是IIS上,HOST是没有能力也没有必 要进行请求的处 ...

  5. Liquibase

    http://www.liquibase.org/documentation/index.html https://github.com/studygolang/studygolang/tree/ma ...

  6. splittability A SequenceFile can be split by Hadoop and distributed across map jobs whereas a GZIP file cannot be.

    splittability CompressedStorage     Skip to end of metadata   Created by Confluence Administrator, l ...

  7. dataware fact 事实 不可更新 data warehousing business intelligence 优劣判据

    不可 Kimball维度建模 维度建模,而非数据建模 文本型度量是对某些事情的描述.虽然以文本方式度量事实是可行的,但是应将其放入维度表中,除非对事实表的每个行,其文本是唯一的. 数据仓库的好坏直接取 ...

  8. github 工具命令集

  9. SURF matlab 检测函数使用

    1.这篇介绍SURF检测blob的函数. 函数/Functions 函数名称:detectSURFFeatures 功能:利用The Speeded-Up Robust Features(SURF)算 ...

  10. linux 脚本统计代码行数

    由于实际需求,需要统计开源产品的代码行数,so,以下命令统计*.c的行数. .h,.java  .同理 find . -name *.c|xargs wc -l