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

        

  • 相关阅读:
    Java学习的第二十八天
    Java学习的第二十七天
    第五章 作用域闭包
    第4章提升
    第3章函数作用域和块级作用域
    Vant中List列表下拉加载更多
    获取当前时间
    js比较两个时间的大小
    边框引起页面抖动
    计算两个数的百分比,保留两位小数
  • 原文地址:https://www.cnblogs.com/413xiaol/p/9949814.html
Copyright © 2011-2022 走看看