Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法将变量传递到for循环中_Java_Sorting_Hadoop_Max_Min - Fatal编程技术网

Java 无法将变量传递到for循环中

Java 无法将变量传递到for循环中,java,sorting,hadoop,max,min,Java,Sorting,Hadoop,Max,Min,我有一个数据帧,包括发送方(id,int)、接收方(id,int)、通信时间(int) 我的目标是找到最大通信时间并返回16,20(格式为A B,C) 由于A1,B6和A1,B7都有最大通信时间20,我只需要保留最小的B id号 在映射步骤中,我已经将A分隔为键,(B,C)作为值 到目前为止,我可以用A和max C返回输出,但是我很难返回B值。我下面的代码无法更改min_接收器,如何解决此问题 public static class IntSumReducer extends Reducer&l

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

我的目标是找到最大通信时间并返回16,20(格式为A B,C) 由于A1,B6和A1,B7都有最大通信时间20,我只需要保留最小的B id号

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

到目前为止,我可以用A和max C返回输出,但是我很难返回B值。我下面的代码无法更改min_接收器,如何解决此问题

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 2147483647,20

在映射中,我已经将A分隔为键,将B、C分隔为值。所以复合线包括两个变量。值中的格式为B,C.

取决于您的delimeter是什么

使用类似这样的行获取具有最大时间的
文本

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
.stream()的布尔选项是,如果希望顺序=
false
或并行=
true
。。。。如果您的分隔符不同或对象稍有不同,您可能需要调整
getComTime
,但这应该非常接近右侧

如果您希望以这种方式处理,请获取可选的

那你就可以了

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

***Sry我用字符串而不是文本完成了大部分工作,但这已经更新,如果有任何问题,请告诉我

第一个循环不应该查看复合字符串[2]?使用调试器甚至打印一些值将帮助您了解发生了什么。不,这里的值只包括B和C。a是关键。什么是
Reducer
?这是一个Hadoop问题吗?如果是这样,请将其标记为hadoop。是的,刚刚添加。感谢您的提醒。您的数据是以空格分隔的值,那么您希望
split(“,”)
如何工作呢?
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
private static int getComTime(Text line){
    String[] vals = line.toString().split(",");
    return  Integer.parseInt(vals[2]);
}
if(answer.isPresent()){/* got an answer do something with it */}