Skip to content

Instantly share code, notes, and snippets.

@ksingh7
Created April 14, 2019 17:34
Show Gist options
  • Save ksingh7/00d5b10a966d6fca3a344a70bce39091 to your computer and use it in GitHub Desktop.
Save ksingh7/00d5b10a966d6fca3a344a70bce39091 to your computer and use it in GitHub Desktop.
pyspark-test.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import pyspark\n",
"sc = pyspark.SparkContext('local[*]')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.14136904\n"
]
}
],
"source": [
"import random\n",
"num_samples = 100000000\n",
"def inside(p): \n",
" x, y = random.random(), random.random()\n",
" return x*x + y*y < 1\n",
"count = sc.parallelize(range(0, num_samples)).filter(inside).count()\n",
"pi = 4 * count / num_samples\n",
"print(pi)\n",
"sc.stop()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment