source: sample/HBaseRecord.java @ 7

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

6/12 modify

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