1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.client.example;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.hadoop.conf.Configuration;
25 import org.apache.hadoop.hbase.HConstants;
26 import org.apache.hadoop.hbase.TableName;
27 import org.apache.hadoop.hbase.client.Connection;
28 import org.apache.hadoop.hbase.client.ConnectionFactory;
29 import org.apache.hadoop.hbase.client.Table;
30 import org.apache.hadoop.hbase.client.coprocessor.Batch;
31 import org.apache.hadoop.hbase.ipc.BlockingRpcCallback;
32 import org.apache.hadoop.hbase.ipc.ServerRpcController;
33 import org.apache.hadoop.hbase.protobuf.generated.RefreshHFilesProtos;
34
35 import java.io.Closeable;
36 import java.io.IOException;
37
38
39
40
41
42 public class RefreshHFilesClient implements Closeable {
43 private static final Log LOG = LogFactory.getLog(RefreshHFilesClient.class);
44 private final Connection connection;
45
46
47
48
49
50
51 public RefreshHFilesClient(Configuration cfg) {
52 try {
53 this.connection = ConnectionFactory.createConnection(cfg);
54 } catch (IOException e) {
55 throw new RuntimeException(e);
56 }
57 }
58
59 @Override
60 public void close() throws IOException {
61 if (this.connection != null && !this.connection.isClosed()) {
62 this.connection.close();
63 }
64 }
65
66 public void refreshHFiles(final TableName tableName) throws Throwable {
67 try (Table table = connection.getTable(tableName)) {
68 refreshHFiles(table);
69 }
70 }
71
72 public void refreshHFiles(final Table table) throws Throwable {
73 final RefreshHFilesProtos.RefreshHFilesRequest request = RefreshHFilesProtos.RefreshHFilesRequest
74 .getDefaultInstance();
75 table.coprocessorService(RefreshHFilesProtos.RefreshHFilesService.class, HConstants.EMPTY_START_ROW,
76 HConstants.EMPTY_END_ROW,
77 new Batch.Call<RefreshHFilesProtos.RefreshHFilesService,
78 RefreshHFilesProtos.RefreshHFilesResponse>() {
79 @Override
80 public RefreshHFilesProtos.RefreshHFilesResponse call(
81 RefreshHFilesProtos.RefreshHFilesService refreshHFilesService)
82 throws IOException {
83 ServerRpcController controller = new ServerRpcController();
84 BlockingRpcCallback<RefreshHFilesProtos.RefreshHFilesResponse> rpcCallback =
85 new BlockingRpcCallback<>();
86 refreshHFilesService.refreshHFiles(controller, request, rpcCallback);
87 if (controller.failedOnException()) {
88 throw controller.getFailedOn();
89 }
90 return rpcCallback.get();
91 }
92 });
93 LOG.debug("Done refreshing HFiles");
94 }
95 }