hbase使用mapreduce操作4(实现将hdfs中的数据写入到hbase表中)(代码片段)

pursue339 pursue339     2023-03-11     434

关键词:

实现将 HDFS 中的数据写入到 HBase 表中

Runner类

 1 package com.yjsj.hbase_mr2;
 2 
 3 import com.yjsj.hbase_mr2.ReadFruitFromHDFSMapper;
 4 import com.yjsj.hbase_mr2.WriteFruitMRFromTxtReducer;
 5 import org.apache.hadoop.conf.Configuration;
 6 import org.apache.hadoop.conf.Configured;
 7 import org.apache.hadoop.fs.Path;
 8 import org.apache.hadoop.hbase.HBaseConfiguration;
 9 import org.apache.hadoop.hbase.client.Put;
10 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
11 import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
12 import org.apache.hadoop.mapreduce.Job;
13 import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
14 import org.apache.hadoop.util.Tool;
15 import org.apache.hadoop.util.ToolRunner;
16 
17 import java.io.IOException;
18 
19 class Txt2FruitRunner extends Configured implements Tool 
20     public int run(String[] args) throws Exception 
21 //得到 Configuration
22         Configuration conf = this.getConf();
23 //创建 Job 任务
24         Job job = Job.getInstance(conf, this.getClass().getSimpleName());
25         job.setJarByClass(Txt2FruitRunner.class);
26         Path inPath = new Path("hdfs://master:9000/input_fruit/fruit.tsv");
27 
28         FileInputFormat.addInputPath(job, inPath);
29         //设置 Mapper
30         job.setMapperClass(ReadFruitFromHDFSMapper.class);
31         job.setMapOutputKeyClass(ImmutableBytesWritable.class);
32         job.setMapOutputValueClass(Put.class);
33         //设置 Reducer
34         TableMapReduceUtil.initTableReducerJob("fruit_hdfs", WriteFruitMRFromTxtReducer.class, job);
35         //设置 Reduce 数量,最少 1 个
36         job.setNumReduceTasks(1);
37         boolean isSuccess = job.waitForCompletion(true);
38         if (!isSuccess) 
39             throw new IOException("Job running with error");
40         
41         return isSuccess ? 0 : 1;
42     
43 
44     public static void main(String[] args) throws Exception 
45         Configuration conf = HBaseConfiguration.create();
46         conf = HBaseConfiguration.create();
47         conf.set("hbase.zookeeper.quorum", "master,node1,node2");
48         conf.set("hbase.zookeeper.property.clientPort", "2181");
49         conf.set("hbase.master", "master:60000");
50         int status = ToolRunner.run(conf, new Txt2FruitRunner(), args);
51         System.exit(status);
52     
53 

Mapper类

 

 1 package com.yjsj.hbase_mr2;
 2 
 3 import java.io.IOException;
 4 
 5 import org.apache.hadoop.hbase.client.Put;
 6 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
 7 import org.apache.hadoop.hbase.util.Bytes;
 8 import org.apache.hadoop.io.LongWritable;
 9 import org.apache.hadoop.io.Text;
10 import org.apache.hadoop.mapreduce.Mapper;
11 
12 public class ReadFruitFromHDFSMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, Put> 
13     @Override
14     protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException 
15         //从 HDFS 中读取的数据
16         String lineValue = value.toString();
17         //读取出来的每行数据使用	 进行分割,存于 String 数组
18         String[] values = lineValue.split("	");
19         //根据数据中值的含义取值
20         String rowKey = values[0];
21         String name = values[1];
22         String color = values[2];
23         //初始化 rowKey
24         ImmutableBytesWritable rowKeyWritable = new ImmutableBytesWritable(Bytes.toBytes(rowKey));
25         //初始化 put 对象
26         Put put = new Put(Bytes.toBytes(rowKey));
27         //参数分别:列族、列、值
28         put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes(name));
29         put.add(Bytes.toBytes("info"), Bytes.toBytes("color"), Bytes.toBytes(color));
30         context.write(rowKeyWritable, put);
31     
32 

 

 

 

Reduce类

 

package com.yjsj.hbase_mr2;

import java.io.IOException;

import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.io.NullWritable;

public class WriteFruitMRFromTxtReducer extends TableReducer<ImmutableBytesWritable, Put, NullWritable> 
    @Override
    protected void reduce(ImmutableBytesWritable key, Iterable<Put> values, Context context) throws IOException, InterruptedException 
        //读出来的每一行数据写入到 fruit_hdfs 表中
        for (Put put : values) 
            context.write(NullWritable.get(), put);
        
    

 

hbase集成mapreduce(代码片段)

...接存储到HBase表中。参考地址:http://hbase.apache.org/book.html#mapreduce1实现方式一读取HBase当中某张表的数据,将数据写入到另外一张表的列族里面去2实现方式二读取HDFS上面的数据,写入到 查看详情

hbase与mapreduce集成(代码片段)

...出。通过HBase的相关JavaApi,我们可以实现伴随HBase操作的MapReduce过程,比如使用MapReduce将数据从本地文件导入HBase的表中,或我们从HBase的表中读取一些原始数据用于MapReduce做数据分析。案例将fruit文件的一部分数据,通过MapReduce... 查看详情

使用 MapReduce 程序将值发送到 HBase 表时出错

】使用MapReduce程序将值发送到HBase表时出错【英文标题】:ErrorinsendingthevaluetoHBasetablebyusingMapReduceprogram【发布时间】:2016-12-2306:27:20【问题描述】:我编写了一个mapreduce程序,我需要从特定列族的HBase表中读取数据。例如,HBase表... 查看详情

Hbase mapreduce 交互

】Hbasemapreduce交互【英文标题】:Hbasemapreduceinteraction【发布时间】:2012-11-0907:39:24【问题描述】:我有一个程序hbase和mapreduce。我将数据存储在HDFS中,这个文件的大小是:100G。现在我将这些数据放到Hbase中。我使用mapreduce扫描这... 查看详情

使用 HBase 表作为 MapReduce 源

】使用HBase表作为MapReduce源【英文标题】:UsinganHBasetableasMapReducesource【发布时间】:2015-04-2419:56:13【问题描述】:据我了解,当使用hbase表作为mapreduce作业的源时,我们已经定义了扫描的值。假设我们将其设置为500,这是否意味... 查看详情

无法从 MapReduce 代码访问 HBase

】无法从MapReduce代码访问HBase【英文标题】:UnabletoaccessHBasefromMapReducecode【发布时间】:2015-05-2520:55:47【问题描述】:我正在尝试使用HDFS文件作为源和HBase作为接收器。我的Hadoop集群具有以下规格:master192.168.4.65slave1192.168.4.176sla... 查看详情

mapreduce操作hbase--table2file

官方手册:http://hbase.apache.org/book.html#mapreduce.example 简单的操作,将hbase表中的数据写入到文件中。 RunJob源码:1importorg.apache.hadoop.conf.Configuration;2importorg.apache.hadoop.fs.FileSystem;3importorg.apach 查看详情

MAPREDUCE - 将数据批量加载到 HBASE 表中

】MAPREDUCE-将数据批量加载到HBASE表中【英文标题】:MAPREDUCE-BULKLOADINGDATAINTOHBASETABLE【发布时间】:2017-01-1416:04:26【问题描述】:为什么我们只使用一个驱动类和一个映射器类而不使用减速器类?【问题讨论】:【参考方案1】:... 查看详情

基于hbase的mapreduce实现大量邮件信息统计分析

一:概述在大多数情况下,如果使用MapReduce进行batch处理,文件一般是存储在HDFS上的,但这里有个很重要的场景不能忽视,那就是对于大量的小文件的处理(此处小文件没有确切的定义,一般指文件大小比较小,比如5M以内的文... 查看详情

熟悉常用的hbase操作,编写mapreduce作业

1.以下关系型数据库中的表和数据,要求将其转换为适合于HBase存储的表并插入数据:学生表(Student)(不包括最后一列)学号(S_No)姓名(S_Name)性别(S_Sex)年龄(S_Age)课程(course)2015001Zhangsanmale23 2015003Maryfemale22 2... 查看详情

hbase聚合操作

...ocol机制. CoprocessorProtocol的原理比较简单,近似于一个mapreduce框架。由client将scan分解为面向多个region的请求,并行发送请求到多个region,然后client做一个reduce的操作,得到最后的结果。 先看一个例子,使用hbase的Agg 查看详情

熟悉常用的hbase操作,编写mapreduce作业

1.以下关系型数据库中的表和数据,要求将其转换为适合于HBase存储的表并插入数据:学生表(Student)(不包括最后一列)学号(S_No)姓名(S_Name)性别(S_Sex)年龄(S_Age)课程(course)2015001Zhangsanmale23 2015003Maryfemale22 2... 查看详情

31-hadoop-hbase-mapreduce操作hbase

有一些大的文件,需要存入HBase中,其思想是先把文件传到HDFS上,利用map阶段读取<key,value>对,可在reduce把这些键值对上传到HBase中。HbaseMapper:packagecom.wenbronk.hbase.hbase;importorg.apache.hadoop.io.LongWritable;importorg.apache.hadoop.io.Te 查看详情

熟悉常用的hbase操作,编写mapreduce作业(代码片段)

1.以下关系型数据库中的表和数据,要求将其转换为适合于HBase存储的表并插入数据: 学号(S_No)姓名(S_Name)性别(S_Sex)年龄(S_Age)课程(course)2015001Zhangsanmale23 2015003Maryfemale22 2015003Lisimale24数学(Math)85 &nb... 查看详情

熟悉常用的hbase操作,编写mapreduce作业(代码片段)

1.以下关系型数据库中的表和数据,要求将其转换为适合于HBase存储的表并插入数据:学生表(Student)(不包括最后一列)学号(S_No)姓名(S_Name)性别(S_Sex)年龄(S_Age)课程(course)2015001Zhangsanmale23 2015003Maryfemale22 2... 查看详情

熟悉常用的hbase操作,编写mapreduce作业(代码片段)

1.以下关系型数据库中的表和数据,要求将其转换为适合于HBase存储的表并插入数据:学生表(Student)(不包括最后一列)学号(S_No)姓名(S_Name)性别(S_Sex)年龄(S_Age)课程(course)2015001Zhangsanmale23 2015003Maryfemale22 2... 查看详情

熟悉常用的hbase操作,编写mapreduce作业(代码片段)

1.以下关系型数据库中的表和数据,要求将其转换为适合于HBase存储的表并插入数据:学生表(Student)(不包括最后一列)学号(S_No)姓名(S_Name)性别(S_Sex)年龄(S_Age)课程(course)2015001Wangwumale23 2015003Maryfemale22 201... 查看详情

对给定的数据利用mapreduce编程实现数据的清洗和预处理,编程实现数据存储到hbase数据库,实现数据的增删改查操作接口(代码片段)

对给定的数据利用MapReduce编程实现数据的清洗和预处理,编程实现数据存储到HBase数据库,实现数据的增删改查操作接口,同时对MapReduce处理好的数据利用Hive实现数据的基本统计。设计要求:根据数据特征,... 查看详情