package com.bank.service;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
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 org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
/**
* 使用MapReduce批量导入Hbase(没有Reduce函数的MapReduce)
* @author mengyao
*
*/
public class DataImportToHbase extends Configured implements Tool {
static class DataImportToHbaseMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, Put> {
private static String familyName = "info";
private static String[] qualifiers = {"gzh", "currency", "version", "valuta", "qfTime", "flag", "machineID"};
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString();
String[] values = line.split("\t");
if (values.length == 7 && values.length == qualifiers.length) {
String row = values[0]+"_"+values[1]+"_"+values[2]+"_"+values[3];
long timestamp = System.currentTimeMillis();
ImmutableBytesWritable immutable = new ImmutableBytesWritable(Bytes.toBytes(row));
Put put = new Put(Bytes.toBytes(row));
for (int i = 0; i < values.length; i++) {
String qualifier = qualifiers[i];
String val = values[i];
put.add(Bytes.toBytes(familyName), Bytes.toBytes(qualifier), timestamp, Bytes.toBytes(val));
}
context.write(immutable, put);
} else {
System.err.println(" ERROR: value length must equale qualifier length ");
}
}
}
@Override
public int run(String[] arg0) throws Exception {
Job job = Job.getInstance(getConf(), DataImportToHbase.class.getSimpleName());
job.setJarByClass(DataImportToHbase.class);
job.setInputFormatClass(TextInputFormat.class);
FileInputFormat.setInputPaths(job, new Path(arg0[0]));
job.setMapperClass(DataImportToHbaseMapper.class);
job.setMapOutputKeyClass(ImmutableBytesWritable.class);
job.setMapOutputValueClass(Put.class);
TableMapReduceUtil.initTableReducerJob(arg0[1], null, job);
job.setNumReduceTasks(0);
TableMapReduceUtil.addDependencyJars(job);
return job.waitForCompletion(true) ? 0 : 1;
}
public static void main(String[] args) throws Exception {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "h5:2181,h6:2181,h7:2181");
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set("dfs.socket.timeout", "3600000");
String[] otherArgs = new GenericOptionsParser(args).getRemainingArgs();
if (otherArgs.length != 2) {
System.err.println(" ERROR: <dataInputDir> <tableName>");
System.exit(2);
}
int status = ToolRunner.run(conf, new DataImportToHbase(), otherArgs);
System.exit(status);
}
}
相关文章
- 使用sqoop1.99.6将mysql数据导入到hdfs
- 现有一使用ACCESS数据库的VB6程序,现在想将数据库改为SQL SERVER,如何将ACCESS数据库中的表和字段直接导入到SQL中
- 使用sqoop将数据从hdfs中导入mysql时,卡在INFO mapreduce.Job: map 100% reduce 0%的解决办法
- 使用 sqoop 将mysql数据导入到hdfs(import)
- 使用MySql的导入向导(odbc)将数据从sqlserver导入到MySql,导入数据与源数据不一致问题
- 使用sqoop将mysql数据导入到hdfs
- 现有一使用ACCESS数据库的VB6程序,现在想将数据库改为SQL SERVER,如何将ACCESS数据库中的表和字段直接导入到SQL中
- 使用 sqoop 将mysql数据导入到hdfs(import)
- 使用sqoop将数据从hdfs中导入mysql时,卡在INFO mapreduce.Job: map 100% reduce 0%的解决办法
- 使用Sqoop-1.4.4将Sqlserver2008中数据导入到Hbase上