|
| 1 | +# Spark Standalone Cluster with Jupyter Notebook in OOD |
| 2 | + |
| 3 | +## Getting Started |
| 4 | +You can run a Spark Standalone Cluster with Jupyter Notebook in OOD by going to the URL [ood.hpc.nyu.edu](http://ood.hpc.nyu.edu) in your browser and selecting `Spark Standalone Cluster` from the `Interactive Apps` pull-down menu at the top of the page. As you can see below, once you've used it and other interactive apps they'll show up on your home screen under the `Recently Used Apps` header. |
| 5 | + |
| 6 | +:::note |
| 7 | +Be aware that when you start from `Recently Used Apps` it will start with the same configuration that you used previously. If you'd like to configure your Spark Standalone Cluster with Jupyter Notebook session differently, you'll need to select it from the menu. |
| 8 | +::: |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +## Configuration |
| 13 | + |
| 14 | +You can select the Spark version, amount of time, number of nodes and cores, amount of memory, gpu type (if any), Jupyter notebook root directory, path to custom pyspark overlay (if any), and optional Slurm options. |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +:::warning |
| 20 | +If you select to use `/home` as your root directory be careful not to go over your quota. You can find your current usage with the `myquota` command. Please see our [Storage documentation](../03_storage/01_intro_and_data_management.mdx) for details about your storage options. |
| 21 | +::: |
| 22 | + |
| 23 | +## Spark Standalone Cluster with Jupyter Notebook running in OOD |
| 24 | + |
| 25 | +After you hit the `Launch` button you'll have to wait for the scheduler to find node(s)for you to run on: |
| 26 | + |
| 27 | + |
| 28 | +Then you'll have a short wait for Spark itself to start up.<br /> |
| 29 | +Once that happens you'll get one last page that will give you links to: |
| 30 | +- open a terminal window on the compute node your Spark session is running on |
| 31 | +- go to the directory associated with your Session ID that stores output, config and other related files for your session |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +Please click the `Connect to the Jupyter Notebook Environment` button and a Jupyter window will open. Please select to create a new notebook and you're ready to go. |
| 36 | + |
| 37 | +### Spark Standalone Cluster Jupyter Notebook Example |
| 38 | + |
| 39 | +Please enter the following commands into the first cell of your Jupyter notebook and execute them by typing `Shift` and `Enter` at the same time. |
| 40 | +```python |
| 41 | +from pyspark import SparkContext |
| 42 | +import requests |
| 43 | + |
| 44 | +# Create a SparkContext |
| 45 | +sc = SparkContext("local", "WordCountExample") |
| 46 | + |
| 47 | +# Get text of Moby Dick from Project Gutenberg |
| 48 | +file_url = 'https://www.gutenberg.org/ebooks/2701.txt.utf-8' |
| 49 | +try: |
| 50 | + response = requests.get(file_url) |
| 51 | + response.raise_for_status() |
| 52 | +except requests.exceptions.RequestException as e: |
| 53 | + print(f"Error during request: {e}") |
| 54 | +# Save text to temp file |
| 55 | +with open('moby_dick_temp_spark_example.txt', "w") as file: |
| 56 | + file.write(response.text) |
| 57 | + |
| 58 | +# Create an RDD from a text file |
| 59 | +lines = sc.textFile("/scratch/rjy1/moby_dick_temp_spark_example.txt") |
| 60 | + |
| 61 | +# FlatMap to split lines into words and convert to lowercase |
| 62 | +words = lines.flatMap(lambda line: line.lower().split(" ")) |
| 63 | + |
| 64 | +# Map each word to a (word, 1) tuple |
| 65 | +word_pairs = words.map(lambda word: (word, 1)) |
| 66 | + |
| 67 | +# ReduceByKey to sum the counts for each word |
| 68 | +word_counts = word_pairs.reduceByKey(lambda a, b: a + b) |
| 69 | + |
| 70 | +# Collect the results to the driver program |
| 71 | +results = word_counts.collect() |
| 72 | + |
| 73 | +# Print the word counts |
| 74 | +for word, count in results: |
| 75 | + print(f"{word}: {count}") |
| 76 | +``` |
| 77 | + |
| 78 | +You should get output like: |
| 79 | +``` |
| 80 | +the: 14512 |
| 81 | +project: 87 |
| 82 | +gutenberg: 25 |
| 83 | +ebook: 8 |
| 84 | +of: 6682 |
| 85 | +moby: 81 |
| 86 | +dick;: 10 |
| 87 | +or,: 17 |
| 88 | +whale: 533 |
| 89 | +: 4318 |
| 90 | +this: 1277 |
| 91 | +is: 1601 |
| 92 | +for: 1555 |
| 93 | +use: 39 |
| 94 | +anyone: 5 |
| 95 | +anywhere: 11 |
| 96 | +in: 4126 |
| 97 | +united: 24 |
| 98 | +states: 13 |
| 99 | +and: 6321 |
| 100 | +most: 284 |
| 101 | +other: 360 |
| 102 | +parts: 32 |
| 103 | +world: 79 |
| 104 | +at: 1310 |
| 105 | +no: 488 |
| 106 | +cost: 3 |
| 107 | +with: 1750 |
| 108 | +almost: 189 |
| 109 | +restrictions: 2 |
| 110 | +whatsoever.: 5 |
| 111 | +you: 843 |
| 112 | +may: 227 |
| 113 | +copy: 15 |
| 114 | +... |
| 115 | +``` |
| 116 | +etc. |
0 commit comments