9 Spark Sample, collect partial results sample, takeordered

Source: Internet
Author: User

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);
 }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.