又接上文
来源:互联网 发布:java中有几种类型的流 编辑:程序博客网 时间:2024/05/17 22:06
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");
}
}
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");
}
}
- 又接上文
- 接上文
- 接上
- 接上
- 接上
- android重写Dialog(接上文)
- 接上文,关于存储问题
- 软件工程 -接上文,知识的获取
- (接上文)n阶 groupby实现
- 动态代理模式示例-接上文
- 集成创建适配器 接上文的适配器
- MainActivity——接上文网路请求
- 接上文,新攒的电脑的缺点
- hadoop cdh4.5 mapreduce yarn 配置文件 (接上文)
- 接上一篇博文,继续双机通信问题研究
- SQL语句,建立索引和视图,接上文
- SQL语句,聚集函和数据分组,接上文
- 【接上文】Ajax处理响应,连接 Web 表单
- 谈java的import使用
- SNS将会复活?
- hdu 1312——Red and Black(DFS)
- 限制Linux普通用户进程数
- memcache dump all items key
- 又接上文
- zoj 3494
- mongoDB for Java 查询显示指定字段
- 通用计算时代来临 论CUDA与OpenCL的异同
- JS 添加多个上传 input
- 程序员的出路之一
- 初窥spring3 MVC for annotation
- 谈谈QT的动画框架
- Android_Zipalign优化apk包