<jupyter_start><jupyter_text>Ingest Data with EMRThis notebook demonstrates how to read the data from the EMR cluster.We are going to use the data we load into S3 in the previous notebook [011_Ingest_tabular_data.ipynb](011_Ingest_tabular_data_v1.ipynb).Amazon EMR is the industry-leading cloud big data platform for processing vast amounts of data using open source tools such as Apache Spark, Apache Hive, Apache HBase, Apache Flink, Apache Hudi, and Presto. With EMR you can run Petabyte-scale analysis at less than half of the cost of traditional on-premises solutions and over 3x faster than standard Apache Spark.   Set up NotebookFirst, we are going to make sure we have the EMR Cluster set up and the connection between EMR and Sagemaker Notebook set up correctly. You can follow the [documentation](https://aws.amazon.com/blogs/machine-learning/build-amazon-sagemaker-notebooks-backed-by-spark-in-amazon-emr/) and [procedure](https://docs.aws.amazon.com/sagemaker/latest/dg/nbi-lifecycle-config-emr.html) to set up this notebook. Once you are done with setting up, restart the kernel and run the following command to check if you set up the EMR and Sagemaker connection correctly.<jupyter_code>%%info
%%local
import sagemaker
from sklearn.datasets import *
import pandas as pd

sagemaker_session = sagemaker.Session()
s3 = sagemaker_session.boto_session.resource("s3")
bucket = sagemaker_session.default_bucket()  # replace with your own bucket name if you have one
prefix = "data/tabular/boston_house"
filename = "boston_house.csv"<jupyter_output><empty_output><jupyter_text>Download data from online resources and write data to S3<jupyter_code>%%local
# helper functions to upload data to s3
def write_to_s3(filename, bucket, prefix):
    # put one file in a separate folder. This is helpful if you read and prepare data with Athena
    filename_key = filename.split(".")[0]
    key = "{}/{}/{}".format(prefix, filename_key, filename)
    return s3.Bucket(bucket).upload_file(filename, key)


def upload_to_s3(bucket, prefix, filename):
    url = "s3://{}/{}/{}".format(bucket, prefix, filename)
    print("Writing to {}".format(url))
    write_to_s3(filename, bucket, prefix)
%%local
tabular_data = load_boston()
tabular_data_full = pd.DataFrame(tabular_data.data, columns=tabular_data.feature_names)
tabular_data_full["target"] = pd.DataFrame(tabular_data.target)
tabular_data_full.to_csv("boston_house.csv", index=False)

upload_to_s3(bucket, "data/tabular", filename)
%%local
data_s3_path = "s3://{}/{}/{}".format(bucket, prefix, filename)
print("this is path to your s3 files: " + data_s3_path)<jupyter_output><empty_output><jupyter_text>Copy the S3 bucket file pathThe S3 bucket file path is required to read the data on EMR Spark. Copy and paste the path string shown above into the next cell.<jupyter_code>### replace this path string with your path shown in last step
data_s3_path = "s3://sagemaker-us-east-2-060356833389/data/tabular/boston_house/boston_house.csv"<jupyter_output><empty_output><jupyter_text>Read the data in EMR spark ClusterOnce we have a path to our data in S3, we can use `spark s3 select` to read data with the following command. You can specify a data format, schema is not necessary but recommended, and in options you can specify `compression`, `delimiter`, `header`, etc. For more details, please see [documentation on using S3 select with Spark](https://docs.aws.amazon.com/emr/latest/ReleaseGuide/emr-spark-s3select.html).<jupyter_code># EMR cell
schema = " CRIM double, ZN double, INDUS double,\
CHAS double, NOX double, RM double,  AGE double, DIS double,  RAD double,  TAX double, PTRATIO double, \
B double,  LSTAT double, target double"
df = spark.read.format("csv").schema(schema).options(header="true").load(data_s3_path)
df.show(5)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09641079381713387, 'fraction_numerical': 0.012313335079905684, 'mean_word_length': 5.515358361774744, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '124929', 'license': 'Apache-2.0', 'n_tokens_mistral': 1208, 'n_tokens_neox': 1174, 'n_words': 410, 'path': 'ingest_data/04_Ingest_data_with_EMR.ipynb', 'repo_name': 'Amirosimani/amazon-sagemaker-examples', 'size': '3817'}