代写COMP9313 Big Data Management Lab2代做Python编程

COMP9313 Big Data Management

Aims

This exercise aims to get you to:

· Compile, run, and debug MapReduce tasks via Hadoop Streaming

· Compile, run, and debug MapReduce tasks via MRJob

· Apply the design pattern “in-mapper combining” you have learned in Chapter 2.2 to MapReduce programming

One Tip on Hadoop File System Shell

Following are the three commands which appear same but have minute differences:

1. hadoop fs {args}

2. hadoop dfs {args}

3. hdfs dfs {args}

The first command: fs relates to a generic file system that can point to any file system like local, HDFS etc. So this can be used when you are dealing with different file systems such as Local FS, HFTP FS, S3 FS, and others.

The second command: dfs is very specific to HDFS. It would work for operations relates to HDFS. This has been deprecated and we should use hdfs dfs instead.

The third command: It is the same as 2nd. It would work for all the operations related to HDFS and is the recommended command instead of hadoop dfs.

Thus, in our labs, it is always recommended to use hdfs dfs {args}.

Hadoop Streaming

Hadoop streaming is a utility that comes with the Hadoop distribution. The utility allows you to create and run Map/Reduce jobs with any executable or script. as the mapper/reducer.

NOTE: Please be careful when directly copying and pasting the commands in this instruction. It is better to enter the command by yourself.

1. In Lab1, you tested an application of word count. In Linux, wc command can be used to find out the number of lines, word count, byte and character count in the files specified in the file arguments. Test wc command:

NOTE: Please use the whereis command to check the location of the cat and wc commands. For example, $whereis wc

