@@ -0,0 +1,1988 @@
+#! /usr/bin/env python%0A# -*- coding: utf-8 -*-%0A# vim:fenc=utf-8%0A#%0A%0A%22%22%22%0ACounting primer numbers from 0 to 10000000 with help of Apache Spark map-reduce %0A+ compare time of parallel (Spark-based) vs sequantial calculation%0A%0ATo see parallelism of apache spark you can open 'top' command in separate %0Aterminal while running this script%0A%22%22%22%0A%0Aimport sys%0Atry:%0A    import pyspark%0Aexcept ImportError:%0A    print(%22ERROR: pyspark module not installed. Use 'sudo pip install pyspark'%22)%0A    sys.exit(1)%0A%0ANUMBERS_THRESHOLD=100000000%0A%0A#running Apache Spark%0Aif not 'sc' in globals():%0A    sc = pyspark.SparkContext()%0A%0A%0A#timeit decorator%0Adef timeit(method):%0A    def timed(*args, **kw):%0A        import time%0A        ts = time.time()%0A        result = method(*args, **kw)%0A        te = time.time()%0A        print(%22time of execution method %7B%7D: %7B%7D ms%22.format(str(method.__name__),int((te-ts)*1000)))%0A    return timed%0A%0Adef is_it_prime(number):%0A    # make sure n is a positive integer%0A    number = abs(int(number))%0A    # simple tests%0A    if number %3C 2:%0A        return False%0A    # 2 is prime%0A    if number == 2:%0A        return True%0A    # other even numbers aren't%0A    if not number & 1:%0A        return False%0A    # check whether number is divisible into it's square root%0A    for x in range(3, int(number**0.5)+1, 2):%0A        if number %25 x == 0:%0A            return False%0A    #if we get this far we are good%0A    return True%0A%0A@timeit%0Adef parallelSparkBasedCalculation():%0A%0A    # create a set of numbers to %0A    numbers = sc.parallelize(xrange(NUMBERS_THRESHOLD))%0A    # count out the number of primes we found%0A    count = numbers.filter(is_it_prime).count()%0A    print(%22parallelSparkBasedCalculation result: %7B%7D%22.format(count))%0A%0A@timeit%0Adef sequantialCalculation():%0A    count = 0%0A    for i in xrange(NUMBERS_THRESHOLD):%0A        if (is_it_prime(i)):%0A            count+=1%0A    print(%22sequantialCalculation result: %7B%7D%22.format(count))%0A%0Aif __name__ == %22__main__%22:    %0A    sequantialCalculation()%0A    parallelSparkBasedCalculation() %0A%0A

Add Apache-Spark-based initial samples: countPrimeNumbers.py