常用的Hadoop 文件查看工具

packages.config

<?xml version="1.0" encoding="utf-8"?>

<packages>
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" />
  <package id="Microsoft.Data.Edm" version="5.2.0" targetFramework="net45" />
  <package id="Microsoft.Data.OData" version="5.2.0" targetFramework="net45" />
  <package id="Microsoft.Hadoop.WebClient" version="0.12.5126.42915" targetFramework="net45" />
  <package id="Microsoft.WindowsAzure.ConfigurationManager" version="1.8.0.0" targetFramework="net45" />
  <package id="Newtonsoft.Json" version="6.0.4" targetFramework="net45" />
  <package id="System.Spatial" version="5.2.0" targetFramework="net45" />
  <package id="WindowsAzure.Storage" version="2.0.4.1" targetFramework="net45" />
</packages>

Explorer.cs

using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Microsoft.Hadoop.WebHDFS;

namespace HdfsExplore
{
    public partial class Explorer : Form
    {
        private WebHDFSClient client;

        public Explorer()
        {
            InitializeComponent();

            if (!CreateClient())
            {
                return;
            }

            // 使用驱动器列表填充TreeView
            PopulateDriveList();
        }

        //此过程使用驱动器列表填充TreeView
        private async void PopulateDriveList()
        {
            TreeNode nodeTreeNode;
            var imageIndex = 0;
            var selectIndex = 0;

            const int Removable = 2;
            const int LocalDisk = 3;
            const int Network = 4;
            const int CD = 5;
            //const int RAMDrive = 6;

            Cursor = Cursors.WaitCursor;
            //清除TreeView
            tvFolders.Nodes.Clear();
            nodeTreeNode = new TreeNode("Hadoop Root", 0, 0);
            tvFolders.Nodes.Add(nodeTreeNode);

            //设置节点集合
            var nodeCollection = nodeTreeNode.Nodes;


            var x = await client.GetDirectoryStatus("/");


            foreach (var mo in x.Directories)
            {
                //switch (int.Parse(mo["DriveType"].ToString()))
                //{
                //    case Removable:           //removable drives
                //        imageIndex = 5;
                //        selectIndex = 5;
                //        break;
                //    case LocalDisk:           //Local drives
                //        imageIndex = 6;
                //        selectIndex = 6;
                //        break;
                //    case CD:              //CD rom drives
                //        imageIndex = 7;
                //        selectIndex = 7;
                //        break;
                //    case Network:         //Network drives
                //        imageIndex = 8;
                //        selectIndex = 8;
                //        break;
                //    default:              //defalut to folder
                //        imageIndex = 2;
                //        selectIndex = 3;
                //        break;
                //}

                imageIndex = 6;
                selectIndex = 6;

                //创建新的驱动器节点
                nodeTreeNode = new TreeNode(mo.PathSuffix, imageIndex, selectIndex);

                //添加新节点
                nodeCollection.Add(nodeTreeNode);
            }


            //初始化文件ListView
            InitListView();

            Cursor = Cursors.Default;
        }

        private void tvFolders_AfterSelect(object sender, TreeViewEventArgs e)
        {
            //选择文件夹时填充文件夹和文件
            Cursor = Cursors.WaitCursor;

            //获取当前所选驱动器或文件夹
            var nodeCurrent = e.Node;

            //清除所有子文件夹
            nodeCurrent.Nodes.Clear();

            if (nodeCurrent.SelectedImageIndex == 0)
            {
                //重新填写驱动器列表
                PopulateDriveList();
            }
            else
            {
                //填充子文件夹和文件夹文件
                PopulateDirectory(nodeCurrent, nodeCurrent.Nodes);
            }
            Cursor = Cursors.Default;
        }

        protected void InitListView()
        {
            //初始化ListView控件
            lvFiles.Clear();
            //创建ListView的列标题
            lvFiles.Columns.Add("名称", 150, HorizontalAlignment.Left);
            lvFiles.Columns.Add("大小", 75, HorizontalAlignment.Right);
            lvFiles.Columns.Add("上次访问", 140, HorizontalAlignment.Left);
            lvFiles.Columns.Add("上次修改", 140, HorizontalAlignment.Left);


            lvFiles.Columns.Add("用有者", 75);
            lvFiles.Columns.Add("权限", 75);


        }

