Spark 2.2.0 Accumulator Use method Java version of Python version __python

Source: Internet
Author: User
Tags pyspark

Java version

Package cn.spark.study.core;

Import Org.apache.spark.Accumulator;
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.VoidFunction;

Import java.util.*;
public class Accumulatorvairable {public
	static void Main (string[] args) {
		sparkconf conf =new sparkconf (). Setap PName ("persist"). Setmaster ("local");
		Javasparkcontext SC =new javasparkcontext (conf);
		Final accumulator<integer> accumulator = sc.accumulator (0);
		List<integer> numberlist =arrays.aslist (1,2,3,4,5);
		javardd<integer> num =sc.parallelize (numberlist);
		Num.foreach (New voidfunction<integer> () {
			
			@Override public
			void Call (Integer arg0) throws Exception { C16/>accumulator.add (arg0);
				
			}
		);
		System.out.println (Accumulator.value ());
		Sc.close ();
	}

Python version

Import OS
import sys
os.environ[' spark_home ' = '/opt/spark '
sys.path.append ("/opt/spark/python")

From Pyspark import sparkcontext from
pyspark import sparkconf


if __name__ = ' __main__ ':
    sc = Sparkcontext (" Local ", reduce")
    sumact = sc.accumulator (0)
    list = [1, 2, 3, 4, 5]
    Listrdd = sc.parallelize (list)
    def f ( num):
        global sumact
        sumact +=num

    Listrdd.foreach (f)
    print Sumact


Related Article

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.