MapReduce操作HBase错误一例

数据库 大数据
操作HBase时经常会遇到MasterNotRunningException的错误,那我们该如何处理这样的错误呢?

运行HBase时常会遇到个错误,我就有这样的经历。

ERROR: org.apache.hadoop.hbase.MasterNotRunningException: Retried 7 times

检查日志:org.apache.hadoop.ipc.RPC$VersionMismatch: Protocol org.apache.hadoop.hdfs.protocol.ClientProtocol version mismatch. (client = 42, server = 41)

如果是这个错误,说明RPC协议不一致所造成的,解决方法:将hbase/lib目录下的hadoop-core的jar文件删除,将hadoop目录下的hadoop-0.20.2-core.jar拷贝到hbase/lib下面,然后重新启动hbase即可。第二种错误是:没有启动hadoop,先启用hadoop,再启用hbase。

在Eclipse开发中,需要加入hadoop所有的jar包以及HBase二个jar包(hbase,zooKooper)。

HBase基础可见帖子:http://www.cnblogs.com/liqizhou/archive/2012/05/14/2499112.html

建表,通过HBaseAdmin类中的create静态方法来创建表。

HTable类是操作表,例如,静态方法put可以插入数据,该类初始化时可以传递一个行键,静态方法getScanner()可以获得某一列上的所有数据,返回Result类,Result类中有个静态方法getFamilyMap()可以获得以列名为key,值为value,这刚好与hadoop中map结果是一样的。

  1. package test;  
  2. import java.io.IOException;  
  3. import java.util.Map;  
  4. import org.apache.hadoop.conf.Configuration;  
  5. import org.apache.hadoop.hbase.HBaseConfiguration;  
  6. import org.apache.hadoop.hbase.HColumnDescriptor;  
  7. import org.apache.hadoop.hbase.HTableDescriptor;  
  8. import org.apache.hadoop.hbase.client.HBaseAdmin;  
  9. import org.apache.hadoop.hbase.client.HTable;  
  10. import org.apache.hadoop.hbase.client.Put;  
  11. import org.apache.hadoop.hbase.client.Result;  
  12.  
  13. public class Htable {  
  14.  
  15.     /**  
  16.      * @param args  
  17.      */ 
  18.     public static void main(String[] args) throws IOException {  
  19.         // TODO Auto-generated method stub  
  20.         Configuration hbaseConf = HBaseConfiguration.create();  
  21.         HBaseAdmin admin = new HBaseAdmin(hbaseConf);  
  22.         HTableDescriptor htableDescriptor = new HTableDescriptor("table" 
  23.                 .getBytes());  //set the name of table  
  24.         htableDescriptor.addFamily(new HColumnDescriptor("fam1")); //set the name of column clusters  
  25.         admin.createTable(htableDescriptor); //create a table   
  26.         HTable table = new HTable(hbaseConf, "table"); //get instance of table.  
  27.         for (int i = 0; i < 3; i++) {   //for is number of rows  
  28.             Put putRow = new Put(("row" + i).getBytes()); //the ith row  
  29.             putRow.add("fam1".getBytes(), "col1".getBytes(), "vaule1" 
  30.                     .getBytes());  //set the name of column and value.  
  31.             putRow.add("fam1".getBytes(), "col2".getBytes(), "vaule2" 
  32.                     .getBytes());  
  33.             putRow.add("fam1".getBytes(), "col3".getBytes(), "vaule3" 
  34.                     .getBytes());  
  35.             table.put(putRow);  
  36.         }  
  37.         for(Result result: table.getScanner("fam1".getBytes())){//get data of column clusters   
  38.             for(Map.Entry<byte[], byte[]> entry : result.getFamilyMap("fam1".getBytes()).entrySet()){//get collection of result  
  39.                 String column = new String(entry.getKey());  
  40.                 String value = new String(entry.getValue());  
  41.                 System.out.println(column+","+value);  
  42.             }  
  43.         }  
  44.         admin.disableTable("table".getBytes()); //disable the table  
  45.         admin.deleteTable("table".getBytes());  //drop the tbale  
  46.     }  

以上代码不难看懂。

下面介绍一下,用mapreduce怎样操作HBase,主要对HBase中的数据进行读取。

现在有一些大的文件,需要存入HBase中,其思想是先把文件传到HDFS上,利用map阶段读取<key,value>对,可在reduce把这些键值对上传到HBase中。

  1. package test;  
  2. import java.io.IOException;  
  3. import java.util.Map;  
  4. import org.apache.hadoop.conf.Configuration;  
  5. import org.apache.hadoop.hbase.HBaseConfiguration;  
  6. import org.apache.hadoop.hbase.HColumnDescriptor;  
  7. import org.apache.hadoop.hbase.HTableDescriptor;  
  8. import org.apache.hadoop.hbase.client.HBaseAdmin;  
  9. import org.apache.hadoop.hbase.client.HTable;  
  10. import org.apache.hadoop.hbase.client.Put;  
  11. import org.apache.hadoop.hbase.client.Result;  
  12.  
  13. public class Htable {  
  14.  
  15.     /**  
  16.      * @param args  
  17.      */ 
  18.     public static void main(String[] args) throws IOException {  
  19.         // TODO Auto-generated method stub  
  20.         Configuration hbaseConf = HBaseConfiguration.create();  
  21.         HBaseAdmin admin = new HBaseAdmin(hbaseConf);  
  22.         HTableDescriptor htableDescriptor = new HTableDescriptor("table" 
  23.                 .getBytes());  //set the name of table  
  24.         htableDescriptor.addFamily(new HColumnDescriptor("fam1")); //set the name of column clusters  
  25.         admin.createTable(htableDescriptor); //create a table   
  26.         HTable table = new HTable(hbaseConf, "table"); //get instance of table.  
  27.         for (int i = 0; i < 3; i++) {   //for is number of rows  
  28.             Put putRow = new Put(("row" + i).getBytes()); //the ith row  
  29.             putRow.add("fam1".getBytes(), "col1".getBytes(), "vaule1" 
  30.                     .getBytes());  //set the name of column and value.  
  31.             putRow.add("fam1".getBytes(), "col2".getBytes(), "vaule2" 
  32.                     .getBytes());  
  33.             putRow.add("fam1".getBytes(), "col3".getBytes(), "vaule3" 
  34.                     .getBytes());  
  35.             table.put(putRow);  
  36.         }  
  37.         for(Result result: table.getScanner("fam1".getBytes())){//get data of column clusters   
  38.             for(Map.Entry<byte[], byte[]> entry : result.getFamilyMap("fam1".getBytes()).entrySet()){//get collection of result  
  39.                 String column = new String(entry.getKey());  
  40.                 String value = new String(entry.getValue());  
  41.                 System.out.println(column+","+value);  
  42.             }  
  43.         }  
  44.         admin.disableTable("table".getBytes()); //disable the table  
  45.         admin.deleteTable("table".getBytes());  //drop the tbale  
  46.     }  

Reduce类,主要是将键值传到HBase表中

  1. package test;  
  2.  
  3. import java.io.IOException;  
  4. import org.apache.hadoop.hbase.client.Put;  
  5. import org.apache.hadoop.hbase.io.ImmutableBytesWritable;  
  6. import org.apache.hadoop.hbase.mapreduce.TableReducer;  
  7. import org.apache.hadoop.io.Text;  
  8.  
  9. public class ReducerClass extends TableReducer<Text,Text,ImmutableBytesWritable>{  
  10.     public void reduce(Text key,Iterable<Text> values,Context context){  
  11.         String k = key.toString();  
  12.         StringBuffer str=null;  
  13.         for(Text value: values){  
  14.             str.append(value.toString());  
  15.         }  
  16.         String v = new String(str);   
  17.         Put putrow = new Put(k.getBytes());  
  18.         putrow.add("fam1".getBytes(), "name".getBytes(), v.getBytes());       
  19.     }  

由上面可知ReducerClass继承TableReduce,在hadoop里面ReducerClass继承Reducer类。它的原型为:TableReducer<KeyIn,Values,KeyOut>可以看出,HBase里面是读出的Key类型是ImmutableBytesWritable。

Map,Reduce,以及Job的配置分离,比较清晰,mahout也是采用这种构架。

  1. package test;  
  2.  
  3. import org.apache.hadoop.conf.Configuration;  
  4. import org.apache.hadoop.conf.Configured;  
  5. import org.apache.hadoop.fs.Path;  
  6. import org.apache.hadoop.hbase.HBaseConfiguration;  
  7. import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;  
  8. import org.apache.hadoop.io.Text;  
  9. import org.apache.hadoop.mapreduce.Job;  
  10. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  11. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;  
  12. import org.apache.hadoop.util.Tool;  
  13.  
  14. public class Driver extends Configured implements Tool{  
  15.  
  16.     @Override 
  17.     public static void run(String[] arg0) throws Exception {  
  18.         // TODO Auto-generated method stub  
  19.         Configuration conf = HBaseConfiguration.create();  
  20.         conf.set("hbase.zookeeper.quorum.""localhost");    
  21.           
  22.         Job job = new Job(conf,"Hbase");  
  23.         job.setJarByClass(TxtHbase.class);  
  24.           
  25.         Path in = new Path(arg0[0]);  
  26.           
  27.         job.setInputFormatClass(TextInputFormat.class);  
  28.         FileInputFormat.addInputPath(job, in);  
  29.           
  30.         job.setMapperClass(MapperClass.class);  
  31.         job.setMapOutputKeyClass(Text.class);  
  32.         job.setMapOutputValueClass(Text.class);  
  33.           
  34.         TableMapReduceUtil.initTableReducerJob("table", ReducerClass.class, job);  
  35.           
  36.        job.waitForCompletion(true);  
  37.     }  
  38.       

Driver中job配置的时候没有设置 job.setReduceClass(); 而是用 TableMapReduceUtil.initTableReducerJob("tab1", THReducer.class, job); 来执行reduce类。

主函数

  1. package test;  
  2.  
  3. import org.apache.hadoop.conf.Configuration;  
  4. import org.apache.hadoop.util.ToolRunner;  
  5.  
  6. public class TxtHbase {  
  7.     public static void main(String [] args) throws Exception{        Driver.run(new Configuration(),new THDriver(),args);     } } 

读取数据时比较简单,编写Mapper函数,读取<key,value>值就行了。

  1. package test;  
  2.  
  3. import java.io.IOException;  
  4. import org.apache.hadoop.conf.Configuration;  
  5. import org.apache.hadoop.hbase.client.Result;  
  6. import org.apache.hadoop.hbase.io.ImmutableBytesWritable;  
  7. import org.apache.hadoop.hbase.mapred.TableMap;  
  8. import org.apache.hadoop.io.Text;  
  9. import org.apache.hadoop.mapred.MapReduceBase;  
  10. import org.apache.hadoop.mapred.OutputCollector;  
  11. import org.apache.hadoop.mapred.Reporter;  
  12.  
  13. public class MapperClass extends MapReduceBase implements 
  14.         TableMap<Text, Text> {  
  15.     static final String NAME = "GetDataFromHbaseTest";  
  16.     private Configuration conf;  
  17.  
  18.     public void map(ImmutableBytesWritable row, Result values,  
  19.             OutputCollector<Text, Text> output, Reporter reporter)  
  20.             throws IOException {  
  21.         StringBuilder sb = new StringBuilder();  
  22.         for (Entry<byte[], byte[]> value : values.getFamilyMap(  
  23.                 "fam1".getBytes()).entrySet()) {  
  24.             String cell = value.getValue().toString();  
  25.             if (cell != null) {  
  26.                 sb.append(new String(value.getKey())).append(new String(cell));  
  27.             }  
  28.         }  
  29.         output.collect(new Text(row.get()), new Text(sb.toString()));  
  30.     } 

要实现这个方法 initTableMapJob(String table, String columns, Class<? extends TableMap> mapper, Class<? extends org.apache.hadoop.io.WritableComparable> outputKeyClass, Class<? extends org.apache.hadoop.io.Writable> outputValueClass, org.apache.hadoop.mapred.JobConf job, boolean addDependencyJars)。

  1. package test;  
  2.  
  3. import org.apache.hadoop.conf.Configuration;  
  4. import org.apache.hadoop.conf.Configured;  
  5. import org.apache.hadoop.fs.Path;  
  6. import org.apache.hadoop.hbase.HBaseConfiguration;  
  7. import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;  
  8. import org.apache.hadoop.io.Text;  
  9. import org.apache.hadoop.mapreduce.Job;  
  10. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  11. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;  
  12. import org.apache.hadoop.util.Tool;  
  13.  
  14. public class Driver extends Configured implements Tool{  
  15.  
  16.     @Override 
  17.     public static void run(String[] arg0) throws Exception {  
  18.         // TODO Auto-generated method stub  
  19.         Configuration conf = HBaseConfiguration.create();  
  20.         conf.set("hbase.zookeeper.quorum.""localhost");    
  21.         Job job = new Job(conf,"Hbase");  
  22.         job.setJarByClass(TxtHbase.class);  
  23.         job.setInputFormatClass(TextInputFormat.class);  
  24.         job.setMapOutputKeyClass(Text.class);  
  25.         job.setMapOutputValueClass(Text.class);  
  26.         TableMapReduceUtilinitTableMapperJob("table", args0[0],MapperClass.class, job);         job.waitForCompletion(true); } } 

主函数

  1. package test;  
  2.  
  3. import org.apache.hadoop.conf.Configuration;  
  4. import org.apache.hadoop.util.ToolRunner;  
  5.  
  6. public class TxtHbase {  
  7.     public static void main(String [] args) throws Exception{  
  8.  
  9.         Driver.run(new Configuration(),new THDriver(),args);  
  10.  
  11.     }   
  12. }  
  13. -------------------------------------------------------------------------------- 

原文链接:http://www.cnblogs.com/liqizhou/archive/2012/05/17/2504279.html

【编辑推荐】

  1. 数据库迁移之何去何从
  2. SQL Server数据库迁移偏方
  3. SQL Server数据库恢复案例分享
  4. SQL Server数据库最小宕机迁移方案
  5. 给你大型数据库迁移的五大建议  
     
责任编辑:彭凡 来源: 博客园
相关推荐

2014-11-19 09:22:48

云计算Dockerpython API

2013-11-12 14:43:43

MySQL数据库

2012-09-24 01:01:49

NginxNginx性能Web服务器

2009-07-16 13:03:05

ibatis resu

2009-12-01 09:15:30

Windows 7系统激活

2009-08-07 09:57:38

2009-09-17 16:38:02

WSUS服务器

2017-01-16 15:43:54

存储虚拟化控制器

2021-03-08 06:29:53

微信僵尸粉移动应用

2013-10-15 09:48:03

C++Lambda函数式编程

2021-07-06 12:07:27

Go 服务性能

2009-08-06 11:37:24

虚拟机NAT连接物理网络

2020-11-10 13:42:07

Go编译器修复

2022-12-26 09:05:35

2017-12-08 11:28:48

Hbase木桶效应操作

2019-09-09 08:30:57

MYSQL代码数据库

2011-03-01 09:43:13

MapReduce架构

2010-06-03 13:55:38

Hbase和Hadoo

2018-11-14 14:33:33

MapReduce数据集计算

2017-05-25 10:58:08

HBase数据库操作系统
点赞
收藏

51CTO技术栈公众号