        protected async void PopulateDirectory(TreeNode nodeCurrent, TreeNodeCollection nodeCurrentCollection)
        {
            TreeNode nodeDir;
            var imageIndex = 2; //未选择的图像索引
            var selectIndex = 3; //选择图像索引

            if (nodeCurrent.SelectedImageIndex != 0)
            {
                //用文件夹填充树视图
                try
                {
                    var directoryStatus = await client.GetDirectoryStatus(getFullPath(nodeCurrent.FullPath));


                    var dirs = directoryStatus.Directories;

                    //check path
                    //if (!dirs.Any())
                    //{
                    //    MessageBox.Show("Directory or path " + nodeCurrent.ToString() + " does not exist.");
                    //}
                    //else
                    //{
                    //填充文件
                    PopulateFiles(nodeCurrent);

                    var stringDirectories = dirs.Select(x => x.PathSuffix).ToArray();
                    var stringFullPath = "";
                    var stringPathName = "";

                    //循环遍历所有目录
                    foreach (var stringDir in stringDirectories)
                    {
                        stringFullPath = stringDir;
                        stringPathName = GetPathName(stringFullPath);

                        //创建目录节点
                        nodeDir = new TreeNode(stringPathName, imageIndex, selectIndex);
                        nodeCurrentCollection.Add(nodeDir);
                    }
                    //}
                }
                catch (IOException e)
                {
                    MessageBox.Show("Error: Drive not ready or directory does not exist.");
                }
                catch (UnauthorizedAccessException e)
                {
                    MessageBox.Show("Error: Drive or directory access denided.");
                }
                catch (Exception e)
                {
                    MessageBox.Show("Error: " + e);
                }
            }
        }

        protected string GetPathName(string stringPath)
        {
            //获取文件夹的名称
            var stringSplit = stringPath.Split('\\');
            var _maxIndex = stringSplit.Length;
            return stringSplit[_maxIndex - 1];
        }

        protected async void PopulateFiles(TreeNode nodeCurrent)
        {
            //使用文件填充列表视图
            var lvData = new string[6];

            //清除列表
            InitListView();

            if (nodeCurrent.SelectedImageIndex != 0)
            {
                var directoryStatus = await client.GetDirectoryStatus(getFullPath(nodeCurrent.FullPath));


                try
                {
                    var files = directoryStatus.Files; // Directory.GetFiles(getFullPath(nodeCurrent.FullPath));
                    var stringFileName = "";
                    string dtCreateDate, dtModifyDate;
                    Int64 lFileSize = 0;

                    //循环遍历所有文件
                    foreach (var file in files)
                    {
                        stringFileName = file.PathSuffix;
                        //FileInfo objFileSize = new FileInfo(stringFileName);
                        lFileSize = file.Length;
                        dtCreateDate = Utility.JavaTicksToDatetime(file.AccessTime).ToString();
                        //GetCreationTime(stringFileName);
                        dtModifyDate = Utility.JavaTicksToDatetime(file.ModificationTime).ToString();
                        //GetLastWriteTime(stringFileName);

                        //创建列表视图数据
                        lvData[0] = GetPathName(stringFileName);
                        lvData[1] = formatSize(lFileSize);

                        lvData[2] = dtCreateDate;

                        lvData[3] = dtModifyDate;

                        lvData[4] = file.Owner;
                        lvData[5] = file.Permission;

                        //创建实际列表项
                        var lvItem = new ListViewItem(lvData, 0);
                        lvFiles.Items.Add(lvItem);
                    }
                }
                catch (IOException e)
                {
                    MessageBox.Show("错误:驱动器未准备好或目录不存在.");
                }
                catch (UnauthorizedAccessException e)
                {
                    MessageBox.Show("错误:驱动器或目录访问被拒绝.");
                }
                catch (Exception e)
                {

                }
                // }
            }
        }

