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 mutations = new ArrayList<Mutation>();
175 mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:")),
176 ByteBuffer.wrap(bytes("")), writeToWal));
177 client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("")),
178 mutations, dummyAttributes);
179
180
181 mutations = new ArrayList<Mutation>();
182 mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(valid), writeToWal));
183 client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(valid), mutations, dummyAttributes);
184
185
186
187 mutations = new ArrayList<Mutation>();
188 mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(invalid), writeToWal));
189 client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(invalid), mutations, dummyAttributes);
190
191
192
193 ArrayList<ByteBuffer> columnNames = new ArrayList<ByteBuffer>();
194 columnNames.add(ByteBuffer.wrap(bytes("entry:")));
195
196 System.out.println("Starting scanner...");
197 int scanner = client.scannerOpen(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("")), columnNames, dummyAttributes);
198
199 while (true) {
200 List<TRowResult> entry = client.scannerGet(scanner);
201 if (entry.isEmpty()) {
202 break;
203 }
204 printRow(entry);
205 }
206
207
208
209
210 for (int i = 100; i >= 0; --i) {
211
212 NumberFormat nf = NumberFormat.getInstance();
213 nf.setMinimumIntegerDigits(5);
214 nf.setGroupingUsed(false);
215 byte[] row = bytes(nf.format(i));
216
217 mutations = new ArrayList<Mutation>();
218 mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("unused:")), ByteBuffer.wrap(bytes("DELETE_ME")), writeToWal));
219 client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
220 printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
221 client.deleteAllRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes);
222
223 mutations = new ArrayList<Mutation>();
224 mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:num")), ByteBuffer.wrap(bytes("0")), writeToWal));
225 mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(bytes("FOO")), writeToWal));
226 client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
227 printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
228
229 Mutation m;
230 mutations = new ArrayList<Mutation>();
231 m = new Mutation();
232 m.column = ByteBuffer.wrap(bytes("entry:foo"));
233 m.isDelete = true;
234 mutations.add(m);
235 m = new Mutation();
236 m.column = ByteBuffer.wrap(bytes("entry:num"));
237 m.value = ByteBuffer.wrap(bytes("-1"));
238 mutations.add(m);
239 client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
240 printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
241
242 mutations = new ArrayList<Mutation>();
243 mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:num")), ByteBuffer.wrap(bytes(Integer.toString(i))), writeToWal));
244 mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:sqr")), ByteBuffer.wrap(bytes(Integer.toString(i * i))), writeToWal));
245 client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
246 printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
247
248
249 try {
250 Thread.sleep(50);
251 } catch (InterruptedException e) {
252
253 }
254
255 mutations.clear();
256 m = new Mutation();
257 m.column = ByteBuffer.wrap(bytes("entry:num"));
258 m.value= ByteBuffer.wrap(bytes("-999"));
259 mutations.add(m);
260 m = new Mutation();
261 m.column = ByteBuffer.wrap(bytes("entry:sqr"));
262 m.isDelete = true;
263 client.mutateRowTs(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, 1, dummyAttributes);
264 printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
265
266 List<TCell> versions = client.getVer(ByteBuffer.wrap(t), ByteBuffer.wrap(row), ByteBuffer.wrap(bytes("entry:num")), 10, dummyAttributes);
267 printVersions(ByteBuffer.wrap(row), versions);
268 if (versions.isEmpty()) {
269 System.out.println("FATAL: wrong # of versions");
270 System.exit(-1);
271 }
272
273 List<TCell> result = client.get(ByteBuffer.wrap(t), ByteBuffer.wrap(row), ByteBuffer.wrap(bytes("entry:foo")), dummyAttributes);
274 if (!result.isEmpty()) {
275 System.out.println("FATAL: shouldn't get here");
276 System.exit(-1);
277 }
278
279 System.out.println("");
280 }
281
282
283
284 columnNames.clear();
285 for (ColumnDescriptor col2 : client.getColumnDescriptors(ByteBuffer.wrap(t)).values()) {
286 System.out.println("column with name: " + new String(col2.name.array()));
287 System.out.println(col2.toString());
288
289 columnNames.add(col2.name);
290 }
291
292 System.out.println("Starting scanner...");
293 scanner = client.scannerOpenWithStop(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("00020")), ByteBuffer.wrap(bytes("00040")), columnNames, dummyAttributes);
294
295 while (true) {
296 List<TRowResult> entry = client.scannerGet(scanner);
297 if (entry.isEmpty()) {
298 System.out.println("Scanner finished");
299 break;
300 }
301 printRow(entry);
302 }
303
304 transport.close();
305 }
306
307 private void printVersions(ByteBuffer row, List<TCell> versions) {
308 StringBuilder rowStr = new StringBuilder();
309 for (TCell cell : versions) {
310 rowStr.append(utf8(cell.value.array()));
311 rowStr.append("; ");
312 }
313 System.out.println("row: " + utf8(row.array()) + ", values: " + rowStr);
314 }
315
316 private void printRow(TRowResult rowResult) {
317
318
319 TreeMap<String, TCell> sorted = new TreeMap<String, TCell>();
320 for (Map.Entry<ByteBuffer, TCell> column : rowResult.columns.entrySet()) {
321 sorted.put(utf8(column.getKey().array()), column.getValue());
322 }
323
324 StringBuilder rowStr = new StringBuilder();
325 for (SortedMap.Entry<String, TCell> entry : sorted.entrySet()) {
326 rowStr.append(entry.getKey());
327 rowStr.append(" => ");
328 rowStr.append(utf8(entry.getValue().value.array()));
329 rowStr.append("; ");
330 }
331 System.out.println("row: " + utf8(rowResult.row.array()) + ", cols: " + rowStr);
332 }
333
334 private void printRow(List<TRowResult> rows) {
335 for (TRowResult rowResult : rows) {
336 printRow(rowResult);
337 }
338 }
339 }