|
| 1 | +import org.apache.spark.SparkConf; |
| 2 | +import org.apache.spark.api.java.JavaPairRDD; |
| 3 | +import org.apache.spark.api.java.JavaRDD; |
| 4 | +import org.apache.spark.api.java.JavaSparkContext; |
| 5 | +import scala.Tuple2; |
| 6 | + |
| 7 | +public class ListeningCount { |
| 8 | + public static void main(String[] args) { |
| 9 | + //SparkConf conf = new SparkConf().setAppName("ListeningCount").setMaster("local[*]"); |
| 10 | + SparkConf conf = new SparkConf().setAppName("ListeningCount").setMaster("yarn"); |
| 11 | + JavaSparkContext sc = new JavaSparkContext(conf); |
| 12 | + JavaRDD<String> file = sc.textFile("hdfs://master:9000/user/root/input/user_artists.dat").repartition(1); |
| 13 | + //JavaRDD<String> file = sc.textFile("input/user_artists.dat").repartition(1); |
| 14 | + String head = file.first(); |
| 15 | + file = file.filter(row -> !row.equals(head)); |
| 16 | + // JavaRDD< String > file = sc.textFile("input/user_artists.dat"). |
| 17 | + // mapPartitionsWithIndex((index, iter) -> { |
| 18 | + // if (index == 0 && iter.hasNext()) { |
| 19 | + // iter.next(); |
| 20 | + // if (iter.hasNext()) {JavaRDD<String> file = sc.textFile("hdfs://master:9000/user/root/input/user_artists.dat").repartition(1) |
| 21 | + // iter.next(); |
| 22 | + // } |
| 23 | + // } |
| 24 | + // return iter; |
| 25 | + // }, true);; |
| 26 | + JavaPairRDD<String,Integer> pair = file. |
| 27 | + mapToPair(s -> new Tuple2(s.split("\t")[1],Integer.parseInt(s.split("\t")[2]))); |
| 28 | + JavaPairRDD<String,Integer> count = pair.reduceByKey((int1,int2) ->(int1+int2)); |
| 29 | + JavaPairRDD<Integer,String> listencount = count.mapToPair(ls -> new Tuple2<>(ls._2,ls._1)); |
| 30 | + JavaPairRDD<Integer,String> listencountsort = listencount.sortByKey(true); |
| 31 | + JavaPairRDD<Integer,String> listencountsort1 = listencount.sortByKey(false); |
| 32 | + JavaPairRDD<String,Integer> list = listencountsort1.mapToPair(listen -> new Tuple2<>(listen._2,listen._1)); |
| 33 | + list.foreach(num->System.out.println("Listening counts of Artists \""+num._1+"\" is "+ num._2)); |
| 34 | + sc.close(); |
| 35 | + |
| 36 | + } |
| 37 | + |
| 38 | + } |
| 39 | + |
0 commit comments