        protected string getFullPath(string stringPath)
        {
            //获取完整路径
            var stringParse = "";
            //从路径中删除根目录.
            stringParse = stringPath.Replace("Hadoop Root\\", "/").Replace("\\", "/");

            return stringParse;
        }

        protected string getFullPath(TreeNode nodeCurrent)
        {
            var path = nodeCurrent.FullPath;

            return getFullPath(path);
        }

        protected string formatSize(Int64 lSize)
        {
            //格式编号为KB
            var stringSize = "";
            var myNfi = new NumberFormatInfo();

            Int64 lKBSize = 0;

            if (lSize < 1024)
            {
                if (lSize == 0)
                {
                    //0 byte
                    stringSize = "0";
                }
                else
                {
                    //小于1K但不是零字节
                    stringSize = "1";
                }
            }
            else
            {
                //转换为KB
                lKBSize = lSize / 1024;
                //默认格式的格式数字
                stringSize = lKBSize.ToString("n", myNfi);
                //移除 decimal
                stringSize = stringSize.Replace(".00", "");
            }

            return stringSize + " KB";
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            //退出应用
            Close();
        }

        private void tvFolders_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button != MouseButtons.Right) return;
            if (e.Node == null) return;

            //MessageBox.Show(e.Node.FullPath);

            tvFolders.SelectedNode = e.Node;

            InitcmsOneRout();

