Skip to content

Commit f071020

Browse files
committed
add user demo
1 parent 8297237 commit f071020

File tree

10 files changed

+895
-0
lines changed

10 files changed

+895
-0
lines changed

example/simple-kv-demo/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
obkv-table-client-java demo project.

example/simple-kv-demo/pom.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.oceanbase.example</groupId>
8+
<artifactId>simple-kv-demo</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
12+
<properties>
13+
<maven.compiler.source>8</maven.compiler.source>
14+
<maven.compiler.target>8</maven.compiler.target>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>com.oceanbase</groupId>
20+
<artifactId>obkv-table-client</artifactId>
21+
<version>0.1.0</version>
22+
</dependency>
23+
</dependencies>
24+
25+
26+
</project>
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
/*-
2+
* #%L
3+
* OBKV Table Client Framework
4+
* %%
5+
* Copyright (C) 2021 OceanBase
6+
* %%
7+
* OBKV Table 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.oceanbase.example;
19+
20+
21+
import com.alipay.oceanbase.rpc.ObTableClient;
22+
import com.alipay.oceanbase.rpc.table.api.TableBatchOps;
23+
24+
import java.util.Map;
25+
import java.util.List;
26+
27+
28+
/*
29+
* 此demo只用作指导使用tableapi,测试前请先使用下列schema在SQL中建立table
30+
* */
31+
32+
/* 测试表schema:
33+
CREATE TABLE IF NOT EXISTS `kv_table` (
34+
`key` varchar(20) NOT NULL,
35+
`val` varchar(20) DEFAULT NULL,
36+
PRIMARY KEY (`key`)
37+
);
38+
*/
39+
40+
public class KVClient {
41+
private ObTableClient obTableClient = null;
42+
private String tableName = "kv_table";
43+
44+
public boolean initial() {
45+
try {
46+
obTableClient = new ObTableClient();
47+
obTableClient.setFullUserName("your user name"); // e.g. root@sys#ocp
48+
obTableClient.setParamURL("your configurl + database=xxx"); // e.g. http://ip:port/services?Action=ObRootServiceInfo&ObRegion=ocp&database=test
49+
obTableClient.setPassword("your user passwd");
50+
obTableClient.setSysUserName("your sys user"); // e.g. proxyro@sys
51+
obTableClient.setSysPassword("your sys user passwd");
52+
obTableClient.init();
53+
} catch (Exception e) {
54+
e.printStackTrace();
55+
System.out.println("fail to init KV client, " + e);
56+
obTableClient = null;
57+
return false;
58+
}
59+
System.out.println("initial kv client success");
60+
return true;
61+
}
62+
63+
public void close() {
64+
if (obTableClient == null) {
65+
return;
66+
}
67+
try {
68+
obTableClient.close();
69+
} catch (Exception e) {
70+
e.printStackTrace();
71+
System.out.println("fail to close kv client, " + e);
72+
}
73+
}
74+
75+
public boolean put(Object key, String value) {
76+
if (obTableClient == null) {
77+
System.out.println("kv client is null");
78+
return false;
79+
}
80+
try {
81+
long rows = obTableClient.insert(tableName, key, new String[]{"val"}, new Object[]{value});
82+
if (rows != 1) {
83+
System.out.println("fail to put kv data");
84+
return false;
85+
}
86+
} catch (Exception e) {
87+
e.printStackTrace();
88+
System.out.println("fail to put kv data, " + e);
89+
return false;
90+
}
91+
System.out.println("put kv data success");
92+
return true;
93+
}
94+
95+
// return all column of row
96+
public String get(Object key) {
97+
if (obTableClient == null) {
98+
System.out.println("kv client is null");
99+
return null;
100+
}
101+
try {
102+
Map<String, Object> result = obTableClient.get(tableName, key, new String[]{"val"});
103+
return (String)result.get("val");
104+
} catch (Exception e) {
105+
System.out.println("fail to get kv data");
106+
return null;
107+
}
108+
}
109+
110+
public boolean update(Object key, String val) {
111+
if (obTableClient == null) {
112+
System.out.println("kv client is null");
113+
return false;
114+
}
115+
try {
116+
long rows = obTableClient.update(tableName, key, new String[]{"val"}, new Object[]{val});
117+
if (rows != 1) {
118+
System.out.println("fail to put kv data");
119+
return false;
120+
}
121+
} catch (Exception e) {
122+
System.out.println("fail to get kv data");
123+
return false;
124+
}
125+
System.out.println("update kv data success");
126+
return true;
127+
}
128+
// del
129+
public boolean del(Object key) {
130+
if (obTableClient == null) {
131+
System.out.println("kv client is null");
132+
return false;
133+
}
134+
try {
135+
long rows = obTableClient.delete(tableName, key);
136+
if (rows != 1) {
137+
System.out.println("del kv data success, row = 0");
138+
return true;
139+
}
140+
} catch (Exception e) {
141+
System.out.println("fail to del kv data");
142+
return false;
143+
}
144+
System.out.println("del kv data success, row = 1");
145+
return true;
146+
}
147+
148+
// insert or update
149+
public boolean insertOrUpdate(Object key, String val) {
150+
if (obTableClient == null) {
151+
System.out.println("kv client is null");
152+
return false;
153+
}
154+
try {
155+
long rows = obTableClient.insertOrUpdate(tableName, key, new String[]{"val"}, new Object[]{val});
156+
if (rows != 1) {
157+
System.out.println("insertOrUpdate kv data success, row = 0");
158+
return true;
159+
}
160+
} catch (Exception e) {
161+
System.out.println("fail to insertOrUpdate kv data" + e);
162+
return false;
163+
}
164+
System.out.println("insertOrUpdate kv data success, row = 1");
165+
return true;
166+
}
167+
168+
// replace
169+
public boolean replace(Object key, String replace_val) {
170+
if (obTableClient == null) {
171+
System.out.println("kv client is null");
172+
return false;
173+
}
174+
try {
175+
long rows = obTableClient.replace(tableName, key, new String[]{"val"}, new Object[]{replace_val});
176+
if (rows != 1) {
177+
System.out.println("replace kv data success, row = 0");
178+
return true;
179+
}
180+
} catch (Exception e) {
181+
System.out.println("fail to replace kv data" + e);
182+
return false;
183+
}
184+
System.out.println("replace kv data success, row = 1");
185+
return true;
186+
}
187+
188+
// append
189+
public String append(Object key, String append_str) {
190+
if (obTableClient == null) {
191+
System.out.println("kv client is null");
192+
return null;
193+
}
194+
try {
195+
Map<String, Object> result = obTableClient.append(tableName, key, new String[]{"val"}, new Object[]{append_str}, (boolean)true);
196+
return (String)result.get("val");
197+
} catch (Exception e) {
198+
System.out.println("fail to increment kv data: " + e);
199+
return null;
200+
}
201+
}
202+
203+
// batch
204+
public boolean batch() {
205+
if (obTableClient == null) {
206+
System.out.println("kv client is null");
207+
return false;
208+
}
209+
try {
210+
TableBatchOps batchOps = obTableClient.batch(tableName);
211+
batchOps.get((String)"key1", new String[]{"val"});
212+
batchOps.insert((String)"key5", new String[]{"val"}, new Object[]{"insert key5"});
213+
batchOps.update((String)"key5", new String[]{"val"}, new Object[]{"update key5"});
214+
batchOps.insertOrUpdate((String)"key6", new String[]{"val"}, new Object[]{"insertOrUpdate key6"});
215+
batchOps.append((String)"key3", new String[]{"val"}, new Object[]{"_append"}, (boolean)true);
216+
List<Object> retObj = batchOps.execute();
217+
if (retObj.size() != 5) {
218+
System.out.println("batch Ops error");
219+
return false;
220+
}
221+
System.out.println("batch Ops success.");
222+
return true;
223+
} catch (Exception e) {
224+
System.out.println("fail to execute batch ops: " + e);
225+
return false;
226+
}
227+
}
228+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*-
2+
* #%L
3+
* OBKV Table Client Framework
4+
* %%
5+
* Copyright (C) 2021 OceanBase
6+
* %%
7+
* OBKV Table 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.oceanbase.example;
19+
20+
21+
import com.alipay.sofa.common.code.LogCode2Description;
22+
import com.alipay.sofa.common.log.LoggerSpaceManager;
23+
import org.slf4j.Logger;
24+
25+
26+
public class LoggerFactory {
27+
public static final String OCEANBASE_TABLE_TEST_LOGGER_SPACE = "oceanbase-table-test";
28+
public static LogCode2Description LCD = LogCode2Description.create(OCEANBASE_TABLE_TEST_LOGGER_SPACE);
29+
30+
31+
public static Logger getLogger(String name) {
32+
if (name == null || name.isEmpty()) {
33+
return null;
34+
}
35+
return LoggerSpaceManager.getLoggerBySpace(name, OCEANBASE_TABLE_TEST_LOGGER_SPACE);
36+
}
37+
38+
public static Logger getLogger(Class<?> klass) {
39+
if (klass == null) {
40+
return null;
41+
}
42+
return getLogger(klass.getCanonicalName());
43+
}
44+
}

0 commit comments

Comments
 (0)