One of the biggest, most time-consuming parts of data science is analysis and experimentation. One of the most popular tools to do so in a graphical, interactive environment is Jupyter.
Combining Jupyter with Apache Spark (through PySpark) merges two extremely powerful tools. AWS EMR lets you set up all of these tools with just a few clicks. In this tutorial I’ll walk through creating a cluster of machines running Spark with a Jupyter notebook sitting on top of it all.
- Create an EMR cluster with Spark 2.0 or later with this file as a bootstrap action: Link.
- Add this as a step: Link.
- SSH in to the head/master node and run pyspark with whatever options you need.
- Open up port 8888 (make sure it’s allowed in the security group) of your head/master node in a web browser and you’re in Jupyter!
Step by Step Screenshots
Find them here: https://github.com/mikestaszel/spark-emr-jupyter/tree/master/screenshots
Creating an EMR Cluster
When creating your EMR cluster, all you need to do is add a bootstrap action file that will install Anaconda and Jupyter Spark extensions to make job progress visible directly in the notebook. Add this as a bootstrap action: https://github.com/mikestaszel/spark-emr-jupyter/blob/master/emr_bootstrap.sh
Running Jupyter through PySpark
When your cluster is ready, you need to run a step that will tell PySpark to launch Jupyter when you run it. You’ll need to copy this file into an S3 bucket and reference it in the step: https://github.com/mikestaszel/spark-emr-jupyter/blob/master/jupyter_step.sh
To do this, add a step to the cluster with the following parameters:
JAR location: s3://[region].elasticmapreduce/libs/script-runner/script-runner.jar
You’re ready to run PySpark! You can go ahead and run something like “pyspark –master yarn” with any options you need (for example in a tmux session on your master node). You should see the Jupyter notebook server start and print out an address and authentication token.
In your browser, open up port 8888 of the head node and paste in the authentication key and you’re all set! You can create a notebook or upload one. You don’t need to initialize a SparkSession – one is automatically created for you, named “spark”. Make sure your security group firewall rules allow access to port 8888!
One last thing to keep in mind is that your notebooks will be deleted when you terminate the cluster, so make sure to download anything you need! There are some Jupyter plugins you can try if you want to store notebooks in S3, but that’s another blog post.
I bought a few Amazon Dash Buttons as part of Prime Day. These are the cheaper $4.99 buttons, not the more expensive $19.99 AWS IoT Buttons. In this blog post I’ll walk through how to make these cheaper buttons do what the more expensive button does.
What You Need
- One or more Amazon Dash Buttons. You’ll need to add the button to your Amazon account, but do not pick a product to buy. Just exit the set-up process without picking a product and you’ll be all set.
- Computer with root (sudo) access (or RaspberryPi or other device capable of running Python).
- The code from GitHub.
How exactly does the dash button work? In a nutshell, every time you press it, the button connects to the Wi-Fi network, pings Amazon, and then shuts back down for power savings. We’ll exploit the first step in that process – connecting to the Wi-Fi network. Using Python, we can listen for special “ARP probe” packets the Dash Button sends when it attempts to connect to Wi-Fi. All you need to know is the MAC address of the Dash Button and then listen for these ARP packets. When an ARP packet with your Dash Button’s MAC address is detected, you know the button was pressed, and you can call whatever Python methods you want.
Finding the MAC Address
The first step once you’ve set up your Dash Button (but have no picked a product to actually buy!) is to find the button’s MAC address. Grab your computer and connect it to the same Wi-Fi network as your button, then run the pydashbutton.py script as root and watch for any MAC addresses that are printed when you press the button. One important thing to note is that there seems to be throttling when pressing the button. Pressing the button multiple times per minute might not work.
Running Methods on Button Press
Now that you have the MAC address to listen for, all you need to do is throw some if/else logic into that same listener script to run code when a MAC address is detected. Check out the script and make any modifications you need. I included a simple example for logging button presses to a Google Sheet when pressing a button.
Check out the code on GitHub. Have fun!
Jupyter Notebook extension for Apache Spark integration.
Includes a progress indicator for the current Notebook cell if it invokes a Spark job. Queries the Spark UI service on the backend to get the required Spark job information.
This is really neat. No more checking another tab for job progress when running cells in a notebook!
If you really want to get into the details of Python and learn about how the language was built and how some of its internals are implemented, Fluent Python is the book for you.
It’s a great book to refresh your knowledge of coroutines, asyncio, and other Python goodies.
You’ll definitely want to read this if you’re using AWS Kinesis with Apache Spark to stream data, it’s been extremely valuable:
If you’re just getting started with Flask or you want to learn about the innards of Django (yep, that’s right), “Flask Web Development” is the perfect place to start. This book dives right in with creating a full web application, including Jinja templates, authentication, building a REST API, forms, databases, security, and deployment to Heroku using Git. This book will get you up and running with Flask and then quickly go into detail on how to build a full web application.
However, in my opinion, Flask should be used for small applications, but this book goes into full detail about creating a half-Django for a full web application.
With that in mind, this book is great for learning about Django – how would you implement CSRF token checks? How would you set up database migrations from scratch? How would you handle forms? Django does all of that, but hides it all from developers. This book goes into full detail reimplementing a lot of what Django gives you out-of-the-box, which is great.
Overall I highly recommend “Flask Web Development” if you’re learning either Flask, Django, or just web-backend development in general. Don’t just use what Django gives you out of the box and ignore how it’s implemented. This book will answer questions like “Why does my Django app need a
SECRET_KEY? What is this CSRF error I keep seeing? How do database migrations work? How do I write my own mail handler?”, making you a better Django developer.
Get it here: http://a.co/73ERCK9
I like to start my projects using Flask and Python because it’s fast and quick for most things, yet lightweight.
By default, Flask doesn’t give you much in terms of test frameworks, application settings, deployment, or running the application in production. I always end up making a skeleton that does some of these things, so I decided to put together a GitHub repository with a skeleton Flask project that does it for me.
Have a look here: https://github.com/mikestaszel/flask_startup