访问hdfs里的文件
2024-09-02 13:13:58
准备工作:
给hdfs里上传一份用于测试的文件
[root@master ~]# cat hello.txt
hello 1
hello 2
hello 3
hello 4
[root@master ~]# hadoop fs -put ./hello.txt /
[root@master ~]# hadoop fs -ls /
Found 1 items
-rw-r--r-- 2 root supergroup 32 2018-11-12 22:42 /hello.txt
java依赖的库:
1.common
hadoop-2.7.3\share\hadoop\common\hadoop-common-2.7.3.jar
2.common依赖的jar
hadoop-2.7.3\share\hadoop\common\lib下的所有
3.hdf
hadoop-2.7.3\share\hadoop\hdfs\hadoop-hdfs-2.7.3.jar
代码:
利用JDK的URL类
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
import java.io.InputStream;
import java.net.URL; public class TestHDFS {
public static void main(String[] args) throws Exception{
// URL url = new URL("http://www.baidu.com");
//URL这个类是Java的,他默认只认识HTTP协议,这里需要设置一下,让他认识HDFS协议
URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
//这里的地址和端口,相当与hdfs里的根目录, 然后在拼上要访问的文件在hdfs里的路径
URL url = new URL("hdfs://192.168.0.104:9000/hello.txt");
InputStream in = url.openStream();
IOUtils.copyBytes(in, System.out, 4096, true);
}
}
利用hadoop的工具类:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils; import java.io.FileInputStream;
import java.util.Properties; public class TestHDFS {
public static void main(String[] args) throws Exception{
Properties properties = System.getProperties();
properties.setProperty("HADOOP_USER_NAME", "root"); Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://192.168.0.104:9000");
FileSystem fs = FileSystem.get(conf); //存在的情况下会覆盖之前的目录
boolean success = fs.mkdirs(new Path("/xiaol"));
System.out.println(success); success = fs.delete(new Path("/xiaol"), true);
System.out.println(success); success = fs.exists(new Path("/xiaol"));
System.out.println(success); success = fs.exists(new Path("/hello.txt"));
System.out.println(success); FileStatus[] statuses = fs.listStatus(new Path("/"));
for(FileStatus status : statuses){
System.out.println(status.getPath());
System.out.println(status.getPermission());
System.out.println(status.getReplication());
} //上传windows上的文件
FSDataOutputStream fsout = fs.create(new Path("/test.data"), true);
FileInputStream in = new FileInputStream("D:/test.txt");
IOUtils.copyBytes(in, fsout, 4096, true);
}
}
最新文章
- Dijkstra 单源最短路径算法
- iOS中使用正则
- java之多线程 二
- vmware workstation9.0 RHEL5.8 oracle 10g RAC安装指南及问题总结
- paip.ikanalyzer 重加载词库的方法.
- impersonate a user
- What version of .NET Framework is integrated into what version of OS?
- vlc_input buffer管理 &; 时钟同步(转)
- [CSAPP笔记][第十一章网络编程]
- 网路流程图 TCP/IP
- 刷新指定行或区 cell
- Flex 国际化(中英语言适配)
- ios用户体验
- Idea(一) 安装与破解
- Python_dict部分功能介绍
- Understanding How Graal Works - a Java JIT Compiler Written in Java
- sass:常用备忘
- cogs2479 偏序 cdq+树套树
- C#程序集系列12,C#编译器和CLR如何找寻程序集
- 防范XSS跨站2