zoukankan      html  css  js  c++  java
  • 访问hdfs里的文件

    准备工作:

      给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.3sharehadoopcommonhadoop-common-2.7.3.jar
      2.common依赖的jar
        hadoop-2.7.3sharehadoopcommonlib下的所有
      3.hdf
        hadoop-2.7.3sharehadoophdfshadoop-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);
        }
    }

        

  • 相关阅读:
    NotFoundHttpException
    postMan
    Extjs win
    Extjs toolbar 如何添加竖杆分隔符
    Extjs iconCls 的用法
    b站Java基本语法4之进制转换
    b站Java基本语法4之基本数据类型的运算规则
    b站数据库课2之连接查询
    b站Java尚硅谷0
    b站数据库课1之分组函数分组查询
  • 原文地址:https://www.cnblogs.com/413xiaol/p/9949814.html
Copyright © 2011-2022 走看看