-
Notifications
You must be signed in to change notification settings - Fork 62
/
example.java
125 lines (110 loc) · 4.21 KB
/
example.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import java.io.IOException;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import ict.wde.domino.client.Domino;
import ict.wde.domino.client.Transaction;
public class testCon {
private Domino domino;
private Transaction transaction;
public testCon(String addressPort) throws IOException{
domino = new Domino(addressPort);
transaction = domino.startTransaction();
}
public void createTable(String tablename){
try {
System.out.println("start create table ......");
if( false == domino.tableExists(tablename) ){
HTableDescriptor tableDescriptor = new HTableDescriptor(tablename);
tableDescriptor.addFamily(new HColumnDescriptor("c1"));
tableDescriptor.addFamily(new HColumnDescriptor("c2"));
tableDescriptor.addFamily(new HColumnDescriptor("c3"));
domino.createTable(tableDescriptor);
System.out.println("create table successfully......");
}else{
System.out.println("the table exists ......");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void listTable(String tableName) throws IOException{
if ( true == domino.tableExists(tableName) ){
Scan scan = new Scan();
ResultScanner rs = transaction.scan(scan, tableName.getBytes());
transaction.commit();
for(Result r : rs){
System.out.printf("the row is " + new String(r.getRow()) );
for(KeyValue keyValue : r.raw() ){
System.out.printf( " | the cloumnfamily : " + new String(keyValue.getFamily())
+ " the value : " + new String(keyValue.getValue()) );
}
System.out.println("\nscan all");
}
}else{
System.out.println("the table " + tableName + "does not exist");
}
}
public void put(String[] args) throws IOException{
if ( true == domino.tableExists(args[1]) ){
Put put = new Put(args[2].getBytes());
put.add("c1".getBytes(),"name".getBytes(),args[3].getBytes());
put.add("c2".getBytes(),"courses".getBytes(),args[4].getBytes());
put.add("c3".getBytes(),"scores".getBytes(),args[5].getBytes());
transaction.put(put, args[1].getBytes());
transaction.commit();
System.out.println("put into table " + args[1] + "successfully");
}else{
System.out.println("the table " + args[1] + "does not exist");
}
}
public void deleteTable(String tableName) throws IOException{
if ( true == domino.tableExists(tableName) ){
domino.dropTable(tableName);
System.out.println("delete table " + tableName + "successfully");
}else{
System.out.println("the table " + tableName + "does not exist");
}
}
public void getValue(String tableName, String rowKey) throws IOException{
if ( true == domino.tableExists(tableName) ){
Get get = new Get(rowKey.getBytes());
Result rs = transaction.get(get, tableName.getBytes());
transaction.commit();
System.out.printf( tableName + "-" + rowKey + ":" );
for (KeyValue keyValue : rs.raw() ){
System.out.printf("cloumn:" + new String(keyValue.getFamily()) + " value"
+ new String(keyValue.getValue()) + "|" );
}
System.out.println("\nget all");
}else{
System.out.println("the table " + tableName + "does not exist");
}
}
public void deleteValue(String tableName, String rowKey) throws IOException{
if ( true == domino.tableExists(tableName) ){
transaction.delete(rowKey.getBytes(), tableName.getBytes());
transaction.commit();
System.out.println("delete row " + rowKey);
}else{
System.out.println("the table " + tableName + "does not exist");
}
}
public static void main(String[] args) throws IOException {
testCon testCon = new testCon("processor018:2181");
switch( args[0] ){
case "createTable":testCon.createTable(args[1]);break;
case "list" :testCon.listTable(args[1]);break;
case "put" :testCon.put(args);break;
case "deleteTable":testCon.deleteTable(args[1]);break;
case "get" :testCon.getValue(args[1] ,args[2]);break;
case "deleteRow":testCon.deleteValue(args[1] ,args[2]);break;
}
}
}