from hdfs.client import Client
class Process_Data_Hdfs():
def __init__(self):
self.client = Client("http://hadoop1:50070")
self.filename = "/user/hdfs/read.txt"
def read_hdfs_file(self):
# with client.read('samples.csv', encoding='utf-8', delimiter='\n') as reader:
# for line in reader:
# pass
lines = []
with self.client.read(self.filename, encoding='utf-8', delimiter='\n') as reader:
for line in reader:
# pass
# print line.strip()
lines.append(line.strip())
return lines
def mkdirs(self, hdfs_path):
self.client.makedirs(hdfs_path)
def delete_hdfs_file(self,hdfs_path):
self.client.delete(hdfs_path)
def put_to_hdfs(self, local_path, hdfs_path):
self.client.upload(hdfs_path, local_path, cleanup=True)
def get_from_hdfs(self, hdfs_path, local_path):
self.client.download(hdfs_path, local_path, overwrite=False)
def append_to_hdfs(self, hdfs_path, data):
self.client.write(hdfs_path, data, overwrite=False, append=True)
def write_to_hdfs(self, hdfs_path, data):
self.client.write(hdfs_path, data, overwrite=True, append=False)
def move_or_rename(self,hdfs_src_path, hdfs_dst_path):
self.client.rename(hdfs_src_path, hdfs_dst_path)
def list(self,hdfs_path):
return self.client.list(hdfs_path, status=False)
如需了解更多,欢迎交流,微信:tan1525859926