DSC650
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Help
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
--- title: DSC 650 (10 Week) Week 04 tags: dsc650, 10-week, lessons subtitle: "Batch Processing Workflow" --- # Week 4 [![hackmd-github-sync-badge](https://hackmd.io/J9CmnGX8RMGZxAqrEMPBkw/badge)](https://hackmd.io/J9CmnGX8RMGZxAqrEMPBkw) In this lesson, you learned how to implement batch processing (i.e., not real-time) using a typical batch processing workflow techniques. You will also gain an understanding of how frameworks such as Hadoop, Spark, and TensorFlow parallelize certain computational tasks. ## Objectives After completing this week, you should be able to: * Implement a rudimentary version of the MapReduce paradigm in Python * Create a simple deep learning network using Keras and TensorFlow * Design, implement and run a big data workflow using Luigi ## Readings * Read chapter 10 in *Designing Data-Intensive Applications* * Read chapter 3 in *Deep Learning with Python* ## Weekly Resources * [Celery Architecture][celery-architecture] * [Kubernetes Architecture][kubernetes-architecture] * [HDFS Architecture][hdfs-architecture] * [YARN Architecture][yarn-architecture] ## Assignment 4 In this assignment, you will be creating a workflow to process emails from Enron that were made available by the Federal Energy Regulatory Commission during its investigation of the company. The original data is not in a machine-friendly format, so we will use Python’s built-in `email` package to read the emails and create a machine-friendly dataset. The `data/external/enron` folder contains a partial copy of the original Enron email dataset (you can download the full dataset [here][enron-dataset]). Each folder represents a single users' email account. Each one of those folders contains that user's top-level folders and those folders contain the individual emails. The following is the directory structure of a single user folder. ```shell enron/zipper-a ├── all_documents │   ├── 1. │   ├── 10. │   ├── 11. │   ├── 12. │   ├── 13. │   ├── 14. . . . │   ├── 8. │   └── 9. └── tss ├── 1. ├── 10. ├── 11. ├── 12. . . . ├── 4. ├── 5. ├── 6. ├── 7. ├── 8. └── 9. ``` Looking at the example of `/enron/zipper-a/inbox/114.` demonstrates the email structure. The email starts with standard email headers and then includes a plain text message body. This is typical of most of the emails except some email bodies being encoded in HTML. ``` Message-ID: <6742786.1075845426893.JavaMail.evans@thyme> Date: Thu, 7 Jun 2001 11:05:33 -0700 (PDT) From: jeffrey.hammad@enron.com To: andy.zipper@enron.com Subject: Thanks for the interview Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-From: Hammad, Jeffrey </O=ENRON/OU=NA/CN=RECIPIENTS/CN=NOTESADDR/CN=CBBE377A-24F58854-862567DD-591AE7> X-To: Zipper, Andy </O=ENRON/OU=NA/CN=RECIPIENTS/CN=AZIPPER> X-cc: X-bcc: X-Folder: \Zipper, Andy\Zipper, Andy\Inbox X-Origin: ZIPPER-A X-FileName: Zipper, Andy.pst Andy, Thanks for giving me the opportunity to meet with you about the Analyst/ Associate program. I enjoyed talking to you, and look forward to contributing to the success that the program has enjoyed. Thanks and Best Regards, Jeff Hammad ``` For this assignment, you will be parsing each one of those emails into a structured format. The following are the fields that should appear in the structured output. | Field | Description | |-------------------------|-------------------------------------------------------| |username |The username of this mailbox (the name of email folder)| |original_msg |The original, unparsed email message | |payload |The unparsed payload of the email | |Message-ID |'Message-ID' from email header | |Date |Parsed datetime from email header | |From |'From' from email header | |To |'To' from email header | |Subject |'Subject' from email header | |Mime-Version |'Mime-Version' from email header | |Content-Type |'Content-Type' from email header | |Content-Transfer-Encoding|'Content-Transfer-Encoding' from email header | |X-From |'X-From' from email header | |X-To |'X-To' from email header | |X-cc |'X-cc' from email header | |X-bcc |'X-bcc' from email header | |X-Folder |'X-Folder' from email header | |X-Origin |'X-Origin' from email header | |X-FileName |'X-FileName' from email header | |Cc |'Cc' from email header | |Bcc |'Bcc' from email header | The `dsc650/assignments/assignment04` folder contains partially completed code and placeholder files for this assignment. ### Assignment 4.1 The first part of the assignment is to implement a single function that takes the path to an email file and returns a dictionary containing the fields listed in the previous table. The folder `dsc650/assignments/assignment04/examples` contains examples of messages with both plain and HTML message payloads. It is recommended that you start by parsing these examples first to ensure your `read_email` function is working properly. ### Assignment 4.2 Next, you will be creating a workflow using the [Luigi][luigi-docs] Python library. This assignment uses Luigi because it is a self-contained Python package and does not require any additional configuration to run. There are many other workflow managers including [Apache Airflow][apache-airflow], [Apache Oozie][apache-oozie], [LinkedIn's Azkaban][azkaban], [Netflix's Conductor][conductor], and [Argo for Kubernetes][argo]. Luigi, like most workflow engines, breaks workflows into discrete tasks. Individual tasks chain together into workflows by telling the workflow engine which tasks depend on one another. These task dependencies take the form of a [directed acyclic graph][dag](DAG). While this may sound complicated, a DAG is a flowchart of which tasks should be executed in what order (the order means it is directed) with the constraint that later tasks cannot loop back and depend on earlier tasks (hence the acyclic requirement). Read the [Luigi documentation on its execution model][luigi-execution] for more information. To start with, you will have one [wrapper task](https://luigi.readthedocs.io/en/stable/luigi_patterns.html#triggering-many-tasks) that triggers a task to process each folder. Later, we will add tasks that process the outputs of those tasks. The following code provides a rough outline of this workflow. ```python import luigi class ProcessMailbox(luigi.Task): mailbox_directory = luigi.Parameter() processed_directory = luigi.Parameter() def output(self): pass def run(self): pass class ProcessEnronEmails(luigi.WrapperTask): emails_directory = luigi.Parameter() processed_directory = luigi.Parameter() def requires(self): for directory in directories: yield ProcessMailbox( mailbox_directory=str(directory), processed_directory=str(self.processed_directory) ) def main(): tasks = [ ProcessEnronEmails(emails_directory=emails_directory, processed_directory=processed_directory) ] luigi.build(tasks, workers=8, local_scheduler=True) if __name__ == '__main__': main() ``` Create and run this workflow. The following is the example of a workflow that completed with one failed task. Failures could be caused by problems with the workflow or problems with the data. The advantage of using a tool like Luigi is that you don't need to re-run all the tasks, only the ones that failed. ```shell ===== Luigi Execution Summary ===== Scheduled 151 tasks of which: * 23 complete ones were encountered: - 23 ProcessMailbox(mailbox_directory=dsc650/data/external/enron/dean-c, processed_directory=dsc650/data/processed/enron) ... * 126 ran successfully: - 126 ProcessMailbox(mailbox_directory=dsc650/data/external/enron/allen-p, processed_directory=dsc650/data/processed/enron) ... * 1 failed: - 1 ProcessMailbox(mailbox_directory=dsc650/data/external/enron/stokley-c, processed_directory=dsc650/data/processed/enron) * 1 were left pending, among these: * 1 had failed dependencies: - 1 ProcessEnronEmails(emails_directory=dsc650/data/external/enron, processed_directory=dsc650/data/processed/enron) This progress looks :( because there were failed tasks ===== Luigi Execution Summary ===== ``` This is an example of a workflow that ran without errors. ```shell INFO: ===== Luigi Execution Summary ===== Scheduled 12 tasks of which: * 12 ran successfully: - 1 ProcessEnronEmails(emails_directory=dsc650/data/external/enron, processed_directory=dsc650/data/processed/enron) - 11 ProcessMailbox(mailbox_directory=dsc650/data/external/enron/davis-d, processed_directory=dsc650/data/processed/enron) ... This progress looks :) because there were no failed tasks or missing dependencies ===== Luigi Execution Summary ===== ``` Additionally, Luigi has a [web-based central scheduler](https://luigi.readthedocs.io/en/stable/central_scheduler.html) that you use to view and manage the progress of your workflows. ### Assignment 4.3 Now that you have processed the emails and extracted the text payload, you are going to further process them with a simple MapReduce program. We will start by performing a simple word count on text data; the *hello world* of MapReduce. The [Spark Examples](http://spark.apache.org/examples.html) page has a simple example of a word count in Python. We will walk through this example together and then you will implement your version of MapReduce in Python. ```python text_file = sc.textFile("hdfs://...") counts = text_file.flatMap(lambda line: line.split(" ")) \ .map(lambda word: (word, 1)) \ .reduceByKey(lambda a, b: a + b) counts.saveAsTextFile("hdfs://...") ``` While the example may look complicated, the entire program consists of applying three functions to a text file. After the program loads the text file and assigns it to the `text_file` variable, it applies a function to each line in the text file. And outputs a list of words. The program uses the `flatMap` because it produces multiple outputs for every input parameter (i.e., multiple words for every line). The `map` function then takes each of those words and outputs a key-value pair for each word. If we had the sentence `The quick brown fox jumps over the lazy dog`, the map function would output the following key-value pairs. ``` def split_to_words(line): for word in line.split(" ") yield (word, 1) split_to_words('The quick brown fox jumps over the lazy dog') (The, 1) (quick, 1) (brown, 1) (fox, 1) (jumps, 1) (over, 1) (the, 1) (lazy, 1) (dog, 1) ``` These steps are part of the map stage of the MapReduce programming paradigm. Map functions scale extremely well to large datasets as they can be run in parallel without any coordination. Each mapper takes a different chunk of the data and outputs a series of key-value pairs. By contrast, the reduce step requires sorting the key-value pairs and combine the values with common keys. To illustrate, suppose we have two mappers with the following output from the first mapper. ``` (the, 1) (data, 1) (the, 1) (data, 1) (jump, 1) (data, 1) ``` Then suppose the second mapper has this output. ``` (the, 1) (hide, 1) (data, 1) (the, 1) (data, 1) ``` The reduce phase sorts each of the mapper outputs by their keys and combines. Thus, the reducer would act on the outputs as follow. ``` reducer( (the, 1), (the, 1), (the, 1), (the, 1) ) -> (the, 4) reducer( (data, 1), (data, 1), (data, 1), (data, 1), (data, 1) ) -> (data, 5) reducer((jump, 1)) -> (jump, 1) reducer((hide, 1)) -> (hide, 1) ``` After combining all the results of all the mappers with the reducers, we then have a count for each of the words. Implement a mapper as a Luigi task that outputs the words and counts for each of the mailboxes. Then implement a reducer that aggregates all of those counts into a combined count. The `dsc650/assignments/assignment04/results/` should look like as follows. ``` results ├── count.txt └── words ├── davis-d.txt ├── gay-r.txt ├── may-l.txt . . . └── zipper-a.txt ``` These tasks should be a part of the previous workflow you created. ## Submission Instructions For this assignment, you will submit a zip archive containing the contents of the `dsc650/assignments/assignment04/` directory. Use the naming convention of `assignment04_LastnameFirstname.zip` for the zip archive. You can create this archive in Bash (or a similar Unix shell) using the following commands. ```shell cd dsc650/assignments zip -r assignment04_DoeJane.zip assignment04 ``` Likewise, you can create a zip archive using Windows PowerShell with the following command. ```shell Compress-Archive -Path assignment04 -DestinationPath 'assignment04_DoeJane.zip ``` ## Discussion For this discussion, describe a batch workflow use case that you would run on a daily, weekly, or monthly basis. What are the inputs and the outputs? Use the DSC 650 Slack channel for discussion and replies. For grading purposes, copy and paste your initial post and at least two replies to the Blackboard discussion board. [apache-airflow]: https://airflow.apache.org/docs/stable/ [apache-oozie]: https://oozie.apache.org/ [argo]: https://argoproj.github.io/ [azkaban]: https://azkaban.github.io/ [celery-architecture]: https://wiki.openstack.org/wiki/Celery [conductor]: https://netflix.github.io/conductor/ [dag]: https://en.wikipedia.org/wiki/Directed_acyclic_graph [enron-dataset]: https://www.cs.cmu.edu/~enron/ [hdfs-architecture]: https://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-hdfs/HdfsDesign.html [kubernetes-architecture]: https://kubernetes.io/docs/concepts/architecture/ [luigi-docs]: https://luigi.readthedocs.io/en/stable/index.html [luigi-execution]: https://luigi.readthedocs.io/en/stable/execution_model.html [yarn-architecture]: https://hadoop.apache.org/docs/current/hadoop-yarn/hadoop-yarn-site/YARN.html

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully