/
HbaseThriftTest.java
92 lines (77 loc) · 3.42 KB
/
HbaseThriftTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package io.pivotal.support;
/*
service hbase-thrift start
= "framed"
hbase thrift start
= "binary"
hbase thrift start -c
= "compact"
*/
import org.apache.hadoop.hbase.thrift.generated.AlreadyExists;
import org.apache.hadoop.hbase.thrift.generated.Hbase;
import org.apache.hadoop.hbase.thrift.generated.ColumnDescriptor;
import org.apache.thrift.transport.*;
import org.apache.thrift.protocol.*;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.transport.TFramedTransport;
import java.util.ArrayList;
import java.util.List;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
public class HbaseThriftTest {
static void usage() {
System.out.println("\nUsage:");
System.out.println("java -classpath HbaseThriftTest.jar:`hbase classpath` io.pivotal.support.HbaseThriftTest <host> <port> <protocol> <table name> <column family>");
System.out.println("\nExamples:");
System.out.println("java -classpath HbaseThriftTest.jar:`hbase classpath` io.pivotal.support.HbaseThriftTest hdm1 9090 compact sample_demo_table demodata");
System.out.println("java -classpath HbaseThriftTest.jar:`hbase classpath` io.pivotal.support.HbaseThriftTest hdm1 9090 framed sample_demo_table demodata");
System.out.println("java -classpath HbaseThriftTest.jar:`hbase classpath` io.pivotal.support.HbaseThriftTest hdm1 9090 binary sample_demo_table demodata");
System.exit(2);
}
public static void main(String[] args) throws Exception {
if (args.length != 5) {
usage();
}
// collect args
String HostName = args[0];
int Port = Integer.parseInt(args[1]);
String Proto = args[2];
String TableName = args[3];
String ColFamily = args[4];
// setup the hbase thrift connection
TTransport Transport;
Transport = new TSocket(HostName, Port);
TCompactProtocol FProtocol = new TCompactProtocol(Transport);
Hbase.Client Client = new Hbase.Client(FProtocol);
if (Proto.equals("binary")) {
TProtocol Protocol = new TBinaryProtocol(Transport, true, true);
Client = new Hbase.Client(Protocol);
} else if ( Proto.equals("framed")) {
Transport = new TFramedTransport(new TSocket(HostName, Port));
TProtocol Protocol = new TBinaryProtocol(Transport, true, true);
Client = new Hbase.Client(Protocol);
} else if ( ! Proto.equals("compact")) {
System.out.println("Protocol must be compact or framed or binary");
usage();
}
Transport.open();
// prepare the column family
List<ColumnDescriptor> Columns = new ArrayList<ColumnDescriptor>();
ColumnDescriptor col = new ColumnDescriptor();
col.name = ByteBuffer.wrap(ColFamily.getBytes());
Columns.add(col);
// dump existing tables
System.out.println("#~ Dumping Existing tables");
for (ByteBuffer tn : Client.getTableNames()) {
System.out.println("-- found: " + new String(tn.array(), Charset.forName("UTF-8")));
}
// create the new table
System.out.println("#~ Creating table: " + TableName);
try {
Client.createTable(ByteBuffer.wrap(TableName.getBytes()), Columns);
} catch (AlreadyExists ae) {
System.out.println("WARN: " + ae.message);
}
Transport.close();
}
}