Spark provides a random sampling of the result set RDD, which is the ability to get a small subset of the data. There are sample, Takesample, takeordered and other methods.
Import Org.apache.spark.api.java.JavaRDD;
Import Org.apache.spark.api.java.JavaSparkContext;
Import org.apache.spark.sql.SparkSession;
Import java.util.List;
/** * sampling * @author Wuweifeng wrote on 2018/4/24. */public class Testsample {public static void main (string[] args) {sparksession sparksession = sparksession
. Builder (). AppName ("Javawordcount"). Master ("local"). Getorcreate ();
Javasparkcontext javasparkcontext = new Javasparkcontext (Sparksession.sparkcontext ());
javardd<string> Javardd = Javasparkcontext.textfile ("/users/wuwf/age");
Take 10% of the data, the random number of seeds themselves set, you can also do not set javardd<string> sample = Javardd.sample (False, 0.1, 1234);
Long sampledatasize = Sample.count ();
Long rawdatasize = Javardd.count ();
System.out.println (Rawdatasize + "and after the sampling:" + sampledatasize);
Take a specified number of random data list<string> List = Javardd.takesample (false, 10);
SYSTEM.OUT.PRINTLN (list); //Take the specified number of sorted data list<string> orderlist = javardd.takeordered (10);
System.out.println (orderlist);
}
}