这里只介绍三种最常用的方式

1.HBase shell

HBase的命令行工具是最简单的接口,主要用于HBase管理

首先启动HBase

帮助

hbase(main):001:0> help

查看HBase服务器状态

hbase(main):001:0> status

查询HBse版本

hbase(main):002:0> version

ddl操作

1.创建一个member表

hbase(main):013:0> create 'table1','tab1_id','tab1_add','tab1_info'

2.查看所有的表

hbase(main):006:0> list

3.查看表结构

hbase(main):007:0> describe 'member'

4.删除一个列簇

5、查看表是否存在

6、判断表是否为"enable"

7、删除一个表

dml操作

1、创建member表

删除一个列簇(一般不超过两个列簇)

2、往member表插入数据

3、扫描查看数据

4、获取数据

获取一个rowkey的所有数据

获取一个rowkey,一个列簇的所有数据

获取一个rowkey,一个列簇中一个列的所有数据

5、更新数据

6、删除列簇中其中一列

7、统计表中总的行数

8、清空表中数据

2.java API

最常规且最高效的访问方式

 import java.io.IOException;

 import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
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.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; public class HbaseTest {
public static Configuration conf;
static{
conf = HBaseConfiguration.create();//第一步
conf.set("hbase.zookeeper.quorum", "header-2,core-1,core-2");
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set("hbase.master", "header-1:60000");
} public static void main(String[] args) throws IOException{
//createTable("member");
//insertDataByPut("member");
//QueryByGet("member");
QueryByScan("member");
//DeleteData("member");
} /**
* 创建表 通过HBaseAdmin对象操作
*
* @param tablename
* @throws IOException
* @throws ZooKeeperConnectionException
* @throws MasterNotRunningException
*
*/
public static void createTable(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
//创建HBaseAdmin对象
HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
//判断表是否存在,若存在就删除
if(hBaseAdmin.tableExists(tableName)){
hBaseAdmin.disableTable(tableName);
hBaseAdmin.deleteTable(tableName);
}
HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
//添加Family
tableDescriptor.addFamily(new HColumnDescriptor("info"));
tableDescriptor.addFamily(new HColumnDescriptor("address"));
//创建表
hBaseAdmin.createTable(tableDescriptor);
//释放资源
hBaseAdmin.close();
} /**
*
* @param tableName
* @throws IOException
*/
@SuppressWarnings("deprecation")
public static void insertDataByPut(String tableName) throws IOException {
//第二步 获取句柄,传入静态配置和表名称
HTable table = new HTable(conf, tableName); //添加rowkey,添加数据, 通过getBytes方法将string类型都转化为字节流
Put put1 = new Put(getBytes("djt"));
put1.add(getBytes("address"), getBytes("country"), getBytes("china"));
put1.add(getBytes("address"), getBytes("province"), getBytes("beijing"));
put1.add(getBytes("address"), getBytes("city"), getBytes("beijing")); put1.add(getBytes("info"), getBytes("age"), getBytes("28"));
put1.add(getBytes("info"), getBytes("birthdy"), getBytes("1998-12-23"));
put1.add(getBytes("info"), getBytes("company"), getBytes("dajiang")); //第三步
table.put(put1); //释放资源
table.close();
} /**
* 查询一条记录
* @param tableName
* @throws IOException
*/
public static void QueryByGet(String tableName) throws IOException {
//第二步
HTable table = new HTable(conf, tableName);
//根据rowkey查询
Get get = new Get(getBytes("djt"));
Result r = table.get(get);
System.out.println("获得到rowkey:" + new String(r.getRow()));
for(KeyValue keyvalue : r.raw()){
System.out.println("列簇:" + new String(keyvalue.getFamily())
+ "====列:" + new String(keyvalue.getQualifier())
+ "====值:" + new String(keyvalue.getValue()));
}
table.close();
} /**
* 扫描
* @param tableName
* @throws IOException
*/
public static void QueryByScan(String tableName) throws IOException {
// 第二步
HTable table = new HTable(conf, tableName);
Scan scan = new Scan();
//指定需要扫描的列簇,列.如果不指定就是全表扫描
scan.addColumn(getBytes("info"), getBytes("company"));
ResultScanner scanner = table.getScanner(scan);
for(Result r : scanner){
System.out.println("获得到rowkey:" + new String(r.getRow()));
for(KeyValue kv : r.raw()){
System.out.println("列簇:" + new String(kv.getFamily())
+ "====列:" + new String(kv.getQualifier())
+ "====值 :" + new String(kv.getValue()));
}
}
//释放资源
scanner.close();
table.close();
} /**
* 删除一条数据
* @param tableName
* @throws IOException
*/
public static void DeleteData(String tableName) throws IOException {
// 第二步
HTable table = new HTable(conf, tableName); Delete delete = new Delete(getBytes("djt"));
delete.addColumn(getBytes("info"), getBytes("age")); table.delete(delete);
//释放资源
table.close();
} /**
* 转换byte数组(string类型都转化为字节流)
*/
public static byte[] getBytes(String str){
if(str == null)
str = "";
return Bytes.toBytes(str);
}
}

