source: sample/hadoop-0.16/test.java @ 24

Last change on this file since 24 was 24, checked in by waue, 16 years ago

upload test

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