import java.util.Arrays;
import java.util.List;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.mllib.recommendation.Rating;
import scala.Tuple2;
public class provicead1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
SparkConf sparkConf = new SparkConf().setAppName("PeopleInfoCalculator").setMaster("local");
JavaSparkContext sc = new JavaSparkContext(sparkConf);
JavaRDD<String> filedata=sc.textFile("file:///home/gyq/eclipse-workspace/ALS/PeopleInfo.txt");
JavaPairRDD<Tuple2<String,String>,Integer> PAdata=filedata.mapToPair(s->{
return new Tuple2<Tuple2<String,String>,Integer>(
new Tuple2<String,String>(s.split(" ")[1],s.split(" ")[4]),1);//将第2列数据和第5列数据取出变成键值对形式
});
JavaPairRDD<Tuple2<String,String>,Integer> ggs=PAdata.reduceByKey((x,y)->x+y);
ggs.foreach(f->System.out.println("jjjjj(省份,"+ "广告),点击数"+f));//第二题
JavaRDD<String> rdd=ggs.map(f->{return f._1+" "+f._2;});
JavaRDD<String> rdd1=rdd.sortBy(f->{return Integer.valueOf(f.split(" ")[1]);},false,1);
rdd1.foreach(f->System.out.println("(省份,"+ "广告),点击数"+f));
List<String> rdd2=rdd1.take(3);
for(int i=0;i<3;i++) {
System.out.println(rdd2.get(i));
}
}
}
正确答案:
?
值得注意的是:出现这种错误因为
?这样
?或者这样
?都不行,string也能排序,但是遇到数字的话只能按第一个数字排须,例如按升序排
9>89>77>6>55
|