[英]BroadCast Variable publish in Spark Program
In the spark - java program I need to read a config file and populate a HashMap , which I need to publish as broadcast variable so that it will be available across all the datanodes . 在spark-java程序中,我需要读取一个配置文件并填充一个HashMap,我需要将其发布为广播变量,以便在所有datanode上都可用。
I need to get the value of this broadcast variable in the CustomInputFormat class which is going to run in the datanodes . 我需要在要在datanodes中运行的CustomInputFormat类中获取此广播变量的值。 How can i specify in my CustomInputFormat class to get value from the specific broadcast variable since the broadcast variable is declared in my driver program ? 由于广播变量在驱动程序中声明,因此如何在CustomInputFormat类中指定以从特定广播变量获取值?
I am adding some code to explain it in more : 我正在添加一些代码以进一步解释它:
In this scenario1 I am using it in Driver Program itself ie the variable is used in the same class : Here I can use Broadcat.value() method 在这种情况下,我在驱动程序本身中使用它,即该变量在同一类中使用:在这里我可以使用Broadcat.value()方法
> final Broadcast<String[]> signPrefixes =
> sc.broadcast(loadCallSignTable());
> JavaPairRDD<String, Integer> countryContactCounts = contactCounts.mapToPair(
> new PairFunction<Tuple2<String, Integer>, String, Integer> (){
> public Tuple2<String, Integer> call(Tuple2<String, Integer> callSignCount) {
> String sign = callSignCount._1();
> String country = lookupCountry(sign, signPrefixes.value());
> return new Tuple2(country, callSignCount._2());
> }}).reduceByKey(new SumInts());
In the scenario 2 I am going to use the Broadcast Variable inside my Custom Input Format class : 在场景2中,我将在“自定义输入格式”类中使用“广播变量”:
Driver Program : 驱动程序:
> final JavaSparkContext sc= new
> JavaSparkContext(sConf.setAppName("ParserSpark").setMaster("yarn-cluster"));
> Broadcast<int[]> broadcastVar = sc.broadcast(new int[] {1, 2, 3});
>
> JavaPairRDD<NullWritable, ArrayList<Record>> baseRDD =
> sc.newAPIHadoopFile(args[2], InputFormat.class, NullWritable.class,
> ArrayList.class, conf);
InputFormat.class InputFormat.class
> public class InputFormat extends FileInputFormat {
>
> @Override public RecordReader<NullWritable, ArrayList<Record>>
> createRecordReader(InputSplit split, TaskAttemptContext context)
> throws IOException, InterruptedException{
> //I want to get the Broadcast Variable Here -- How will I do it
>
> RecordReader reader = new RecordReader(); reader.initialize(split, context); return reader; } @Override
> protected boolean isSplitable(JobContext context, Path file) {
> return false; } }
您将在驱动程序w / val bcVariable = sc.broadcast(myVariableToBroadcast)
上创建广播var val bcVariable = sc.broadcast(myVariableToBroadcast)
,稍后再使用bcVariable.value
访问
I ran into this myself recently. 我最近遇到这个问题。 Ended being rather simple actually (after a few hours and then a... a Ha!) 实际上结束了相当简单(几个小时之后,然后是...一个哈!)
Create a new Configuration, set your vars, and pass it to a slightly different implementation of the newAPIHadoopFile function. 创建一个新的Configuration,设置您的var,并将其传递给newAPIHadoopFile函数的稍有不同的实现。
From the driver program (using Scala here): 从驱动程序(此处使用Scala):
val myConf = new Configuration();
myConf.set("var1", v1)
myConf.set("var2", v2)
myConf.set("var3", v3)
val yourFile = sc.newAPIHadoopFile("yourFilePath", classOf[MyFileInputFormat],classOf[org.apache.hadoop.io.Text], classOf[org.apache.hadoop.io.DoubleWritable],myConf)
From your InputFormat or InputReader..or wherever you have a context (Java this time) 从您的InputFormat或InputReader ..或有上下文的任何地方(这次是Java)
context.getConfiguration().get("var1");
or maybe 或者可能
job.getConfiguration().get("var2");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.