ASP.NET Hadoop 추가 삭제 검사 의 예제 코드 구현

본 고 는 ASP.NET 에서 Hadoop 의 첨삭 과 검 사 를 실현 하 는 예시 코드 를 소개 하고 여러분 에 게 공유 합 니 다.구체 적 으로 다음 과 같 습 니 다.
packages.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
 <package id="Microsoft.AspNet.WebApi.Client" version="4.0.20505.0" targetFramework="net46" />
 <package id="Microsoft.Data.Edm" version="5.2.0" targetFramework="net46" />
 <package id="Microsoft.Data.OData" version="5.2.0" targetFramework="net46" />
 <package id="Microsoft.Hadoop.WebClient" version="0.12.5126.42915" targetFramework="net46" />
 <package id="Microsoft.Net.Http" version="2.0.20505.0" targetFramework="net46" />
 <package id="Microsoft.WindowsAzure.ConfigurationManager" version="1.8.0.0" targetFramework="net46" />
 <package id="Newtonsoft.Json" version="4.5.11" targetFramework="net46" />
 <package id="System.Spatial" version="5.2.0" targetFramework="net46" />
 <package id="WindowsAzure.Storage" version="2.0.4.1" targetFramework="net46" />
</packages>
HDFSAccess.cs

using Microsoft.Hadoop.WebHDFS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;

namespace Physical
{
  public sealed class HDFSAccess
  {
    private readonly WebHDFSClient webHDFSClient;

    public HDFSAccess(string uriString, string userName)
    {
      this.webHDFSClient = new WebHDFSClient(new Uri(uriString), userName);
    }

    public List<string> GetDirectories(string path)
    {
      var directoryStatus = this.webHDFSClient.GetDirectoryStatus(path).Result;

      return directoryStatus.Directories.Select(d => d.PathSuffix).ToList();
    }

    public List<string> GetFiles(string path)
    {
      var directoryStatus = this.webHDFSClient.GetDirectoryStatus(path).Result;

      return directoryStatus.Files.Select(d => d.PathSuffix).ToList();
    }

    public bool CreateDirectory(string path)
    {
      //            ,       「/」  
      return this.webHDFSClient.CreateDirectory(path).Result;
    }

    public bool DeleteDirectory(string path)
    {
      //            ,       「/」  
      return this.webHDFSClient.DeleteDirectory(path).Result;
    }

    public string CreateFile(string localFile, string remotePath)
    {
      //              ,       「/」  
      return this.webHDFSClient.CreateFile(localFile, remotePath).Result;
    }

    public bool DeleteFile(string path)
    {
      //            ,       「/」  
      return this.webHDFSClient.DeleteDirectory(path).Result;
    }

    public HttpResponseMessage OpenFile(string path)
    {
      //            ,       「/」  
      return this.webHDFSClient.OpenFile(path).Result;
    }
  }
}
Program.cs

