source: sample/hadoop-0.16/tw/org/nchc/code/HBaseRecord2.java @ 27

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

test!

File size: 5.5 KB
Line 
1/**
2 * Program: HBaseRecord.java
3 * Editor: Waue Chen
4 * From :  NCHC. Taiwn
5 * Last Update Date: 07/01/2008
6 */
7
8/**
9 * Purpose :
10 *  Parse your record and then store in HBase.
11 *
12 * HowToUse :
13 *  Make sure Hadoop file system and Hbase are running correctly.
14 *  1. put test.txt in t1 directory which content is
15  ---------------
16  name:locate:years
17  waue:taiwan:1981
18  shellon:taiwan:1981
19  ---------------
20 *  2. hadoop_root/$ bin/hadoop dfs -put t1 t1
21    ----------------
22 * Check Result:
23 *  Go to hbase console, type :
24 *    hql > select * from t1_table;
2508/06/06 12:20:48 INFO hbase.HTable: Creating scanner over t1_table starting at key
26+-------------------------+-------------------------+-------------------------+
27| Row                     | Column                  | Cell                    |
28+-------------------------+-------------------------+-------------------------+
29| 0                       | person:locate           | locate                  |
30+-------------------------+-------------------------+-------------------------+
31| 0                       | person:name             | name                    |
32+-------------------------+-------------------------+-------------------------+
33| 0                       | person:years            | years                   |
34+-------------------------+-------------------------+-------------------------+
35| 19                      | person:locate           | taiwan                  |
36+-------------------------+-------------------------+-------------------------+
37| 19                      | person:name             | waue                    |
38+-------------------------+-------------------------+-------------------------+
39| 19                      | person:years            | 1981                    |
40+-------------------------+-------------------------+-------------------------+
41| 36                      | person:locate           | taiwan                  |
42+-------------------------+-------------------------+-------------------------+
43| 36                      | person:name             | shellon                 |
44+-------------------------+-------------------------+-------------------------+
45| 36                      | person:years            | 1981                    |
46+-------------------------+-------------------------+-------------------------+
473 row(s) in set. (0.04 sec)
48 */
49
50
51
52
53package tw.org.nchc.code;
54
55import java.io.IOException;
56import java.util.Iterator;
57
58import org.apache.hadoop.fs.Path;
59import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
60import org.apache.hadoop.hbase.mapred.TableReduce;
61import org.apache.hadoop.io.LongWritable;
62import org.apache.hadoop.io.MapWritable;
63import org.apache.hadoop.io.Text;
64import org.apache.hadoop.mapred.JobClient;
65import org.apache.hadoop.mapred.JobConf;
66import org.apache.hadoop.mapred.OutputCollector;
67import org.apache.hadoop.mapred.Reporter;
68import org.apache.hadoop.mapred.lib.IdentityMapper;
69import org.apache.hadoop.mapred.lib.IdentityReducer;
70
71
72public class HBaseRecord2 {
73
74  /* Denify parameter */
75  static String[] bf = {"person:name","person:local","person:birthyear"};
76  // file path in hadoop file system (not phisical file system)
77  String file_path = "/user/waue/t1/test.txt";
78  // Hbase table name
79  String table_name = "testtable";
80 
81 
82  // setup MapTask and Reduce Task
83  int mapTasks = 1;
84  int reduceTasks = 1;
85 
86  private static class ReduceClass extends TableReduce<LongWritable, Text> {
87
88
89   
90    // on this sample, map is nonuse, we use reduce to handle
91    public void reduce(LongWritable key, Iterator<Text> values,
92        OutputCollector<Text, MapWritable> output, Reporter reporter)
93        throws IOException {
94      // this map holds the columns per row
95      MapWritable map = new MapWritable(); 
96      // values.next().getByte() can get value and transfer to byte form,
97      String stro = new String(values.next().getBytes());
98      String str[] = stro.split(":");
99     
100      int length = bf.length;
101     
102      // Column id is created dymanically,
103      Text[] col_n = new Text[length];
104      byte[][] b_l = new byte[length][];
105      // contents must be ImmutableBytesWritable
106      ImmutableBytesWritable[] w_l = new ImmutableBytesWritable[length];
107      map.clear();
108      for(int i = 0; i < length; i++){
109        col_n[i] = new Text(bf[i]);
110        b_l[i] = str[i].getBytes();
111        w_l[i] = new ImmutableBytesWritable(b_l[i]);
112        // populate the current row
113        map.put(col_n[i], w_l[i]);
114      }
115      // add the row with the key as the row id
116      output.collect(new Text(key.toString()), map);
117    }
118  }
119
120  private HBaseRecord2() {
121  }
122
123  /**
124   * Runs the demo.
125   */
126  public static void main(String[] args) throws IOException {
127
128   
129    HBaseRecord2 setup = new HBaseRecord2();
130    String[] tmp = bf[0].split(":");
131    String[] CF = {tmp[0]};
132    BuildHTable build_table = new BuildHTable(setup.table_name, CF);
133    if (!build_table.checkTableExist(setup.table_name)) {
134      if (!build_table.createTable()) {
135        System.out.println("create table error !");
136      }
137    } else {
138      System.out.println("Table \"" + setup.table_name
139          + "\" has already existed !");
140    }
141   
142    JobConf conf = new JobConf(HBaseRecord2.class);
143
144    //Job name; you can modify to any you like 
145    conf.setJobName("PersonDataBase");
146
147    // Hbase table name must be correct , in our profile is t1_table
148    TableReduce.initJob(setup.table_name, ReduceClass.class, conf);
149   
150    // below are map-reduce profile
151    conf.setNumMapTasks(setup.mapTasks);
152    conf.setNumReduceTasks(setup.reduceTasks);
153    conf.setInputPath(new Path(setup.file_path));
154    conf.setMapperClass(IdentityMapper.class);
155    conf.setCombinerClass(IdentityReducer.class);
156    conf.setReducerClass(ReduceClass.class);
157    JobClient.runJob(conf);
158  }
159}
Note: See TracBrowser for help on using the repository browser.