Skip to content

Commit 41246de

Browse files
committed
fix flask-hadoop
1 parent 07dba0e commit 41246de

File tree

13 files changed

+82
-38
lines changed

13 files changed

+82
-38
lines changed

18.flask-hadoop/Dockerfile

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ RUN apt-get update && apt-get install -y \
88
python3-pip \
99
python3-wheel \
1010
openjdk-8-jdk \
11-
supervisor
11+
supervisor \
12+
wget
1213

13-
RUN wget http://apache.crihan.fr/dist/hadoop/common/hadoop-3.1.0/hadoop-3.1.0.tar.gz && \
14-
tar -xzf hadoop-3.1.0.tar.gz && \
15-
mv hadoop-3.1.0 $HADOOP_HOME && \
14+
RUN wget http://www-eu.apache.org/dist/hadoop/common/hadoop-3.1.1/hadoop-3.1.1.tar.gz && \
15+
tar -xzf hadoop-3.1.1.tar.gz && \
16+
mv hadoop-3.1.1 $HADOOP_HOME && \
1617
for user in hadoop hdfs yarn mapred; do \
1718
useradd -U -M -d /opt/hadoop/ --shell /bin/bash ${user}; \
1819
done && \
@@ -27,7 +28,7 @@ RUN wget http://apache.crihan.fr/dist/hadoop/common/hadoop-3.1.0/hadoop-3.1.0.ta
2728
echo "export YARN_NODEMANAGER_USER=root" >> $HADOOP_HOME/etc/hadoop/yarn-env.sh && \
2829
echo "PATH=$PATH:$HADOOP_HOME/bin" >> ~/.bashrc
2930

30-
RUN \
31+
RUN apt-get install openssh-client -y && \
3132
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa && \
3233
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys && \
3334
chmod 0600 ~/.ssh/authorized_keys
@@ -36,14 +37,13 @@ ADD *xml $HADOOP_HOME/etc/hadoop/
3637

3738
ADD ssh_config /root/.ssh/config
3839

39-
ADD start-all.sh start-all.sh
40-
41-
RUN pip3 install Flask
42-
RUN pip3 install werkzeug
43-
44-
ENV LC_ALL C.UTF-8
45-
ENV LANG C.UTF-8
40+
RUN pip3 install Flask werkzeug pydoop==2.0a3
4641

4742
WORKDIR /app
4843

4944
COPY . /app
45+
46+
RUN apt-get install ssh -y
47+
48+
ENV LC_ALL C.UTF-8
49+
ENV LANG C.UTF-8

18.flask-hadoop/app.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,43 @@
11
from flask import Flask, request
22
from werkzeug import secure_filename
33
import os
4+
import pydoop.hdfs as hdfs
5+
import json
6+
47
app = Flask(__name__)
58
app.config['UPLOAD_FOLDER'] = os.getcwd()
69

10+
print(hdfs.hdfs().list_directory('/user'))
11+
712
@app.route('/')
813
def hello_world():
914
return 'Hey, we have Flask in a Docker container!'
1015

11-
@app.route('/file', methods = ['POST'])
12-
def members():
16+
@app.route('/test', methods = ['POST'])
17+
def test():
1318
f = request.files['file']
1419
f.save(os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(f.filename)))
1520
return f.filename
1621

22+
@app.route('/wordcount', methods = ['POST'])
23+
def wordcount():
24+
f = request.files['file']
25+
f.save(os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(f.filename)))
26+
with open(f.filename,'r') as fopen:
27+
hdfs.dump(fopen.read(), '/user/input_wordcount/text')
28+
os.system('pydoop script -c combiner wordcount.py /user/input_wordcount /user/output_wordcount')
29+
list_files = hdfs.hdfs().list_directory('/user/output_wordcount')
30+
return json.dumps([hdfs.load(file['name'], mode='rt') for file in list_files if 'SUCCESS' not in file['name']])
31+
32+
@app.route('/lowercase', methods = ['POST'])
33+
def lowercase():
34+
f = request.files['file']
35+
f.save(os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(f.filename)))
36+
with open(f.filename,'r') as fopen:
37+
hdfs.dump(fopen.read(), '/user/input_lowercase/text')
38+
os.system("pydoop script --num-reducers 0 -t '' lowercase.py /user/input_lowercase /user/output_lowercase")
39+
list_files = hdfs.hdfs().list_directory('/user/output_lowercase')
40+
return json.dumps([hdfs.load(file['name'], mode='rt') for file in list_files if 'SUCCESS' not in file['name']])
41+
1742
if __name__ == '__main__':
1843
app.run(debug=True, host='0.0.0.0',port=5000)

18.flask-hadoop/compose/bash

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
#!/bin/sh
22

3-
docker-compose -f $(dirname "$0")/docker-compose.yml exec processing bash
4-
3+
docker-compose -f $(dirname "$0")/docker-compose.yml exec flask-hadoop bash

18.flask-hadoop/compose/develop

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
#!/bin/sh
22

33
bash start-all.sh
4+
python3 app.py

18.flask-hadoop/compose/docker-compose.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
version: '2.3'
22
services:
3-
hadoop:
3+
flask-hadoop:
44
build:
55
context: ..
66
target: base
77
command: compose/develop
88
environment:
99
PYTHONPATH: .
1010
ports:
11-
- '5000:5000' # flask
11+
- '5000:5000'
1212
- '8088:8088'
13+
- '9000:9000'
1314
- '9870:9870'
1415
- '9864:9864'
1516
- '19888:19888'

18.flask-hadoop/lowercase.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def mapper(_, record, writer):
2+
writer.emit("", record.lower())

18.flask-hadoop/screenshot1.png

221 KB
Loading

18.flask-hadoop/screenshot2.png

326 KB
Loading

18.flask-hadoop/start-all.sh

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
/etc/init.d/ssh start
44

5-
# START HADOOP
6-
##############
7-
85
$HADOOP_HOME/bin/hdfs namenode -format
96

10-
$HADOOP_HOME/sbin/start-dfs.sh
117
$HADOOP_HOME/sbin/start-yarn.sh
12-
$HADOOP_HOME/sbin/mr-jobhistory-daemon.sh start historyserver
8+
$HADOOP_HOME/sbin/start-dfs.sh
9+
10+
$HADOOP_HOME/bin/hdfs dfs -mkdir /user
11+
$HADOOP_HOME/bin/hdfs dfs -mkdir /user/input_wordcount
12+
$HADOOP_HOME/bin/hdfs dfs -mkdir /user/input_lowercase
13+
$HADOOP_HOME/bin/hdfs dfs -put $HADOOP_HOME/etc/hadoop/*.xml /user

18.flask-hadoop/supervisord.conf

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)