帮我写一个用Mapreduce写一个将数据导入到Hbase中的代码
时间: 2024-05-24 13:13:43 浏览: 15
好的,以下是MapReduce导入数据到HBase的代码:
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
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.TableOutputFormat;
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;
public class HBaseImport {
static class HBaseImportMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, Put> {
private final byte[] FAMILY = "cf1".getBytes();
private ImmutableBytesWritable outkey = new ImmutableBytesWritable();
private Put outvalue = new Put();
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String[] words = value.toString().split(",");
outkey.set(words[0].getBytes());
outvalue.addColumn(FAMILY, "name".getBytes(), words[1].getBytes());
outvalue.addColumn(FAMILY, "email".getBytes(), words[2].getBytes());
context.write(outkey, outvalue);
}
}
public static void main(String[] args) throws Exception {
Configuration conf = HBaseConfiguration.create();
conf.set(TableOutputFormat.OUTPUT_TABLE, "mytable");
Job job = Job.getInstance(conf, "HBase Import");
job.setJarByClass(HBaseImport.class);
job.setMapperClass(HBaseImportMapper.class);
job.setOutputFormatClass(TableOutputFormat.class);
job.getConfiguration()
.set(TableOutputFormat.OUTPUT_TABLE, "mytable");
FileInputFormat.setInputPaths(job, new Path(args[0]));
job.setOutputKeyClass(ImmutableBytesWritable.class);
job.setOutputValueClass(Put.class);
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
希望对您有所帮助。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)