MapReduce编程实现txt文件中的内容导入HBase

一、创建java项目。

写入代码,如下:
[java]  view plain  copy
 print ?
  1. package translate1;  
  2. import java.io.IOException;  
  3. import org.apache.hadoop.conf.*;  
  4. import org.apache.hadoop.fs.Path;  
  5. import  org.apache.hadoop.mapreduce.*;  
  6. import org.apache.hadoop.io.LongWritable;  
  7. import  org.apache.hadoop.io.Text;  
  8. import org.apache.hadoop.mapreduce.lib.input.*;  
  9. import org.apache.hadoop.hbase.HBaseConfiguration;  
  10. import org.apache.hadoop.hbase.io.*;  
  11. import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;  
  12. import org.apache.hadoop.hbase.client.Put;  
  13. import org.apache.hadoop.hbase.util.Bytes;  
  14. public class translate1 {      
  15.        public static Job createSubmittableJob(Configuration conf, String[] args)throws IOException {  
  16.                        String tableName = args[0];  
  17.                        Path inputDir = new Path(args[1]);  
  18.                        @SuppressWarnings("deprecation")  
  19.                        Job job = new Job (conf, "hac_chapter2_recipe3");  
  20.                        job.setJarByClass(HourlyImporter.class);  
  21.                        FileInputFormat.setInputPaths(job, inputDir);   
  22.                       job.setMapperClass(HourlyImporter.class);  
  23.                       TableMapReduceUtil.initTableReducerJob(tableName, null, job);   
  24.                       job.setNumReduceTasks(0);  
  25.                       TableMapReduceUtil.addDependencyJars(job);  
  26.                       return job;   
  27.                               }   
  28.        public static void main(String[] args)throws Exception {  
  29.                       Configuration conf = HBaseConfiguration.create();  
  30.                       Job job = createSubmittableJob(conf, args);  
  31.                       System.exit (job.waitForCompletion(true) ? 0 : 1);  
  32.                               }  
  33.                      }  
  34.       class HourlyImporter extends Mapper<LongWritable, Text, ImmutableBytesWritable, Put> {  
  35.          private long ts;  
  36.          static byte[] family = Bytes.toBytes("n");  
  37.          @Override  
  38.          protected void setup(Context context) {   
  39.          ts = System.currentTimeMillis();   
  40.           }  
  41.                      public static String change(String str,int n,boolean j){  
  42.                                if(str==null||str.length()>=n) return str;  
  43.                                 String s="";  
  44.                                for(int i=str.length();i<n;i++)  
  45.                                       s+="0";  
  46.                               if(j) return s+str;  
  47.                               else    return str+s;    
  48.                               }  
  49.                             @SuppressWarnings("deprecation")  
  50.                     public void map(LongWritable offset, Text value, Context context)throws IOException {  
  51.                            try {  
  52.                                      String line = value.toString();  
  53.                                      String stationID = line.substring(04);  
  54.                                      String month = line.substring(57);  
  55.                                      String day = line.substring(79);  
  56.                                      String rowkey = stationID + month + day;  
  57.                                      byte[] bRowKey = Bytes.toBytes(rowkey);  
  58.                                      ImmutableBytesWritable rowKey =  new ImmutableBytesWritable(bRowKey);  
  59.                                      Put p = new Put(bRowKey);  
  60.                                      for (int i = 1; i < 4 ; i++) {  
  61.                                            String columnI ="v" + change(String.valueOf(i),2,true);   
  62.                                            int beginIndex = i * 2 + 8;  
  63.                                            String valueI =line.substring(beginIndex, beginIndex + 2).trim();  
  64.                                            p.add(family, Bytes.toBytes(columnI),ts, Bytes.toBytes(valueI));  
  65.                                             }  
  66.                                     context.write(rowKey, p);  
  67.                                }catch (InterruptedException e) {  
  68.                                     e.printStackTrace();   
  69.                                }   
  70.                           }   
  71.               }   

二、上传txt文件到HDFS系统

HDFS中的文件内容为:

三、在HBase终端上创建HBase表格

创建时只需指定要创建表格的表名和列族名

四、配置java项目执行的参数

配置内容如图:


五、执行结果

如图:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值