예제 #1
0
        private static void FileTestSystem(HdfsFileSystem hdfsSystem)
        {
            string localfilename = "MobileSampleData.txt";
            string localfilepath = Path.GetFullPath(@"..\..\..\Sample") + @"\" + localfilename;

            string filename = BaseHdfsPath + "/testdata.txt";

            if (File.Exists(localfilepath))
            {
                string localhdfsfilename = BaseHdfsPath + @"\" + localfilename;

                // Show blocks used by file
                List <string> hosts = hdfsSystem.GetHosts(localhdfsfilename, 0, 1024);
                if (hosts != null)
                {
                    Console.WriteLine("File Hosts:");
                    foreach (string host in hosts)
                    {
                        Console.WriteLine("\t" + host);
                    }
                }
            }

            // Duplicate the file and modify some properties
            string subhdfspath = BaseHdfsPath + "/duplicate";
            string subfilename = subhdfspath + "/duplicatedata.txt";

            Console.WriteLine();
            Console.WriteLine("Creating file duplicate: " + subfilename);
            hdfsSystem.CreateDirectory(subhdfspath);
            HdfsFileSystem.Copy(hdfsSystem, filename, hdfsSystem, subhdfspath);
            hdfsSystem.SetTimes(filename, DateTime.Now.AddDays(-2), DateTime.Now.AddDays(-1));

            // Perform a directory parse and display
            Console.WriteLine();
            hdfsSystem.SetWorkingDirectory(BaseHdfsPath);
            using (HdfsFileInfoEntry pathinfo = hdfsSystem.GetPathInfo(BaseHdfsPath))
            {
                if (pathinfo != null)
                {
                    string kind = pathinfo.Kind == HdfsFileInfoEntryKind.Directory ? "Directory" : "\tFile";
                    Console.WriteLine(string.Format(@"{0}:""{1}"", Modified/Accessed:""{2:G}, {3:G}"", Owner:""{4}""", kind, pathinfo.Name, pathinfo.LastModified, pathinfo.LastAccessed, pathinfo.Owner));
                }
            }

            Action <string> processDirectory = null;

            processDirectory = (looppath) =>
            {
                using (HdfsFileInfoEntries entries = hdfsSystem.ListDirectory(looppath))
                {
                    foreach (HdfsFileInfoEntry entry in entries.Entries)
                    {
                        string kind = entry.Kind == HdfsFileInfoEntryKind.Directory ? "Directory" : "\tFile";
                        Console.WriteLine(string.Format(@"{0}:""{1}"", Modified/Accessed:""{2:G}, {3:G}"", Owner:""{4}""", kind, entry.Name, entry.LastModified, entry.LastAccessed, entry.Owner));
                        if (entry.Kind == HdfsFileInfoEntryKind.Directory)
                        {
                            processDirectory(entry.Name);
                        }
                    }
                }
            };
            processDirectory(BaseHdfsPath);

            Console.WriteLine();
        }