我尝试从javaRDD获取最小值并删除它。然后获取下一个值,但我得到了ex min值。例如,在下面的样本中,pt_min的值不会改变。
public class Main {
static Double min= Double.MAX_VALUE;
static String pt_min="";
static JavaRDD<String> input;
public static void main(String[] a){
Logger.getLogger("org").setLevel(Level.OFF);
Logger.getLogger("akka").setLevel(Level.OFF);
String inputFile = "/home/k/Desktop/exemple/test";
SparkConf conf = new SparkConf().setAppName("test").setMaster("local");
JavaSparkContext sc = new JavaSparkContext(conf);
input = sc.textFile(inputFile);
input.foreach(x-> System.out.println(x));
pt_min=getMin(input);
System.out.println("********** "+pt_min);
input= input.filter(x->! x.equals(pt_min));
input.foreach(x-> System.out.println(x));
pt_min=getMin(input);
System.out.println("********** "+pt_min);
}
private static String getMin(JavaRDD<String> input){
input.foreach(x->{
if(min>Double.parseDouble(x)) {
min = Double.parseDouble(x);
pt_min=x;
}
});
return pt_min;
}
}
结果是:
12
7
1
2
9
********** 1
12
7
2
9
********** 1
请帮我解决一下!
答案 0 :(得分:0)
首先,您应该将min字段重新初始化为Double.MAX_VALUE
aldsfkalkd | aldsfkalkd | aldsfkalkd
ldfkjlj | ldfkjlj | ldfkjlj
another line | | feature line
---------------------| ---------------------| ----------------------
---------------------| ---------------------| ----------------------
---------------------| ---------------------| ----------------------
---------------------| ---------------------| ----------------------
~ | ~ | ~
~ | ~ | ~
~ | ~ | ~
./file2_LOCAL_82335 ./file2_BASE_82335 ./file2_REMOTE_82335
aldsfka
ldfk
<<<<<<< HEAD
another line
=======
feature line
>>>>>>> feature
~
~
~
~
file2
你应该在方法input.foreach(x-> System.out.println(x));
pt_min=getMin(input);
System.out.println("********** "+pt_min);
input= input.filter(x->! x.equals(pt_min));
// reset min
min= Double.MAX_VALUE;
input.foreach(x-> System.out.println(x));
pt_min=getMin(input);
System.out.println("********** "+pt_min);
中使用缓存,因为输入没有保存,你应该使用缓存。
getMin(input)