1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.thrift;
20
21 import java.io.UnsupportedEncodingException;
22 import java.nio.ByteBuffer;
23 import java.nio.charset.CharacterCodingException;
24 import java.nio.charset.Charset;
25 import java.nio.charset.CharsetDecoder;
26 import java.text.NumberFormat;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.TreeMap;
31 import java.util.SortedMap;
32
33 import org.apache.hadoop.hbase.thrift.generated.AlreadyExists;
34 import org.apache.hadoop.hbase.thrift.generated.ColumnDescriptor;
35 import org.apache.hadoop.hbase.thrift.generated.Hbase;
36 import org.apache.hadoop.hbase.thrift.generated.IOError;
37 import org.apache.hadoop.hbase.thrift.generated.IllegalArgument;
38 import org.apache.hadoop.hbase.thrift.generated.Mutation;
39 import org.apache.hadoop.hbase.thrift.generated.TCell;
40 import org.apache.hadoop.hbase.thrift.generated.TRowResult;
41
42 import org.apache.thrift.TException;
43 import org.apache.thrift.protocol.TBinaryProtocol;
44 import org.apache.thrift.protocol.TProtocol;
45 import org.apache.thrift.transport.TSocket;
46 import org.apache.thrift.transport.TTransport;
47
48
49
50
51 public class DemoClient {
52
53 static protected int port;
54 static protected String host;
55 CharsetDecoder decoder = null;
56
57 public static void main(String[] args)
58 throws IOError, TException, UnsupportedEncodingException, IllegalArgument, AlreadyExists {
59
60 if (args.length != 2) {
61
62 System.out.println("Invalid arguments!");
63 System.out.println("Usage: DemoClient host port");
64
65 System.exit(-1);
66 }
67
68 port = Integer.parseInt(args[1]);
69 host = args[0];
70
71
72 DemoClient client = new DemoClient();
73 client.run();
74 }
75
76 DemoClient() {
77 decoder = Charset.forName("UTF-8").newDecoder();
78 }
79
80
81 private String utf8(byte[] buf) {
82 try {
83 return decoder.decode(ByteBuffer.wrap(buf)).toString();
84 } catch (CharacterCodingException e) {
85 return "[INVALID UTF-8]";
86 }
87 }
88
89
90 private byte[] bytes(String s) {
91 try {
92 return s.getBytes("UTF-8");
93 } catch (UnsupportedEncodingException e) {
94 e.printStackTrace();
95 return null;
96 }
97 }
98
99 private void run() throws IOError, TException, IllegalArgument,
100 AlreadyExists {
101
102 TTransport transport = new TSocket(host, port);
103 TProtocol protocol = new TBinaryProtocol(transport, true, true);
104 Hbase.Client client = new Hbase.Client(protocol);
105
106 transport.open();
107
108 byte[] t = bytes("demo_table");
109
110
111
112
113 System.out.println("scanning tables...");
114 for (ByteBuffer name : client.getTableNames()) {
115 System.out.println(" found: " + utf8(name.array()));
116 if (utf8(name.array()).equals(utf8(t))) {
117 if (client.isTableEnabled(name)) {
118 System.out.println(" disabling table: " + utf8(name.array()));
119 client.disableTable(name);
120 }
121 System.out.println(" deleting table: " + utf8(name.array()));
122 client.deleteTable(name);
123 }
124 }
125
126
127
128
129 ArrayList<ColumnDescriptor> columns = new ArrayList<ColumnDescriptor>();
130 ColumnDescriptor col;
131 col = new ColumnDescriptor();
132 col.name = ByteBuffer.wrap(bytes("entry:"));
133 col.maxVersions = 10;
134 columns.add(col);
135 col = new ColumnDescriptor();
136 col.name = ByteBuffer.wrap(bytes("unused:"));
137 columns.add(col);
138
139 System.out.println("creating table: " + utf8(t));
140 try {
141 client.createTable(ByteBuffer.wrap(t), columns);
142 } catch (AlreadyExists ae) {
143 System.out.println("WARN: " + ae.message);
144 }
145
146 System.out.println("column families in " + utf8(t) + ": ");
147 Map<ByteBuffer, ColumnDescriptor> columnMap = client.getColumnDescriptors(ByteBuffer.wrap(t));
148 for (ColumnDescriptor col2 : columnMap.values()) {
149 System.out.println(" column: " + utf8(col2.name.array()) + ", maxVer: " + Integer.toString(col2.maxVersions));
150 }
151
152 Map<ByteBuffer, ByteBuffer> dummyAttributes = null;
153 boolean writeToWal = false;
154
155
156
157
158 byte[] invalid = {(byte) 'f', (byte) 'o', (byte) 'o', (byte) '-',
159 (byte) 0xfc, (byte) 0xa1, (byte) 0xa1, (byte) 0xa1, (byte) 0xa1};
160 byte[] valid = {(byte) 'f', (byte) 'o', (byte) 'o', (byte) '-',
161 (byte) 0xE7, (byte) 0x94, (byte) 0x9F, (byte) 0xE3, (byte) 0x83,
162 (byte) 0x93, (byte) 0xE3, (byte) 0x83, (byte) 0xBC, (byte) 0xE3,
163 (byte) 0x83, (byte) 0xAB};
164
165 ArrayList<Mutation> mutations;
166
167 mutations = new ArrayList<Mutation>();
168 mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")),
169 ByteBuffer.wrap(invalid), writeToWal));
170 client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("foo")),
171 mutations, dummyAttributes);
172
173
174
175 mutations = new ArrayList<Mutation>();
176 mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(valid), writeToWal));
177 client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(valid), mutations, dummyAttributes);
178
179
180
181 mutations = new ArrayList<Mutation>();
182 mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(invalid), writeToWal));
183 client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(invalid), mutations, dummyAttributes);
184
185
186
187 ArrayList<ByteBuffer> columnNames = new ArrayList<ByteBuffer>();
188 columnNames.add(ByteBuffer.wrap(bytes("entry:")));
189
190 System.out.println("Starting scanner...");
191 int scanner = client.scannerOpen(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("")), columnNames, dummyAttributes);
192
193 while (true) {
194 List<TRowResult> entry = client.scannerGet(scanner);
195 if (entry.isEmpty()) {
196 break;
197 }
198 printRow(entry);
199 }
200
201
202
203
204 for (int i = 100; i >= 0; --i) {
205
206 NumberFormat nf = NumberFormat.getInstance();
207 nf.setMinimumIntegerDigits(5);
208 nf.setGroupingUsed(false);
209 byte[] row = bytes(nf.format(i));
210
211 mutations = new ArrayList<Mutation>();
212 mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("unused:")), ByteBuffer.wrap(bytes("DELETE_ME")), writeToWal));
213 client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
214 printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
215 client.deleteAllRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes);
216
217 mutations = new ArrayList<Mutation>();
218 mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:num")), ByteBuffer.wrap(bytes("0")), writeToWal));
219 mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(bytes("FOO")), writeToWal));
220 client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
221 printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
222
223 Mutation m;
224 mutations = new ArrayList<Mutation>();
225 m = new Mutation();
226 m.column = ByteBuffer.wrap(bytes("entry:foo"));
227 m.isDelete = true;
228 mutations.add(m);
229 m = new Mutation();
230 m.column = ByteBuffer.wrap(bytes("entry:num"));
231 m.value = ByteBuffer.wrap(bytes("-1"));
232 mutations.add(m);
233 client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
234 printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
235
236 mutations = new ArrayList<Mutation>();
237 mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:num")), ByteBuffer.wrap(bytes(Integer.toString(i))), writeToWal));
238 mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:sqr")), ByteBuffer.wrap(bytes(Integer.toString(i * i))), writeToWal));
239 client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
240 printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
241
242
243 try {
244 Thread.sleep(50);
245 } catch (InterruptedException e) {
246
247 }
248
249 mutations.clear();
250 m = new Mutation();
251 m.column = ByteBuffer.wrap(bytes("entry:num"));
252 m.value= ByteBuffer.wrap(bytes("-999"));
253 mutations.add(m);
254 m = new Mutation();
255 m.column = ByteBuffer.wrap(bytes("entry:sqr"));
256 m.isDelete = true;
257 client.mutateRowTs(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, 1, dummyAttributes);
258 printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
259
260 List<TCell> versions = client.getVer(ByteBuffer.wrap(t), ByteBuffer.wrap(row), ByteBuffer.wrap(bytes("entry:num")), 10, dummyAttributes);
261 printVersions(ByteBuffer.wrap(row), versions);
262 if (versions.isEmpty()) {
263 System.out.println("FATAL: wrong # of versions");
264 System.exit(-1);
265 }
266
267 List<TCell> result = client.get(ByteBuffer.wrap(t), ByteBuffer.wrap(row), ByteBuffer.wrap(bytes("entry:foo")), dummyAttributes);
268 if (!result.isEmpty()) {
269 System.out.println("FATAL: shouldn't get here");
270 System.exit(-1);
271 }
272
273 System.out.println("");
274 }
275
276
277
278 columnNames.clear();
279 for (ColumnDescriptor col2 : client.getColumnDescriptors(ByteBuffer.wrap(t)).values()) {
280 System.out.println("column with name: " + new String(col2.name.array()));
281 System.out.println(col2.toString());
282
283 columnNames.add(col2.name);
284 }
285
286 System.out.println("Starting scanner...");
287 scanner = client.scannerOpenWithStop(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("00020")), ByteBuffer.wrap(bytes("00040")), columnNames, dummyAttributes);
288
289 while (true) {
290 List<TRowResult> entry = client.scannerGet(scanner);
291 if (entry.isEmpty()) {
292 System.out.println("Scanner finished");
293 break;
294 }
295 printRow(entry);
296 }
297
298 transport.close();
299 }
300
301 private void printVersions(ByteBuffer row, List<TCell> versions) {
302 StringBuilder rowStr = new StringBuilder();
303 for (TCell cell : versions) {
304 rowStr.append(utf8(cell.value.array()));
305 rowStr.append("; ");
306 }
307 System.out.println("row: " + utf8(row.array()) + ", values: " + rowStr);
308 }
309
310 private void printRow(TRowResult rowResult) {
311
312
313 TreeMap<String, TCell> sorted = new TreeMap<String, TCell>();
314 for (Map.Entry<ByteBuffer, TCell> column : rowResult.columns.entrySet()) {
315 sorted.put(utf8(column.getKey().array()), column.getValue());
316 }
317
318 StringBuilder rowStr = new StringBuilder();
319 for (SortedMap.Entry<String, TCell> entry : sorted.entrySet()) {
320 rowStr.append(entry.getKey());
321 rowStr.append(" => ");
322 rowStr.append(utf8(entry.getValue().value.array()));
323 rowStr.append("; ");
324 }
325 System.out.println("row: " + utf8(rowResult.row.array()) + ", cols: " + rowStr);
326 }
327
328 private void printRow(List<TRowResult> rows) {
329 for (TRowResult rowResult : rows) {
330 printRow(rowResult);
331 }
332 }
333 }