本文介绍了如何在for循环中的文本之间进行迭代并在MapReduce()中查找特定文本的计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,这是一个特定数据集上的Reduce()代码,该数据集具有很多键"名称和一个特定的已命名人员的工资值"

So here is a piece of Reduce() code on a particular dataset which has a bunch of designations as 'key' and the salary of designation of a particular named person as 'value'

public static class ReduceEmployee extends
        Reducer<Text, IntWritable, Text, IntWritable> 
{
public void reduce(Text key, Iterable<IntWritable> values,
                   Context context) throws IOException, InterruptedException {
        int sum = 0; 
        for (IntWritable val : values) {
            sum += val.get();  
        }
        context.write(key, new IntWritable(sum));
    }
}        

如果我正确理解的话,它的作用是,它具有一个公共键(一堆诸如Manager,Steward的列名,按列给出),以及一堆整数(薪水)作为按列给出的值,这些整数中的每一个都是迭代并加到0,以获取特定键的总薪水(将它们映射到相似的键之后)

What this does if I understand correctly is that, It has a common key (A bunch of designations like Manager, Steward given columnwise), and a bunch of integers (salaries) as the values given columnwise, each of these integers is iterated and added to 0 to get the total salary of a particular key (after mapping them into similar keys)

我得到了另一个数据集,其中有很多城市名称以'key'列的形式显示,并且以 text 格式的区域类型(不是像以前的数据集那样的整数工资)示例(住宅或木材等)按列给出

I was given another dataset, where there were a bunch of city names as 'key'columnwise and the type of area in text format (not integer salaries like the previous dataset) example (Residential or Wood etc) given columnwise

public static class ReduceEmployee extends
            Reducer<Text, Text, Text, IntWritable> {


        public void reduce(Text key, Iterable<Text> values,
                           Context context) throws IOException, InterruptedException {
            int count = 0; 
            Text r; 
            for (Text val : values) {
                r = val.get(); 
                if (r=="Residential")
                {
                count++;
                }
            }
            context.write(key, new IntWritable(count));
        }
    }        

我想在我的Reduce()中实现的是,我想逐列地在所有这些文本值之间进行迭代,并扫描每个文本并检查其是否读取为"Residential",如果这样,则增加计数.但是对于文本类型,方法get()是未定义的. (我认为我可以很聪明地用此数据集的文本替换int)显然,我在这些文本列之间进行迭代的知识很少.有人会帮我解决这个问题吗?

What I want to achieve in my Reduce() is that, I want to iterate among all those text values column by column, and scan each text and check if it reads as "Residential", if so increase the count. But the method get() is undefined for the type text. (I thought I could be clever and casually replace int with text for this data set) Obviously I have very less knowledge to iterate among those column of text. Would someone help me out and give me a solution on how I must go about this?

推荐答案

尝试用此替换您的for循环

Try replacing your for loop with this

for (Text val : values) {
   if (val.toString().equals("Residential")){
        count++;
   }
}

由于您的值为Text,因此需要使用equals使其与"Residential"匹配.和.get()未为Text定义.

As your value is Text you need to use equals for matching it with "Residential". and .get() is undefined for Text.

希望这就是您需要的

这篇关于如何在for循环中的文本之间进行迭代并在MapReduce()中查找特定文本的计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 20:07