Skip to content

Added the exists method to the FileSystem. #909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 19, 2021
Merged

Conversation

sunildixit
Copy link
Contributor

@sunildixit sunildixit commented Apr 10, 2021

This PR adds the exists(string filePath) api to the FileSystem, which will allow the users to check whether a given folder or file exists in the file system or not.

I'll be adding more methods once I know, that I have done this work correctly.

Example: Check for an existing file

SparkSession spark = ....
FileSystem fs = FileSystem.Get(spark.SparkContext.HadoopConfiguration());
bool fileExists = fs.Exists("abfss://[email protected]/myfolder/file.csv");

This PR relates to #328

@dnfadmin
Copy link

dnfadmin commented Apr 10, 2021

CLA assistant check
All CLA requirements met.

@sunildixit
Copy link
Contributor Author

@AFFogarty Sorry it took me some time to get this PR out. Please take a look and let me know.

Copy link
Contributor

@AFFogarty AFFogarty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your PR, @sunildixit. I have left a few comments. 😃

using var tempDirectory = new TemporaryDirectory();
string path = Path.Combine(tempDirectory.Path, "temp-table");

_spark
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this fit on one line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added it to one line.


Assert.NotNull(dataFile);

Assert.True(fs.Exists(dataFile));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should add an assertion to test that fs.Exists() returns false on a non-existent path?

For example you could do something like this:

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

Assert.False(fs.Exists(path));
_spark.Range(25).Write().Csv(path);
Assert.True(fs.Exists(path));

That way, you prove that the Exists() return value changes after writing to that path.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch. I have added the assertion.

@@ -55,6 +55,14 @@ public bool Delete(string path, bool recursive = true)
return (bool)_jvmObject.Invoke("delete", pathObject, recursive);
}

public bool Exists(string path)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a documentation comment describing the method? You can copy its content from open-source org.apache.hadoop.fs.FileSystem.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -52,5 +53,28 @@ public void TestDelete()

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

[Fact]
public void TestExists()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a documentation comment describing the test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the documentation about the test

AFFogarty
AFFogarty previously approved these changes Apr 12, 2021
Copy link
Contributor

@AFFogarty AFFogarty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks @sunildixit !

Niharikadutta
Niharikadutta previously approved these changes Apr 13, 2021
Copy link
Collaborator

@Niharikadutta Niharikadutta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @sunildixit !

suhsteve
suhsteve previously approved these changes Apr 13, 2021
Copy link
Member

@suhsteve suhsteve left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Copy link
Contributor

@imback82 imback82 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @sunildixit!

@imback82 imback82 dismissed stale reviews from suhsteve, Niharikadutta, and AFFogarty via 84b3a43 April 19, 2021 18:19
@imback82 imback82 merged commit 2632d8d into dotnet:main Apr 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants