0
点赞
收藏
分享

微信扫一扫

又接上文

古月无语 2022-08-09 阅读 52

package test.hdfs;

import java.io.BufferedInputStream;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;



import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.FileSystem;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.IOUtils;



public class HDFStest {

private Configuration conf;

private FileSystem fs;

public HDFStest() throws IOException{

conf=new Configuration();


conf.addResource("hdfstest-site.xml");


conf.addResource("core-site.xml");


conf.addResource("hdfs-site.xml");

fs=FileSystem.get(conf);

}



/** O

* 上传文件,

* @param localFile 本地路径

* @param hdfsPath 格式为hdfs://ip:port/destination

* @throws IOException

*/

public void upFile(String localFile,String hdfsPath) throws IOException{

InputStream in=new BufferedInputStream(new FileInputStream(localFile));

OutputStream out=fs.create(new Path(hdfsPath));

IOUtils.copyBytes(in, out, conf);

}

/**

* 附加文件

* @param localFile

* @param hdfsPath

* @throws IOException

*/

public void appendFile(String localFile,String hdfsPath) throws IOException{

InputStream in=new FileInputStream(localFile);

OutputStream out=fs.append(new Path(hdfsPath));

IOUtils.copyBytes(in, out, conf);

}

/**

* 下载文件

* @param hdfsPath

* @param localPath

* @throws IOException

*/

public void downFile(String hdfsPath, String localPath) throws IOException{

InputStream in=fs.open(new Path(hdfsPath));

OutputStream out=new FileOutputStream(localPath);

IOUtils.copyBytes(in, out, conf);

}

/**

* 删除文件或目录

* @param hdfsPath

* @throws IOException

*/

public void delFile(String hdfsPath) throws IOException{

fs.delete(new Path(hdfsPath), true);

}





public static void main(String[] args) throws IOException {

HDFStest hdfs=new HDFStest();

hdfs.upFile("/home/cloudwave/update.sh", "hdfs:/test");

// hdfs.downFile("hdfs://test", "192.168.30.119:8080//home/cloudwave/update.sh");

// hdfs.appendFile("192.168.30.119:8080//home/cloudwave/update.sh", "hdfs://test");

hdfs.delFile("hdfs://test");

}

}

举报

相关推荐

0 条评论