首页 > 解决方案 > 如何使用 python 在 mapreduce 中获得直方图(Graph)中的结果?

问题描述

当我运行此代码时,我在集群上的 reduce 部分出现错误。我正在考虑概率并使用 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()

标签: pythonpython-3.xhadoopmapreducebigdata

解决方案


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

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

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


推荐阅读