Reading files with a Java stored procedure¶
You can read the contents of a file with handler code. The file must be on a Snowflake stage that’s available to your handler. For example, you might want to read a file to process unstructured data in the handler.
To read the contents of staged files, your handler can call methods in either the SnowflakeFile class or the InputStream
class. You might do this if you need to access the file dynamically during compute. For more information, see
Reading a dynamically-specified file with SnowflakeFile or Reading a dynamically-specified file with InputStream in this topic.
SnowflakeFile provides features not available with InputStream, as described in the following table.
Class |
Input |
Notes |
|---|---|---|
|
URL formats:
The file must be located in a named internal stage or an external stage. |
Easily access additional file attributes, such as file size. |
|
URL formats:
The file must be located in a named internal stage or an external stage. |
Reading a dynamically-specified file with SnowflakeFile¶
Code in the following example has a handler function execute that takes a String and returns a String
with the file’s contents. At run time, Snowflake initializes the handler’s fileName variable from the incoming file path in the
procedure’s input variable. The handler code uses a SnowflakeFile instance to read the file.
Code in the following CALL example creates a scoped file URL that points to the file. This is an encoded URL that permits temporary access to a staged file without granting privileges to the stage itself.
Note
For an owner’s rights stored procedure, the procedure’s owner must have access to any files that are not scoped URLs. For caller’s rights
procedures, the caller must have access to any files that are not scoped URLs. In either case, you can read the staged file by having the
handler code call the SnowflakeFile.newInstance method with a boolean value for a new requireScopedUrl parameter.
The following example uses SnowflakeFile.newInstance while specifying that a scoped URL is not required.
Reading a dynamically-specified file with InputStream¶
Code in the following example has a handler function execute that takes an InputStream and returns a String
with the file’s contents. At run time, Snowflake initializes the handler’s stream variable from the incoming file path in the
procedure’s input argument. The handler code uses the InputStream to read the file.
Code in the following CALL example creates a scoped file URL that points to the file. This is an encoded URL that permits temporary access to a staged file without granting privileges to the stage itself.