手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

HDFS导出数据到HBase的ROW VALUE设置tricks

[日期:2012-10-25] 来源:Linux公社  作者:fansy1990 [字体:大 中 小]

第二个想法,再看回原始数据,可以发现如果我们是做用户推荐的话,那么数据很可能每条记录的长度都相差不大,如果这样的话,那么是否可以用记录的偏移值除以这条记录的长度,最后再四舍五入,那么得到的row值就基本按照1递增了。

具体代码如下:

  1. package org.fansy.date1023; 
  2.  
  3. import java.io.IOException; 
  4.  
  5. import org.apache.Hadoop.conf.Configuration; 
  6. import org.apache.hadoop.hbase.HBaseConfiguration; 
  7. import org.apache.hadoop.hbase.HColumnDescriptor; 
  8. import org.apache.hadoop.hbase.HTableDescriptor; 
  9. import org.apache.hadoop.hbase.client.HBaseAdmin; 
  10. import org.apache.hadoop.hbase.client.Put; 
  11. import org.apache.hadoop.hbase.io.ImmutableBytesWritable; 
  12. import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil; 
  13. import org.apache.hadoop.hbase.util.Bytes; 
  14. import org.apache.hadoop.io.LongWritable; 
  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.input.FileInputFormat; 
  19. import org.apache.hadoop.util.GenericOptionsParser; 
  20.  
  21. public class HDFSToHBase { 
  22.      
  23.     /** 
  24.      * import data from hdfs to hbase 
  25.      */ 
  26.  
  27.     public static class MapperClass extends Mapper<LongWritable,Text,ImmutableBytesWritable,Put>{   
  28.         private Put put; 
  29.         private byte[] family,qulifier1,qulifier2,qulifier3; 
  30.          
  31.         public void setup(Context context){ 
  32.             family=Bytes.toBytes(context.getConfiguration().get("FAMILY")); 
  33.             qulifier1=Bytes.toBytes(context.getConfiguration().get("QULIFIER1")); 
  34.             qulifier2=Bytes.toBytes(context.getConfiguration().get("QULIFIER2")); 
  35.             qulifier3=Bytes.toBytes(context.getConfiguration().get("QULIFIER3")); 
  36.         } 
  37.          
  38.         public void map(LongWritable key,Text line,Context context)throws IOException,InterruptedException{   
  39.             String[] values=line.toString().split(",");   
  40.             if(values.length!=3){ // if there are not three args,then return   
  41.                 return ;   
  42.             }   
  43.             //  set the row value  
  44.             int tempRow=Math.round(Float.parseFloat(key.toString())/(line.toString().length())); 
  45.             byte [] row=Bytes.toBytes(tempRow); 
  46.               byte [] qulifier1_value = Bytes.toBytes(values[0]);   
  47.               byte [] qulifier2_value = Bytes.toBytes(values[1]);   
  48.               byte [] qulifier3_value = Bytes.toBytes(values[2]);   
  49.              put=new Put(row);   
  50.              put.add(family,qulifier1,qulifier1_value);  //  user  
  51.              context.write(new ImmutableBytesWritable(row),put);   
  52.              put=new Put(row);   
  53.              put.add(family,qulifier2,qulifier2_value);  //  item  
  54.              context.write(new ImmutableBytesWritable(row),put);   
  55.              put=new Put(row);   
  56.              put.add(family,qulifier3,qulifier3_value);  //  item  
  57.              context.write(new ImmutableBytesWritable(row),put);   
  58.         }   
  59.     } 
  60.     public static void main(String[] args) throws IOException { 
  61.         // TODO Auto-generated method stub  
  62.          
  63.         //check if there exist the table then delete the data and alter the family  
  64.         if(args.length<3){ 
  65.             System.out.println("Wrong number of arguments "+args.length); 
  66.              System.out.println("Usage: <input> <tablename> <family> <qulifier1> <qulifier2> <qulifier3>");   
  67.              System.exit(-1);   
  68.         } 
  69.         String tableName=args[1]; 
  70.         String family=args[2]; 
  71.         if(existTable(tableName)){ 
  72.             alterTable(tableName); 
  73.         } 
  74.         createTable(tableName,family); 
  75.         // import data to hbase  
  76.         HDFSToHBase.runMyJob(args); 
  77.     } 
  78.     public static void runMyJob(String[] args) { 
  79.         Configuration conf = HBaseConfiguration.create();   
  80.         try{ 
  81.             String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();   
  82.             if(otherArgs.length != 6) {   
  83.               System.out.println("Wrong number of arguments: " + otherArgs.length);   
  84.               System.out.println("Usage: <input> <tablename> <family> <qulifier1> <qulifier2> <qulifier3>");   
  85.               System.exit(-1);   
  86.             } 
  87.              
  88.             String family=otherArgs[2];   
  89.             String qulifier1=otherArgs[3]; 
  90.             String qulifier2=otherArgs[4]; 
  91.             String qulifier3=otherArgs[5]; 
  92.             conf.set("FAMILY", family); 
  93.             conf.set("QULIFIER1", qulifier1); 
  94.             conf.set("QULIFIER2", qulifier2); 
  95.             conf.set("QULIFIER3", qulifier3); 
  96.              
  97.             Job job=new Job(conf,"import data to hbase");   
  98.             job.setJarByClass(HDFSToHBase.class);   
  99.             job.setMapperClass(MapperClass.class);   
  100.             job.setMapOutputKeyClass(ImmutableBytesWritable.class);   
  101.             job.setMapOutputValueClass(Put.class);   
  102.             TableMapReduceUtil.initTableReducerJob(otherArgs[1], null, job);   
  103.             job.setNumReduceTasks(0);   
  104.             FileInputFormat.setInputPaths(job, otherArgs[0]);   
  105.             System.exit(job.waitForCompletion(true) ? 0 : 1); 
  106.          }catch(Exception e){ 
  107.            e.printStackTrace(); 
  108.          } 
  109.     } 
  110.      
  111.     //  check whether the given table exists if and delete the data and create a new one else create a new one  
  112.     public static boolean existTable(String tableName) throws IOException{ 
  113.         boolean flag=false; 
  114.  
  115.           Configuration conf = HBaseConfiguration.create();   
  116.  
  117.          HBaseAdmin admin=new HBaseAdmin(conf);       
  118.          flag=admin.isTableAvailable(Bytes.toBytes(tableName)); 
  119.          admin.close(); 
  120.          return flag; 
  121.     } 
  122.      
  123.     //  if the table exists then delete the data and alter the family  
  124.     public static void alterTable(String tableName ) throws IOException{ 
  125.         System.out.println("table "+tableName+" exists ,delete it  ..."); 
  126.         Configuration conf = HBaseConfiguration.create();   
  127.          HBaseAdmin admin=new HBaseAdmin(conf); 
  128.         admin.disableTable(tableName); 
  129.         admin.deleteTable(tableName); 
  130.         admin.close(); 
  131.     } 
  132.      
  133.     //  if the table does not exist then create the table  
  134.     public static void createTable( String tableName,String family) throws IOException{ 
  135.          System.out.println("create the new table :"+tableName+" ..."); 
  136.          Configuration conf = HBaseConfiguration.create();   
  137.          HBaseAdmin admin=new HBaseAdmin(conf); 
  138.         HTableDescriptor desc=new HTableDescriptor(Bytes.toBytes(tableName)); 
  139.         HColumnDescriptor coldef=new HColumnDescriptor(Bytes.toBytes(family)); 
  140.         desc.addFamily(coldef); 
  141.         admin.createTable(desc); 
  142.         admin.close(); 
  143.     } 
  144.  
  145. } 

上面的代码同时包含了先判断输入的参数(即表名)是否已存在HBase中,如果存在则删除,否则新建一个表,最后把数据通过MR导入HBase.其中的44行就是第二种想法的实现。

导入到HBase中的数据如下:

疑问:(1)如果数据文件是多个的话,那么第二种想法就会导致有些数据被覆盖。多个数据的话是否可以用一个MR 把所有的数据先放入一个文件然后在调用上面的代码?

(2)是否有其他的比较好的做法呢?

linux
相关资讯       HDFS  HBase HDFS  Hadoop HDFS 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款