Skip to content

Commit

Permalink
feat(hbase-client): support obkv-hbase (#369)
Browse files Browse the repository at this point in the history
  • Loading branch information
caojiajun committed Dec 23, 2024
1 parent 651b4ec commit ce1698b
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 13 deletions.
6 changes: 6 additions & 0 deletions camellia-hbase-client/camellia-hbase/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ public class HBaseConstants {
public static final String PASSWORD = "hbase.client.password";
public static final String HBASE_CLIENT_CONNECTION_IMPL = "hbase.client.connection.impl";
public static final String HBASE_CLIENT_CONNECTION_LINDORM_IMPL = "org.apache.hadoop.hbase.client.AliHBaseUEClusterConnection";


public static final String HBASE_OCEANBASE_PARAM_URL = "hbase.oceanbase.paramURL";
public static final String HBASE_OCEANBASE_FULL_USER_NAME = "hbase.oceanbase.fullUserName";
public static final String HBASE_OCEANBASE_PASSWORD = "hbase.oceanbase.password";
public static final String HBASE_OCEANBASE_SYS_USER_NAME = "hbase.oceanbase.sysUserName";
public static final String HBASE_OCEANBASE_SYS_PASSWORD = "hbase.oceanbase.sysPassword";
public static final String HBASE_CLIENT_CONNECTION_OBKV_IMPL = "com.alipay.oceanbase.hbase.util.OHConnectionImpl";
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,26 @@ private void init() {
configuration.set(entry.getKey(), entry.getValue());
}
}
configuration.set(HBaseConstants.ZK, hBaseResource.getZk());
configuration.set(HBaseConstants.ZK_PARENT, hBaseResource.getZkParent());
if (hBaseResource.getUserName() != null) {
configuration.set(HBaseConstants.USER_NAME, hBaseResource.getUserName());
}
if (hBaseResource.getPassword() != null) {
configuration.set(HBaseConstants.PASSWORD, hBaseResource.getPassword());
}
if (hBaseResource.isLindorm()) {
if (hBaseConf == null || !hBaseConf.getConfMap().containsKey(HBaseConstants.HBASE_CLIENT_CONNECTION_IMPL)) {
configuration.set(HBaseConstants.HBASE_CLIENT_CONNECTION_IMPL, HBaseConstants.HBASE_CLIENT_CONNECTION_LINDORM_IMPL);
if (hBaseResource.isObkv()) {
configuration.set(HBaseConstants.HBASE_OCEANBASE_FULL_USER_NAME, hBaseResource.getObkvFullUserName());
configuration.set(HBaseConstants.HBASE_OCEANBASE_PASSWORD, hBaseResource.getObkvPassword());
configuration.set(HBaseConstants.HBASE_OCEANBASE_PARAM_URL, hBaseResource.getObkvParamUrl());
configuration.set(HBaseConstants.HBASE_OCEANBASE_SYS_USER_NAME, hBaseResource.getObkvSysUserName());
configuration.set(HBaseConstants.HBASE_OCEANBASE_SYS_PASSWORD, hBaseResource.getObkvSysPassword());
configuration.set(HBaseConstants.HBASE_CLIENT_CONNECTION_IMPL, HBaseConstants.HBASE_CLIENT_CONNECTION_OBKV_IMPL);
} else {
configuration.set(HBaseConstants.ZK, hBaseResource.getZk());
configuration.set(HBaseConstants.ZK_PARENT, hBaseResource.getZkParent());
if (hBaseResource.getUserName() != null) {
configuration.set(HBaseConstants.USER_NAME, hBaseResource.getUserName());
}
if (hBaseResource.getPassword() != null) {
configuration.set(HBaseConstants.PASSWORD, hBaseResource.getPassword());
}
if (hBaseResource.isLindorm()) {
if (hBaseConf == null || !hBaseConf.getConfMap().containsKey(HBaseConstants.HBASE_CLIENT_CONNECTION_IMPL)) {
configuration.set(HBaseConstants.HBASE_CLIENT_CONNECTION_IMPL, HBaseConstants.HBASE_CLIENT_CONNECTION_LINDORM_IMPL);
}
}
}
this.configuration = configuration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.netease.nim.camellia.core.model.Resource;


/**
*
* Created by caojiajun on 2020/3/20.
Expand All @@ -16,12 +17,41 @@ public class HBaseResource extends Resource {
private final String password;
private final Boolean lindorm;

private final Boolean obkv;

private String obkvParamUrl;
private String obkvFullUserName;
private String obkvPassword;
private String obkvSysUserName;
private String obkvSysPassword;

public HBaseResource(String zk, String zkParent) {
this(zk, zkParent, null, null);
}

public HBaseResource(String zk, String zkParent, String userName, String password) {
this(zk, zkParent, userName, password, null);
this(zk, zkParent, userName, password, (Boolean) null);
}

public HBaseResource(String obkvParamUrl, String obkvFullUserName, String obkvPassword, String obkvSysUserName, String obkvSysPassword) {
this.zk = null;
this.zkParent = null;
this.userName = null;
this.password = null;
this.lindorm = null;
this.obkvParamUrl = obkvParamUrl;
this.obkvFullUserName = obkvFullUserName;
this.obkvPassword = obkvPassword;
this.obkvSysUserName = obkvSysUserName;
this.obkvSysPassword = obkvSysPassword;
String url = prefix +
"obkv%" + obkvParamUrl + "%" +
"obkvFullUserName=" + obkvFullUserName + "&" +
"obkvPassword=" + obkvPassword + "&" +
"obkvSysUserName=" + obkvSysUserName + "&" +
"obkvSysPassword=" + obkvSysPassword;
setUrl(url);
this.obkv = true;
}

public HBaseResource(String zk, String zkParent, String userName, String password, Boolean lindorm) {
Expand All @@ -30,6 +60,7 @@ public HBaseResource(String zk, String zkParent, String userName, String passwor
this.userName = userName;
this.password = password;
this.lindorm = lindorm;
this.obkv = null;
StringBuilder builder = new StringBuilder();
builder.append(prefix);
builder.append(zk).append(zkParent);
Expand Down Expand Up @@ -70,4 +101,28 @@ public String getPassword() {
public boolean isLindorm() {
return lindorm != null && lindorm;
}

public boolean isObkv() {
return obkv != null && obkv;
}

public String getObkvParamUrl() {
return obkvParamUrl;
}

public String getObkvFullUserName() {
return obkvFullUserName;
}

public String getObkvPassword() {
return obkvPassword;
}

public String getObkvSysUserName() {
return obkvSysUserName;
}

public String getObkvSysPassword() {
return obkvSysPassword;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,32 @@ public static HBaseResource parseResourceByUrl(Resource resource) {
throw new IllegalArgumentException("not start with '" + HBaseResource.prefix + "'");
}
String substring = resource.getUrl().substring(HBaseResource.prefix.length());
if (substring.startsWith("obkv%")) {
try {
int index = substring.lastIndexOf("%");
String obkvParamUrl = substring.substring(5, index);
Map<String, String> params = getParams(substring.substring(index+1));
String obkvFullUserName = params.get("obkvFullUserName");
if (obkvFullUserName == null) {
throw new IllegalArgumentException("missing 'obkvFullUserName'");
}
String obkvPassword = params.get("obkvPassword");
if (obkvPassword == null) {
throw new IllegalArgumentException("missing 'obkvPassword'");
}
String obkvSysUserName = params.get("obkvSysUserName");
if (obkvSysUserName == null) {
throw new IllegalArgumentException("missing 'obkvSysUserName'");
}
String obkvSysPassword = params.get("obkvSysPassword");
if (obkvSysPassword == null) {
throw new IllegalArgumentException("missing 'obkvSysPassword'");
}
return new HBaseResource(obkvParamUrl, obkvFullUserName, obkvPassword, obkvSysUserName, obkvSysPassword);
} catch (Exception e) {
throw new IllegalArgumentException("parse obkv url error", e);
}
}
if (!substring.contains("/")) {
throw new IllegalArgumentException("miss '/'");
}
Expand All @@ -66,7 +92,7 @@ public static HBaseResource parseResourceByUrl(Resource resource) {
}
}
HBaseResource hBaseResource = new HBaseResource(zk, zkParent, userName, password, lindorm);
if (zk == null || zk.length() == 0) {
if (zk == null || zk.isEmpty()) {
throw new IllegalArgumentException("zk is empty");
}
return hBaseResource;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.netease.nim.camellia.hbase.test;

import com.netease.nim.camellia.core.model.Resource;
import com.netease.nim.camellia.hbase.resource.HBaseResource;
import com.netease.nim.camellia.hbase.util.HBaseResourceUtil;
import org.junit.Assert;
import org.junit.Test;

/**
* Created by caojiajun on 2024/12/23
*/
public class TestHBaseResource {

@Test
public void test1() {
String url = "hbase://nim-xx1.163.org,nim-xx2.163.org,nim-xx3.163.org/hbase1";
HBaseResource resource = HBaseResourceUtil.parseResourceByUrl(new Resource(url));
Assert.assertEquals(resource.getZk(), "nim-xx1.163.org,nim-xx2.163.org,nim-xx3.163.org");
Assert.assertEquals(resource.getZkParent(), "/hbase1");
Assert.assertFalse(resource.isLindorm());
Assert.assertFalse(resource.isObkv());
Assert.assertEquals(url, resource.getUrl());
}

@Test
public void testObkv() {
String url = "hbase://obkv%http://10.1.1.1:8080/services?Action=ObRootServiceInfo&ObRegion=nimtestob&database=obkv%obkvFullUserName=kvtest@testkv#nimtestob&obkvPassword=abcdef&obkvSysUserName=root&obkvSysPassword=111234abc";
HBaseResource resource = HBaseResourceUtil.parseResourceByUrl(new Resource(url));
Assert.assertNull(resource.getZk());
Assert.assertNull(resource.getZkParent());
Assert.assertFalse(resource.isLindorm());
Assert.assertTrue(resource.isObkv());
Assert.assertEquals(resource.getObkvParamUrl(), "http://10.1.1.1:8080/services?Action=ObRootServiceInfo&ObRegion=nimtestob&database=obkv");
Assert.assertEquals(resource.getObkvFullUserName(), "kvtest@testkv#nimtestob");
Assert.assertEquals(resource.getObkvPassword(), "abcdef");
Assert.assertEquals(resource.getObkvSysUserName(), "root");
Assert.assertEquals(resource.getObkvSysPassword(), "111234abc");

Assert.assertEquals(url, resource.getUrl());
}

@Test
public void testLindorm() {
String url = "hbase://ld-xxxx-lindorm.lindorm.rds.aliyuncs.com:30020/?userName=nim_lindorm_1&password=xxabc&lindorm=true";
HBaseResource resource = HBaseResourceUtil.parseResourceByUrl(new Resource(url));
Assert.assertEquals(resource.getZk(), "ld-xxxx-lindorm.lindorm.rds.aliyuncs.com:30020");
Assert.assertEquals(resource.getZkParent(), "/");
Assert.assertTrue(resource.isLindorm());
Assert.assertFalse(resource.isObkv());
Assert.assertEquals(resource.getUserName(), "nim_lindorm_1");
Assert.assertEquals(resource.getPassword(), "xxabc");

Assert.assertEquals(url, resource.getUrl());

}
}

0 comments on commit ce1698b

Please sign in to comment.