|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Window Operations Exercise" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "1. What is Window Operations(better with some graphs)\n", |
| 15 | + "2. Explain parameters (window length and sliding interval)\n", |
| 16 | + "3. Some of the popular Window operations\n", |
| 17 | + " * Window\n", |
| 18 | + " * countByWindow\n", |
| 19 | + " * reduceByKeyAndWindow\n", |
| 20 | + " * countByValueAndWindow\n" |
| 21 | + ] |
| 22 | + }, |
| 23 | + { |
| 24 | + "cell_type": "markdown", |
| 25 | + "metadata": {}, |
| 26 | + "source": [ |
| 27 | + "### Exercise" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "code", |
| 32 | + "execution_count": null, |
| 33 | + "metadata": { |
| 34 | + "collapsed": true |
| 35 | + }, |
| 36 | + "outputs": [], |
| 37 | + "source": [ |
| 38 | + "from pyspark import SparkConf, SparkContext\n", |
| 39 | + "from pyspark.streaming import StreamingContext\n", |
| 40 | + "import sys\n", |
| 41 | + "import random\n", |
| 42 | + "from apache_log_parser import ApacheAccessLog\n", |
| 43 | + "\n", |
| 44 | + "random.seed(15)\n", |
| 45 | + "\n", |
| 46 | + "if len(sys.argv) != 2:\n", |
| 47 | + " print('Please provide the path to Apache log file')\n", |
| 48 | + " print('10_10.py <path_to_log_directory>')\n", |
| 49 | + " sys.exit(2)\n", |
| 50 | + "\n", |
| 51 | + "conf = (SparkConf().setMaster(\"local[4]\").setAppName(\"log processor\").set(\"spark.executor.memory\", \"2g\"))\n", |
| 52 | + "\n", |
| 53 | + "sc = SparkContext(conf=conf)\n", |
| 54 | + "\n", |
| 55 | + "ssc = StreamingContext(sc, 2)\n", |
| 56 | + "ssc.checkpoint(\"checkpoint\")\n", |
| 57 | + " \n", |
| 58 | + "directory = sys.argv[1]\n", |
| 59 | + "print(directory)\n", |
| 60 | + "\n", |
| 61 | + "# create DStream from text file\n", |
| 62 | + "# Note: the spark streaming checks for any updates to this directory.\n", |
| 63 | + "# So first, start this program, and then copy the log file logs/access_log.log to 'directory' location\n", |
| 64 | + "log_data = ssc.textFileStream(directory)\n", |
| 65 | + "access_log_dstream = log_data.map(ApacheAccessLog.parse_from_log_line).filter(lambda parsed_line: parsed_line is not None)\n", |
| 66 | + "ip_dstream = access_log_dstream.map(lambda parsed_line: (parsed_line.ip, 1)) \n", |
| 67 | + "ip_count = ip_dstream.reduceByKey(lambda x,y: x+y)\n", |
| 68 | + "ip_count.pprint(num = 30)\n", |
| 69 | + "ip_bytes_dstream = access_log_dstream.map(lambda parsed_line: (parsed_line.ip, parsed_line.content_size))\n", |
| 70 | + "ip_bytes_sum_dstream = ip_bytes_dstream.reduceByKey(lambda x,y: x+y)\n", |
| 71 | + "ip_bytes_request_count_dstream = ip_count.join(ip_bytes_sum_dstream)\n", |
| 72 | + "ip_bytes_request_count_dstream.pprint(num = 30)\n", |
| 73 | + "\n", |
| 74 | + "####### TODO: use window()to count data over a window##########################\n", |
| 75 | + "access_logs_window = access_log_dstream.window(windowDuration = 6, slideDuration=4) \n", |
| 76 | + "window_counts = access_logs_window.count()\n", |
| 77 | + "print( \" Window count: \")\n", |
| 78 | + "window_counts.pprint()\n", |
| 79 | + "\n", |
| 80 | + "####### Exercise End ##########################################################\n", |
| 81 | + "\n", |
| 82 | + "ssc.start() \n", |
| 83 | + "ssc.awaitTermination()\n" |
| 84 | + ] |
| 85 | + }, |
| 86 | + { |
| 87 | + "cell_type": "markdown", |
| 88 | + "metadata": {}, |
| 89 | + "source": [ |
| 90 | + "## References\n", |
| 91 | + "1. https://spark.apache.org/docs/latest/streaming-programming-guide.html#discretized-streams-dstreams" |
| 92 | + ] |
| 93 | + }, |
| 94 | + { |
| 95 | + "cell_type": "markdown", |
| 96 | + "metadata": {}, |
| 97 | + "source": [ |
| 98 | + " " |
| 99 | + ] |
| 100 | + } |
| 101 | + ], |
| 102 | + "metadata": { |
| 103 | + "kernelspec": { |
| 104 | + "display_name": "Python 3", |
| 105 | + "language": "python", |
| 106 | + "name": "python3" |
| 107 | + }, |
| 108 | + "language_info": { |
| 109 | + "codemirror_mode": { |
| 110 | + "name": "ipython", |
| 111 | + "version": 3 |
| 112 | + }, |
| 113 | + "file_extension": ".py", |
| 114 | + "mimetype": "text/x-python", |
| 115 | + "name": "python", |
| 116 | + "nbconvert_exporter": "python", |
| 117 | + "pygments_lexer": "ipython3", |
| 118 | + "version": "3.6.3" |
| 119 | + } |
| 120 | + }, |
| 121 | + "nbformat": 4, |
| 122 | + "nbformat_minor": 2 |
| 123 | +} |
0 commit comments