{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Using the SparkMagic Scala kernel to connect to EMR\n", "\n", "This is a SparkMagic Scala notebook that lets you run jobs against an existing EMR cluster. See the instructions in the [README.md file](./README.md) for instructions.\n", "\n", "The following cell is the parameter cell. Upload a text file to your S3 bucket and run your job with a parameter (`-p input=s3://...` argument to `run-notebook`) to point at the object. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "parameters" ] }, "outputs": [], "source": [ "# This is a \"var\" instead of a \"val\" so that it can be changed with the -p flag to run-notebook\n", "var input=\"s3://mybucket/myobject.txt\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "val textFile = sc.textFile(input)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "val counts = textFile.flatMap(line => line.split(\" \")).\n", " map(word => (word, 1)).\n", " reduceByKey(_ + _)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "counts.collect()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "counts.toDF(\"word\", \"occurences\").createOrReplaceTempView(\"wordCounts\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%sql\n", "show tables" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%sql -o wordCounts\n", "select * from wordCounts order by occurences desc limit 20" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%local\n", "wordCounts" ] }, { "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.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }