hbase表读取数据存到hbase表

import java.io.IOException;
import java.util.List;
 
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Mutation;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
 
 
public class WordCountHbaseMapreduce03 {
 
 
     public static void main(String[] args) throws Exception {
         System.exit(run());
     }
 
 
     public static class  MyHBaseMap03 extends TableMapper<immutablebyteswritable, immutablebyteswritable= "" >{
 
         @Override
         protected void map(ImmutableBytesWritable key, Result value,
                 Mapper<immutablebyteswritable, immutablebyteswritable= "" >.Context context)
                         throws IOException, InterruptedException {
             // TODO Auto-generated method stub
             ImmutableBytesWritable word= null ;
             ImmutableBytesWritable num= null ;
             List<cell> cs=value.listCells();
             for (Cell cell:cs){
                  word= new ImmutableBytesWritable(CellUtil.cloneRow(cell));
                  num= new ImmutableBytesWritable(CellUtil.cloneValue(cell));
             }
 
             context.write(word, num);
         }
     }
 
     private static int run() throws Exception {
         Configuration conf = new Configuration();
         conf = HBaseConfiguration.create(conf);
         conf.set( "hbase.zookeeper.quorum" , "192.168.52.140" );
 
         Job job = Job.getInstance(conf, "wordcount3" );
         job.setJarByClass(WordCountHbaseMapreduce03. class );
 
 
         Scan scan = new Scan();
         //取对业务有用的数据 tags, nickname
         scan.addColumn(Bytes.toBytes( "wordcount" ), Bytes.toBytes( "num" ));
 
         //数据来源 hbase
         //TableInputFormat.addColumns(scan, columns);
         //ImmutableBytesWritable来自hbase数据的类型
         TableMapReduceUtil.initTableMapperJob( "word" , scan, MyHBaseMap03. class ,
                 ImmutableBytesWritable. class ,  ImmutableBytesWritable. class , job);
 
         createTable(conf);
         TableMapReduceUtil.initTableReducerJob( "word2" , MyHBaseReduce03. class , job);
 
 
         return job.waitForCompletion( true ) ? 0 : 1 ;
     }
 
     private static void createTable(Configuration conf) throws IOException{
         Connection con = ConnectionFactory.createConnection(conf);
         Admin admin = con.getAdmin();
         TableName tn = TableName.valueOf( "word2" );
         if (!admin.tableExists(tn)){
             HTableDescriptor htd = new HTableDescriptor(tn);
             HColumnDescriptor hcd = new HColumnDescriptor( "wordcount" );
             htd.addFamily(hcd);
             admin.createTable(htd);
             System.out.println( "表不存在,新创建表成功...." );
         }
     }
 
     public static class MyHBaseReduce03 extends TableReducer<immutablebyteswritable, immutablebyteswritable= "" >{
 
         @Override
         protected void reduce(ImmutableBytesWritable key, Iterable<immutablebyteswritable> values,
                 Reducer<immutablebyteswritable, mutation= "" >.Context context)
                         throws IOException, InterruptedException {
             // TODO Auto-generated method stub
 
             Put put= new Put(key.get());
 
             put.addColumn(Bytes.toBytes( "wordcount" ), Bytes.toBytes( "num" ), values.iterator().next().get());
 
             context.write( new ImmutableBytesWritable(key.get()), put);
         }
     }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值