This repository has been archived by the owner on Aug 4, 2022. It is now read-only.
/
HadoopFileSystem.cs
171 lines (152 loc) · 6.41 KB
/
HadoopFileSystem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Org.Apache.REEF.Tang.Annotations;
using Org.Apache.REEF.Utilities.Logging;
namespace Org.Apache.REEF.IO.FileSystem.Hadoop
{
/// <summary>
/// Implements the IFileSystem interface for HDFS using external commands.
/// </summary>
/// <remarks>
/// Note that operations with this class are enormously slow. If you can, use a more native way to access the file system
/// in question.
/// </remarks>
/// <a href="http://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-common/FileSystemShell.html">
/// FileSystemShell</a>
internal sealed class HadoopFileSystem : IFileSystem
{
private static readonly Logger Logger = Logger.GetLogger(typeof(HadoopFileSystem));
private static readonly Regex NoSuchFileOrDirectoryRegEx = new Regex("^ls: `.*': No such file or directory");
private static readonly Regex LsFirstLineRegex = new Regex("^Found .* items");
private readonly HdfsCommandRunner _commandRunner;
private readonly string _uriPrefix;
private const string PrefixTemplate = "{0}://{1}/";
[Inject]
private HadoopFileSystem(HdfsCommandRunner commandRunner)
{
_commandRunner = commandRunner;
_uriPrefix = GetUriPrefix();
}
/// <summary>
/// Create Uri from a given file name
/// If the path already contains prefix, use it directly and verify the prefix after it is created.
/// Otherwise add the prefix in fron of the relative path.
/// If path is null or the prefix doesn't match the prefix in the FileSystem, throw ArgumentException
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public Uri CreateUriForPath(string path)
{
Logger.Log(Level.Info, string.Format(CultureInfo.CurrentCulture, "CreateUriForPath with path: {0}, _uriPrefix: {1}.", path, _uriPrefix));
if (path == null)
{
throw new ArgumentException("null path passed in CreateUriForPath");
}
Uri uri;
try
{
uri = new Uri(path);
Logger.Log(Level.Info, string.Format(CultureInfo.CurrentCulture, "Uri {0} created in CreateUriForPath.", uri));
}
catch (UriFormatException)
{
uri = new Uri(_uriPrefix + path);
Logger.Log(Level.Info, string.Format(CultureInfo.CurrentCulture, "Uri {0} created in CreateUriForPath with prefix added.", uri));
}
return uri;
}
/// <summary>
/// Not implemented by this IFileSystem.
/// </summary>
/// <param name="remoteFileUri"></param>
/// <returns></returns>
public FileStatus GetFileStatus(Uri remoteFileUri)
{
throw new NotImplementedException("GetFileStatus() is not implemented for HadoopFileSystem");
}
/// <summary>
/// Not implemented by this IFileSystem.
/// </summary>
/// <param name="fileUri"></param>
/// <returns></returns>
public Stream Open(Uri fileUri)
{
throw new NotImplementedException(
"Open() is not supported by HadoopFileSystem. Use CopyToLocal and open the local file instead.");
}
/// <summary>
/// Not implemented by this IFileSystem.
/// </summary>
/// <param name="fileUri"></param>
/// <returns></returns>
public Stream Create(Uri fileUri)
{
throw new NotImplementedException(
"Create() is not supported by HadoopFileSystem. Create a local file and use CopyFromLocal instead.");
}
public void Delete(Uri fileUri)
{
// Delete the file via the hdfs command line.
_commandRunner.Run("dfs -rm " + fileUri);
}
public bool Exists(Uri fileUri)
{
// This determines the existence of a file based on the 'ls' command.
// Ideally, we'd use the 'test' command's return value, but we did not find a way to access that.
return
_commandRunner.Run("dfs -ls " + fileUri).StdErr
.All(line => !NoSuchFileOrDirectoryRegEx.IsMatch(line));
}
public void Copy(Uri sourceUri, Uri destinationUri)
{
_commandRunner.Run("dfs -cp " + sourceUri + " " + destinationUri);
}
public void CopyToLocal(Uri remoteFileUri, string localName)
{
_commandRunner.Run("dfs -get " + remoteFileUri + " " + localName);
}
public void CopyFromLocal(string localFileName, Uri remoteFileUri)
{
_commandRunner.Run("dfs -put " + localFileName + " " + remoteFileUri);
}
public void CreateDirectory(Uri directoryUri)
{
_commandRunner.Run("dfs -mkdir " + directoryUri);
}
public void DeleteDirectory(Uri directoryUri)
{
_commandRunner.Run("dfs -rmdir " + directoryUri);
}
public IEnumerable<Uri> GetChildren(Uri directoryUri)
{
return _commandRunner.Run("dfs -ls " + directoryUri)
.StdOut.Where(line => !LsFirstLineRegex.IsMatch(line))
.Select(line => line.Split())
.Select(x => new Uri(x[x.Length - 1]));
}
private string GetUriPrefix()
{
return _commandRunner.Run("getconf -confKey fs.default.name").StdOut.First();
}
}
}