1 | import java.io.BufferedReader; |
---|
2 | import java.io.BufferedWriter; |
---|
3 | import java.io.File; |
---|
4 | import java.io.FileOutputStream; |
---|
5 | import java.io.FileReader; |
---|
6 | import java.io.FileWriter; |
---|
7 | import java.io.IOException; |
---|
8 | import java.io.RandomAccessFile; |
---|
9 | import java.util.StringTokenizer; |
---|
10 | import org.apache.hadoop.fs.FileStatus; |
---|
11 | import org.apache.hadoop.fs.FileSystem; |
---|
12 | import org.apache.hadoop.fs.Path; |
---|
13 | import org.apache.hadoop.hbase.HBaseAdmin; |
---|
14 | import org.apache.hadoop.hbase.HBaseConfiguration; |
---|
15 | import org.apache.hadoop.hbase.HColumnDescriptor; |
---|
16 | import org.apache.hadoop.hbase.HTableDescriptor; |
---|
17 | import org.apache.hadoop.io.Text; |
---|
18 | import org.apache.hadoop.mapred.JobConf; |
---|
19 | |
---|
20 | |
---|
21 | // this example is testing for static valiable value |
---|
22 | // it will show the value of static valiable is related to Class, not object |
---|
23 | |
---|
24 | class testb { |
---|
25 | // object variable |
---|
26 | public void read() { |
---|
27 | System.out.println(test.sna); |
---|
28 | } |
---|
29 | |
---|
30 | } |
---|
31 | |
---|
32 | public class test { |
---|
33 | int na; |
---|
34 | |
---|
35 | static int sna; |
---|
36 | |
---|
37 | static String str; |
---|
38 | |
---|
39 | public test() { |
---|
40 | |
---|
41 | } |
---|
42 | |
---|
43 | public test(String st) { |
---|
44 | str = st; |
---|
45 | } |
---|
46 | |
---|
47 | public test(int a, int b, String st) { |
---|
48 | na = a; |
---|
49 | sna = b; |
---|
50 | str = st; |
---|
51 | |
---|
52 | } |
---|
53 | |
---|
54 | public void print(String str) { |
---|
55 | System.out.println(str); |
---|
56 | } |
---|
57 | |
---|
58 | @SuppressWarnings( { "static-access" }) |
---|
59 | public void strToken() throws IOException { |
---|
60 | test a = new test(1, 2, "a"); |
---|
61 | a.print("a.na = " + a.na); |
---|
62 | a.print("test.sna = " + test.sna); |
---|
63 | |
---|
64 | a.print("a.sna = " + a.sna); // warning about it's not a regular |
---|
65 | // accessing method |
---|
66 | |
---|
67 | test b = new test(3, 4, "a"); |
---|
68 | b.print("b.na = " + b.na); |
---|
69 | b.print("test.sna = " + test.sna); |
---|
70 | b.print("b.sna = " + b.sna); // warning about it's not a regular |
---|
71 | // accessing method |
---|
72 | |
---|
73 | a.print("a.sna = " + a.sna); // a.sna = test.sna -> b.sna |
---|
74 | String tmp[] = test.str.split(":"); |
---|
75 | String Column_Family = tmp[0] + ":"; |
---|
76 | a.print("column family = " + Column_Family); |
---|
77 | a.print("ori str = " + test.str); |
---|
78 | // test fileoutputstream |
---|
79 | FileOutputStream out = new FileOutputStream(new File( |
---|
80 | "/home/waue/mr-result.txt")); |
---|
81 | out.write("hello".getBytes()); |
---|
82 | out.close(); |
---|
83 | // test randomaccessfile |
---|
84 | RandomAccessFile raf = new RandomAccessFile("/home/waue/mr-result.txt", |
---|
85 | "rw"); |
---|
86 | raf.seek(raf.length()); // move pointer to end |
---|
87 | raf.write("\n go \t ahead".getBytes()); |
---|
88 | raf.close(); |
---|
89 | // test StringTokenizer |
---|
90 | StringTokenizer st = new StringTokenizer("this is a test"); |
---|
91 | while (st.hasMoreTokens()) { |
---|
92 | System.out.println(st.nextToken()); |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | String parseFirstLine(String in, String ou) throws IOException { |
---|
97 | BufferedReader fi = new BufferedReader(new FileReader(new File(in))); |
---|
98 | BufferedWriter fw = new BufferedWriter(new FileWriter(new File(ou))); |
---|
99 | String first_line, data; |
---|
100 | first_line = fi.readLine(); |
---|
101 | do { |
---|
102 | data = fi.readLine(); |
---|
103 | if (data == null) { |
---|
104 | break; |
---|
105 | } else { |
---|
106 | fw.write(data + "\n"); |
---|
107 | } |
---|
108 | } while (true); |
---|
109 | fw.close(); |
---|
110 | |
---|
111 | return first_line; |
---|
112 | } |
---|
113 | |
---|
114 | boolean deleteFile(String str) throws IOException { |
---|
115 | File ttt = new File(str); |
---|
116 | |
---|
117 | if (ttt.exists()) { |
---|
118 | if (!ttt.delete()) { |
---|
119 | System.err.print("delete file error !"); |
---|
120 | } |
---|
121 | } else { |
---|
122 | System.out.println("file not exit!"); |
---|
123 | } |
---|
124 | |
---|
125 | return true; |
---|
126 | } |
---|
127 | |
---|
128 | void ttt() throws IOException { |
---|
129 | Path pi; |
---|
130 | JobConf conf = new JobConf(test.class); |
---|
131 | FileStatus[] fi = FileSystem.get(conf).listStatus( |
---|
132 | new Path("/user/waue/input/")); |
---|
133 | for (int i = 0; i < fi.length; i++) { |
---|
134 | pi = fi[i].getPath(); |
---|
135 | System.out.println(pi.getName()); |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | public static void main(String[] args) throws IOException { |
---|
140 | // test a = new test(); |
---|
141 | // a.strToken(); |
---|
142 | // System.out.println(a.parseFirstLine("/home/waue/test.txt", |
---|
143 | // "/home/waue/out.tmp.txt")); |
---|
144 | // ttt(); |
---|
145 | |
---|
146 | |
---|
147 | // setup Table name |
---|
148 | String table_name = "test_create_table2"; |
---|
149 | Text text_table_name = new Text(table_name); |
---|
150 | // setup Column Family |
---|
151 | String[] Column_Family = { "http:", "url:", "referrer:" }; |
---|
152 | HBaseConfiguration conf = new HBaseConfiguration(); |
---|
153 | |
---|
154 | HBaseAdmin admin = new HBaseAdmin(conf); |
---|
155 | boolean b = admin.tableExists(text_table_name); |
---|
156 | System.out.println(b); |
---|
157 | |
---|
158 | if (!admin.tableExists(text_table_name)) { |
---|
159 | |
---|
160 | System.out.println("HTable : " + table_name |
---|
161 | + " creating ... please wait"); |
---|
162 | HTableDescriptor tableDesc = new HTableDescriptor(table_name); |
---|
163 | for (int i = 0; i < Column_Family.length; i++) { |
---|
164 | String st = Column_Family[i]; |
---|
165 | // check name format "string:" |
---|
166 | if (!st.endsWith(":")) { |
---|
167 | Column_Family[i] = st + ":"; |
---|
168 | System.out.println("normize :" + st + "->" |
---|
169 | + Column_Family[i]); |
---|
170 | } |
---|
171 | // add column family |
---|
172 | tableDesc.addFamily(new HColumnDescriptor(Column_Family[i])); |
---|
173 | } |
---|
174 | admin.createTable(tableDesc); |
---|
175 | } else { |
---|
176 | System.out.println("table exist!"); |
---|
177 | } |
---|
178 | |
---|
179 | |
---|
180 | } |
---|
181 | } |
---|