|
| 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 | +} |
0 commit comments