编辑pom.xml

<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.2.0</version>
</dependency> <dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.0</version>
</dependency>

java文件

package com.cenzhongman.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.io.IOUtils; /**
*
* 演示 HBase 的 JDBC 连接及 CRUD 操作
*
* @author cen
*
*/ public class HBaseOperation {
static HTable table = null;
static String TABLE_NAME = "user"; /**
* getHTablebyTbaleName
*
* @param tableName
* @return
*/
@SuppressWarnings("deprecation")
public static HTable getHTable() {
// 1.get instance of Default Configuration 读取 core-site.xml hdfs-site.xml 信息
// Configuration conf = new Configuration();//HDFS中获取core-site.xml hdfs-site.xml
// 的方法
// conf.addResource("hbase-default.xml");
// conf.addResource("hbase-site.xml");
// Hbase封装的获取方法(详见源码),增加了读取HBase 的配置文件
Configuration conf = HBaseConfiguration.create(); // 2.get Table instance
try {
table = new HTable(conf, TABLE_NAME);
} catch (IOException e) {
e.printStackTrace();
} return table;
} /**
* getData by table,rowKey,columns[{cfs}][columns]
*
* @param table
* @param rowKey
* @param columns
*/
public static void getData(String rowKey, String[][] columns) { try {
table = getHTable();
// create get with RowKey 选定Get的RowKey
Get get = new Get(Bytes.toBytes(rowKey)); if (columns != null) {
// add column 增加列查询条件
for (int i = 0; i < columns[0].length; i++) {
for (int j = 0; j < columns[1].length; j++) {
get.addColumn(Bytes.toBytes(columns[0][i]), Bytes.toBytes(columns[1][j]));
}
}
}
// get Result
Result result = null;
result = table.get(get); // Key:rowKwy + cf + c + version + type +
// 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)));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeStream(table);
}
} public static void getData(String rowKey) {
getData(rowKey, null);
} /**
* putData to HBase by table,rowKey,column,value 通常一张表的表名和列簇都设置常量 使用Map比数组更简单
*
* @param table
* @param rowKey
* @param column
* @param value
*/
@SuppressWarnings("deprecation")
public static void putData(String rowKey, String cf, String column, String value) { try {
table = getHTable();
// create put with rowKey
Put put = new Put(Bytes.toBytes(rowKey)); // 增加列数据
put.add(Bytes.toBytes(cf), Bytes.toBytes(column), Bytes.toBytes(value)); // put The data to put.
// 实际使用list<Puts>
table.put(put);
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeStream(table);
}
} /**
* 删除一张表中的数据
*
* @param table
* @param rowKey
* @param cf
* @param column
*/
public static void deleteData(String rowKey, String cf, String column) {
// create Delete
try {
table = getHTable();
Delete delete = new Delete(Bytes.toBytes(rowKey)); // 需要 delete 的 data
// delete.addColumn(Bytes.toBytes(cf), Bytes.toBytes(column));//删除最新的版本
delete.addColumns(Bytes.toBytes(cf), Bytes.toBytes(column));// 删除所有的版本 table.delete(delete);
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeStream(table);
}
} /**
* scan data from HBase 包头不包尾
*/
public static void scanData() {
scanData(null, null, null, null);
} public static void scanData(String startRow) {
scanData(startRow, null, null, null);
} public static void scanData(String startRow, String stopRow) {
scanData(startRow, stopRow, null, null);
} public static void scanData(String startRow, String stopRow, String family, String qualifier) {
ResultScanner resultScanner = null; try {
Scan scan = new Scan(); //三种方式
//1.范围扫描可用构造函数来设置
// Scan scan2 = new Scan(startRow, stopRow) //2.设置起始范围
if (startRow != null) {
scan.setStartRow(Bytes.toBytes(startRow));
}
if (stopRow != null) {
scan.setStopRow(Bytes.toBytes(stopRow));
} // 列过滤条件
if (family != null) {
if (qualifier != null) {
scan.addFamily(Bytes.toBytes(family));
}
scan.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
} //3.使用过滤器
/*
* 使用Filter查询速度会下降
* PrefixFilter : 前缀过滤
* PageFilter : 分页过滤
*/
// scan.setFilter(filter); //设置缓存
// scan.setCacheBlocks(cacheBlocks);//把常用的数据缓存到 RegionServer 的 BlocksCache 中
// scan.setCaching(caching);//每一次扫描时获取列的数目 table = getHTable();
resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
System.out.println("Row:" + Bytes.toString(result.getRow()));
for (Cell cell : result.rawCells()) {
System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)) + ":"
+ Bytes.toString(CellUtil.cloneQualifier(cell)) + "->"
+ Bytes.toString(CellUtil.cloneValue(cell)));
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeStream(resultScanner);
IOUtils.closeStream(table);
} } public static void main(String[] args) {
String rowKey = "00002";
String cf = "info";
String column = "name";
String value = "gugjhg";
putData(rowKey, cf, column, value); // deleteData(rowKey, cf, column); // String[][] columns = { { "info" }, { "name" ,"tel"} };
// getData(rowKey); scanData();
System.out.println("finish");
}
}

