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

运行结果如图:

这里写图片描述

相关文章
|
2月前
|
SQL 分布式计算 Hadoop
Hadoop-37 HBase集群 JavaAPI 操作3台云服务器 POM 实现增删改查调用操作 列族信息 扫描全表
Hadoop-37 HBase集群 JavaAPI 操作3台云服务器 POM 实现增删改查调用操作 列族信息 扫描全表
33 3
|
2月前
|
分布式计算 Hadoop Shell
Hadoop-36 HBase 3节点云服务器集群 HBase Shell 增删改查 全程多图详细 列族 row key value filter
Hadoop-36 HBase 3节点云服务器集群 HBase Shell 增删改查 全程多图详细 列族 row key value filter
58 3
|
7月前
|
资源调度 分布式计算 Ubuntu
retry.RetryInvocationHandler: java.net.NoRouteToHostException: No Route to Host from hadoop100/192.
Error: NoRouteToHostException connecting hadoop100 to hadoop101. Possible causes include firewall issues or misconfigured IP mappings. Check firewall status with `ufw` (Ubuntu) or `firewalld` (CentOS), ensure correct hosts and hostname entries, and verify passwordless SS
45 0
|
7月前
|
SQL 开发框架 .NET
ASP.NET Web——GridView完整增删改查示例(全篇幅包含sql脚本)大二结业考试必备技能
ASP.NET Web——GridView完整增删改查示例(全篇幅包含sql脚本)大二结业考试必备技能
83 0
|
7月前
in thread “main“ java.lang.IllegalArgumentException:java.net.UnknownHostException:hadoop102
in thread “main“ java.lang.IllegalArgumentException:java.net.UnknownHostException:hadoop102
71 0
|
分布式计算 安全 Hadoop
hadoop 入门 java.net.ConnectException: Connection refused
hadoop 入门 java.net.ConnectException: Connection refused
258 0
|
分布式计算 Hadoop
flume报错记录:java.net.ConnectException:拒绝连接; For more details see: http://wiki.apache.org/hadoop/ConnectionRefused
flume报错记录:java.net.ConnectException:拒绝连接; For more details see: http://wiki.apache.org/hadoop/ConnectionRefused
663 0
flume报错记录:java.net.ConnectException:拒绝连接; For more details see:  http://wiki.apache.org/hadoop/ConnectionRefused
|
开发框架 前端开发 .NET
ASP.NET MVC增删改查带图片路径读取
ASP.NET MVC增删改查带图片路径读取
188 0
ASP.NET MVC增删改查带图片路径读取
|
开发框架 前端开发 .NET
[原创]Asp.net MVC 学习之路-003(增删改查,后端手工,前端生成)
[原创]Asp.net MVC 学习之路-003(增删改查,后端手工,前端生成)
[原创]Asp.net MVC 学习之路-003(增删改查,后端手工,前端生成)
|
关系型数据库 测试技术 API
asp.net core webapi 使用ef 对mysql进行增删改查,并生成Docker镜像构建容器运行
1.构建运行mysql容器,添加数据库user 参考Docker创建运行多个mysql容器,地址 http://www.cnblogs.com/heyangyi/p/9288402.html 添加user数据库,添加tbusers表 2.
3036 0

热门文章

最新文章

相关实验场景

更多
下一篇
无影云桌面