Hadoop的基本使用(2)——通过代码操作HDFS
来源:互联网 发布:mac怎样建立文件夹 编辑:程序博客网 时间:2024/06/01 10:17
1、导入相关jar包
hadoop-common 相关jar包
hadoop-hdfs 相关jar包
2、指定hadoop入口获得fileSystem对象
public void before() throws IOException{ Configuration conf=new Configuration(); conf.set("fs.defaultFS", "hdfs://192.168.11.128:9999"); conf.setInt("dfs.replication", 1); fileSystem=FileSystem.get(conf);}
3、通过fileSystem对象对hdfs进行操作
//创建文件夹@Testpublic void mkdir() throws IOException{ Path path=new Path("/demo/test2"); boolean mkdirs = fileSystem.mkdirs(path); System.out.println(mkdirs);}//判断路径是否存在@Testpublic void exist() throws IOException{ Path path=new Path("/gjhg"); boolean exists = fileSystem.exists(path); System.out.println(exists);}//判断所给路径是否为文件夹或文件@Testpublic void isFile() throws IOException{ Path src=new Path("/demo/test/helloworld.txt"); boolean dir = fileSystem.isDirectory(src); boolean file = fileSystem.isFile(src); System.out.println(dir); System.out.println(file);}//上传文件:1、路径 2、流@Testpublic void copyFromLocalFile() throws IOException{ Path src=new Path("H:\\hadoopTest\\text.txt"); Path dst=new Path("/demo/test2/text"); fileSystem.copyFromLocalFile(src, dst);}@Testpublic void copyFormLocalStream() throws IOException{ Path dst=new Path("/demo/test/hahah.txt"); FSDataOutputStream fos=fileSystem.create(dst); FileInputStream fis=new FileInputStream("H:\\hadoopTest\\howareyou.txt"); IOUtils.copyBytes(fis, fos, 1024); IOUtils.closeStream(fis); IOUtils.closeStream(fos);}//下载文件:1、路径 2、流@Testpublic void copyToLocalFile() throws IOException{ Path src=new Path("/demo/test/helloworld.txt"); Path dst=new Path("H:/hadoopTest/hi.txt"); fileSystem.copyToLocalFile(false, src, dst, true);}@Testpublic void copyToLocalStream() throws IOException{ Path dst=new Path("/demo/test/hahah.txt"); InputStream fis=fileSystem.open(dst); FileOutputStream fos=new FileOutputStream("H:\\hadoopTest\\hah.txt"); IOUtils.copyBytes(fis, fos, 1024); IOUtils.closeStream(fis); IOUtils.closeStream(fos);}//输出文件信息@Testpublic void fileList() throws FileNotFoundException, IOException{ Path dst=new Path("/"); RemoteIterator<LocatedFileStatus> listFiles = fileSystem.listFiles(dst, true); while(listFiles.hasNext()){ LocatedFileStatus next=listFiles.next(); System.out.println(next);}
4、关闭fileSystem
@Afterpublic void after() throws IOException{ fileSystem.close();}
0 0
- Hadoop的基本使用(2)——通过代码操作HDFS
- 使用HDFS API实现hadoop HDFS文件系统的基本操作
- hadoop的HDFS基本操作
- Hadoop的基本使用(1)——环境搭建、HDFS的简单使用
- 初识hadoop--(2)通过java操作hdfs
- Hadoop HDFS文件操作的Java代码
- Hadoop HDFS文件操作的Java代码
- Hadoop HDFS文件操作的Java代码
- hadoop hdfs api基本操作
- hadoop hdfs dfs基本操作
- 使用Hadoop的C API操作HDFS
- 使用Hadoop的Java API操作HDFS
- hadoop的HDFS操作
- Hadoop的基本使用(3)——MapReduce的基本操作(实现字符统计)
- 使用HDFS FS API的基本操作
- hadoop学习:Java对HDFS的基本操作
- hadoop的HDFS之增删改查基本操作
- Hadoop笔记六之Hdfs的基本shell操作
- iOS10 UILabel和UIButton显示不全的解决方案
- 我的我的JAVA世界
- 07 Unikernels
- SpringAOP 处理通知中的参数(注解与xml配置)
- 08 Microservices
- Hadoop的基本使用(2)——通过代码操作HDFS
- pdfmake运用笔记
- linux 增加用户(default)并修改密码
- ANR分析
- Talloc内存池使用教程——(4)使用析构函数
- JavaSE_网络编程_URL类
- php __FILE__ __DIR__魔术常量的使用【PHP进阶教程】
- NanoPi2- SD使用
- 第一章—无线传感网的技术概述