HBase java API 的使用范例(增,删,查,扫描)的更多相关文章

  1. Java API实现Hadoop文件系统增删改查

    Java API实现Hadoop文件系统增删改查 Hadoop文件系统可以通过shell命令hadoop fs -xx进行操作,同时也提供了Java编程接口 maven配置 <project x ...

  2. ElasticSearch6(三)-- Java API实现简单的增删改查

    基于ElasticSearch6.2.4, Java API创建索引.查询.修改.删除,pom依赖和获取es连接 可查看此文章. package com.xsjt.learn; import java ...

  3. Neo4j学习笔记(1)——使用Java API实现简单的增删改查

    阅读目录 项目的创建及配置 使用嵌入式数据库 创建节点和关系 查询及更新 删除关系和节点 完整代码 参考资料 回到顶部 项目的创建及配置 因为Neo4j依赖的jar包比较多,所以推荐使用Maven来管 ...

  4. Android 系统API实现数据库的增删改查和SQLite3工具的使用

    在<Android SQL语句实现数据库的增删改查>中介绍了使用sql语句来实现数据库的增删改查操作,本文介绍Android 系统API实现数据库的增删改查和SQLite3工具的使用. 系 ...

  5. 使用java对sql server进行增删改查

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import ...

  6. ASP.NET Web API基于OData的增删改查,以及处理实体间关系

    本篇体验实现ASP.NET Web API基于OData的增删改查,以及处理实体间的关系. 首先是比较典型的一对多关系,Supplier和Product. public class Product { ...

  7. [转]ASP.NET Web API基于OData的增删改查,以及处理实体间关系

    本文转自:http://www.cnblogs.com/darrenji/p/4926334.html 本篇体验实现ASP.NET Web API基于OData的增删改查,以及处理实体间的关系. 首先 ...

  8. java对xml文件做增删改查------摘录

    java对xml文件做增删改查 package com.wss; import java.io.File;import java.util.ArrayList;import java.util.Lis ...

  9. Hbase(六) hbase Java API

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

随机推荐

  1. POJ-2886 Who Gets the Most Candies?---线段树+约瑟夫环

    题目链接: https://cn.vjudge.net/problem/POJ-2886 题目大意: N个人围成一圈第一个人跳出圈后会告诉你下一个谁跳出来跳出来的人(如果他手上拿的数为正数,从他左边数 ...

  2. 【9.29 模拟】T3 小清新最优化(easy)

    [题目描述] 给出一个长度为 n 的序列,序列的每个元素为一个二元组,代表一种单目运算: • \((0,x)\): 对于一个数\(a\),将其变为 \(a\&x\).\((\&=x)\ ...

  3. 关于APIT定位算法的讨论

    关于APIT定位算法的讨论 [摘要]   无线传感器网络节点定位机制的研究中,基于距离无关的定位技术得到快速发展,其中基于重叠区域的APIT定位技术在实际环境中的定位精度高,被广泛研究和应用. [关键 ...

  4. HDU 5687 Problem C 【字典树删除】

    传..传送:http://acm.hdu.edu.cn/showproblem.php?pid=5687 Problem C Time Limit: 2000/1000 MS (Java/Others ...

  5. (转)超级实用且不花哨的js代码大全

    事件源对象 event.srcElement.tagName event.srcElement.type 捕获释放 event.srcElement.setCapture();  event.srcE ...

  6. 剑指offer——27. 二叉搜索树与双向链表(Java版)

    题目: 剑指offer的题目有挺多都挺典型的,就像这一道.不过书中的代码写的真是ugly,有很多题目LeetCode上都有,可以去LeetCode讨论区看看,经常有一些大神分享,写的代码真是高效.简洁 ...

  7. autofac 注册

    1 注册的概念和方式 使用autofac 的ContainerBuilder 来注册组件(components---通常指实现类),并把它的服务(service---通常指接口,抽象类,类实例)暴露给 ...

  8. 自定义AngularJS中的services服务

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  9. 全局变量&局部变量&Static存储&Register变量

    1.局部变量 局部变量也称为内部变量.局部变量是在函数内作定义说明的.其作用域仅限于函数内:函数的形参就是局部变量: 2.全局变量 全局变量也称为外部变量,它是在函数外部定义的变量.全局变量的说明符为 ...

  10. 用c#语言编写分解质因数

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...