{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Using the SparkMagic PySpark kernel to connect to EMR\n", "\n", "This is a SparkMagic PySpark 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": [ "input=\"s3://bucket/object.txt\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%info" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%sh\n", "hostname" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "spark.sql(\"show databases\").show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sc.parallelize(range(1000)).count()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "constText = sc.textFile(input)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import string\n", "\n", "xlate_tbl = str.maketrans('','', string.punctuation)\n", "words = constText.map(lambda line: line.translate(xlate_tbl).lower()).flatMap(lambda line: line.split(\" \"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "wordCounts = words.map(lambda word: (word, 1)).reduceByKey(lambda a,b:a +b)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "wordCounts.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", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%local\n", "wordCounts" ] } ], "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 }