source: sample/hadoop-0.17/tw/org/nchc/code/HBaseRecord.java @ 20

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

將改完的 hadoop 0.17版package 放來備份
目前繼續開發 hadoop 0.16 + hbase 1.3

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