I have a python script which currently accesses an API which returns JSON. It then takes the JSON string and saves it off as a file on the local file system, where i then move it into HDFS manually. I would like to change this so my python script is saving directly to HDFS instead of hitting the local file system first. I am currently trying to save the file using HDFS and DFS command but I don't think the copy command is the correct way to do this because it isn't a file but rather a JSON string when I am trying to save it.
Current Code
import urllib2
import json
import os
f = urllib2.urlopen('RESTful_API_URL.json')
json_string = json.loads(f.read().decode('utf-8'))
with open('\home\user\filename.json', 'w') as outfile:
json.dump(json_string,outfile)
New Code
f = urllib2.urlopen('RESTful_API_URL.json')
json_string = json.loads(f.read().decode('utf-8'))
os.environ['json_string'] = json.dump(json_string)
os.system('hdfs dfs -cp -f $json_string hdfs/user/test')