准备工作:

  给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);
}
}

    

最新文章

  1. Dijkstra 单源最短路径算法
  2. iOS中使用正则
  3. java之多线程 二
  4. vmware workstation9.0 RHEL5.8 oracle 10g RAC安装指南及问题总结
  5. paip.ikanalyzer 重加载词库的方法.
  6. impersonate a user
  7. What version of .NET Framework is integrated into what version of OS?
  8. vlc_input buffer管理 & 时钟同步(转)
  9. [CSAPP笔记][第十一章网络编程]
  10. 网路流程图 TCP/IP
  11. 刷新指定行或区 cell
  12. Flex 国际化(中英语言适配)
  13. ios用户体验
  14. Idea(一) 安装与破解
  15. Python_dict部分功能介绍
  16. Understanding How Graal Works - a Java JIT Compiler Written in Java
  17. sass:常用备忘
  18. cogs2479 偏序 cdq+树套树
  19. C#程序集系列12,C#编译器和CLR如何找寻程序集
  20. 防范XSS跨站2

热门文章

  1. 【Objective-C】02-Objective-C学习及iOS开发的准备
  2. redis 服务相关
  3. 0050 MyBatis关联映射--一对多关系
  4. OSI七层模型与TCP/IP五层模型详解
  5. root-me web server 20-30 writeup
  6. Nginx+PHP-FPM优化技巧总结
  7. Exercise:Sparse Autoencoder
  8. C++ 类模板一(类模板的定义)
  9. UE问题分部解决
  10. SQLAllocStmt与SQLFreeStmt