Skip to content Skip to sidebar Skip to footer

I Want To Call Hdfs Rest Api To Upload A File

I want to call HDFS REST api to upload a file using httplib. My program created the file, but no content is in it. ===================================================== Here is my

Solution 1:

It looks like your code is not using the "location" header from the 307 in the second PUT request.

I've been working on a fork of a python WebHDFS wrapper that may be of use, you can see the full code here: https://github.com/carlosmarin/webhdfs-py/blob/master/webhdfs/webhdfs.py

The method you'd be interested in is:

def copyfromlocal(self, source_path, target_path, replication=1, overwrite=True):
    url_path = WEBHDFS_CONTEXT_ROOT + target_path + '?op=CREATE&overwrite=' + 'true'if overwrite else'false'

    with _NameNodeHTTPClient('PUT', url_path, self.namenode_host, self.namenode_port, self.username) as response:
        logger.debug("HTTP Response: %d, %s" % (response.status, response.reason))
        redirect_location = response.msg["location"]
        logger.debug("HTTP Location: %s" % redirect_location)
        (redirect_host, redirect_port, redirect_path, query) = self.parse_url(redirect_location)

        # Bug in WebHDFS 0.20.205 => requires param otherwise a NullPointerException is thrown
        redirect_path = redirect_path + "?" + query + "&replication=" + str(replication)

        logger.debug("Redirect: host: %s, port: %s, path: %s " % (redirect_host, redirect_port, redirect_path))
        fileUploadClient = HTTPConnection(redirect_host, redirect_port, timeout=600)

        # This requires currently Python 2.6or higher
        fileUploadClient.request('PUT', redirect_path, open(source_path, "r").read(), headers={})
        response = fileUploadClient.getresponse()
        logger.debug("HTTP Response: %d, %s" % (response.status, response.reason))
        fileUploadClient.close()

        return json.loads(response.read())

Post a Comment for "I Want To Call Hdfs Rest Api To Upload A File"