Skip to content

Commit

Permalink
Expose FileSystem.Exists API (#909)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunildixit authored Apr 19, 2021
1 parent d5b4f42 commit 2632d8d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/csharp/Microsoft.Spark.E2ETest/Hadoop/FileSystemTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.IO;
using System.Linq;
using Microsoft.Spark.Hadoop.Fs;
using Microsoft.Spark.Sql;
using Microsoft.Spark.UnitTest.TestUtils;
Expand Down Expand Up @@ -52,5 +53,30 @@ public void TestDelete()

Assert.False(Directory.Exists(path));
}

/// <summary>
/// Tests that Exists() returns true if the file exist.
/// Tests that Exists() returns false if the file doesn't exist.
/// </summary>
[Fact]
public void TestExists()
{
using FileSystem fs = FileSystem.Get(_spark.SparkContext.HadoopConfiguration());

using var tempDirectory = new TemporaryDirectory();

string path = Path.Combine(tempDirectory.Path, "temp-table");

Assert.False(fs.Exists(path));

_spark.Range(25).Coalesce(1).Write().Csv(path);

Assert.True(fs.Exists(path));

string dataFile = Directory.GetFiles(path, "*.csv").FirstOrDefault();

Assert.NotNull(dataFile);
Assert.True(fs.Exists(dataFile));
}
}
}
13 changes: 13 additions & 0 deletions src/csharp/Microsoft.Spark/Hadoop/Fs/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ public bool Delete(string path, bool recursive = true)
return (bool)_jvmObject.Invoke("delete", pathObject, recursive);
}

/// <summary>
/// Check if a path exists.
/// </summary>
/// <param name="path">Source path</param>
/// <returns>True if the path exists else false.</returns>
public bool Exists(string path)
{
JvmObjectReference pathObject =
SparkEnvironment.JvmBridge.CallConstructor("org.apache.hadoop.fs.Path", path);

return (bool)_jvmObject.Invoke("exists", pathObject);
}

public void Dispose() => _jvmObject.Invoke("close");
}
}

0 comments on commit 2632d8d

Please sign in to comment.