Databricks How To Data Import PDF
Databricks How To Data Import PDF
Databricks How To Data Import PDF
How-To Guide
3. Click on the bucket you have just created. For the demonstration
purposes, the name of my bucket is my-data-for-databricks.
From here, click on the Upload button.
4. In the Upload Select Files and Folders dialog, you will be able to
add your files into S3.
5. Click on Add Files and you will be able to upload your data into S3.
Below is the dialog to choose sample web logs from my local box.
Click Choose when you have selected your file(s) and then click Start
Upload.
6. Once your files have been uploaded, the Upload dialog will show
the files that have been uploaded into your bucket (in the left pane),
as well as the transfer process (in the right pane).
Now that you have uploaded data into Amazon S3, you are ready to use your
Databricks account. Additional information:
H
ow to upload data using alternate methods, continue reading this
document.
H
ow to connect your Databricks account to the data you just uploaded,
please skip ahead to Connecting to Databricks on page 9.
To learn more about Amazon S3, please refer to What is Amazon S3.
b) For Mac or Linux systems, ensure you are running Python 2.6.5 or
higher (for most new systems, you would already have Python 2.7.2
installed) and install using pip.
pip install awscli
F ind your own user name whom you will be using the user
credentials
Scroll down the menu to Security Credentials > Access Keys
A
t this point, you can either Create Access Key or use an
existing key if you already have one.
For more information, please refer to AWS security credentials.
This command allows you to set your AWS security credentials (click
for more information). When configuring your credentials, the resulting
output should look something similar to the screenshot below.
Note, the default region name is us-west-2 for the purpose of this demo.
Based on your geography, your default region name may be different.
You can get the full listing of S3 region-specific end points at Region and
End Points > Amazon Simple Storage Service (S3).
4. Copy your files to S3
Create a bucket for your files (for this demo, the bucket being created is
my-data-for-databricks) using the make bucket (mb) command.
aws s3 mb s3://my-data-for-databricks/
Then, you can copy your files up to S3 using the copy (cp) command.
aws s3 cp . s3://my-data-for-databricks/ --recursive
If you would like to use the sample logs that are used in this technical
note, you can download the log files from http://bit.ly/1MuGbJy.
The output of from a successful copy command should be similar the
one below.
upload: ./ex20111215.log to s3://my-data-fordatabricks/ex20111215.log
upload: ./ex20111214.log to s3://my-data-fordatabricks/ex20111214.log
Connecting to Databricks
In the previous section, we covered the steps required to upload your data
into S3. In this section, we will cover how you can access this data within
Databricks. This section presumes the following
Y ou have completed the previous section and/or have AWS
credentials to access data.
Y ou have a Databricks account; if you need one, please go to
Databricks Account for more information.
You have a running Databricks cluster.
For more information, please refer to:
Introduction to Databricks video
Welcome to Databricks notebook in the Databricks Guide (top
item under Workspace when you log into your Databricks account).
2. M
ount your S3 bucket to the Databricks File System (DBFS).
This allows you to avoid entering AWS keys every time you connect
to S3 to access your data (i.e. you only have to enter the keys once).
A DBFS mount is a pointer to S3 and allows you to access the data
as if your files were stored locally.
import urllib
ACCESS_KEY = "REPLACE_WITH_YOUR_ACCESS_KEY"
SECRET_KEY = "REPLACE_WITH_YOUR_SECRET_KEY"
ENCODED_SECRET_KEY = urllib.quote(SECRET_KEY, "")
AWS_BUCKET_NAME = "REPLACE_WITH_YOUR_S3_BUCKET"
MOUNT_NAME = "REPLACE_WITH_YOUR_MOUNT_NAME"
dbutils.fs.mount("s3n://%s:%s@%s" % (ACCESS_KEY, ENCODED_
SECRET_KEY, AWS_BUCKET_NAME), "/mnt/%s" % MOUNT_NAME)
10
11
The output from this command should be similar to the output below.
> myApacheLogs.count()
Out[18]: 4468
Command took 0.98s
12
# sc is an existing SparkContext.
from pyspark.sql import SQLContext, Row
# Load the space-delimited web logs (text files)
parts = myApacheLogs.map(lambda l: l.split(" "))
apachelogs = parts.map(lambda p: Row(ipaddress=p[0],
clientidentd=p[1], userid=p[2], datetime=p[3], tmz=p[4],
method=p[5], endpoint=p[6], protocol=p[7], responseCode=p[8],
contentSize=p[9]))
# Infer the schema, and register the DataFrame as a table.
schemaWebLogs = sqlContext.createDataFrame(apachelogs)
schemaWebLogs.registerTempTable("apachelogs")
endpoint=u/index.html),
endpoint=u/Cascades/rss.xml),
endpoint=u/Olympics/rss.xml),
endpoint=u/Hurricane+Ridge/rss.xml),
endpoint=u/index.html),
endpoint=u/Cascades/rss.xml),
endpoint=u/index.html),
endpoint=u/Olympics/rss.xml),
endpoint=u/index.html),
endpoint=u/index.html)]
13
Because you had registered the weblog DataFrame, you can also access this
directly from a Databricks SQL notebook. For example, below is screenshot
of running the SQL command
select ipaddress, endpoint from weblogs limit 10;
With the query below, you can start working with the notebook graphs.
select ipaddress, count(1) as events from apachelogs
group by ipaddress order by events desc;
14
Summary
This How-To Guide has provided a quick jump start on how to import your
data into AWS S3 as well as into Databricks.
For next steps, please continue with:
Continue the Introduction sections of the Databricks Guide
Review the Log Analysis Example: How-to Guide.
Watch a Databricks Webinar including
Building a Turbo-fast Data Warehousing Platform with Databricks
Spark DataFrames: Simple and Fast Analysis of Structured Data.
15
Additional Resoures
If youd like to analyze your Apache access logs with Databricks, you can
evaluate Databricks with a trial account now. You can also find the source
code on Github.
Other Databricks how-tos can be found at:
The Easiest Way to Run Spark Jobs
16