通过MapReduce程序导出Hbase到Hadoop

来源:互联网 发布:液晶屏参数查询软件 编辑:程序博客网 时间:2024/06/02 01:18


  1. import java.io.IOException;  
  2. import java.util.List;  
  3.   
  4. import org.apache.hadoop.conf.Configuration;  
  5. import org.apache.hadoop.fs.Path;  
  6. import org.apache.hadoop.hbase.Cell;  
  7. import org.apache.hadoop.hbase.CellUtil;  
  8. import org.apache.hadoop.hbase.HBaseConfiguration;  
  9. import org.apache.hadoop.hbase.client.Result;  
  10. import org.apache.hadoop.hbase.client.Scan;  
  11. import org.apache.hadoop.hbase.io.ImmutableBytesWritable;  
  12. import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;  
  13. import org.apache.hadoop.hbase.mapreduce.TableMapper;  
  14. import org.apache.hadoop.hbase.util.Bytes;  
  15. import org.apache.hadoop.io.Text;  
  16. import org.apache.hadoop.mapreduce.Job;  
  17. import org.apache.hadoop.mapreduce.Mapper;  
  18. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  19. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;  
  20.   
  21.   
  22. public class ExportToHdfs {  
  23.   
  24.     public static class NewMapper extends TableMapper<Text, Text>{  
  25.         @Override  
  26.   
  27.         //key是hbase中的行键  
  28.         //value是hbase中的所行键的所有数据  
  29.         protected void map(ImmutableBytesWritable key, Result value,  
  30.                 Mapper<ImmutableBytesWritable, Result, Text, Text>.Context context)  
  31.                         throws IOException, InterruptedException {  
  32.             Text v=null;  
  33.             String columns = "";  
  34.             List<Cell> cs=value.listCells();  
  35.             for(Cell cell:cs){  
  36.                 columns += new String(CellUtil.cloneValue(cell)) + "|";  
  37.                 System.out.println(new String(key.get()) + " -->> " + new String(CellUtil.cloneValue(cell)));  
  38.             }  
  39.             columns = columns.substring(0, columns.length()-1);  
  40.             context.write(new Text(key.get()), new Text(columns));  
  41.         }  
  42.   
  43.     }  
  44.   
  45.   
  46.   
  47.   
  48.     public static void main(String[] args) throws Exception {  
  49.         Configuration conf = HBaseConfiguration.create();   
  50.         conf.set("hbase.zookeeper.quorum""192.168.42.132,192.168.42.131,192.168.42.130");  
  51.         Job job = Job.getInstance(conf, ExportToHdfs.class.getSimpleName());   
  52.         job.setJarByClass(ExportToHdfs.class);//将此类打成jar包  
  53.   
  54.         job.setMapperClass(NewMapper.class);   
  55.         job.setMapOutputKeyClass(Text.class);   
  56.         job.setMapOutputValueClass(Text.class);  
  57.         job.setOutputFormatClass(TextOutputFormat.class);   
  58.         job.setNumReduceTasks(0);   
  59.           
  60.         Scan scan=new Scan();  
  61.   
  62.         TableMapReduceUtil.initTableMapperJob(args[0], scan, NewMapper.class, Text.class, Text.class, job);  
  63.   
  64.         FileOutputFormat.setOutputPath(job, new Path(args[1]));   
  65.   
  66.         System.exit(job.waitForCompletion(true) ? 0 : 1);  
  67.     }  
  68. }  



2、导出为jar包,并把Hbase的lib路径添加到HADOOP_CLASSPATH。

[plain] view plain copy
 print?
  1. export HADOOP_CLASSPATH=/home/takchi/Bigdata/hbase-1.2.4/lib/*:$HADOOP_CLASSPATH  


3、运行。

[plain] view plain copy
 print?
  1. bin/hadoop jar /home/takchi/Desktop/_export.jar chan.takchi.mr.ExportToHdfs  students /tmp/students_mr  

 

原创粉丝点击