{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Spark\n", "\n", "[Apache Spark](https://spark.apache.org/) is a lightning-fast cluster computing API based on Scala. Deeper integration with Spark is on the [agenda](https://github.com/twosigma/beakerx/issues/6513)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "%classpath add mvn org.apache.spark spark-sql_2.11 2.2.1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import org.apache.spark.sql.SparkSession\n", "\n", "val spark = SparkSession.builder()\n", " .appName(\"Simple Application\")\n", " .master(\"local[4]\")\n", " .config(\"spark.ui.enabled\", \"false\")\n", " .getOrCreate()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "val NUM_SAMPLES = 10000000\n", "\n", "val count = spark.sparkContext.parallelize(1 to NUM_SAMPLES).map{i =>\n", " val x = Math.random()\n", " val y = Math.random()\n", " if (x*x + y*y < 1) 1 else 0\n", "}.reduce(_ + _)\n", "\n", "println(\"Pi is roughly \" + 4.0 * count / NUM_SAMPLES)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "spark.stop()" ] } ], "metadata": { "kernelspec": { "display_name": "Scala", "language": "scala", "name": "scala" }, "language_info": { "codemirror_mode": "text/x-scala", "file_extension": ".scala", "mimetype": "", "name": "Scala", "nbconverter_exporter": "", "version": "2.11.11" } }, "nbformat": 4, "nbformat_minor": 2 }