mapReduce求文本偏移量和每行数据的和

mapReduce求文本偏移量和每行数据的和

一.文本
在这里插入图片描述
二.建三个java类
Map Count Reduce
在这里插入图片描述
三.写Map

***1、实例一个实体类,继承Mapper<输入放入key的类型,输入的value的类型,输出放入key的类型,输出的value的类型>***
public class WordCountMap extends Mapper<LongWritable,Text,LongWritable,LongWritable> {
***2、重写map(LongWritablekey,Textvalue,Contextcontext)
key每行行首的偏移量 
value每一行的数据 
context上下文对象*** 
@Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
***3、实现自己业务逻辑的代码
将数据进行拆分,并进行逐一输出。***
        //转化value
        String datas = value.toString();
        //截取
        String[] split = datas.split(" ");
        //遍历
        for (String data : split) {
            if ("".equals(data.trim())){
                continue;
            }
            context.write(key,new LongWritable(Long.parseLong(data.trim())));
        } }}

在这里插入图片描述
四.写Reduce

***1、实例一个实体类,继承Reducer<输入放入key的类型,输入的value的类型,输出放入key的类型,输出的value的类型>***

public class WordCountReduce extends Reducer<LongWritable,LongWritable,Text,LongWritable> {
    ***2、重写reduce(Textkey,Iterablevalues,Contextcontext)
key:每一个唯一的数据(游戏中的图形)
values:每一个图形,1的标记(1的list)
context:上下文对象***
    @Override
    protected void reduce(LongWritable key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
        long sum=0;
        for (LongWritable value : values) {
            sum+=value.get();
        }
        context.write(new Text("偏移量"+key),new LongWritable(sum));
    }
}

在这里插入图片描述
五.编写drive

public class WordCountDrive  extends Configured implements Tool {

    @Override
    public int run(String[] strings) throws Exception {
        Configuration conf=new Configuration();
    ***1、实例一个job***
        Job job = Job.getInstance(conf, "Sam02");
        FileSystem fileSystem = FileSystem.get(conf);




       ***2、使用job设置读物数据(包括数据的路径)***
        job.setInputFormatClass(TextInputFormat.class);
        TextInputFormat.addInputPath(job,new Path("C:\\SumData.txt"));




       ***3.使用job设置数据的输出路径***
        job.setOutputFormatClass(TextOutputFormat.class);
        TextOutputFormat.setOutputPath(job,new Path("D:\\cs\\ddd01"));

        ***4、使用job设置MAP类(map输入和输出的类型)***
        job.setMapperClass(WordCountMap.class);
        job.setMapOutputValueClass(LongWritable.class);
        job.setMapOutputKeyClass(LongWritable.class);
       ***5 、使用job设置Reduce类(Reduce输入和输出的类型)***
        job.setReducerClass(WordCountReduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);
        ***6.返回执行状态编号***
        return job.waitForCompletion(true)? 0 : 1 ;
    }

    public static void main(String[] args) throws Exception {
        ToolRunner.run(new WordCountDrive(),args);
    }
}

在这里插入图片描述

六.打开在这里插入图片描述
1.见到如下画面说明成功
在这里插入图片描述
七.注意
1.输入和输出路径请自行更改
2.输出路径不能覆盖,如果覆盖就会报错

由于MapReduce框架是用于分布式处理数据的,因此对于分批查询数据库处理大批量数据,我们可以通过以下步骤来实现: 1. 将大批量数据按照一定的规则划分为多个小批量数据,每个小批量数据都可以作为一个Map任务的输入。 2. 在Map任务中,我们可以通过数据库连接池来获取数据库连接,并使用分页查询的方式从数据库中获取每个小批量数据数据。 3. 对于每个小批量数据,我们可以将其作为一个键值对传递给Reduce任务进行处理,其中键为某个唯一标识符,值为小批量数据。 4. 在Reduce任务中,我们可以将所有相同键的小批量数据合并起来进行处理,最终得到处理结果。 以下是一个简单的MapReduce代码示例,用于分批查询数据库处理大批量数据: Map任务: ```java public class MyMapper extends Mapper<LongWritable, Text, Text, Text> { private DataSource dataSource; private Connection connection; private PreparedStatement statement; @Override protected void setup(Context context) throws IOException, InterruptedException { try { dataSource = new DataSource(); connection = dataSource.getConnection(); statement = connection.prepareStatement("SELECT * FROM data LIMIT ?, ?"); } catch (SQLException e) { e.printStackTrace(); } } @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String[] params = value.toString().split(","); int offset = Integer.parseInt(params[0]); int limit = Integer.parseInt(params[1]); try { statement.setInt(1, offset); statement.setInt(2, limit); ResultSet resultSet = statement.executeQuery(); while (resultSet.next()) { String id = resultSet.getString("id"); String data = resultSet.getString("data"); context.write(new Text(id), new Text(data)); } } catch (SQLException e) { e.printStackTrace(); } } @Override protected void cleanup(Context context) throws IOException, InterruptedException { try { statement.close(); connection.close(); dataSource.close(); } catch (SQLException e) { e.printStackTrace(); } } } ``` Reduce任务: ```java public class MyReducer extends Reducer<Text, Text, Text, Text> { @Override protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException { // 处理相同键的小批量数据 // ... context.write(key, new Text(result)); } } ``` Driver程序: ```java public class MyDriver { public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "my job"); job.setJarByClass(MyDriver.class); job.setMapperClass(MyMapper.class); job.setReducerClass(MyReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class); job.setInputFormatClass(TextInputFormat.class); job.setOutputFormatClass(TextOutputFormat.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } } ``` 在上面的代码中,我们使用了TextInputFormat作为输入格式,将每个小批量数据偏移量和限制数量以文本格式输入到Map任务中。Reduce任务的输出格式也是文本格式。根据具体情况,我们可以选择其他的输入输出格式或者自定义格式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值