How to iterate among text in the for loop and find count of a particular text in MapReduce()

醉酒当歌 提交于 2019-12-11 16:16:23

问题


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));
    }
}        

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)

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));
        }
    }        

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?


回答1:


Try replacing your for loop with this

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

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

Hope this is what you need



来源:https://stackoverflow.com/questions/34958593/how-to-iterate-among-text-in-the-for-loop-and-find-count-of-a-particular-text-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!