def file_status(self, path='/'): """Return a file status object that represents the path. Args: path: The path we want information from Returns: FileStatus JSON object: { "FileStatus": { "accessTime" : 0, "blockSize" : 0, "group" : "supergroup", "length" : 0, //in bytes, zero for directories "modificationTime": 1320173277227, "owner" : "webuser", "pathSuffix" : "", "permission" : "777", "replication" : 0, "type" : "DIRECTORY" //enum {FILE, DIRECTORY} } } """ url = '%s%s?user.name=%s&op=%s' % (self.prefix, path, self.user, 'GETFILESTATUS') response = request.push(self.host, self.port, 'GET', url) js = json.loads(response) return js
def list(self, path): """Get list of directory's content in WebHDFS JSON format. Args: path: Given path. Returns: The client receives a response with a FileStatuses JSON object: """ url = '%s%s?user.name=%s&op=%s' % (self.prefix, path, self.user, 'LISTSTATUS') response = request.push(self.host, self.port, "GET", url) js = json.loads(response) return js['FileStatuses']['FileStatus']
def rename(self, src_path, dst_path): """Rename file or directory. Args: src_path: Current path dst_path: New path Returns: True if operation is successful and False if not. """ url = '%s%s?user.name=%s&op=%s&destination=%s' % ( self.prefix, src_path, self.user, 'RENAME', dst_path) response = request.push(self.host, self.port, 'PUT', url) js = json.loads(response) return js['boolean']
def delete(self, path, recursive="false"): """ Args: path: The path to file or directory we want delete. Returns: curl -i -X DELETE "http://<host>:<port>/webhdfs/v1/<path>?op=DELETE [&recursive=<true|false>]" """ url = '%s%s?user.name=%s&op=DELETE&recursive=%s' % ( self.prefix, path, self.user, recursive) response = request.push(self.host, self.port, 'DELETE', url) js = json.loads(response) return js['boolean']
def mkdir(self, path, permission=775): """Create directory. Has the semantics of Unix 'mkdir -p'. Args: path: Path to the directory you want to create. permission: The permission of a file/directory. <Octal> Returns: True if successful or False if not. """ url = '%s%s?user.name=%s&op=%s&permission=%s' % ( self.prefix, path, self.user, 'MKDIRS', permission) response = request.push(self.host, self.port, 'PUT', url) js = json.loads(response) return js['boolean']
def set_rf(self, path, rf): """Set replication factor for file. Args: path: The path we want change RF for. rf: Replication factor <SHORT> Returns: True if changing of replication factor is successful, False if not. """ url = '%s%s?user.name=%s&op=SETREPLICATION&replication=%s' % ( self.prefix, path, self.user, rf) response = request.push(self.host, self.port, 'PUT', url) js = json.loads(response) return js['boolean']
def file_checksum(self, path): """Get the checksum of a file. Args: path: Given path. Returns: The checksum object contains: self.algorithm: The name of the checksum algorithm. self.bytes: The byte sequence of the checksum in hexadecimal self.length: The length of the bytes (not the length of the string). """ url = '%s%s?user.name=%s&op=%s' % (self.prefix, path, self.user, 'GETFILECHECKSUM') response = request.push(self.host, self.port, 'GET', url) js = json.loads(response) result = structures.Checksum() result.algorithm = js['FileChecksum']['algorithm'] result.bytes = js['FileChecksum']['bytes'] result.length = js['FileChecksum']['length'] return result