|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": { |
| 6 | + "deletable": false, |
| 7 | + "editable": false, |
| 8 | + "trusted": true |
| 9 | + }, |
| 10 | + "source": [ |
| 11 | + "\n", |
| 12 | + "# Glue Studio Notebook\n", |
| 13 | + "You are now running a **Glue Studio** notebook; before you can start using your notebook you *must* start an interactive session.\n", |
| 14 | + "\n", |
| 15 | + "## Available Magics\n", |
| 16 | + "| Magic | Type | Description |\n", |
| 17 | + "|-----------------------------|--------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------|\n", |
| 18 | + "| %%configure | Dictionary | A json-formatted dictionary consisting of all configuration parameters for a session. Each parameter can be specified here or through individual magics. |\n", |
| 19 | + "| %profile | String | Specify a profile in your aws configuration to use as the credentials provider. |\n", |
| 20 | + "| %iam_role | String | Specify an IAM role to execute your session with. |\n", |
| 21 | + "| %region | String | Specify the AWS region in which to initialize a session |\n", |
| 22 | + "| %session_id | String | Returns the session ID for the running session. |\n", |
| 23 | + "| %connections | List | Specify a comma separated list of connections to use in the session. |\n", |
| 24 | + "| %additional_python_modules | List | Comma separated list of pip packages, s3 paths or private pip arguments. |\n", |
| 25 | + "| %extra_py_files | List | Comma separated list of additional Python files from S3. |\n", |
| 26 | + "| %extra_jars | List | Comma separated list of additional Jars to include in the cluster. |\n", |
| 27 | + "| %number_of_workers | Integer | The number of workers of a defined worker_type that are allocated when a job runs. worker_type must be set too. |\n", |
| 28 | + "| %worker_type | String | Standard, G.1X, *or* G.2X. number_of_workers must be set too. Default is G.1X |\n", |
| 29 | + "| %glue_version | String | The version of Glue to be used by this session. Currently, the only valid options are 2.0 and 3.0 (eg: %glue_version 2.0) |\n", |
| 30 | + "| %security_config | String | Define a security configuration to be used with this session. |\n", |
| 31 | + "| %sql | String | Run SQL code. All lines after the initial %%sql magic will be passed as part of the SQL code. |\n", |
| 32 | + "| %streaming | String | Changes the session type to Glue Streaming. |\n", |
| 33 | + "| %etl | String | Changes the session type to Glue ETL. |\n", |
| 34 | + "| %status | | Returns the status of the current Glue session including its duration, configuration and executing user / role. |\n", |
| 35 | + "| %stop_session | | Stops the current session. |\n", |
| 36 | + "| %list_sessions | | Lists all currently running sessions by name and ID. |\n", |
| 37 | + "| %spark_conf | String | Specify custom spark configurations for your session. E.g. %spark_conf spark.serializer=org.apache.spark.serializer.KryoSerializer |" |
| 38 | + ] |
| 39 | + }, |
| 40 | + { |
| 41 | + "cell_type": "code", |
| 42 | + "execution_count": null, |
| 43 | + "metadata": { |
| 44 | + "trusted": true, |
| 45 | + "vscode": { |
| 46 | + "languageId": "python_glue_session" |
| 47 | + } |
| 48 | + }, |
| 49 | + "outputs": [], |
| 50 | + "source": [ |
| 51 | + "# Adding required libraries and extra jars to the job - # <------- PLEASE REPLACE ${BUCKET_NAME} BELOW!!!\n", |
| 52 | + "\n", |
| 53 | + "%extra_py_files s3://${BUCKET_NAME}/library/pycountry_convert.zip\n", |
| 54 | + "%extra_jars s3://crawler-public/json/serde/json-serde.jar\n", |
| 55 | + "\n", |
| 56 | + "# Adding required properties to the job - # <------- PLEASE REPLACE ${BUCKET_NAME} BELOW!!!\n", |
| 57 | + "\n", |
| 58 | + "%%configure \n", |
| 59 | + "{\n", |
| 60 | + " \"--enable-spark-ui\": \"true\",\n", |
| 61 | + " \"--spark-event-logs-path\": \"s3://${BUCKET_NAME}/output/lab3/sparklog/\",\n", |
| 62 | + " \"max_retries\": \"0\" \n", |
| 63 | + "}" |
| 64 | + ] |
| 65 | + }, |
| 66 | + { |
| 67 | + "cell_type": "code", |
| 68 | + "execution_count": null, |
| 69 | + "metadata": { |
| 70 | + "editable": true, |
| 71 | + "trusted": true, |
| 72 | + "vscode": { |
| 73 | + "languageId": "python_glue_session" |
| 74 | + } |
| 75 | + }, |
| 76 | + "outputs": [], |
| 77 | + "source": [ |
| 78 | + "#Importing all the basic Glue, Spark libraries \n", |
| 79 | + "\n", |
| 80 | + "import sys\n", |
| 81 | + "from awsglue.transforms import *\n", |
| 82 | + "from awsglue.utils import getResolvedOptions\n", |
| 83 | + "from pyspark.context import SparkContext\n", |
| 84 | + "from awsglue.context import GlueContext\n", |
| 85 | + "from awsglue.dynamicframe import DynamicFrame\n", |
| 86 | + "from awsglue.job import Job\n", |
| 87 | + "\n", |
| 88 | + "# Important further required libraries\n", |
| 89 | + "\n", |
| 90 | + "from pyspark.sql.functions import udf, col\n", |
| 91 | + "from pyspark.sql.types import IntegerType, StringType\n", |
| 92 | + "from pyspark import SparkContext\n", |
| 93 | + "from pyspark.sql import SQLContext\n", |
| 94 | + "from datetime import datetime\n", |
| 95 | + "\n", |
| 96 | + "# Starting Spark/Glue Context\n", |
| 97 | + "\n", |
| 98 | + "sc = SparkContext.getOrCreate()\n", |
| 99 | + "glueContext = GlueContext(sc)\n", |
| 100 | + "spark = glueContext.spark_session\n", |
| 101 | + "job = Job(glueContext)" |
| 102 | + ] |
| 103 | + }, |
| 104 | + { |
| 105 | + "cell_type": "code", |
| 106 | + "execution_count": null, |
| 107 | + "metadata": { |
| 108 | + "trusted": true, |
| 109 | + "vscode": { |
| 110 | + "languageId": "python_glue_session" |
| 111 | + } |
| 112 | + }, |
| 113 | + "outputs": [], |
| 114 | + "source": [ |
| 115 | + "# Important pycountry_convert function from the external python library (pycountry_convert.zip)\n", |
| 116 | + "\n", |
| 117 | + "from pycountry_convert import (\n", |
| 118 | + " convert_country_alpha2_to_country_name,\n", |
| 119 | + " convert_country_alpha2_to_continent,\n", |
| 120 | + " convert_country_name_to_country_alpha2,\n", |
| 121 | + " convert_country_alpha3_to_country_alpha2,\n", |
| 122 | + ")\n", |
| 123 | + "\n", |
| 124 | + "\n", |
| 125 | + "# Defining the function code\n", |
| 126 | + "def get_country_code2(country_name):\n", |
| 127 | + " country_code2 = 'US'\n", |
| 128 | + " try:\n", |
| 129 | + " country_code2 = convert_country_name_to_country_alpha2(country_name)\n", |
| 130 | + " except KeyError:\n", |
| 131 | + " country_code2 = ''\n", |
| 132 | + " return country_code2\n", |
| 133 | + "\n", |
| 134 | + "# leveraging the Country Code UDF\n", |
| 135 | + "\n", |
| 136 | + "udf_get_country_code2 = udf(lambda z: get_country_code2(z), StringType())\n", |
| 137 | + "\n", |
| 138 | + "\n", |
| 139 | + "# Reading the dataset into a DataFrame\n", |
| 140 | + "s3_bucket = \"s3://${BUCKET_NAME}/\" # <------- PLEASE REPLACE ONLY THE ${BUCKET_NAME} HERE (Keep the \"s3://\" and the final \"/\" part)!!!\n", |
| 141 | + "job_time_string = datetime.now().strftime(\"%Y%m%d%H%M%S\")\n", |
| 142 | + "\n", |
| 143 | + "df = spark.read.load(s3_bucket + \"input/lab2/sample.csv\", \n", |
| 144 | + " format=\"csv\", \n", |
| 145 | + " sep=\",\", \n", |
| 146 | + " inferSchema=\"true\", \n", |
| 147 | + " header=\"true\")\n", |
| 148 | + "\n", |
| 149 | + "# Performing a transformation that adds a new Country Code column to the dataframe based on the Country Code UDF output\n", |
| 150 | + "\n", |
| 151 | + "new_df = df.withColumn('country_code_2', udf_get_country_code2(col(\"country\")))" |
| 152 | + ] |
| 153 | + }, |
| 154 | + { |
| 155 | + "cell_type": "code", |
| 156 | + "execution_count": null, |
| 157 | + "metadata": { |
| 158 | + "trusted": true, |
| 159 | + "vscode": { |
| 160 | + "languageId": "python_glue_session" |
| 161 | + } |
| 162 | + }, |
| 163 | + "outputs": [], |
| 164 | + "source": [ |
| 165 | + "# Sinking the data into another S3 bucket path\n", |
| 166 | + "\n", |
| 167 | + "new_df.write.csv(s3_bucket + \"/output/lab3/notebook/\" + job_time_string + \"/\")" |
| 168 | + ] |
| 169 | + } |
| 170 | + ], |
| 171 | + "metadata": { |
| 172 | + "kernelspec": { |
| 173 | + "display_name": "Glue PySpark", |
| 174 | + "language": "python", |
| 175 | + "name": "glue_pyspark" |
| 176 | + }, |
| 177 | + "language_info": { |
| 178 | + "codemirror_mode": { |
| 179 | + "name": "python", |
| 180 | + "version": 3 |
| 181 | + }, |
| 182 | + "file_extension": ".py", |
| 183 | + "mimetype": "text/x-python", |
| 184 | + "name": "Python_Glue_Session", |
| 185 | + "pygments_lexer": "python3" |
| 186 | + } |
| 187 | + }, |
| 188 | + "nbformat": 4, |
| 189 | + "nbformat_minor": 4 |
| 190 | +} |
0 commit comments