$ wc $HADOOP_HOME/etc/hadoop/*.xml

2. Run a streaming task with /bin/cat as the mapper and /bin/wc as the reducer:

$ hadoop jar $HADOOP_HOME/share/hadoop/tools/lib/hadoop-streaming-*.jar \

-input input \

-output output2 \

-mapper /bin/cat \

-reducer /bin/wc

3. Check out the output:

$ hdfs dfs -cat output2/*

If you are using Mac OS, please add ‘\’ before the ‘*’ if the file/folder exists but you still see some errors. For example, in step 3 (check out the output)

$ hdfs dfs -cat output2/\*

4. To specify the number of reducers, for example two, use:

$ hadoop jar $HADOOP_HOME/share/hadoop/tools/lib/hadoop-streaming-*.jar \

-D mapreduce.job.reduces=2 \

-input input \

-output output3 \

-mapper /bin/cat \

-reducer /usr/bin/wc

Specifying Python Scripts as Mapper/Reducer

Next, you will learn to run Map/Reduce jobs with Python script. as the mapper/reducer.

#!/usr/bin/python3

import sys

for line in sys.stdin:

line = line.strip()

words = line.split()

for word in words:

print (word + "\t" + "1")

1. Create a folder “Lab2”, and put all your codes written in this weeks lab in this folder. Next, create a file named mapper.py and copy the codes below into the file.

Make sure this file has the execution permission:

$ chmod +x mapper.py

#!/usr/bin/python3

import sys

results = {}

for line in sys.stdin:

word, frequency = line.strip().split('\t', 1)

results[word] = results.get(word, 0) + int(frequency)

words = list(results.keys())

for word in words:

print(word, results[word])

2. Similarly, create a file named reducer.py and copy the codes below into the file.

Also, make sure this file has the execution permission:

$ chmod +x reducer.py

Compare the above code with that provided in slide 38 of Lecture 2.1. What are the differences? What problem the above approach may encounter?

3. Run the application (you need to enter the Lab2 folder):

$ hadoop jar $HADOOP_HOME/share/hadoop/tools/lib/hadoop-streaming-*.jar \

-input input \

-output python_output \

-mapper mapper.py \

-reducer reducer.py \

-file mapper.py \

-file reducer.py

4. Check out the output (you can also download the file from HDFS):

$ hdfs dfs -cat python_output/*

Try to Write Your First Hadoop Streaming Job

1. Download the test file “pg100.txt” to your home folder from WebCMS3, and put it to HDFS:

$ hdfs dfs -rm input/*

$ hdfs dfs -put ~/pg100.txt input

2. Now please write your first MapReduce job with Hadoop Streaming to accomplish the following task:

Output the number of words that start with each letter. This means that for every letter we want to count the total number of words that start with that letter. In your implementation,  please first convert all words to lowercase. You can ignore all non-alphabetic characters.  Create “mapper. py”, “reducer.py” and “combiner.py” scripts in the folder “LetterCount” to finish this task.

Hint: In the (key, value) output, each letter is the key, and its count is the value.

You can run the three scripts on Hadoop MapReduce by:

$ hadoop jar $HADOOP_HOME/share/hadoop/tools/lib/hadoop-streaming-*.jar \

-input input \

-output python_output \

-mapper mapper.py \

-reducer reducer.py \

-combiner combiner.py \

-file mapper.py \

-file reducer.py \

-file combiner.py

Compare your results with the answer provided at:

https://webcms3.cse.unsw.edu.au/COMP9313/23T3/resources/92020

Install MRJob

If you use Mac, you can skip installing pip3, since it is already in Mac OS.

As you will install mrjob with pip3, you should first install pip3:

$ sudo apt install python3-pip

Then, you will be asked to enter the sudo password: comp9313

When pip3 is successfully installed, install mrjob with pip3:

$ pip3 install mrjob

Warning: You also need to configure and start YARN since running mrjob on Hadoop requires YARN. Please edit the mapred-site.xml and yarn-site.xml by following Lab 1 instructions. Then, start HDFS and YARN by running “start-dfs.sh” and “start-yarn.sh”.

An Example of MRJob

Create a file called mr_word_count.py in the folder Lab2 and type this into it:

from mrjob.job import MRJob

class MRWordFrequencyCount(MRJob):

def mapper(self, _, line):

yield "chars", len(line)

yield "words", len(line.split())

yield "lines", 1

def reducer(self, key, values):

yield key, sum(values)

if __name__ == '__main__':

MRWordFrequencyCount.run()

We still use the downloaded file “pg100.txt” as input (make sure it exists in /home/comp9313). Run the codes locally with:

$ python3 mr_word_count.py ~/pg100.txt

You will see the result:

If you want to run in Hadoop, use the “-r hadoop” option, and then you can check the result in the file “output”:

$ python3 mr_word_count.py -r hadoop pg100.txt > output

If your files are in HDFS, you can run like:

$ python3 mr_word_count.py -r hadoop hdfs://localhost:9000/user/comp9313/input

If you want to store your results in HDFS (e.g., in the “output” folder) as well, you can run:

$ python3 mr_word_count.py -r hadoop hdfs://localhost:9000/user/comp9313/input -o hdfs://localhost:9000/user/comp9313/output

There are different ways to run your job, see more details here.

Try to Write Your First MRJob Program

Please write your first mrjob program to complete the above “letter count” task, and compare it with the Hadoop streaming approach.

Improve WordCount by In-Mapper Combining

A better tokenization method:

Use the following codes to tokenize a line of document:

import re

words = re.split("[ *$&#/\t\n\f\"\'\\,.:;?!\[\](){}<>~\-_]", line.lower())

Documents will be split according to all characters specified (i.e., " *$&#/\t \n\f"'\,.:;?![](){}<>~-_"), and higher quality terms will be generated.

Convert all terms to lower case as well (by using lower() method).

Apply this to mapper.py of WordCount we have used. Note that you need to check if the word is an empty string now.

a) Put the input file to HDFS by:

$ hdfs dfs –rm input/*

$ hdfs dfs –put ~/pg100.txt input

b) Go into the folder Lab2 and use the existing mapper.py and reducer.py scripts.

c) Use the new method to tokenize a line of document

d) Run the application with Hadoop Streaming/mrjob

e) Remember to delete to output folder if it exists in HDFS

f) If you forget the details, please refer to the previous instructions.

Type the following command in the terminal:

$ hdfs dfs -cat output/* | head

You should see results:

Use the following command:

$ hdfs dfs -cat output/* | tail

You should see:


Apply the in-mapper combing approach:

Based on the previous python scripts, you are required to write an improved version using the “in-mapper combining” approach.

Hadoop Streaming: Create a new script “mapper2.py” in the folder Lab2 and solve this problem. Your results should be the same as generated by mapper.py.

mrjob: Create a new script “mr_word_count2.py” in the folder Lab2 and solve this problem. Your results should be the same as generated by mapper.py.

Hints:

1. Refer to the pseudo-code shown in slide 23 of Chapter 2.2.

2. You can use a dictionary in the mapper script. to buffer the partial results for Hadoop streaming.

3. You need to override the methods mapper_init() and mapper_final() in mrjob

4. Do you need to change the reducer?

Solutions of these Problems

I hope that you are able to finish all problems by yourself, since the hints are already given. All the source codes will be published in the course homepage on Friday.


热门主题

课程名

mktg2509 csci 2600 38170 lng302 csse3010 phas3226 77938 arch1162 engn4536/engn6536 acx5903 comp151101 phl245 cse12 comp9312 stat3016/6016 phas0038 comp2140 6qqmb312 xjco3011 rest0005 ematm0051 5qqmn219 lubs5062m eee8155 cege0100 eap033 artd1109 mat246 etc3430 ecmm462 mis102 inft6800 ddes9903 comp6521 comp9517 comp3331/9331 comp4337 comp6008 comp9414 bu.231.790.81 man00150m csb352h math1041 eengm4100 isys1002 08 6057cem mktg3504 mthm036 mtrx1701 mth3241 eeee3086 cmp-7038b cmp-7000a ints4010 econ2151 infs5710 fins5516 fin3309 fins5510 gsoe9340 math2007 math2036 soee5010 mark3088 infs3605 elec9714 comp2271 ma214 comp2211 infs3604 600426 sit254 acct3091 bbt405 msin0116 com107/com113 mark5826 sit120 comp9021 eco2101 eeen40700 cs253 ece3114 ecmm447 chns3000 math377 itd102 comp9444 comp(2041|9044) econ0060 econ7230 mgt001371 ecs-323 cs6250 mgdi60012 mdia2012 comm221001 comm5000 ma1008 engl642 econ241 com333 math367 mis201 nbs-7041x meek16104 econ2003 comm1190 mbas902 comp-1027 dpst1091 comp7315 eppd1033 m06 ee3025 msci231 bb113/bbs1063 fc709 comp3425 comp9417 econ42915 cb9101 math1102e chme0017 fc307 mkt60104 5522usst litr1-uc6201.200 ee1102 cosc2803 math39512 omp9727 int2067/int5051 bsb151 mgt253 fc021 babs2202 mis2002s phya21 18-213 cege0012 mdia1002 math38032 mech5125 07 cisc102 mgx3110 cs240 11175 fin3020s eco3420 ictten622 comp9727 cpt111 de114102d mgm320h5s bafi1019 math21112 efim20036 mn-3503 fins5568 110.807 bcpm000028 info6030 bma0092 bcpm0054 math20212 ce335 cs365 cenv6141 ftec5580 math2010 ec3450 comm1170 ecmt1010 csci-ua.0480-003 econ12-200 ib3960 ectb60h3f cs247—assignment tk3163 ics3u ib3j80 comp20008 comp9334 eppd1063 acct2343 cct109 isys1055/3412 math350-real math2014 eec180 stat141b econ2101 msinm014/msing014/msing014b fit2004 comp643 bu1002 cm2030
联系我们
EMail: 99515681@qq.com
QQ: 99515681
留学生作业帮-留学生的知心伴侣!
工作时间:08:00-21:00
python代写
微信客服:codinghelp
站长地图