using Physical;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyTest
{
  class Program
  {
    // HDFS cluster               
    //     :http://[    ]:50070
    //     :Wu
    private static HDFSAccess access = new HDFSAccess(@"http://127.0.0.1:50070", "Wu");

    static void Main(string[] args)
    {
      GetDirectoriesTest();
      Console.WriteLine("----------------------------------------------");
      GetFilesTest();
      Console.WriteLine("----------------------------------------------");
      DirectoryTest();
      Console.WriteLine("----------------------------------------------");
      FileTest();
      Console.WriteLine("----------------------------------------------");
      OpenFileTest();
      Console.ReadKey();
    } 

    public void TestCleanup()
    {
      //        
      var directories = access.GetDirectories(@"/");

      //       :tmp、user     
      foreach (var directory in directories)
      {
        if ("tmp".Equals(directory) || "user".Equals(directory))
        {
          continue;
        }
        else
        {
          access.DeleteDirectory(directory);
        }
      }

      //        
      var files = access.GetFiles(@"/");

      //       
      foreach (var file in files)
      {
        access.DeleteFile(file);
      }

      //    OpenFile     
      File.Delete(Path.Combine(Directory.GetCurrentDirectory(), "Test.jpg"));
    }

    //_     _        
    public static void GetDirectoriesTest()
    {
      //            :tmp、user
      var expected = new List<string>() { "tmp", "user", };

      var actual = access.GetDirectories(@"/");

      foreach (var item in actual)
      {
        Console.WriteLine(item);
      }

    }

    //_     _       
    public static void GetFilesTest()
    {
      //           
      var expected = new List<string>();

      var actual = access.GetFiles(@"/");
      foreach (var item in actual)
      {
        Console.WriteLine(item);
      }

    }

    //_  zzz  _    _       zzz  _  zzz  _    _       zzz  
    public static void DirectoryTest()
    {
      var directoryName = "zzz";

      //   zzz  
      var boolCreateDirectory = access.CreateDirectory(directoryName);

      Console.WriteLine("  zzz  _    :"+boolCreateDirectory);

      //   zzz  _    _       zzz  
      //            :tmp、user、zzz
      var expectedCreateDirectory = new List<string>() { "tmp", "user", directoryName, };

      var actualCreateDirectory = access.GetDirectories(@"/");


      foreach (var item in actualCreateDirectory)
      {
        Console.WriteLine(item);
      }

      Console.WriteLine("********************************************");
      //   zzz  
      var boolDeleteDirectory = access.DeleteDirectory(directoryName);

      Console.WriteLine("  zzz  _    :" + boolDeleteDirectory);


      //   zzz  _    _       zzz  
      //            :tmp、user
      var expectedDeleteDirectory = new List<string>() { "tmp", "user", };

      var actualDeleteDirectory = access.GetDirectories(@"/");

      foreach (var item in actualDeleteDirectory)
      {
        Console.WriteLine(item);
      }
    }

    //_  Test  _       Test  _  Test  _    _       Test  
    public static void FileTest()
    {
      var localFile = Path.Combine(Directory.GetCurrentDirectory(), "TestFolder", "Test.jpg");

      var remotePath = "Test.jpg";

      //   Test  
      var boolCreateFile = access.CreateFile(localFile, remotePath);

      //   Test  _       Test  
      var expectedCreateFile = new List<string>() { remotePath, };

      var actualCreateFile = access.GetFiles(@"/");

      foreach (var item in actualCreateFile)
      {
        Console.WriteLine(item);
      }
      Console.WriteLine("********************************************");
      //   Test  
      var boolDeleteFile = access.DeleteDirectory(remotePath);

      Console.WriteLine("  Test  _    :"+boolDeleteFile);

      //   Test  _    _       Test  
      var expectedDeleteFile = new List<string>();

      var actualDeleteFile = access.GetFiles(@"/");

      foreach (var item in actualDeleteFile)
      {
        Console.WriteLine(item);
      }
    }

    //_  Test  _       Test  _  Test  _    _    Test  Stream     
    public static void OpenFileTest()
    {
      var localFile = Path.Combine(Directory.GetCurrentDirectory(), "TestFolder", "Test.jpg");
      var remotePath = "Test.jpg";
      var saveFile = Path.Combine(Directory.GetCurrentDirectory(), "Test.jpg");

      Console.WriteLine("saveFile:" + saveFile);
      Console.WriteLine("********************************************");
      //   Test  
      var boolCreateFile = access.CreateFile(localFile, remotePath);

      //   Test  _       Test  
      var expectedCreateFile = new List<string>() { remotePath, };

      var actualCreateFile = access.GetFiles(@"/");

      foreach (var item in actualCreateFile)
      {
        Console.WriteLine(item);
      }


      Console.WriteLine("********************************************");
      //   Test  
      var response = access.OpenFile(remotePath);

      //   Test  _    
      response.EnsureSuccessStatusCode();

      //   Test  _    _    Test  Stream     
      using (var fs = File.Create(saveFile))
      {
        response.Content.CopyToAsync(fs).Wait();
      }

      Console.WriteLine(File.Exists(saveFile));

    }
  }
}
실행 결 과 는 그림 과 같 습 니 다.

이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기