读取hdfs上的文件

上传文件在hdfs上,访问namenode节点:ip:50070
读取hdfs上的文件_第1张图片

读取hdfs的java代码如下:

 public void ReadFile(String hdfs) throws IOException {
          Configuration conf = new Configuration();
          FileSystem fs = FileSystem.get(URI.create(hdfs),conf);
          System.out.println(URI.create(hdfs));
          //fs.listFiles(f, recursive)
          FileStatus[] status = fs.listStatus(new Path(hdfs));
            for (FileStatus file : status) {
                System.out.println(file.getPath().getName());
                if (!file.getPath().getName().endsWith(".txt")) {
                    continue;
                }
                System.out.println(file.getPath().getName());
          FSDataInputStream hdfsInStream = fs.open(file.getPath());      
          byte[] ioBuffer = new byte[1024];
          int readLen = hdfsInStream.read(ioBuffer);
          while(readLen!=-1)
          {           
           System.out.write(ioBuffer, 0, readLen);
           readLen = hdfsInStream.read(ioBuffer);
          }
          hdfsInStream.close();
         // fs.close();

         }
         }
 public static void main(String[] args) throws IOException { 
          String hdfs = "hdfs://ip:8020/文件夹";
          text t = new text(); 
          //t.WriteFile(hdfs);
          t.ReadFile(hdfs);

          }

创建目录:

public static void mkdir(String dir) throws IOException  
        {  
            Configuration conf = new Configuration();  
            FileSystem fs = FileSystem.get(conf);  
            fs.mkdirs(new Path(dir));  
            fs.close();  
        }  

写入hdfs中:public void WriteFile(String hdfs) throws IOException {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(hdfs),conf);
FSDataOutputStream hdfsOutStream = fs.create(new Path(hdfs));
hdfsOutStream.writeChars("hello");
hdfsOutStream.close();
fs.close();
}

你可能感兴趣的:(开发笔记)