Skip to content

Commit 8554f31

Browse files
maochongxinJackShi148
authored andcommitted
MetaExecutor interface definition and demo
1 parent 4b563a8 commit 8554f31

File tree

5 files changed

+200
-1
lines changed

5 files changed

+200
-1
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.alipay.oceanbase.hbase.execute;
2+
3+
import com.alipay.oceanbase.rpc.ObTableClient;
4+
import com.alipay.oceanbase.rpc.exception.ObTableException;
5+
import com.alipay.oceanbase.rpc.meta.ObTableMetaRequest;
6+
import com.alipay.oceanbase.rpc.meta.ObTableMetaResponse;
7+
import com.alipay.oceanbase.rpc.table.ObTable;
8+
9+
import java.io.IOException;
10+
11+
public abstract class AbstractObTableMetaExecutor<T> implements ObTableMetaExecutor<T> {
12+
13+
@Override
14+
public T execute(ObTableClient client, ObTableMetaRequest request) throws IOException {
15+
if (request.getMetaType() != getMetaType()) {
16+
throw new IOException("Invalid meta type, expected " + getMetaType());
17+
}
18+
ObTable table = client.randomTable();
19+
ObTableMetaResponse response;
20+
try {
21+
response = (ObTableMetaResponse) client.executeWithRetry(
22+
table,
23+
request,
24+
null /*tableName*/
25+
);
26+
} catch (Exception e) {
27+
throw new IOException("Failed to execute request", e);
28+
}
29+
return parse(response);
30+
}
31+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.alipay.oceanbase.hbase.execute;
2+
3+
import com.alipay.oceanbase.rpc.ObTableClient;
4+
import com.alipay.oceanbase.rpc.meta.ObTableMetaRequest;
5+
import com.alipay.oceanbase.rpc.meta.ObTableMetaResponse;
6+
import com.alipay.oceanbase.rpc.meta.ObTableRpcMetaType;
7+
8+
import java.io.IOException;
9+
10+
public interface ObTableMetaExecutor<T> {
11+
/**
12+
* 执行元数据请求
13+
* @param request 元数据请求
14+
* @return 解析后的元数据对象
15+
* @throws IOException 如果执行失败或解析失败
16+
*/
17+
T execute(ObTableClient client, ObTableMetaRequest request) throws IOException;
18+
19+
/**
20+
* 解析元数据响应, 用户需要重写
21+
* @param response 元数据响应
22+
* @return 解析后的元数据对象
23+
* @throws IOException 如果解析失败
24+
*/
25+
T parse(ObTableMetaResponse response) throws IOException;
26+
27+
/**
28+
* 获取元信息类型, 用户需要重写
29+
* @return 元信息类型
30+
*/
31+
ObTableRpcMetaType getMetaType() throws IOException;
32+
}

src/main/java/com/alipay/oceanbase/hbase/util/OHConnectionImpl.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.alipay.oceanbase.hbase.OHTable;
2121
import com.alipay.oceanbase.hbase.exception.FeatureNotSupportedException;
22+
import com.alipay.oceanbase.rpc.ObTableClient;
2223
import org.apache.hadoop.classification.InterfaceAudience;
2324
import org.apache.hadoop.conf.Configuration;
2425
import org.apache.hadoop.hbase.*;
@@ -140,7 +141,9 @@ public BufferedMutator getBufferedMutator(BufferedMutatorParams params) throws I
140141

141142
@Override
142143
public RegionLocator getRegionLocator(TableName tableName) throws IOException {
143-
throw new FeatureNotSupportedException("not supported yet'");
144+
ObTableClient obTableClient = ObTableClientManager.getOrCreateObTableClient(null/*args*/);
145+
OHRegionLocatorExecutor executor = new OHRegionLocatorExecutor(obTableClient);
146+
return executor.getRegionLocator(String.valueOf(tableName));
144147
}
145148

146149
@Override
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.alipay.oceanbase.hbase.util;
2+
3+
import com.alipay.oceanbase.rpc.ObTableClient;
4+
import org.apache.hadoop.hbase.HRegionLocation;
5+
import org.apache.hadoop.hbase.TableName;
6+
import org.apache.hadoop.hbase.client.RegionLocator;
7+
import org.apache.hadoop.hbase.util.Pair;
8+
9+
import java.io.IOException;
10+
import java.util.Collections;
11+
import java.util.List;
12+
13+
public class OHRegionLocator implements RegionLocator {
14+
public OHRegionLocator(byte[][] startKeys, byte[][] endKeys) {
15+
16+
}
17+
18+
@Override
19+
public HRegionLocation getRegionLocation(byte[] bytes) throws IOException {
20+
return null;
21+
}
22+
23+
@Override
24+
public HRegionLocation getRegionLocation(byte[] bytes, boolean b) throws IOException {
25+
return null;
26+
}
27+
28+
@Override
29+
public List<HRegionLocation> getAllRegionLocations() throws IOException {
30+
return Collections.emptyList();
31+
}
32+
33+
/**
34+
* Gets the starting row key for every region in the currently open table.
35+
* <p>
36+
* This is mainly useful for the MapReduce integration.
37+
*
38+
* @return Array of region starting row keys
39+
* @throws IOException if a remote or network exception occurs
40+
*/
41+
@Override
42+
public byte[][] getStartKeys() throws IOException {
43+
return null;
44+
}
45+
46+
/**
47+
* Gets the ending row key for every region in the currently open table.
48+
* <p>
49+
* This is mainly useful for the MapReduce integration.
50+
*
51+
* @return Array of region ending row keys
52+
* @throws IOException if a remote or network exception occurs
53+
*/
54+
@Override
55+
public byte[][] getEndKeys() throws IOException {
56+
return null;
57+
}
58+
59+
/**
60+
* Gets the starting and ending row keys for every region in the currently
61+
* open table.
62+
* <p>
63+
* This is mainly useful for the MapReduce integration.
64+
*
65+
* @return Pair of arrays of region starting and ending row keys
66+
* @throws IOException if a remote or network exception occurs
67+
*/
68+
@Override
69+
public Pair<byte[][], byte[][]> getStartEndKeys() throws IOException {
70+
return null;
71+
}
72+
73+
@Override
74+
public TableName getName() {
75+
return null;
76+
}
77+
78+
private ObTableClient tableClient;
79+
80+
@Override
81+
public void close() throws IOException {
82+
83+
}
84+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.alipay.oceanbase.hbase.util;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import com.alipay.oceanbase.hbase.execute.AbstractObTableMetaExecutor;
5+
import com.alipay.oceanbase.rpc.ObTableClient;
6+
import com.alipay.oceanbase.rpc.exception.ObTableException;
7+
import com.alipay.oceanbase.rpc.location.model.TableEntry;
8+
import com.alipay.oceanbase.rpc.meta.ObTableMetaRequest;
9+
import com.alipay.oceanbase.rpc.meta.ObTableMetaResponse;
10+
import com.alipay.oceanbase.rpc.meta.ObTableRpcMetaType;
11+
12+
import java.io.IOException;
13+
import java.util.HashMap;
14+
import java.util.Map;
15+
16+
public class OHRegionLocatorExecutor extends AbstractObTableMetaExecutor<OHRegionLocator> {
17+
OHRegionLocatorExecutor(ObTableClient client) {
18+
this.client = client;
19+
}
20+
21+
@Override
22+
public ObTableRpcMetaType getMetaType() {
23+
return ObTableRpcMetaType.HTABLE_REGION_LOCATOR;
24+
}
25+
26+
@Override
27+
public OHRegionLocator parse(ObTableMetaResponse response) throws IOException {
28+
try {
29+
String jsonData = response.getData();
30+
// process json
31+
return new OHRegionLocator(null, null);
32+
} catch (IllegalArgumentException e) {
33+
throw new IOException("msg", e);
34+
}
35+
}
36+
37+
public OHRegionLocator getRegionLocator(String tableName) throws IOException {
38+
ObTableMetaRequest request = new ObTableMetaRequest();
39+
request.setMetaType(getMetaType());
40+
Map<String, Object> requestData = new HashMap<>();
41+
requestData.put("table_name", tableName);
42+
String jsonData = JSON.toJSONString(requestData);
43+
request.setData(jsonData);
44+
45+
return execute(client, request);
46+
}
47+
48+
private final ObTableClient client;
49+
}

0 commit comments

Comments
 (0)