-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose Hadoop Configuration/FileSystem (#787)
- Loading branch information
Showing
8 changed files
with
150 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/csharp/Microsoft.Spark.E2ETest/Hadoop/FileSystemTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.IO; | ||
using Microsoft.Spark.Hadoop.Fs; | ||
using Microsoft.Spark.Sql; | ||
using Microsoft.Spark.UnitTest.TestUtils; | ||
using Xunit; | ||
|
||
namespace Microsoft.Spark.E2ETest.Hadoop | ||
{ | ||
[Collection("Spark E2E Tests")] | ||
public class FileSystemTests | ||
{ | ||
private readonly SparkSession _spark; | ||
|
||
public FileSystemTests(SparkFixture fixture) | ||
{ | ||
_spark = fixture.Spark; | ||
} | ||
|
||
/// <summary> | ||
/// Test that methods return the expected signature. | ||
/// </summary> | ||
[Fact] | ||
public void TestSignatures() | ||
{ | ||
using var tempDirectory = new TemporaryDirectory(); | ||
|
||
using FileSystem fs = FileSystem.Get(_spark.SparkContext.HadoopConfiguration()); | ||
|
||
Assert.IsType<bool>(fs.Delete(tempDirectory.Path, true)); | ||
} | ||
|
||
/// <summary> | ||
/// Test that Delete() deletes the file. | ||
/// </summary> | ||
[Fact] | ||
public void TestDelete() | ||
{ | ||
using FileSystem fs = FileSystem.Get(_spark.SparkContext.HadoopConfiguration()); | ||
|
||
using var tempDirectory = new TemporaryDirectory(); | ||
string path = Path.Combine(tempDirectory.Path, "temp-table"); | ||
_spark.Range(25).Write().Format("parquet").Save(path); | ||
|
||
Assert.True(Directory.Exists(path)); | ||
|
||
Assert.True(fs.Delete(path, true)); | ||
Assert.False(fs.Delete(path, true)); | ||
|
||
Assert.False(Directory.Exists(path)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Microsoft.Spark.Interop.Ipc; | ||
|
||
namespace Microsoft.Spark.Hadoop.Conf | ||
{ | ||
/// <summary> | ||
/// Provides access to configuration parameters. | ||
/// </summary> | ||
public class Configuration : IJvmObjectReferenceProvider | ||
{ | ||
private readonly JvmObjectReference _jvmObject; | ||
|
||
internal Configuration(JvmObjectReference jvmObject) | ||
{ | ||
_jvmObject = jvmObject; | ||
} | ||
|
||
JvmObjectReference IJvmObjectReferenceProvider.Reference => _jvmObject; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using Microsoft.Spark.Hadoop.Conf; | ||
using Microsoft.Spark.Interop; | ||
using Microsoft.Spark.Interop.Ipc; | ||
|
||
namespace Microsoft.Spark.Hadoop.Fs | ||
{ | ||
/// <summary> | ||
/// A fairly generic filesystem. It may be implemented as a distributed filesystem, or as a "local" one | ||
/// that reflects the locally-connected disk. The local version exists for small Hadoop instances and for | ||
/// testing. | ||
/// | ||
/// All user code that may potentially use the Hadoop Distributed File System should be written to use a | ||
/// FileSystem object. The Hadoop DFS is a multi-machine system that appears as a single disk. It's | ||
/// useful because of its fault tolerance and potentially very large capacity. | ||
/// </summary> | ||
public class FileSystem : IJvmObjectReferenceProvider, IDisposable | ||
{ | ||
private readonly JvmObjectReference _jvmObject; | ||
|
||
internal FileSystem(JvmObjectReference jvmObject) | ||
{ | ||
_jvmObject = jvmObject; | ||
} | ||
|
||
JvmObjectReference IJvmObjectReferenceProvider.Reference => _jvmObject; | ||
|
||
/// <summary> | ||
/// Returns the configured <see cref="FileSystem"/>. | ||
/// </summary> | ||
/// <param name="conf">The configuration to use.</param> | ||
/// <returns>The FileSystem.</returns> | ||
public static FileSystem Get(Configuration conf) => | ||
new FileSystem((JvmObjectReference)SparkEnvironment.JvmBridge.CallStaticJavaMethod( | ||
"org.apache.hadoop.fs.FileSystem", | ||
"get", | ||
conf)); | ||
|
||
/// <summary> | ||
/// Delete a file. | ||
/// </summary> | ||
/// <param name="path">The path to delete.</param> | ||
/// <param name="recursive">If path is a directory and set to true, the directory is deleted else | ||
/// throws an exception. In case of a file the recursive can be set to either true or false.</param> | ||
/// <returns>True if delete is successful else false.</returns> | ||
public bool Delete(string path, bool recursive = true) | ||
{ | ||
JvmObjectReference pathObject = | ||
SparkEnvironment.JvmBridge.CallConstructor("org.apache.hadoop.fs.Path", path); | ||
|
||
return (bool)_jvmObject.Invoke("delete", pathObject, recursive); | ||
} | ||
|
||
public void Dispose() => _jvmObject.Invoke("close"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters