变量不能传递给for循环 [英] Variable can not be passed into for loop

查看:77
本文介绍了变量不能传递给for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据帧,包括发送方(id,int),接收方(id,int),通信时间(int).

I have a data frame including sender (id, int), receiver(id, int), communication times (int).

A B C
1 5 10
1 6 20
1 7 20
1 8 11

我的目标是找到最大通信时间并返回1 6,20(格式为A B,C) 由于A1,B6和A1,B7的最大通讯时间均为20,因此我只需要保持最小的B id编号即可.

my goal is to find the max communication times and return as 1 6,20 (format as A B,C) Since A1, B6 and A1, B7 both have max communication times 20, I just need to keep the smallest B id number.

在映射步骤中,我已经将A分隔为键,将(B,C)分隔为值.

In map step, I already separated A as key, (B,C) as value.

到目前为止,我可以返回A和最大C的输出,但是我很难返回B值.我下面的代码无法更改min_Receiver,如何解决此问题?

So far I can return the output with A and max C, but I have trouble to return the B value. My code below cannot change the min_Receiver, how can I fix this issue?

public static class IntSumReducer
extends Reducer<Text,Text,Text,Text> {
    //private IntWritable result = new IntWritable();

    public void reduce(Text key, Iterable<Text> values,
                       Context context
                       ) throws IOException, InterruptedException {
        int max_val = 0;
    int val_str = 0;
    int val_str_1 = 0;
    int min_Receiver = Integer.MAX_VALUE;
    int tempReceiver = 0;
        for (Text val : values) {
    String[] compositeString = val.toString().split(",");
    val_str = Integer.parseInt(compositeString[1]);
    //tempReceiver = Integer.parseInt(compositeString[0]);
            if( val_str>max_val) {
                max_val = val_str;

    }


    }

   for (Text val_1 : values){
    String[] compositeString = val_1.toString().split(",");
    tempReceiver = Integer.parseInt(compositeString[0]);        
    val_str_1 = Integer.parseInt(compositeString[1]);

    if (val_str_1 == max_val && tempReceiver < min_Receiver)
        {
           min_Receiver =tempReceiver;
        }

    }

        //result.set(max_val);
        context.write(key, new Text(min_Receiver + "," + max_val));}}

预期输出为

1 6,20

实际输出是

1 2147483647,20

在地图中,我已经将A分隔为键,将B,C分隔为值.因此,compositeString包含两个变量.值的格式为B,C.

In the map, I already separated A as key, and B,C as value. So the compositeString includes two variables. The format in value is B,C.

推荐答案

取决于您的分度数

使用这样的一行以获取最长时间的Text

use a line like this to get the Text with the max time

Optional<Text> answer = StreamSupport.stream(values.spliterator(),false)  //all this does is get you a stream of Text
.max(Comparator.comparingInt(s->getComTime(s))); // return the object that evaluates to the max value if one exists

以及创建一个从这样的字符串/文本获取通信时间的方法:

along with creating a method that gets the Communication time from a string/Text like this:

private static int getComTime(Text line){
    String[] vals = line.toString().split(",");
    return  Integer.parseInt(vals[2]);
}

.stream()的布尔选项是,如果您要顺序= false或并行= true ....如果分隔符不同或对象稍有不同,则可能需要调整但这应该非常接近正确.

the boolean option for the .stream() is if you want sequential=false or parallel =true .... if your delimiter is different or the object is a little different you may need to adjust getComTime but this should be pretty close to right.

如果要以这种方式处理它,则获取一个Optional.

get an Optional if you want to handle it that way.

那么您就可以

if(answer.isPresent()){/* got an answer do something with it */}

***很抱歉,我大部分使用String而不是Text进行操作,但是此更新已更新,如果有任何问题,请通知我.

***Sry i did most of this with Strings instead of Text but this is updated let me know if there are any issues.

这篇关于变量不能传递给for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