hBase官方文档以及HBase基础操作封装类
HBase 官方文档 0.97 http://abloz.com/hbase/book.html
HBase基本操作封装类(以课堂爬虫为例)
package cn.crxy.spider.utils; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
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.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.util.Bytes; public class HbaseUtils { /**
* HBASE 表名称
*/
public static final String TABLE_NAME = "spider";
/**
* 列簇1 商品信息
*/
public static final String COLUMNFAMILY_1 = "goodsinfo";
/**
* 列簇1中的列
*/
public static final String COLUMNFAMILY_1_DATA_URL = "data_url";
public static final String COLUMNFAMILY_1_PIC_URL = "pic_url";
public static final String COLUMNFAMILY_1_TITLE = "title";
public static final String COLUMNFAMILY_1_PRICE = "price";
/**
* 列簇2 商品规格
*/
public static final String COLUMNFAMILY_2 = "spec";
public static final String COLUMNFAMILY_2_PARAM = "param"; HBaseAdmin admin = null;
Configuration conf = null; /**
* 构造函数加载配置
*/
public HbaseUtils() {
conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "192.168.1.177:2181");
conf.set("hbase.rootdir", "hdfs://192.168.1.177:9000/hbase");
try {
admin = new HBaseAdmin(conf);
} catch (IOException e) {
e.printStackTrace();
}
} public static void main(String[] args) throws Exception {
HbaseUtils hbase = new HbaseUtils();
// 创建一张表
// hbase.createTable("stu","cf");
// //查询所有表名
// hbase.getALLTable();
// //往表中添加一条记录
// hbase.addOneRecord("stu","key1","cf","name","zhangsan");
// hbase.addOneRecord("stu","key1","cf","age","24");
// //查询一条记录
// hbase.getKey("stu","key1");
// //获取表的所有数据
// hbase.getALLData("stu");
// //删除一条记录
// hbase.deleteOneRecord("stu","key1");
// //删除表
// hbase.deleteTable("stu");
// scan过滤器的使用
// hbase.getScanData("stu","cf","age");
// rowFilter的使用
// 84138413_20130313145955
} /**
* rowFilter的使用
*
* @param tableName
* @param reg
* @throws Exception
*/
public void getRowFilter(String tableName, String reg) throws Exception {
HTable hTable = new HTable(conf, tableName);
Scan scan = new Scan();
// Filter
RowFilter rowFilter = new RowFilter(CompareOp.NOT_EQUAL,
new RegexStringComparator(reg));
scan.setFilter(rowFilter);
ResultScanner scanner = hTable.getScanner(scan);
for (Result result : scanner) {
System.out.println(new String(result.getRow()));
}
} public void getScanData(String tableName, String family, String qualifier)
throws Exception {
HTable hTable = new HTable(conf, tableName);
Scan scan = new Scan();
scan.addColumn(family.getBytes(), qualifier.getBytes());
ResultScanner scanner = hTable.getScanner(scan);
for (Result result : scanner) {
if (result.raw().length == 0) {
System.out.println(tableName + " 表数据为空!");
} else {
for (KeyValue kv : result.raw()) {
System.out.println(new String(kv.getKey()) + "\t"
+ new String(kv.getValue()));
}
}
}
} private void deleteTable(String tableName) {
try {
if (admin.tableExists(tableName)) {
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println(tableName + "表删除成功!");
}
} catch (IOException e) {
e.printStackTrace();
System.out.println(tableName + "表删除失败!");
} } /**
* 删除一条记录
*
* @param tableName
* @param rowKey
*/
public void deleteOneRecord(String tableName, String rowKey) {
HTablePool hTablePool = new HTablePool(conf, 1000);
HTableInterface table = hTablePool.getTable(tableName);
Delete delete = new Delete(rowKey.getBytes());
try {
table.delete(delete);
System.out.println(rowKey + "记录删除成功!");
} catch (IOException e) {
e.printStackTrace();
System.out.println(rowKey + "记录删除失败!");
}
} /**
* 获取表的所有数据
*
* @param tableName
*/
public void getALLData(String tableName) {
try {
HTable hTable = new HTable(conf, tableName);
Scan scan = new Scan();
ResultScanner scanner = hTable.getScanner(scan);
for (Result result : scanner) {
if (result.raw().length == 0) {
System.out.println(tableName + " 表数据为空!");
} else {
for (KeyValue kv : result.raw()) {
System.out.println(new String(kv.getKey()) + "\t"
+ new String(kv.getValue()));
}
}
}
} catch (IOException e) {
e.printStackTrace();
} } // 读取一条记录
/*
* @SuppressWarnings({ "deprecation", "resource" }) public Article
* get(String tableName, String row) { HTablePool hTablePool = new
* HTablePool(conf, 1000); HTableInterface table =
* hTablePool.getTable(tableName); Get get = new Get(row.getBytes());
* Article article = null; try {
*
* Result result = table.get(get); KeyValue[] raw = result.raw(); if
* (raw.length == 4) { article = new Article(); article.setId(row);
* article.setTitle(new String(raw[3].getValue())); article.setAuthor(new
* String(raw[0].getValue())); article.setContent(new
* String(raw[1].getValue())); article.setDescribe(new
* String(raw[2].getValue())); } } catch (IOException e) {
* e.printStackTrace(); } return article; }
*/ // 添加一条记录
public void put(String tableName, String row, String columnFamily,
String column, String data) throws IOException {
HTablePool hTablePool = new HTablePool(conf, 1000);
HTableInterface table = hTablePool.getTable(tableName);
Put p1 = new Put(Bytes.toBytes(row));
p1.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column),
Bytes.toBytes(data));
table.put(p1);
System.out.println("put'" + row + "'," + columnFamily + ":" + column
+ "','" + data + "'");
} /**
* 查询所有表名
*
* @return
* @throws Exception
*/
public List<String> getALLTable() throws Exception {
ArrayList<String> tables = new ArrayList<String>();
if (admin != null) {
HTableDescriptor[] listTables = admin.listTables();
if (listTables.length > 0) {
for (HTableDescriptor tableDesc : listTables) {
tables.add(tableDesc.getNameAsString());
System.out.println(tableDesc.getNameAsString());
}
}
}
return tables;
} /**
* 创建一张表
*
* @param tableName
* @param column
* @throws Exception
*/
public void createTable(String tableName, String column) throws Exception {
if (admin.tableExists(tableName)) {
System.out.println(tableName + "表已经存在!");
} else {
HTableDescriptor tableDesc = new HTableDescriptor(tableName);
tableDesc.addFamily(new HColumnDescriptor(column.getBytes()));
admin.createTable(tableDesc);
System.out.println(tableName + "表创建成功!");
}
}
}
hBase官方文档以及HBase基础操作封装类的更多相关文章
- hbase官方文档(转)
FROM:http://www.just4e.com/hbase.html Apache HBase™ 参考指南 HBase 官方文档中文版 Copyright © 2012 Apache Soft ...
- HBase 官方文档
HBase 官方文档 Copyright © 2010 Apache Software Foundation, 盛大游戏-数据仓库团队-颜开(译) Revision History Revision ...
- HBase官方文档
HBase官方文档 目录 序 1. 入门 1.1. 介绍 1.2. 快速开始 2. Apache HBase (TM)配置 2.1. 基础条件 2.2. HBase 运行模式: 独立和分布式 2.3. ...
- HBase 官方文档0.90.4
HBase 官方文档0.90.4 Copyright © 2010 Apache Software Foundation, 盛大游戏-数据仓库团队-颜开(译) Revision History Rev ...
- HBase官方文档 之 Region的相关知识
HBase是以Region为最小的存储和负载单元(这里可不是HDFS的存储单元),因此Region的负载管理,关系到了数据读写的性能.先抛开Region如何切分不说,看看Region是如何分配到各个R ...
- HBase 官方文档中文版
地址链接: http://abloz.com/hbase/book.html 里面包含基本的API和使用说明
- lavarel5.2官方文档阅读——架构基础
<目录> 1.请求的生命周期 2.应用的架构 3.服务提供者 4.服务容器 5.Facades外立面(从这节起,看中文版的:https://phphub.org/topics/1783) ...
- gRPC官方文档(异步基础: C++)
文章来自gRPC 官方文档中文版 异步基础: C++ 本教程介绍如何使用 C++ 的 gRPC 异步/非阻塞 API 去实现简单的服务器和客户端.假设你已经熟悉实现同步 gRPC 代码,如gRPC 基 ...
- 常用SQL_官方文档使用
SQL语句基础理论 SQL是操作和检索关系型数据库的标准语言,标准SQL语句可用于操作关系型数据库. 5大主要类型: ①DQL(Data Query Language,数据查询语言)语句,主要由于se ...
随机推荐
- Python endswith() 方法
描述 endswith() 方法用于判断字符串是否以指定后缀结尾,如果是则返回 True,否则返回 False. 语法 endswith() 方法语法: S.endswith(suffix[,star ...
- Cocos2d-x动画工具类
1.此工具类的目的是为了方便运行动画.使用TexturePackerGUI工具能够导出plist文件和png图片,这里我演示样例图片叫bxjg.plist和bxjg.png ///////////// ...
- 关于 Content-Type:application/x-www-form-urlencoded 和 Content-Type:multipart/related
最近项目中用到的一个是用一个页面接收c程序post过来的一断字符串..总接收不到值... 我用C#写一个测试可以正常接收到值. 最后抓包比较 区别只是 Content-Type:application ...
- 常见的web负载均衡方法总结
Web负载均衡的方法有很多,下面介绍几种常见的负载均衡方法. 1.用户手动选择方法 这是一种较为古老的方式.通过在主站首页入口提供不同线路.不同服务器连接的方式,来实现负载均衡.这种方式在一些提供下载 ...
- QT4编程过程中遇到的问题及解决办法
1.QLineEdit显示内容的格式函数: QLineEdit *lineEditPassword = new QLineEdit: lineEditPassword -> setEchoMod ...
- iptables的自定义链--子链
我个人理解:子链的作用就是为了减少重复设置,有的时候可能对数据包进行一系列的处理,而且还被多种规则引用.这样就可以设置成子链,一起跳转过去处理. -j subchain 子链用-N来创建. iptab ...
- Litjson序列化
var jsonStr = JsonMapper.ToJson(tmpType); var tmpObject = JsonMapper.ToObject<TestClass>(jsonS ...
- google protocol buffer的原理和使用(三)
介绍下怎么反序列化GoogleBuffer数据.并在最后提供本系列文章中所用到的代码整理供下载. 上一篇文章介绍了如何将数据序列化到了addressbook.data中.那么对于接受方而言该怎么解析出 ...
- Executor , ExecutorService 和 Executors
三者的主要区别和关系如下: Executor 和 ExecutorService 这两个接口主要的区别是:ExecutorService 接口继承了 Executor 接口,是 Executor 的子 ...
- 找不到编译动态表达式所需的一种或多种类型。是否缺少对 Microsoft.CSharp.dll 和 System.Core.dll 的引用?
提示“找不到编译动态表达式所需的一种或多种类型.是否缺少对 Microsoft.CSharp.dll 和 System.Core.dll 的引用? ”错误 解决方法: 将引入的COM对象(misc ...