spark之从外部文件获取广播变量
Posted wangbin2188
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spark之从外部文件获取广播变量相关的知识,希望对你有一定的参考价值。
public static Map<String,String> loadDataFromFile() { Map<String,String> map = new ConcurrentHashMap<>(); try { File file = new File("filename.txt"); if (file.isFile() && file.exists()) { InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8"); BufferedReader bufferedReader = new BufferedReader(read); String line = null; while ((line = bufferedReader.readLine()) != null) { String[] split = line.split(","); if (split.length==2) { map.put(split[0],split[1]); } } bufferedReader.close(); read.close(); } } catch (Exception e) { e.printStackTrace(); } return map; }
//获取文件内容并广播 Map<String,String> pidMap = loadDataFromFile(); Broadcast<Map<String,String>> broadcast = sc.broadcast(pidMap); //在worker中使用 broadcast.getValue();
以上是关于spark之从外部文件获取广播变量的主要内容,如果未能解决你的问题,请参考以下文章