Sha256: 1ffb630f1a5a7a51e0cbe755bf0c303fb42939e042b09a359525a8c521ae9e23

Contents?: true

Size: 1.48 KB

Versions: 21

Compression:

Stored size: 1.48 KB

Contents

#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

"""
This is an example Spark script to get you started. This script will run
a simple Spark script that will calculate the value of pi.

To Run:
mortar spark sparkscripts/<%= project_name %>_pi.py [partitions]"
"""

import sys
from random import random
from operator import add

from pyspark import SparkContext


if __name__ == "__main__":
    sc = SparkContext(appName="PythonPi")
    partitions = int(sys.argv[1]) if len(sys.argv) > 1 else 2
    n = 100000 * partitions

    def f(_):
        x = random() * 2 - 1
        y = random() * 2 - 1
        return 1 if x ** 2 + y ** 2 < 1 else 0

    count = sc.parallelize(xrange(1, n + 1), partitions).map(f).reduce(add)
    print "Pi is roughly %f" % (4.0 * count / n)

    sc.stop()

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
mortar-0.15.53 lib/mortar/templates/project/sparkscripts/sparkscript.py
mortar-0.15.52 lib/mortar/templates/project/sparkscripts/sparkscript.py
mortar-0.15.51 lib/mortar/templates/project/sparkscripts/sparkscript.py
mortar-0.15.50 lib/mortar/templates/project/sparkscripts/sparkscript.py
mortar-0.15.49 lib/mortar/templates/project/sparkscripts/sparkscript.py
mortar-0.15.48 lib/mortar/templates/project/sparkscripts/sparkscript.py
mortar-0.15.47 lib/mortar/templates/project/sparkscripts/sparkscript.py
mortar-0.15.46 lib/mortar/templates/project/sparkscripts/sparkscript.py
mortar-0.15.45 lib/mortar/templates/project/sparkscripts/sparkscript.py
mortar-0.15.44 lib/mortar/templates/project/sparkscripts/sparkscript.py
mortar-0.15.43 lib/mortar/templates/project/sparkscripts/sparkscript.py
mortar-0.15.42 lib/mortar/templates/project/sparkscripts/sparkscript.py
mortar-0.15.41 lib/mortar/templates/project/sparkscripts/sparkscript.py
mortar-0.15.40 lib/mortar/templates/project/sparkscripts/sparkscript.py
mortar-0.15.39 lib/mortar/templates/project/sparkscripts/sparkscript.py
mortar-0.15.38 lib/mortar/templates/project/sparkscripts/sparkscript.py
mortar-0.15.37 lib/mortar/templates/project/sparkscripts/sparkscript.py
mortar-0.15.36 lib/mortar/templates/project/sparkscripts/sparkscript.py
mortar-0.15.35 lib/mortar/templates/project/sparkscripts/sparkscript.py
mortar-0.15.34 lib/mortar/templates/project/sparkscripts/sparkscript.py