MapReduce implementation

Use python to implement programming to realize file merging and deduplication operations

1. View the python environment

image-20211228140722877

2. Write mapper function and reducer function

Under the /usr/local/hadoop/wc folder (create one if there is no folder)

gedit mapper.py

mapper.py

#!/usr/bin/env python3
# encoding=utf-8

lines = {
    
    }

import sys
for line in sys.stdin:
    line = line.strip()
    key, value = line.split()
    if key not in lines.keys():
        lines[key] = []
    if value not in lines[key]:
        lines[key].append(value)
        lines[key] = sorted(lines[key])

for key, value in lines.items():
    print(key, end = ' ')
    for i in value:
        print(i, end = ' ')
    print()

gedit reducer.py

reducer.py

#!/usr/bin/env python3
# encoding=utf-8

import sys

key, values = None, []

for line in sys.stdin:
    line = line.strip().split(' ')
    if key == None:
        key = line[0]
    if line[0] != key:
        key = line[0]
        values = []
    if line[1] not in values:
        print('%s\t%s' % (line[0], line[1]))
        values.append(line[1])

3. Runnable permission setting and viewing

chomod a+x ./mapper.py
chomod a+x ./mapper.py

image-20211228141022424

4. Running on Hadoop to implement file merging and deduplication operations

Write A.txt, B.txt

gedit A.txt

20150101 x
20150102 y
20150103 x
20150104 y
20150105 z
20150106 x

image-20211228170127914

gedit B.txt

20150101 y
20150102 y
20150103 x
20150104 z
20150105 y

Run Python code on Hadoop

If there is a problem with the hdfs command, add the environment variable

image-20211228170420365

export PATH=$PATH:/usr/local/hadoop/bin

image-20211228170525076

If there is no input folder, you need to create it

image-20211228170213590

export STREAM=$HADOOP_HOME/share/hadoop/tools/lib/hadoop-streaming-*.jar

hadoop jar $STREAM  \
-file /usr/local/hadoop/wc/mapper.py \
-mapper /usr/local/hadoop/wc/mapper.py \
-file /usr/local/hadoop/wc/reducer.py \
-reducer /usr/local/hadoop/wc/reducer.py \
-input /input/* \
-output /output

Guess you like

Origin blog.csdn.net/asd1484507772/article/details/122198305