hbase通过api写入和查询

hbase通过java客户端写入数据和查询数据。

写入数据

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
public static Configuration hbaseConfiguration = HBaseConfiguration.create();
public static Connection connection = null;
public static Admin admin = null;
//date format
public static SimpleDateFormat dateFornat = new SimpleDateFormat("yyyyMMddHHmmss");
//初始化hbase的连接和获取admin对象
public static void init() {
hbaseConfiguration.set("hbase.zookeeper.property.clientPort", "2181");
hbaseConfiguration.set("hbase.zookeeper.quorum", "192.168.1.180");
try {
connection = ConnectionFactory.createConnection(hbaseConfiguration);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
init();
insert("person_sale", "186xxxxxxxx_", "content", "content", "11111============2222=========333");
close();
}
//批量写入
public static void insert(String tableName, String rowKey, String colFamily, String col, String value)
throws IOException, InterruptedException {
Table table = connection.getTable(TableName.valueOf(tableName));
Calendar calendar = null;
List<Put> putList = new ArrayList<Put>();
for(int i = 0; i < 20; i++)
{
calendar = Calendar.getInstance();
Date date = calendar.getTime();
System.out.println("========" + dateFornat.format(date));
Put put = new Put(Bytes.toBytes(rowKey + dateFornat.format(date)));
put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col), Bytes.toBytes(value));
putList.add(put);
java.util.concurrent.TimeUnit.SECONDS.sleep(1);
}
//批量添加数据
table.put(putList);
table.close();
}
public static void close() {
try {
if (null != admin) {
admin.close();
}
if (null != connection) {
connection.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

查询数据

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
public static Configuration hbaseConfiguration = HBaseConfiguration.create();
public static Connection connection = null;
public static Admin admin = null;
//date format
public static SimpleDateFormat dateFornat = new SimpleDateFormat("yyyyMMddHHmmss");
//初始化hbase的连接和获取admin对象
public static void init() {
hbaseConfiguration.set("hbase.zookeeper.property.clientPort", "2181");
hbaseConfiguration.set("hbase.zookeeper.quorum", "192.168.1.180");
try {
connection = ConnectionFactory.createConnection(hbaseConfiguration);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
init();
showTablesContent("person_sale");
close();
}
//范围查询数据
public static void showTablesContent(final String tableName) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
scan.setMaxVersions();
scan.setBatch(10000);
//开始位置
scan.setStartRow(Bytes.toBytes("186xxxxxxxx_20170330132405"));
//结束位置
scan.setStopRow(Bytes.toBytes("186xxxxxxxx_20170330132418"));
//返回结果集
ResultScanner result = table.getScanner(scan);
//循环处理结果集
for(Result r : result) {
System.out.println(Bytes.toString(r.getValue(Bytes.toBytes("content"), Bytes.toBytes("content"))));
}
table.close();
}
public static void close() {
try {
if (null != admin) {
admin.close();
}
if (null != connection) {
connection.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

注意点

1、hbase的机器名称要在客户端机器上面的hosts文件里面标明,否则会一直卡在connection.getAdmin();这句上。这里会从zk中获取master的hbase实例,zk返回的是master的主机名,不是ip地址。所以在这里一定要在客户端hosts中添加主机名到ip的映射关系。

文章目录
  1. 1. 写入数据
  2. 2. 查询数据
  3. 3. 注意点