gpt4 book ai didi

java - 如何根据其在spark java中的值对列表进行排序?

转载 作者:行者123 更新时间:2023-11-30 10:35:51 25 4
gpt4 key购买 nike

我想根据这个价格按升序对房屋进行排序。

public class Home implements Serializable{ 

private double price = Math.Random() * 1000;

}

这就是我按顺序执行的方式。

ArrayList<Home> city; // Assume it is initallized with some values
Arrays.sort(this.city,new Comparator<House>(){
public int compare (House o1, House o2){
if (o1.getPrice() > o2.getPrice()) {
return -1;
} else if (o1.getPrice() < o2.getPrice()) {
return 1;
}
return 0;
}
});

现在我想使用 Apache Spark Java 对其进行排序。

Method one

  JavaRDD<House> r2 = houseRDD.sortBy(  i -> {return i.getPrice(); }, true, 1 );

Method two:

JavaRDD<House> r = populationRDD.sortBy( new Function<House, Double>() {
private static final long serialVersionUID = 1L;

@Override
public Double call(Individual value ) throws Exception {
return value.getPrice();
}

}, true, 1 );

上述方法有什么问题,我没有遇到异常 -

java.lang.ClassCastException: House 无法转换为 java.lang.Comparable

java.lang.ClassCastException: House cannot be cast to java.lang.Comparable
at org.spark_project.guava.collect.NaturalOrdering.compare(NaturalOrdering.java:28)
at scala.math.LowPriorityOrderingImplicits$$anon$7.compare(Ordering.scala:153)
at scala.math.Ordering$$anon$4.compare(Ordering.scala:111)
at org.apache.spark.util.collection.Utils$$anon$1.compare(Utils.scala:35)
at org.spark_project.guava.collect.Ordering.max(Ordering.java:551)
at org.spark_project.guava.collect.Ordering.leastOf(Ordering.java:667)
at org.apache.spark.util.collection.Utils$.takeOrdered(Utils.scala:37)
at org.apache.spark.rdd.RDD$$anonfun$takeOrdered$1$$anonfun$30.apply(RDD.scala:1393)
at org.apache.spark.rdd.RDD$$anonfun$takeOrdered$1$$anonfun$30.apply(RDD.scala:1390)
at org.apache.spark.rdd.RDD$$anonfun$mapPartitions$1$$anonfun$apply$23.apply(RDD.scala:785)
at org.apache.spark.rdd.RDD$$anonfun$mapPartitions$1$$anonfun$apply$23.apply(RDD.scala:785)
at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:38)
at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:319)
at org.apache.spark.rdd.RDD.iterator(RDD.scala:283)
at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:70)
at org.apache.spark.scheduler.Task.run(Task.scala:86)
at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:274)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

As per the comment the new Home Class

public class Home implements Serializable,Comparable<Home>{ 

private double price = Math.Random() * 1000;

@Override
public int compareTo(House o) {
return Double.compare(this.getPrice(),o.getPrice());
}
}

最佳答案

House 似乎没有实现类似的接口(interface)。

How to implement the Java comparable interface?

关于java - 如何根据其在spark java中的值对列表进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40900570/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com