package com.it.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.mapreduce.TableOutputFormat;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Counter;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class HbaseMRBatchImport {
private static String tabName="logHTab";
private static String familyNm="cf";
public static void main(String[] args) {
try {
Configuration configuration = new Configuration();
configuration.set("hbase.rootdir","hdfs://master:9000/hbase");
configuration.set("hbase.zookeeper.quorum","master");
//将该值改大,防止hbase超时退出
configuration.set("dfs.socket.timeout", "180000");
//设置hbase表名称
configuration.set(TableOutputFormat.OUTPUT_TABLE, "wlan_log");
Job job = new Job(configuration,HbaseMRBatchImport.class.getSimpleName()); //
//JobConf entries = new JobConf(configuration, HbaseMRBatchImport.class);
job.setJarByClass(HbaseMRBatchImport.class);
FileInputFormat.setInputPaths(job, args[0]);
job.setInputFormatClass(TextInputFormat.class);
job.setMapperClass(HbaseMrBatchImportMapper.class);
job.setReducerClass(HbaseMrBatchImportReduce.class);
job.setMapOutputKeyClass(LongWritable.class);
job.setMapOutputValueClass(Text.class);
////不再设置输出路径,而是设置输出格式类型
job.setOutputFormatClass(TableOutputFormat.class);
//FileOutputFormat.setOutputPath(job,new Path(args[1]));
//job.waitForCompletion(true);
System.exit(job.waitForCompletion(true)?0:1);
} catch (Exception e) {
e.printStackTrace();
}
}
static class HbaseMrBatchImportMapper extends Mapper<LongWritable,Text,LongWritable,Text>{
SimpleDateFormat dateformat1=new SimpleDateFormat("yyyyMMddHHmmss");
Text v2 = new Text();
protected void map(LongWritable key, Text value, Context context) throws java.io.IOException ,InterruptedException {
final String[] splited = value.toString().split(" ");
try {
final Date date = new Date(Long.parseLong(splited[0].trim()));
final String dateFormat = dateformat1.format(date);
String rowKey = splited[1]+":"+dateFormat;
v2.set(rowKey+"\t"+value.toString());
context.write(key, v2);
} catch (NumberFormatException e) {
final Counter counter = context.getCounter("BatchImport", "ErrorFormat");
counter.increment(1L);
System.out.println("出错了"+splited[0]+" "+e.getMessage());
}
};
}
static class HbaseMrBatchImportReduce extends TableReducer<LongWritable,Text,NullWritable> {
protected void reduce(LongWritable key, Iterable<Text> values, Context context) throws java.io.IOException ,InterruptedException {
for (Text text : values) {
String[] splited = text.toString().split("\t");
Put put = new Put(Bytes.toBytes(splited[0]));
put.addColumn(familyNm.getBytes(),"a".getBytes(),splited[1].getBytes());
context.write(NullWritable.get(), put);
}
};
}
}
ERROR:2017-05-15 12:15:44,224 INFO [main] org.apache.hadoop.service.AbstractService: Service org.apache.hadoop.mapreduce.v2.app.MRAppMaster failed in state INITED; cause: org.apache.hadoop.yarn.exceptions.YarnRuntimeException: java.lang.RuntimeException: java.lang.ClassNotFoundException: Class org.apache.hadoop.hbase.mapreduce.TableOutputFormat not foundorg.apache.hadoop.yarn.exceptions.YarnRuntimeException: java.lang.RuntimeException: java.lang.ClassNotFoundException: Class org.apache.hadoop.hbase.mapreduce.TableOutputFormat not found at org.apache.hadoop.mapreduce.v2.app.MRAppMaster$2.call(MRAppMaster.java:518) at org.apache.hadoop.mapreduce.v2.app.MRAppMaster$2.call(MRAppMaster.java:498) at org.apache.hadoop.mapreduce.v2.app.MRAppMaster.callWithJobClassLoader(MRAppMaster.java:1593) at org.apache.hadoop.mapreduce.v2.app.MRAppMaster.createOutputCommitter(MRAppMaster.java:498) at org.apache.hadoop.mapreduce.v2.app.MRAppMaster.serviceInit(MRAppMaster.java:284) at org.apache.hadoop.service.AbstractService.init(AbstractService.java:163) at org.apache.hadoop.mapreduce.v2.app.MRAppMaster$5.run(MRAppMaster.java:1551) at java.security.AccessController.doPrivileged(Native Method) at javax.security.auth.Subject.doAs(Subject.java:422) at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1698) at org.apache.hadoop.mapreduce.v2.app.MRAppMaster.initAndStartAppMaster(MRAppMaster.java:1548) at org.apache.hadoop.mapreduce.v2.app.MRAppMaster.main(MRAppMaster.java:1481)Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: Class org.apache.hadoop.hbase.mapreduce.TableOutputFormat not found at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:2195) at org.apache.hadoop.mapreduce.task.JobContextImpl.getOutputFormatClass(JobContextImpl.java:222) at org.apache.hadoop.mapreduce.v2.app.MRAppMaster$2.call(MRAppMaster.java:514) ... 11 moreCaused by: java.lang.ClassNotFoundException: Class org.apache.hadoop.hbase.mapreduce.TableOutputFormat not found at org.apache.hadoop.conf.Configuration.getClassByName(Configuration.java:2101) at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:2193) ... 13 more
问:这是什么原因导致的? hbase的jar包都有加载,我的hadoop 版本: 2.7.3 , hbase 版本: 1.2.4 。我按照网上的一些建议执行还是不可以。
|
|