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