            cmsOneRout.Show(tvFolders, e.X, e.Y);
        }

        private void InitcmsOneRout()
        {
            cmsOneRout = new ContextMenuStrip();

            var tmiEditRoutStation = new ToolStripMenuItem("修改Owner");
            cmsOneRout.Items.Add(tmiEditRoutStation);

            var changePermission = new ToolStripMenuItem("修改权限");

            changePermission.Click += changePress_Click;
            cmsOneRout.Items.Add(changePermission);
            var tmiDel = new ToolStripMenuItem("删除");

            tmiDel.Click += tmiDel_Click;
            cmsOneRout.Items.Add(tmiDel);

        }

        async void changePress_Click(object sender, EventArgs e)
        {
            //if (lvFiles.SelectedItems.Count == 0)
            //    return;

            if (tvFolders.SelectedNode == null)
                return;

            var remotepath = getFullPath(tvFolders.SelectedNode);
            //var remotefile = remotepath.TrimEnd('/') + "/" + lvFiles.SelectedItems[0].Text;
            var permissions = await client.GetFileStatus(remotepath);

            InputName ip = new InputName("修改权限(" + remotepath + ")", permissions.Permission);

            if (ip.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (string.IsNullOrWhiteSpace(ip.StrInput))
                {
                    MessageBox.Show("不能为空项");
                    return;
                }

                await client.SetPermissions(remotepath, ip.StrInput);
            }
        }




        private void tmiDel_Click(object sender, EventArgs e)
        {
            var path = getFullPath(tvFolders.SelectedNode);

            //client = new WebHDFSClient(new Uri("http://zhangbaowei:50070"), "hadoop");

            client.DeleteDirectory(path, true);

            MessageBox.Show("删除完成");

            tvFolders.SelectedNode.Remove();
        }

        private void menuItem5_Click(object sender, EventArgs e)
        {
            var parentPath = "/";

            if (tvFolders.SelectedNode != null)
            {
                parentPath = getFullPath(tvFolders.SelectedNode);
            }

            var name = new InputName();

            if (name.ShowDialog() != DialogResult.OK)
                return;


            var newname = name.StrInput;


            client.CreateDirectory(parentPath.TrimEnd('/') + "/" + newname);
        }

        private void menuItem6_Click(object sender, EventArgs e)
        {
            showSetting();
            PopulateDriveList();
        }

        private void showSetting()
        {
            var setting = new Setting();

            if (setting.ShowDialog() == DialogResult.OK)
            {
                CreateClient();
            }
        }

        private bool CreateClient()
        {
            var url = AppConfig.GetValue("hdfsurl");

            var username = AppConfig.GetValue("username");


            if (string.IsNullOrWhiteSpace(url) || string.IsNullOrWhiteSpace(username))
            {
                MessageBox.Show("未进行正确配置");
                showSetting();

                return false;
            }

            client = new WebHDFSClient(new Uri(url), username);

            return true;
        }

        private void menuItem7_Click(object sender, EventArgs e)
        {
            if (tvFolders.SelectedNode == null)
            {
                MessageBox.Show("未选中上传目标");
                return;
            }

            OpenFileDialog of = new OpenFileDialog();
            of.Multiselect = true;

            if (of.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {

                var remotepath = getFullPath(tvFolders.SelectedNode);


                var filename = of.FileNames;

                foreach (var s in filename)
                {
                    client.CreateFile(s, remotepath.TrimEnd('/') + "/" + Path.GetFileName(s));
                }

                PopulateFiles(tvFolders.SelectedNode);

            }
        }

        private void menuItem8_Click(object sender, EventArgs e)
        {
            var items = lvFiles.SelectedItems;
            var remotepath = getFullPath(tvFolders.SelectedNode);
            foreach (var item in items)
            {


                var filename = ((ListViewItem)item).Text;

                client.DeleteDirectory(remotepath.TrimEnd('/') + "/" + filename);
            }
        }

        private async void lvFiles_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ListViewHitTestInfo info = lvFiles.HitTest(e.X, e.Y);
            ListViewItem item = info.Item;

            if (item != null)
            {
                //MessageBox.Show("The selected Item Name is: " + item.Text);

                var remotepath = getFullPath(tvFolders.SelectedNode);
                var remotefile = remotepath.TrimEnd('/') + "/" + item.Text;
                var message = await client.OpenFile(remotefile);

                var content = await message.Content.ReadAsStringAsync();


                ContentViewer cv = new ContentViewer(content, remotefile);
                cv.Show();

            }
            else
            {
                this.lvFiles.SelectedItems.Clear();
                MessageBox.Show("No Item is selected");
            }
        }

        private void lvFiles_MouseClick(object sender, MouseEventArgs e)
        {
            //判断鼠标是否右键
            if (e.Button == MouseButtons.Right)
            {
                if (this.lvFiles.SelectedItems.Count > 0)  //选中项
                {

                    var cmslv = new ContextMenuStrip();

                    var tmiEditRoutStation = new ToolStripMenuItem("权限");
                    tmiEditRoutStation.Click += tmiEditRoutStation_Click;
                    cmslv.Items.Add(tmiEditRoutStation);

                    var tmiDelte = new ToolStripMenuItem("删除");
                    tmiDelte.Click += tmiDelte_Click;
                    cmslv.Items.Add(tmiDelte);


                    var downloadFile = new ToolStripMenuItem("下载");
                    downloadFile.Click += downloadFile_Click;
                    cmslv.Items.Add(downloadFile);


                    cmslv.Show(lvFiles, e.Location);
                    //this.lvFiles.ContextMenuStrip = cms选中菜单;
                }
                else   //未选中项
                {
                    //this.lvFiles.ContextMenuStrip = cms未选中菜单;
                }
            }
        }

        async void downloadFile_Click(object sender, EventArgs e)
        {
            if (lvFiles.SelectedItems.Count == 0)
                return;

            var remotepath = getFullPath(tvFolders.SelectedNode);


            var filefolder = new FolderBrowserDialog();

            if (filefolder.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                return;

            var locafolder = filefolder.SelectedPath;


            foreach (var lvFile in lvFiles.SelectedItems)
            {
                var removefilename = ((ListViewItem)lvFile).Text;
                var remotefile = remotepath.TrimEnd('/') + "/" + removefilename;

                var file = await client.OpenFile(remotefile);

                var filebytes = await file.Content.ReadAsByteArrayAsync();

                var locafile = locafolder + "/" + removefilename;

                System.IO.File.WriteAllBytes(locafile, filebytes);
                //lvFiles.Items.Remove((ListViewItem)lvFile);


            }

            MessageBox.Show("下载成功");
        }

        async void tmiDelte_Click(object sender, EventArgs e)
        {
            if (lvFiles.SelectedItems.Count == 0)
                return;

            var remotepath = getFullPath(tvFolders.SelectedNode);


            foreach (var lvFile in lvFiles.SelectedItems)
            {
                var remotefile = remotepath.TrimEnd('/') + "/" + ((ListViewItem)lvFile).Text;

                await client.DeleteDirectory(remotefile);

                lvFiles.Items.Remove((ListViewItem)lvFile);
            }


        }

        async void tmiEditRoutStation_Click(object sender, EventArgs e)
        {
            if (lvFiles.SelectedItems.Count == 0)
                return;

            var remotepath = getFullPath(tvFolders.SelectedNode);
            var remotefile = remotepath.TrimEnd('/') + "/" + lvFiles.SelectedItems[0].Text;


            InputName ip = new InputName("修改权限(" + remotefile + ")");

            if (ip.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (string.IsNullOrWhiteSpace(ip.StrInput))
                {
                    MessageBox.Show("不能为空项");
                    return;
                }

                await client.SetPermissions(remotefile, ip.StrInput);
            }
        }
    }
}

