Spark's broadcast variables

Source: Internet
Author: User
Tags arrays split
private static void Broadcastops (Javasparkcontext SC) {//Load user table to Rdd javardd<string> Linesrdd = Sc.parallel
    Ize (Arrays.aslist ("1, 3, Zhang San, Hebei", "2, 1, John Doe, Beijing", "3, 0, Harry, Tianjin", "4, 1, Zhao Liu, Guangdong"));
    javardd<string> Sexlinerdd = sc.parallelize (Arrays.aslist ("1, Male", "0, female"));
    /** * Pull the rdd corresponding to the driver node * Use the appropriate data structure to load the broadcast variable */list<string> sexlist = Sexlinerdd.collect ();
    map<string, string> sexmap = new HashMap ();
        for (String sexline:sexlist) {string[] sexsplits = Sexline.split (",");
    Sexmap.put (Sexsplits[0], sexsplits[1]);
    } broadcast<map<string, string>> SEXMAPBC = Sc.broadcast (Sexmap);  javardd<string> Retrdd = Linesrdd.map (New function<string, string> () {@Override public String
            Call (String line) throws Exception {string[] splits = Line.split (",");
            if (splits = = NULL | | Splits.length < 4) {return null;
           } String sid = Splits[1].trim ();
            It is not recommended to call an external variable directly in transformation, but to get the external variable//string sName = sexmap.get (SID) from the broadcast variable;
            String sName = Sexmapbc.value (). Getordefault (SID, "Unknown");
        return splits[0] + "" + SName + "" + splits[2] + "" + splits[3 ";
    }
    });
Retrdd.foreach (STR-System.out.println (str)); }

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.