hbase 增删改查 api 简单操作
package com.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.Get;
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.PrefixFilter;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
public class HBaseTest {
HBaseAdmin admin=null;
Configuration conf=null;
/**
* 构造函数载入配置
*/
public HBaseTest(){
conf = new Configuration();
conf.set("hbase.zookeeper.quorum","192.168.80.20,192.168.80.21,192.168.80.22");
conf.set("hbase.rootdir", "hdfs://cluster/hbase");
conf.set("hbase.master", "192.168.80.20:60010");
try {
admin = new HBaseAdmin(conf);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
HBaseTest hbase = new HBaseTest();
//创建一张表
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
hbase.getRowFilter("waln_log","^*_201303131400\\d*$");
}
public void getRowFilter(String tableName, String reg) throws Exception {
HTable hTable = new HTable(conf, tableName);
Scan scan = new Scan();
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();
}
}
/**
* 查询一条记录
* @param tableName
* @param rowKey
*/
public void getKey(String tableName, String rowKey) {
HTablePool hTablePool = new HTablePool(conf, 1000);
HTableInterface table = hTablePool.getTable(tableName);
Get get = new Get(rowKey.getBytes());
try {
Result result = table.get(get);
if (result.raw().length==0) {
System.out.println("查询的关键词"+rowKey+"不存在");
}else {
for (KeyValue kv : result.raw()) {
System.out.println(new String(kv.getKey())+"\t"+new String(kv.getValue()));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 往表中加入一条记录
* @param tableName
* @param rowKey
* @param column
* @param qua
* @param value
*/
public void addOneRecord(String tableName, String rowKey, String column,
String qua, String value) {
HTablePool hTablePool = new HTablePool(conf, 1000);
HTableInterface table = hTablePool.getTable(tableName);
Put put = new Put(rowKey.getBytes());
put.add(column.getBytes(), qua.getBytes(), value.getBytes());
try {
table.put(put);
System.out.println("加入记录 "+rowKey+ " 成功!
");
} catch (IOException e) {
e.printStackTrace();
System.out.println("加入记录 "+rowKey+ " 失败。");
}
}
/**
* 查询全部表名
* @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 增删改查 api 简单操作的更多相关文章
- C# 对MongoDB 进行增删改查的简单操作
		C# 对MongoDB 进行增删改查的简单操作 下面演示下C#操作MongoDB驱动的简单的增删改查代码 运用到的MongoDB支持的C#驱动,当前版本为1.6.0 1,连接数据库 /// & ... 
- C# 对MongoDB 进行增删改查的简单操作 (转)
		运用到的MongoDB支持的C#驱动,当前版本为1.6.0 下载地址:https://github.com/mongodb/mongo-csharp-driver/downloads 1,连接数据库 ... 
- MongoDB入门 常用命令以及增删改查的简单操作
		1,运行MongoDB服务mongod --dbpath=/usr/local/developmentTool/mongo/data/db/然后启动客户端mongo2,sudo service mon ... 
- Yii2.0高级框架数据库增删改查的一些操作(转)
		yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2.0高级框架数据库增删改查的一些操作 --------------------------- ... 
- OracleHelper(对增删改查分页查询操作进行了面向对象的封装,对批量增删改操作的事务封装)
		公司的一个新项目使用ASP.NET MVC开发,经理让我写个OracleHelper,我从网上找了一个比较全的OracleHelper类,缺点是查询的时候返回DataSet,数据增删改要写很多代码(当 ... 
- Yii2.0高级框架数据库增删改查的一些操作
		yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2.0高级框架数据库增删改查的一些操作 --------------------------- ... 
- MongoDB - 增删改查及聚合操作
		目录 MongoDB - 增删改查及聚合操作 一. 数据库操作(database) 1. 创建及查看库 2. 删除库 二. 集合collectionc=操作(相当于SQL数据库中的表table) 1. ... 
- MyBatis简单的增删改查以及简单的分页查询实现
		MyBatis简单的增删改查以及简单的分页查询实现 <? xml version="1.0" encoding="UTF-8"? > <!DO ... 
- HBase入门操作 常用命令和增删改查的简单应用操作
		这里启动关闭Hadoop和HBase的顺序一定是: 启动Hadoop—>启动HBase—>关闭HBase—>关闭Hadoop ssh localhost 开启hadoopcd /us ... 
随机推荐
- vue 自定义指令使用
			<template> <div> <!-- 让input元素在打开页面的时候就获得焦点 --> <input type="text" v- ... 
- 在SpringBoot框架中使用拦截器
			1.继承WebMvcConfigureAdapter类,覆盖其addInterceptors接口,注册我们自定义的拦截器 package com.eth.wallet.config; import c ... 
- VMWare NAT网络配置
			1. 打开CMD,输入以下命令 ipconfig -all 2. 设置主机Wi-Fi网络对VMnet8虚拟网卡的网络共享 3. 虚拟机NAT模式配置 4. 配置虚拟机网卡信息 切换到ROOT cd / ... 
- memcached协议解析 及使用
			本文转载自:http://www.ccvita.com/306.html 协议memcached 的客户端使用TCP链接与服务器通讯.(UDP接口也同样有效,参考后文的 “UDP协议” )一个运行中的 ... 
- ruby cucumber安装
			创建rails工程 
- Python之函数作业
			Python之函数作业 爬页面 #爬虫页面,send一次爬一次 from urllib.request import urlopen def get(): while True: url = yiel ... 
- Cadence中画原理图的时候器件标号与黄色的参数不同的解决办法
			方法是Accessories->Transfer Occ. Prop to Instance->Push Occ. Prop into Instance 将黄色的参数同样应用到源参数. 版 ... 
- conflunce安装配置
			下载 下载Confluence-v5.4.4.zip包,其中包含 atlassian-confluence-5.4.4-x64.bin #程序二进制文件 confluence5.x-crack.z ... 
- php.ini中date.timezone设置分析
			date.timezone设置php5默认date.timezone为utc,改为date.timezone = PRC即可解决时间相差八小时的问题,但我在php的官方文档中看了半天也没找到这个参数啊 ... 
- js重新讲解继承,es5的一些继承,es6继承的改变  ----------由浅入深
			es5 利用原型公有私有继承 function Parent(name) { this.name = name } Parent.prototype.home = '北京'; function Chi ... 
