/** * Program: HBaseRecord.java * Editor: Waue Chen * From : NCHC. Taiwn * Last Update Date: 06/01/2008 */ /** * Purpose : * Parse your record and then store in HBase. * * HowToUse : * Make sure Hadoop file system and Hbase are running correctly. * 1. put test.txt in t1 directory which content is --------------- name:locate:years waue:taiwan:1981 shellon:taiwan:1981 --------------- * 2. hadoop_root/$ bin/hadoop dfs -put t1 t1 * 3. hbase_root/$ bin/hbase shell * 4. hql > create table t1_table("person"); * 5. Come to Eclipse and run this code, and we will let database as that t1_table -> person ---------------- | name | locate | years | | waue | taiwan | 1981 | | shellon | taiwan | 1981 | ---------------- * Check Result: * Go to hbase console, type : * hql > select * from t1_table; 08/06/06 12:20:48 INFO hbase.HTable: Creating scanner over t1_table starting at key +-------------------------+-------------------------+-------------------------+ | Row | Column | Cell | +-------------------------+-------------------------+-------------------------+ | 0 | person:locate | locate | +-------------------------+-------------------------+-------------------------+ | 0 | person:name | name | +-------------------------+-------------------------+-------------------------+ | 0 | person:years | years | +-------------------------+-------------------------+-------------------------+ | 19 | person:locate | taiwan | +-------------------------+-------------------------+-------------------------+ | 19 | person:name | waue | +-------------------------+-------------------------+-------------------------+ | 19 | person:years | 1981 | +-------------------------+-------------------------+-------------------------+ | 36 | person:locate | taiwan | +-------------------------+-------------------------+-------------------------+ | 36 | person:name | shellon | +-------------------------+-------------------------+-------------------------+ | 36 | person:years | 1981 | +-------------------------+-------------------------+-------------------------+ 3 row(s) in set. (0.04 sec) */ package tw.org.nchc.code; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Iterator; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.hbase.io.ImmutableBytesWritable; import org.apache.hadoop.hbase.mapred.TableReduce; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.MapWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapred.JobClient; import org.apache.hadoop.mapred.JobConf; import org.apache.hadoop.mapred.OutputCollector; import org.apache.hadoop.mapred.Reporter; import org.apache.hadoop.mapred.lib.IdentityMapper; import org.apache.hadoop.mapred.lib.IdentityReducer; class ReduceClass extends TableReduce { // on this sample, map is nonuse, we use reduce to handle public void reduce(LongWritable key, Iterator values, OutputCollector output, Reporter reporter) throws IOException { String sp = ":"; String bf = "person:"; // this map holds the columns per row MapWritable map = new MapWritable(); // values.next().getByte() can get value and transfer to byte form, String stro = new String(values.next().getBytes()); String str[] = stro.split(sp); BufferedReader fconf = new BufferedReader(new FileReader(new File("/tmp/fi_conf.tmp"))); // int length = cf.length; // test for debug FileOutputStream out = new FileOutputStream(new File( "/home/waue/mr-result.txt")); String first_line = fconf.readLine(); // test for debug String[] cf = first_line.split(sp); int length = cf.length; for(int i=0 ; i