Rest和Thrift接口

  • hbase的rest和thrift接口支持xml、protobuf、和二进制数据编码等操作

java API

  • 大体包括:hbase自身管理部分、avro部分、hbase客户端部分、mapreduce部分、rest部分、thrift部分、zookeeper等等。
    hbaseJavaAPI
  • 使用Eclipse开发HBase
    • 第三方访问hbase时,首先需访问zookeeper,因为hbase重要信息保存在zookeeper中,而zookeeper集群是由hbase-site.xml指定的,所以需找到hbase-site.xml文件。通过classpath来指定
    • 将所需jar包(hbase的lib目录下全部加上hadoop-core-1.x.jar,window下注意fileUtil类去掉check),拷贝hbse-site.xml到项目目录下,在libraries里点add class folder选择hbase-site.xml目录
    • 列子:输入hdfs上的两个文件,采用mapReduce统计,输出结果到Hbase中。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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.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.mapreduce.TableOutputFormat;
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.LongWritable;
import org.apache.hadoop.io.NullWritable;
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;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
public class HBaseWordCount {
public static class Map extends Mapper<LongWritable, Text, Text, IntWritable>{
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String[] s = value.toString().trim().split(" ");
for (String str : s) {
context.write(new Text(str), new IntWritable(1));
}
}
}
public static class Reduce extends TableReducer<Text, IntWritable, NullWritable>{
protected void reduce(Text key, Iterable<IntWritable> values,Context context)
throws IOException, InterruptedException {
int sum=0;
for (IntWritable i : values) {
sum+=i.get();
}
Put put= new Put(Bytes.toBytes(key.toString()));
//列族为content,修饰符为count,列值数目
put.add(Bytes.toBytes("content"),Bytes.toBytes("count"),Bytes.toBytes(String.valueOf(sum)));
context.write(NullWritable.get(), put);
}
}
@SuppressWarnings({ "deprecation", "resource" })
public static void createHBaseTable(String table) throws Exception{
HTableDescriptor htd = new HTableDescriptor(table);
HColumnDescriptor col = new HColumnDescriptor("content");
htd.addFamily(col);
HBaseConfiguration config = new HBaseConfiguration();
HBaseAdmin admin = new HBaseAdmin(config);
if(admin.tableExists(table)){
System.out.println("table:'"+table+"' exits");
admin.disableTable(table);
admin.deleteTable(table);
}
System.out.println("create '"+table+"' table");
admin.createTable(htd);
}
public static void main(String[] args) throws Exception {
String tableName = "wordcount";
Configuration conf = new Configuration();
conf.set(TableOutputFormat.OUTPUT_TABLE, tableName);
createHBaseTable(tableName);
Job job = new Job(conf,"wordcount table with "+args[0]);
job.setJarByClass(HBaseWordCount.class);
job.setNumReduceTasks(3);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TableOutputFormat.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
System.exit(job.waitForCompletion(true)?0:1);
}
}
文章目录
  1. 1. Rest和Thrift接口
  2. 2. java API

Sides

IT技术分享博客