def get_data(): client = Client("http://t3.dev:50070", "hadoop") # client = InsecureClient(url="http://t3.dev:50070", user="******", root="/") print(client.list("/huiqu/common/area.txt")) with client.read("/huiqu/common/area.txt/part-00000") as read: # print(read.read().decode('utf8')) return {"data": read.read().decode('utf8')}
def main(): client = Client("http://127.0.0.1:50070", root="/", timeout=100, session=False) #client.makedirs("/news") client.upload("/input", "x.html") print(client.list("/"))
print(rowkey) mutations = [Mutation(column=self.columnFamily + ":ipaddr", value=ipaddr), \ Mutation(column=self.columnFamily + ":visitTime", value=visitTime), \ Mutation(column=self.columnFamily + ":user_id", value=user_id), \ Mutation(column=self.columnFamily + ":link", value=link) ] # 一次提交多行 mutations_batch.append( BatchMutation(row=rowkey, mutations=mutations)) if len(mutations_batch) % batch_size == 0: self.client.mutateRows(self.tablename, mutations_batch) mutations_batch = [] if __name__ == "__main__": # 建立hbase连接 hbasewriteer = CreateTableAndImportData('user_log_info', 'cf_log') hbasewriteer.createTable() # 连接HDFS client = Client(HDFSNN) # 获取文件列表 logFiles = client.list(LOGPATH) # 读取文件 for logfile in logFiles: with client.read(os.path.join(LOGPATH, logfile)) as deal_file_handle: hbasewriteer.importData(deal_file_handle)
from hdfs import Client client = Client("http://master:9870") #client.makedirs("/abc/xyz") x = client.list("/") y = client.list("/", status=True)
class HDFSUtil: def __init__(self, url): self._client = Client(url) def make_dir(self, hdfs_path): """ 支持递归创建多级目录 :param hdfs_path: :return: """ self._client.makedirs(hdfs_path) def delete_hdfs_file(self, hdfs_path): """ 删除HDFS文件 如果是目录, 必须为空 :param hdfs_path: :return: """ self._client.delete(hdfs_path) def delete_hdfs_dir(self, hdfs_dir): """ 删除HDFS文件/目录 如果目录不为空, 递归删除 :param hdfs_dir: :return: """ dir_list = self.hdfs_dir_list(hdfs_dir) if dir_list is None or len(dir_list) == 0: print('Delete File: {0}'.format(hdfs_dir)) self._client.delete(hdfs_dir) else: for file_name in dir_list: self.delete_hdfs_dir(hdfs_dir + '/' + file_name) print('Delete Dir: {0}'.format(hdfs_dir)) self._client.delete(hdfs_dir) def upload_to_hdfs(self, local_path, hdfs_path): """ 将本地文件/目录上传到HDFS上 如果目录不存在, 会自动创建 :param local_path: :param hdfs_path: :return: """ self._client.upload(hdfs_path, local_path, cleanup=True) def download_from_hdfs(self, hdfs_path, local_path): """ 将HDFS上的文件/目录下载到本地 :param hdfs_path: :param local_path: :return: """ self._client.download(hdfs_path, local_path, overwrite=True) def write_to_hdfs(self, hdfs_path, data, overwrite=False, append=True): """ 追加: overwrite=false, append=true => Default 复写: overwrite=true, append=false overwrite和append逻辑必须互斥 :param hdfs_path: :param data: :param overwrite: Boolean 是否复写 :param append: Boolean 是否追加 :return: """ if not self._client.content(hdfs_path, strict=False): print('File Not exist in HDFS') self._client.write(hdfs_path, data, overwrite=overwrite, append=append) def move_or_rename(self, hdfs_src_path, hdfs_dst_path): """ 文件移动/重命名 :param hdfs_src_path: :param hdfs_dst_path: :return: """ self._client.rename(hdfs_src_path, hdfs_dst_path) def hdfs_dir_list(self, hdfs_path): """ 获取指定目录下的文件 当hdfs_path不是目录, 捕获异常并返回None :param hdfs_path: :return: List[filename] or None """ try: return self._client.list(hdfs_path, status=False) except HdfsError: return None
if __name__ == '__main__': hdfs_ip = "192.168.146.133" hdfs_version = 3 hdfs_root = "~/test" filepath = r"C:\Users\daqige\PycharmProjects\newLeetCode\convert.py" hdfs_addr = "http://" + hdfs_ip + ":" + str(9870 if (hdfs_version == 3) else 90070) client = Client(hdfs_addr) # print("创建文件夹") # client.makedirs(hdfs_root) # print(client.list("/")) # # print("上传文件") # client.upload(hdfs_root, filepath) # print(client.list(hdfs_root)) # # print("修改文件名") # client.rename(hdfs_root + "/convert.py", hdfs_root + "/ubuntu.py") # print(client.list(hdfs_root)) print("下载文件") client.download(hdfs_root + "/ubuntu.py", ".") print(os.listdir(".")) print("删除文件") client.delete(hdfs_root + "/ubuntu.py") print(client.list(hdfs_root))
# 写入文件(覆盖) client.write(file_name, data="hello hdfs !", overwrite=True) # 写入文件(追加) client.write(file_name, data="hello way !", overwrite=False, append=True) # 读取文件内容 with client.read(file_name, encoding='utf-8') as f: print(f.read()) # 文件下载 client.download(file_name, loacl_file_name, overwrite=True) # 文件上传 client.upload(file_name + '111', loacl_file_name, cleanup=True) # 删除文件 client.delete(file_name2) # 文件重命名 client.rename(file_name, file_name2) # 文件夹底下文件 files = client.list(file_dir, status=False) for file in files: print(file) # 删除文件夹(递归删除、谨慎) # client.delete(file_dir, recursive=True)
class RF_HDFS(object): def __init__(self): self.client = None self.directory = None def connect_and_login(self, **kwargs): import requests host = None port = None user = None password = None root = None timeout = None proxy = None if 'host' in kwargs: host = kwargs['host'] if 'port' in kwargs: port = kwargs['port'] if 'kdc' in kwargs: kdc = kwargs['kdc'] if 'user' in kwargs: user = kwargs['user'] if 'password' in kwargs: password = kwargs['password'] if 'root' in kwargs: root = kwargs['root'] if 'proxy' in kwargs: proxy = kwargs['proxy'] if 'timeout' in kwargs: timeout = kwargs['timeout'] self.session = requests.Session() adapter = requests.adapters.HTTPAdapter(pool_maxsize=0) self.session.mount('http://', adapter) self.session.mount('https://', adapter) self.session.headers.update({'Connection':'Keep-Alive'}) self.connectionStatus = False try: timeout = int(timeout) url = "http://" + host + ":" + str(port) hdfsLogin = WebHDFS(url, kdc) cookieStr = hdfsLogin.authenticate(user, password) if cookieStr != None: cookieList = cookieStr.split('=', 1) cookieDict = {cookieList[0]: cookieList[1]} requests.utils.add_dict_to_cookiejar(self.session.cookies, cookieDict) self.client = Client(url, root=root, proxy=proxy, timeout=timeout, session=self.session) except HdfsError as hdfsError: raise HdfsLibraryError(str(hdfsError)) except Exception as exception: raise HdfsLibraryError(str(exception)) self.connectionStatus = True return self.client def checkConnectionStatus(self): return self.connectionStatus def list_dir(self, directory): output = [] try: if directory != None: output = self.client.list(directory, status=True) else: output = self.client.list(self.client.root, status=True) except HdfsError as hdfsError: raise HdfsLibraryError(str(hdfsError)) except Exception as exception: raise HdfsLibraryError(str(exception)) return output def list_names(self, directory): output = [] try: if directory != None: output = self.client.list(directory, status=False) else: output = self.client.list(self.client.root, status=False) except HdfsError as hdfsError: raise HdfsLibraryError(str(hdfsError)) except Exception as exception: raise HdfsLibraryError(str(exception)) return output def upload(self, remote_path, local_path, overwrite=False, permission=None): output = None try: output = self.client.upload(remote_path, local_path, overwrite, permission=permission) except HdfsError as hdfsError: # For some reason this exception includes the entire stack trace after # the error message, so split on '\n' and only return the first line. error = str(hdfsError).splitlines()[0] raise HdfsLibraryError(error) except Exception as exception: raise HdfsLibraryError(str(exception)) return output def download(self, remote_path, local_path, overwrite=False): output = None try: output = self.client.download(remote_path, local_path, overwrite) except HdfsError as hdfsError: raise HdfsLibraryError(str(hdfsError)) except Exception as exception: raise HdfsLibraryError(str(exception)) return output def mkdir(self, directory, permission): try: # no return value self.client.makedirs(directory, permission=permission) except HdfsError as hdfsError: raise HdfsLibraryError(str(hdfsError)) except Exception as exception: raise HdfsLibraryError(str(exception)) def rmdir(self, directory): try: # no return value if self.client.delete(directory, recursive=True) == False: raise HdfsLibraryError("Directory does not exist: %r", directory) except HdfsError as hdfsError: raise HdfsLibraryError(str(hdfsError)) except Exception as exception: raise HdfsLibraryError(str(exception)) def rename(self, src_file, dst_file): try: # no return value self.client.rename(src_file, dst_file) except HdfsError as hdfsError: raise HdfsLibraryError(str(hdfsError)) except Exception as exception: raise HdfsLibraryError(str(exception)) def delete(self, file): try: # no return value if self.client.delete(file) == False: raise HdfsLibraryError("File does not exist: %r", file) except HdfsError as hdfsError: raise HdfsLibraryError(str(hdfsError)) except Exception as exception: raise HdfsLibraryError(str(exception)) def set_time(self, file, mod_time): try: # no return value self.client.set_times(file, -1, mod_time) except HdfsError as hdfsError: raise HdfsLibraryError(str(hdfsError)) except Exception as exception: raise HdfsLibraryError(str(exception)) def set_owner(self, file, owner, group): try: # no return value self.client.set_owner(file, owner=owner, group=group) except HdfsError as hdfsError: raise HdfsLibraryError(str(hdfsError)) except Exception as exception: raise HdfsLibraryError(str(exception)) def set_permission(self, file, permission): try: # no return value self.client.set_permission(file, permission=permission) except HdfsError as hdfsError: raise HdfsLibraryError(str(hdfsError)) except Exception as exception: raise HdfsLibraryError(str(exception)) def set_acl(self, file, aclspec): try: # no return value self.client.set_acl(file, aclspec=aclspec) except HdfsError as hdfsError: raise HdfsLibraryError(str(hdfsError)) except Exception as exception: raise HdfsLibraryError(str(exception)) def status(self, path): output = '' try: output = self.client.status(path) except HdfsError as hdfsError: raise HdfsLibraryError(str(hdfsError)) except Exception as exception: raise HdfsLibraryError(str(exception)) return output def checksum(self, path): output = '' try: output = self.client.checksum(path) except HdfsError as hdfsError: raise HdfsLibraryError(str(hdfsError)) except Exception as exception: raise HdfsLibraryError(str(exception)) return output def close(self): self.session.close()