运行结果如图:

原创声明,本文系作者授权云+社区发表,未经许可,不得转载。

如有侵权,请联系 yunjia_community@tencent.com 删除。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏更流畅、简洁的软件开发方式

分页解决方案 之 QuickPager的使用方法(在UserControl里面使用分页控件的方法)

      因为我一直没有在UserControl里面使用过QuickPager分页控件,我都是直接在.aspx里面使用,所以这个bug一直没有发现。后来告诉我...

1907
来自专栏跟着阿笨一起玩NET

在RichTextBox中对关键字进行高亮显示

若要实现更复杂的功能,可以研究一下这个C#的IDE编辑器的代码。http://www.icsharpcode.net/OpenSource/SD/Defaul...

1380
来自专栏GreenLeaves

C#核编之System.Console类

      顾名思义,Console类封装了基于控制台的输入输出和错误流的操作,下面列举一些System.Console类常用的成员的,这些成员能为简单的命令行...

2005
来自专栏跟着阿笨一起玩NET

关于asp.net与winform导出excel的代码

一、asp.net中导出Execl的方法: 在asp.net中导出Execl有两种方法,一种是将导出的文件存放在服务器某个文件夹下面,然后将文件地址输出在...

921
来自专栏偏前端工程师的驿站

GridView实战一:自定义分页、排序、修改、插入、删除

前言:   在某次公司面试时被问到对GridView操作的熟悉程度,在那之前一直用Repeater内嵌table标签对GridView操作确实很少,于是最近在项...

35910
来自专栏木宛城主

SharePoint 2013 How to Backup Site Collection Automatically With a PowerShell Script

In this post I will introduce a way how to run a script for backing up SharePoin...

2417
来自专栏林德熙的博客

win10 uwp 读取保存WriteableBitmap 、BitmapImage 保存 WriteableBitmap 到文件从文件读 WriteableBitmapIma

我们在UWP,经常使用的图片,数据结构就是 BitmapImage 和 WriteableBitmap。关于 BitmapImage 和 WriteableBi...

1071
来自专栏飞扬的花生

将Json数据保存在静态脚本文件中读取

      一些常用的数据例如一些网站的区域信息被改变的可能性不大,一般不通过请求获取,于是我们选择存在静态文件中,例如以下Demo: 1.动态加载Json数据...

2566
来自专栏草根专栏

用VSCode开发一个asp.net core2.0+angular5项目(5): Angular5+asp.net core 2.0 web api文件上传

这部分就讲从angular5的客户端上传图片到asp.net core 2.0的 web api. 这是需要的源码: https://pan.baidu.com...

3935
来自专栏逸鹏说道

万恶的剪贴板==》为存储而生

近几天逆天经常大量复制粘贴一些图文信息,在某些特定的场合,图片都是无法直接粘贴进去的,就比如博客园的编辑器。 源码:https://github.com/dun...

2808

扫码关注云+社区