Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from hdfs import InsecureClient
import os
import io
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
# ====== Connection ======
# Connecting to Webhdfs by providing hdfs host ip and webhdfs port (50070 by default)
client_hdfs = InsecureClient('http://' + os.environ['IP_HDFS'] + ':50070')
# ====== Figure Creation ======
# Creating Matplotlib figure but not printing it
plt.ioff()
plt.figure(figsize=(15,9))
plt.scatter(range(10),range(10),c=range(10),marker='o',s=500)
plt.title('Exemple of figure')
plt.tight_layout()
# ====== Writing to hdfs ======
# Writing figure to hdfs through a BytesIO python object
buf = io.BytesIO()
plt.savefig(buf, format='png')
buf.seek(0)
with client_hdfs.write('/user/hdfs/data/figure.png',overwrite=True) as writer :
writer.write(buf.getvalue())
buf.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment