HDFS的java操作方式
来源:互联网 发布:淘宝购物网页面源代码 编辑:程序博客网 时间:2024/05/18 08:03
HDFS的java操作方式
(1)使用协议方式访问hdfs上的文件(模仿http)
public class App1 {
// 访问的文件路径
static final String PATH = "hdfs://chaoren:9000/hello";
public static void main(String[] args) throws Exception {
//设置访问流的协议类型,指定为hdfs
URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
URL url = new URL(PATH);
InputStream in = url.openStream();
IOUtils.copyBytes(in, System.out, 1024, true);}
} (2)使用FileSystem类对hdfs进行全方位操作(常用)
public class App2 {
static final String PATH = "hdfs://chaoren:9000/";
static final String DIR = "/dl";
static final String FILE = "/dl/hello";
public static void main(String[] args) throws Exception {
FileSystem fileSystem = getFileSystem();
// 创建文件夹 hadoop fs -mkdir /dl
//madirs(fileSystem);
//上传文件 hadoop fs -put src des
//putData(fileSystem);
//下载文件 hadoop fs -get src des
//getData(fileSystem);
//浏览文件夹
list(fileSystem);
//删除文件(夹)
//fileSystem.delete(new Path(DIR), true);
}
//浏览文件夹
private static void list(FileSystem fileSystem) throws IOException {
FileStatus[] listStatus = fileSystem.listStatus(new Path("/"));
for (FileStatus fileStatus : listStatus) {
String isDir= fileStatus.isDir()? "文件夹": "文件";
String permission = fileStatus.getPermission().toString();
short replication = fileStatus.getReplication();
long len = fileStatus.getLen();
String path = fileStatus.getPath().toString();
System.out.println(isDir+"\t"+permission+"\t"+replication+"\t"+len+"\t"+path);
}
}
@SuppressWarnings("unused")
//下载文件 hadoop fs -get src des
private static void getData(FileSystem fileSystem) throws IOException {
FSDataInputStream in = fileSystem.open(new Path(FILE));
IOUtils.copyBytes(in, System.out, 1024, true);
}
@SuppressWarnings("unused")
//上传文件 hadoop fs -put src des
private static void putData(FileSystem fileSystem) throws IOException,
FileNotFoundException {
FSDataOutputStream out = fileSystem.create(new Path(FILE));
FileInputStream in = new FileInputStream("G:/haha.txt");
IOUtils.copyBytes(in, out, 1024,true);
}
@SuppressWarnings("unused")
// 创建文件夹 hadoop fs -mkdir /dl
private static void madirs(FileSystem fileSystem) throws IOException {
fileSystem.mkdirs(new Path(DIR));
}
private static FileSystem getFileSystem() throws Exception {
return FileSystem.get(new URI(PATH), new Configuration());
}
} 0 0
- HDFS的java操作方式
- Hadoop HDFS 的 Java API 操作方式
- 对HDFS的操作方式
- 用流的方式操作HDFS文件 JAVA API
- HDFS 的 JAVA操作
- HDFS的java操作
- HDFS的java操作
- HDFS java 操作 HDFS
- hdfs客户端的理解,多种方式操作hdfs
- HDFS的JAVA API操作
- HDFS的JAVA API 操作
- HDFS的Java API的访问方式
- HDFS 的Java操作“ Wrong FS: hdfs://xxx, expected: file:///”
- HDFS的java接口——简化HDFS文件系统操作
- _00002 Hadoop HDFS体系结构及shell、java操作方式
- Hdfs体系结果及shell java操作方式 笔记五
- HDFS的JAVA接口API操作实例
- HDFS的JAVA接口API操作实例
- [LeetCode]Sudoku Solver
- 带重复元素的子集
- Customizing Your Build With Gradle
- flume基础知识
- iOS-Core-Animation-Advanced-Techniques(五)
- HDFS的java操作方式
- RPC远程过程调用
- 用MapReduce查找100万个数中的最大值
- hadoop2
- Repeated DNA Sequences
- Redis安装、配置、操作、持久化、主从、phpredis扩展安装使用详解之安装配置
- JIRA安装
- PhpStorm的使用
- Codeforces Round #321 (Div. 2) A. Kefa and First Steps