3.MapReduce

直接使用MapReduce作业处理HBase数据

import java.io.IOException;
import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; /**
* 将hdfs里面的数据导入hbase
* @author Administrator
*
*/
public class MapReduceWriteHbaseDriver { public static class WordCountMapperHbase extends Mapper<Object, Text,
ImmutableBytesWritable, IntWritable>{
private final static IntWritable one = new IntWritable(1);
private Text word = new Text(); public void map(Object key, Text value, Context context) throws IOException, InterruptedException{
StringTokenizer itr = new StringTokenizer(value.toString());
while(itr.hasMoreTokens()){
word.set(itr.nextToken());
//输出到hbase的key类型为ImmutableBytesWritable
context.write(new ImmutableBytesWritable(Bytes.toBytes(word.toString())), one);
}
}
} public static class WordCountReducerHbase extends TableReducer<ImmutableBytesWritable,
IntWritable, ImmutableBytesWritable>{
private IntWritable result = new IntWritable();
public void reduce(ImmutableBytesWritable key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException{
int sum = 0;
for(IntWritable val : values){
sum += val.get();
}
//put实例化 key代表主键,每个单词存一行
Put put = new Put(key.get());
//三个参数分别为:列簇content 列count 列值为词频
put.add(Bytes.toBytes("content"), Bytes.toBytes("count"), Bytes.toBytes(String.valueOf(sum)));
context.write(key, put);
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException{
String tableName = "wordcount";//hbase数据库表名 也可以通过命令行传入表名args
Configuration conf = HBaseConfiguration.create();//实例化Configuration
conf.set("hbase.zookeeper.quorum", "header-2,core-1,core-2");
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set("hbase.master", "header-1"); //如果表已经存在就先删除
HBaseAdmin admin = new HBaseAdmin(conf);
if(admin.tableExists(tableName)){
admin.disableTable(tableName);
admin.deleteTable(tableName);
} HTableDescriptor htd = new HTableDescriptor(tableName);//指定表名
HColumnDescriptor hcd = new HColumnDescriptor("content");//指定列簇名
htd.addFamily(hcd);//创建列簇
admin.createTable(htd);//创建表 Job job = new Job(conf, "import from hdfs to hbase");
job.setJarByClass(MapReduceWriteHbaseDriver.class); job.setMapperClass(WordCountMapperHbase.class); //设置插入hbase时的相关操作
TableMapReduceUtil.initTableReducerJob(tableName, WordCountReducerHbase.class, job, null, null, null, null, false); //map输出
job.setMapOutputKeyClass(ImmutableBytesWritable.class);
job.setMapOutputValueClass(IntWritable.class); //reduce输出
job.setOutputKeyClass(ImmutableBytesWritable.class);
job.setOutputValueClass(Put.class); //读取数据
FileInputFormat.addInputPaths(job, "hdfs://header-1:9000/user/test.txt");
System.out.println(job.waitForCompletion(true) ? 0 : 1);
}
}
import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; /**
* 读取hbse数据存入HDFS
* @author Administrator
*
*/
public class MapReduceReaderHbaseDriver {
public static class WordCountHBaseMapper extends TableMapper<Text, Text>{
protected void map(ImmutableBytesWritable key, Result values,Context context) throws IOException, InterruptedException{
StringBuffer sb = new StringBuffer("");
//获取列簇content下面的值
for(java.util.Map.Entry<byte[], byte[]> value : values.getFamilyMap("content".getBytes()).entrySet()){
String str = new String(value.getValue());
if(str != null){
sb.append(str);
}
context.write(new Text(key.get()), new Text(new String(sb)));
}
}
} public static class WordCountHBaseReducer extends Reducer<Text, Text, Text, Text>{
private Text result = new Text();
public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException{
for(Text val : values){
result.set(val);
context.write(key, result);
}
}
} public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
String tableName = "wordcount";//表名称
Configuration conf = HBaseConfiguration.create();//实例化Configuration
conf.set("hbase.zookeeper.quorum", "header-2,core-1,core-2");
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set("hbase.master", "header-1:60000"); Job job = new Job(conf, "import from hbase to hdfs");
job.setJarByClass(MapReduceReaderHbaseDriver.class); job.setReducerClass(WordCountHBaseReducer.class);
//配置读取hbase的相关操作
TableMapReduceUtil.initTableMapperJob(tableName, new Scan(), WordCountHBaseMapper.class, Text.class, Text.class, job, false); //输出路径
FileOutputFormat.setOutputPath(job, new Path("hdfs://header-1:9000/user/out"));
System.out.println(job.waitForCompletion(true) ? 0 : 1);
}
}

HBase的访问方式的更多相关文章

  1. hbase的几种访问方式

    Hbase的访问方式 1.Native Java API:最常规和高效的访问方式: 2.HBase Shell:HBase的命令行工具,最简单的接口,适合HBase管理使用: 3.Thrift Gat ...

  2. Hbase访问方式

    Hbase访问方式 Hbase shell命令操作 Hbase shell命令操作--general操作 首先启动Hbase 启动shell 查看表结构 删除一个表 创建表和查看表结构 插入几条数据 ...

  3. HBase数据访问的一些常用方式

    类型 特点 场合 优缺点分析 Native Java API 最常规和高效的访问方式 适合MapReduce作业并行批处理HBase表数据 Hbase Shell HBase的命令行工具,最简单的访问 ...

  4. 分布式结构化存储系统-HBase访问方式

    分布式结构化存储系统-HBase访问方式 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. HBase提供了多种访问方式,包括HBase shell,HBase API,数据收集组件( ...

  5. ADO.NET编程之美----数据访问方式(面向连接与面向无连接)

    最近,在学习ADO.NET时,其中提到了数据访问方式:面向连接与面向无连接.于是,百度了一下,发现并没有很好的资料,然而,在学校图书馆中发现一本好书(<ASP.NET MVC5 网站开发之美&g ...

  6. Objective-C 中self.与_访问方式的区别

    Objective-C中属性self.a与_a访问的区别: 在OC中我们可以通过指令@property定义属性. OC对属性封装了许多方法,同时也会自动实现一些方法,相比实例变量,感觉更加面向对象些. ...

  7. 在APACHE服务器上的访问方式上去除index.php

      在APACHE服务器上的访问方式上去除index.php   下面我说下 apache 下 ,如何 去掉URL 里面的 index.php 例如: 你原来的路径是: localhost/index ...

  8. Django 之 ForeignKey、ManyToMany的访问方式

    1.ForeignKey 情况I: from django.db import models class Blog(models.Model): pass class Entry(models.Mod ...

  9. Java中Map集合的四种访问方式(转)

    最近学习Java发现集合类型真是很多,访问方式也很灵活,在网上找的方法,先放下备用 public static void main(String[] args) { Map<String, St ...

随机推荐

  1. TweenMax的GSAP(GreenSock动画平台)GSAP,专业的Web动画库

    很好奇红框框里面的内容是什么,于是点了进去,又百度了下这个英文缩写具体指的什么东西. GSAP的全名是GreenSock Animation Platform,是一个从flash时代一直发展到今天的专 ...

  2. es6 语法使用

    一.相关背景介绍 我们现在大多数人用的语法javascript 其实版本是ecmscript5,也是就es5.这个版本己经很多年了,且完美被各大浏览器所支持.所以很多学js的朋友可以一直分不清楚es5 ...

  3. 内联元素的盒子模型与文档流定位padding属性

            内联元素的盒子模型 1.内联元素不能设置width宽度和高度height span{width:200px ; height:200px}   与     span{width:100 ...

  4. makefile 打印

    $(warning ----------$(abc)) $(info -----------------$(abc))

  5. 小程序封装wx.request,以及调用

    1.新建一个api目录,与pages同级 2.在api目录下新建一个api.js文件 3.编写代码 const host = 'http://test.test.cn' const wxRequest ...

  6. [CSP-S模拟测试]:树(树上上升序列+主席树+线段树)

    题目传送门(内部题78) 输入格式 第一行输入两个整数$n,q$,表示节点数和询问数. 第二行输入$n$个整数$w_i$,表示第$i$个点的智商. 第三行至第$n+1$行每行输入两个数$x,y$,表示 ...

  7. sed的一些应用

    1. sed 使用变量进行替换,注意使用参数 r 时,需要放在参数 i 的前面 下面这个例子是用2.txt中的版本号替换docker-compose.yml中的版本号,其中变量UPGRADE_NAME ...

  8. 基于Anaconda安装Tensorflow 并实现在Spyder中的应用

    基于Anaconda安装Tensorflow 并实现在Spyder中的应用 Anaconda可隔离管理多个环境,互不影响.这里,在anaconda中安装最新的python3.6.5 版本. 一.安装 ...

  9. HDU 6592 (LIS+输出字典序最大最小)

    题意:给你一个序列,让你找长度最长的字典序最小和最大的单峰序列,单峰序列就是满足先增后降的序列. 思路:先正着求一遍LIS,再反着求一遍LIS,然后用单调栈来模拟. 求字典序最小的话,首先找到第一个顶 ...

  10. SpringMVC开发中遇到的异常1:No primary or default constructor found for interface java.util.List

    Request processing failed; nested exception is java.lang.IllegalStateException: No primary or defaul ...