• BroadCast


    package com.bjsxt.spark.others;

    import java.util.Arrays;
    import java.util.List;

    import org.apache.spark.SparkConf;
    import org.apache.spark.api.java.JavaRDD;
    import org.apache.spark.api.java.JavaSparkContext;
    import org.apache.spark.api.java.function.Function;
    import org.apache.spark.api.java.function.VoidFunction;
    import org.apache.spark.broadcast.Broadcast;
    /**
    * 广播变量:
    * 1.不能将一个RDD使用广播变量广播出去,因为RDD是不存数据的,可以将RDD的结果广播出去。
    * 2.广播变量只能在Driver端定义,不能在Executor端定义。
    * 3.在Driver端可以修改广播变量的值,在Executor端不能修改广播变量的值。
    *
    * @author root
    *
    */

    public class BroadCast {
    public static void main(String[] args) {
    SparkConf conf = new SparkConf();
    conf.setMaster("local").setAppName("broadcast");
    JavaSparkContext sc = new JavaSparkContext(conf);

    List<String> list = Arrays.asList("hello bjsxt");
    //广播变量将list广播出去
    final Broadcast<List<String>> broadCastList = sc.broadcast(list);

    JavaRDD<String> lines = sc.textFile("./words.txt");
    JavaRDD<String> result = lines.filter(new Function<String, Boolean>() {

    /**
    *
    */
    private static final long serialVersionUID = 1L;

    @Override
    public Boolean call(String s) throws Exception {
    return broadCastList.value().contains(s);
    }
    });
    result.foreach(new VoidFunction<String>() {

    /**
    *
    */
    private static final long serialVersionUID = 1L;

    @Override
    public void call(String t) throws Exception {
    System.out.println(t);
    }
    });

    sc.close();

    }
    }

  • 相关阅读:
    mysql索引最左匹配的理解(转载于知乎回答)
    mysql深度优化与理解(迄今为止读到最优秀的mysql博客)
    PHP数组函数总结与使用
    进程(process)和线程(thread)
    联合索引使用规则(转载)
    mysql优化大全(转自别人 )
    HTTP隧道解决的问题
    HTTP代理协议 HTTP/1.1的CONNECT方法
    vant弹窗提示
    vue获取验证码倒计时
  • 原文地址:https://www.cnblogs.com/huiandong/p/9194601.html
Copyright © 2020-2023  润新知