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

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

test!

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