我如何使用python在mapreduce中得到直方图(Graph)的结果?

问题描述 投票:0回答:1

[当我运行这段代码时,我在群集的缩减部分中遇到错误。并使用Matplotlib将我的输出图形化,但将失败。我正在Google Cloud集群上运行此代码。我在excel csv文件中的数据。

#!/usr/bin/env python3
"""mapper.py"""
import sys

# Get input lines from stdin
for line in sys.stdin:
    # Remove spaces from beginning and end of the line
    #line = line.strip()

    # Split it into tokens
    #tokens = line.split()

    #Get probability_mass values
    for prob in line:
        print("None\t{}".format(prob))
        #print(str(probability_mass)+ '\t1')
        #print('%s\t%s' % (probability_mass, None))
#!/usr/bin/env python3
"""reducer.py"""
import sys
import matplotlib.pyplot as plt
from collections import defaultdict

counts = defaultdict(float)

# Get input from stdin
for line in sys.stdin:
    #Remove spaces from beginning and end of the line
    #line = line.strip()

    # skip empty lines
    if not line:
        continue  

    # parse the input from mapper.py
    k,v = line.split('\t', 1)
    counts[v] += 1
total = (float(sum(counts.values())))
#total = sum(counts.values())
probability_mass = {k:v/total for k,v in counts.items()}
#print(probability_mass)
grad = probability_mass.keys()
prob = probability_mass.values()
print(str(grad))
print(str(prob))
   #bins = 20
plt.hist(prob,bins=20, normed=1, facecolor='blue', alpha=0.5)
   #plt.plot(bins, hist, 'r--')
plt.xlabel('Probability')
plt.ylabel('Number Of Students')
plt.title('Histogram of Students Grade')
plt.subplots_adjust(left=0.15)
plt.show()
python python-3.x hadoop mapreduce bigdata
1个回答
0
投票

您需要将结果导出到文件,然后下载并将其绘制为两个单独的步骤。

MapReduce没有GUI,并且您不应该让每个reduce任务都试图生成绘图

或者您可以将结果导出到某些GCP工具,例如BigQuery或Datastore,您可以在其中插入适当的BI工具以进行视觉分析

© www.soinside.com 2019 - 2024. All rights reserved.