forked from radanalyticsio/tutorial-sparkpi-python-flask
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
33 lines (21 loc) · 761 Bytes
/
Copy pathapp.py
File metadata and controls
33 lines (21 loc) · 761 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import os
from flask import Flask
from flask import request
from pyspark.sql import SparkSession
app = Flask(__name__)
def cuenta(palabra):
spark = SparkSession.builder.appName("PythonPi").getOrCreate()
count = palabra.flatMap(lambda line: line.split("")).map(lambda char: (char, 1)).reduceByKey(lambda a, b: a+b)
spark.stop()
return count
@app.route("/")
def index():
return "Python Flask SparkPi server running. Add the 'sparkpi' route to this URL to invoke the app."
@app.route("/sparkpi")
def sparkpi():
palabra = request.args.get('palabra')
response = "Letras".format(cuenta(palabra))
return response
if __name__ == "__main__":
port = int(os.environ.get("PORT", 8080))
app.run(host='0.0.0.0', port=port)