hbase的javaapi

pom.xml里添加依赖
添加依赖

org.apache.hbase
hbase-server
0.98.6-hadoop2


org.apache.hbase
hbase-client
0.98.6-hadoop2

复制hbase-site.xml到工程
拷贝hbase-site.xml到resource里面去
弄好之后右键项目 maven ->update project
然后开始码代码
代码
1
package com.cniao5.mapreduce;
2
3
import org.apache.hadoop.conf.Configuration;
4
import org.apache.hadoop.hbase.Cell;
5
import org.apache.hadoop.hbase.CellUtil;
6
import org.apache.hadoop.hbase.HBaseConfiguration;
7
import org.apache.hadoop.hbase.client.Delete;
8
import org.apache.hadoop.hbase.client.Get;
9
import org.apache.hadoop.hbase.client.HTable;
10
import org.apache.hadoop.hbase.client.Put;
11
import org.apache.hadoop.hbase.client.Result;
12
import org.apache.hadoop.hbase.client.ResultScanner;
13
import org.apache.hadoop.hbase.client.RetriesExhaustedWithDetailsException;
14
import org.apache.hadoop.hbase.client.Scan;
15
import org.apache.hadoop.hbase.util.Bytes;
16
17
public class HBaseClient {
18
19
public static HTable getTable(String name) throws Exception {
20
//获取配置,使用HBaseConfiguration
21
Configuration conf = HBaseConfiguration.create();
22
//操作的表
23
HTable table = new HTable(conf, name);
24
25
return table;
26
}
27
28
29
public static void getData(HTable table) throws Exception {
30
31
//实例化一个get,指定一个rowkey
32
Get get = new Get(Bytes.toBytes("20180218_1325"));
33
//get某列的值
34
//get.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("age"));
35
//get一个列簇
36
get.addFamily(Bytes.toBytes("f1"));
37
38
//定义一个result
39
Result rs = table.get(get);
40
//打印数据
41
for (Cell cell : rs.rawCells()) {
42
System.out.println(
43
Bytes.toString(CellUtil.cloneFamily(cell)) + " *** " + Bytes.toString(CellUtil.cloneQualifier(cell))
44
+ " *** " + Bytes.toString(CellUtil.cloneValue(cell)) + " *** " + cell.getTimestamp());
45
System.out.println("---------------------------------------------");
46
}
47
48
// get加载到表中
49
table.get(get);
50
}
51
52
public static void putData(HTable table) throws Exception {
53
54
Put put = new Put(Bytes.toBytes("20180218_1325"));
55
put.add(Bytes.toBytes("f1"), Bytes.toBytes("sex"), Bytes.toBytes("male"));
56
57
table.put(put);
58
59
getData(table);
60
}
61
62
public static void deleteData(HTable table) throws Exception {
63
Delete del = new Delete(Bytes.toBytes("20180218_1325"));
64
del.deleteColumns(Bytes.toBytes("f1"), Bytes.toBytes("sex"));
65
66
table.delete(del);
67
68
getData(table);
69
}
70
71
public static void scanData(HTable table) throws Exception {
72
73
Scan scan = new Scan();
74
75
ResultScanner rsscan = table.getScanner(scan);
76
77
for (Result rs : rsscan) {
78
System.out.println(Bytes.toString(rs.getRow()));
79
for (Cell cell : rs.rawCells()) {
80
System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)) + " *** "
81
+ Bytes.toString(CellUtil.cloneQualifier(cell)) + " *** "
82
+ Bytes.toString(CellUtil.cloneValue(cell)) + " *** " + cell.getTimestamp());
83
}
84
System.out.println("---------------------------------------------");
85
}
86
}
87
88
public static void rangeData(HTable table) throws Exception {
89
90
Scan scan = new Scan();
91
92
// conf the scan
93
//scan.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("name"));
94
scan.setStartRow(Bytes.toBytes("20180218_1325"));
95
scan.setStopRow(Bytes.toBytes("20180218_1328"));
96
97
ResultScanner rsscan = table.getScanner(scan);
98
for (Result rs : rsscan) {
99
System.out.println(Bytes.toString(rs.getRow()));
100
for (Cell cell : rs.rawCells()) {
101
System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)) + " *** "
102
+ Bytes.toString(CellUtil.cloneQualifier(cell)) + " *** "
103
+ Bytes.toString(CellUtil.cloneValue(cell)) + " *** " + cell.getTimestamp());
104
}
105
System.out.println("---------------------------------------------");
106
}
107
}
108
109
public static void main(String[] args) throws Exception {
110
111
HTable table = getTable("ns1:t1");
112
//查询数据
113
//getData(table);
114
【hbase的javaapi】115
//添加数据
116
//putData(table);
117
118
//删除数据
119
//deleteData(table);
120
121
//scan数据
122
//scanData(table);
123
124
//scan范围查看
125
rangeData(table);
126
}
127
}
完了之后打成jar包 去运行

    推荐阅读