Skip to content

Commit 62c5c58

Browse files
committed
create_table; region_locator; table_descriptor
1 parent 8f0752a commit 62c5c58

File tree

11 files changed

+691
-32
lines changed

11 files changed

+691
-32
lines changed

src/main/java/com/alipay/oceanbase/hbase/OHTable.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -499,13 +499,15 @@ public Configuration getConfiguration() {
499499
}
500500

501501
@Override
502-
public HTableDescriptor getTableDescriptor() {
503-
throw new FeatureNotSupportedException("not supported yet.");
502+
public HTableDescriptor getTableDescriptor() throws IOException {
503+
OHTableDescriptorExecutor executor = new OHTableDescriptorExecutor(tableNameString, obTableClient);
504+
return executor.getTableDescriptor();
504505
}
505506

506507
@Override
507508
public TableDescriptor getDescriptor() throws IOException {
508-
throw new FeatureNotSupportedException("not supported yet.");
509+
OHTableDescriptorExecutor executor = new OHTableDescriptorExecutor(tableNameString, obTableClient);
510+
return executor.getTableDescriptor();
509511
}
510512

511513
/**

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

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,53 @@ public TableName[] listTableNames(String s, boolean b) throws IOException {
135135

136136
@Override
137137
public HTableDescriptor getTableDescriptor(TableName tableName) throws TableNotFoundException, IOException {
138-
throw new FeatureNotSupportedException("does not support yet");
138+
OHConnectionConfiguration connectionConf = new OHConnectionConfiguration(conf);
139+
ObTableClient tableClient = ObTableClientManager.getOrCreateObTableClientByTableName(tableName, connectionConf);
140+
OHTableDescriptorExecutor executor = new OHTableDescriptorExecutor(tableName.getNameAsString(), tableClient);
141+
try {
142+
return executor.getTableDescriptor();
143+
} catch (IOException e) {
144+
if (e.getCause() instanceof ObTableTransportException
145+
&& ((ObTableTransportException) e.getCause()).getErrorCode() == TransportCodes.BOLT_TIMEOUT) {
146+
throw new TimeoutIOException(e.getCause());
147+
} else {
148+
throw e;
149+
}
150+
}
139151
}
140152

141153
@Override
142154
public TableDescriptor getDescriptor(TableName tableName) throws TableNotFoundException, IOException {
143-
throw new FeatureNotSupportedException("does not support yet");
155+
OHConnectionConfiguration connectionConf = new OHConnectionConfiguration(conf);
156+
ObTableClient tableClient = ObTableClientManager.getOrCreateObTableClientByTableName(tableName, connectionConf);
157+
OHTableDescriptorExecutor executor = new OHTableDescriptorExecutor(tableName.getNameAsString(), tableClient);
158+
try {
159+
return executor.getTableDescriptor();
160+
} catch (IOException e) {
161+
if (e.getCause() instanceof ObTableTransportException
162+
&& ((ObTableTransportException) e.getCause()).getErrorCode() == TransportCodes.BOLT_TIMEOUT) {
163+
throw new TimeoutIOException(e.getCause());
164+
} else {
165+
throw e;
166+
}
167+
}
144168
}
145169

146170
@Override
147171
public void createTable(TableDescriptor tableDescriptor) throws IOException {
148-
throw new FeatureNotSupportedException("does not support yet");
172+
OHConnectionConfiguration connectionConf = new OHConnectionConfiguration(conf);
173+
ObTableClient tableClient = ObTableClientManager.getOrCreateObTableClientByTableName(tableDescriptor.getTableName(), connectionConf);
174+
OHCreateTableExecutor executor = new OHCreateTableExecutor(tableClient);
175+
try {
176+
executor.createTable(tableDescriptor, null);
177+
} catch (IOException e) {
178+
if (e.getCause() instanceof ObTableTransportException
179+
&& ((ObTableTransportException) e.getCause()).getErrorCode() == TransportCodes.BOLT_TIMEOUT) {
180+
throw new TimeoutIOException(e.getCause());
181+
} else {
182+
throw e;
183+
}
184+
}
149185
}
150186

151187
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public RegionLocator getRegionLocator(TableName tableName) throws IOException {
145145
// to avoid change the database in original param url by namespace in tableName
146146
OHConnectionConfiguration connectionConf = new OHConnectionConfiguration(conf);
147147
ObTableClient obTableClient = ObTableClientManager.getOrCreateObTableClientByTableName(tableName, connectionConf);
148-
OHRegionLocatorExecutor executor = new OHRegionLocatorExecutor(obTableClient);
148+
OHRegionLocatorExecutor executor = new OHRegionLocatorExecutor(tableName.toString(), obTableClient);
149149
return executor.getRegionLocator(String.valueOf(tableName));
150150
}
151151

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*-
2+
* #%L
3+
* OBKV HBase Client Framework
4+
* %%
5+
* Copyright (C) 2025 OceanBase Group
6+
* %%
7+
* OBKV HBase Client Framework is licensed under Mulan PSL v2.
8+
* You can use this software according to the terms and conditions of the Mulan PSL v2.
9+
* You may obtain a copy of Mulan PSL v2 at:
10+
* http://license.coscl.org.cn/MulanPSL2
11+
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
12+
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
13+
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
14+
* See the Mulan PSL v2 for more details.
15+
* #L%
16+
*/
17+
18+
package com.alipay.oceanbase.hbase.util;
19+
20+
import com.alibaba.fastjson.JSON;
21+
import com.alipay.oceanbase.hbase.execute.AbstractObTableMetaExecutor;
22+
import com.alipay.oceanbase.rpc.ObTableClient;
23+
import com.alipay.oceanbase.rpc.meta.ObTableMetaRequest;
24+
import com.alipay.oceanbase.rpc.meta.ObTableMetaResponse;
25+
import com.alipay.oceanbase.rpc.meta.ObTableRpcMetaType;
26+
import org.apache.hadoop.hbase.HColumnDescriptor;
27+
import org.apache.hadoop.hbase.HTableDescriptor;
28+
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
29+
import org.apache.hadoop.hbase.client.TableDescriptor;
30+
31+
import java.io.IOException;
32+
import java.util.*;
33+
34+
public class OHCreateTableExecutor extends AbstractObTableMetaExecutor<Void> {
35+
private final ObTableClient client;
36+
37+
OHCreateTableExecutor(ObTableClient client) {
38+
this.client = client;
39+
}
40+
41+
@Override
42+
public ObTableRpcMetaType getMetaType() {
43+
return ObTableRpcMetaType.HTABLE_CREATE_TABLE;
44+
}
45+
46+
@Override
47+
public Void parse(ObTableMetaResponse response) throws IOException {
48+
// success, do nothing
49+
return null;
50+
}
51+
52+
public void createTable(TableDescriptor tableDescriptor, byte[][] splitKeys) throws IOException {
53+
final ObTableMetaRequest request = new ObTableMetaRequest();
54+
request.setMetaType(getMetaType());
55+
Map<String, Object> requestData = new HashMap<>();
56+
requestData.put("htable_name", tableDescriptor.getTableName().getName());
57+
Map<String, Map<String, Integer>> columnFamilies = new HashMap<>();
58+
for (ColumnFamilyDescriptor columnDescriptor : tableDescriptor.getColumnFamilies()) {
59+
Map<String, Integer> columnFamily = new HashMap<>();
60+
columnFamily.put("ttl", columnDescriptor.getTimeToLive());
61+
columnFamily.put("max_version", columnDescriptor.getMaxVersions());
62+
columnFamilies.put(columnDescriptor.getNameAsString(), columnFamily);
63+
}
64+
requestData.put("column_families", columnFamilies);
65+
String jsonData = JSON.toJSONString(requestData);
66+
request.setData(jsonData);
67+
execute(client, request);
68+
}
69+
}

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

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,66 @@
11
package com.alipay.oceanbase.hbase.util;
22

33
import com.alipay.oceanbase.rpc.ObTableClient;
4+
import com.alipay.oceanbase.rpc.bolt.transport.TransportCodes;
5+
import com.alipay.oceanbase.rpc.exception.ObTableTransportException;
46
import org.apache.hadoop.hbase.HRegionLocation;
57
import org.apache.hadoop.hbase.TableName;
68
import org.apache.hadoop.hbase.client.RegionLocator;
9+
import org.apache.hadoop.hbase.exceptions.TimeoutIOException;
710
import org.apache.hadoop.hbase.util.Pair;
811

912
import java.io.IOException;
1013
import java.util.Collections;
1114
import java.util.List;
1215

1316
public class OHRegionLocator implements RegionLocator {
14-
public OHRegionLocator(byte[][] startKeys, byte[][] endKeys) {
15-
17+
private byte[][] startKeys;
18+
private byte[][] endKeys;
19+
private TableName tableName;
20+
21+
private List<HRegionLocation> regionLocations;
22+
23+
public OHRegionLocator(byte[][] startKeys, byte[][] endKeys, List<HRegionLocation> regionLocations) {
24+
this.startKeys = startKeys;
25+
this.endKeys = endKeys;
26+
this.regionLocations = regionLocations;
1627
}
1728

1829
@Override
1930
public HRegionLocation getRegionLocation(byte[] bytes) throws IOException {
31+
// check if bytes is in the range of startKeys and endKeys
32+
for (HRegionLocation regionLocation : regionLocations) {
33+
if (regionLocation.getRegionInfo().containsRow(bytes)) {
34+
return regionLocation;
35+
}
36+
}
2037
return null;
2138
}
2239

2340
@Override
2441
public HRegionLocation getRegionLocation(byte[] bytes, boolean b) throws IOException {
25-
return null;
42+
if (b) {
43+
OHRegionLocatorExecutor executor = new OHRegionLocatorExecutor(tableName.toString(), tableClient);
44+
try {
45+
RegionLocator location = executor.getRegionLocator(tableName.toString());
46+
this.startKeys = location.getStartKeys();
47+
this.endKeys = location.getEndKeys();
48+
this.regionLocations = location.getAllRegionLocations();
49+
} catch (IOException e) {
50+
if (e.getCause() instanceof ObTableTransportException
51+
&& ((ObTableTransportException) e.getCause()).getErrorCode() == TransportCodes.BOLT_TIMEOUT) {
52+
throw new TimeoutIOException(e.getCause());
53+
} else {
54+
throw e;
55+
}
56+
}
57+
}
58+
return getRegionLocation(bytes);
2659
}
2760

2861
@Override
2962
public List<HRegionLocation> getAllRegionLocations() throws IOException {
30-
return Collections.emptyList();
63+
return regionLocations;
3164
}
3265

3366
/**
@@ -40,7 +73,7 @@ public List<HRegionLocation> getAllRegionLocations() throws IOException {
4073
*/
4174
@Override
4275
public byte[][] getStartKeys() throws IOException {
43-
return null;
76+
return startKeys;
4477
}
4578

4679
/**
@@ -53,7 +86,7 @@ public byte[][] getStartKeys() throws IOException {
5386
*/
5487
@Override
5588
public byte[][] getEndKeys() throws IOException {
56-
return null;
89+
return endKeys;
5790
}
5891

5992
/**
@@ -67,18 +100,18 @@ public byte[][] getEndKeys() throws IOException {
67100
*/
68101
@Override
69102
public Pair<byte[][], byte[][]> getStartEndKeys() throws IOException {
70-
return null;
103+
return Pair.newPair(startKeys, endKeys);
71104
}
72105

73106
@Override
74107
public TableName getName() {
75-
return null;
108+
return tableName;
76109
}
77110

78111
private ObTableClient tableClient;
79112

80113
@Override
81114
public void close() throws IOException {
82-
115+
return;
83116
}
84117
}

0 commit comments

Comments
 (0)