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