从 snowflake java UDF 读取外部阶段文件
Read external stage file from snowflake java UDF
我创建了一个 java UDF 来从我的外部阶段读取 word 文档。我在导入所有外部阶段文件并在我的 java UDF 中使用它们时使用了 imports 子句。它看起来不适合我。
还有其他方法可以动态地从舞台上读取文件吗?
Snowflake 支持使用标准 SQL 查询位于内部(即 Snowflake)阶段或命名为外部(Amazon S3、Google 云存储或 Microsoft Azure)阶段的数据文件。这对于 inspecting/viewing 暂存文件的内容很有用,尤其是在加载数据之前或卸载数据之后
https://docs.snowflake.com/en/user-guide/querying-stage.html
尚不支持使用 Java UDF 从阶段动态读取文件,如 Snowflake 峰会所示。作为执行的一部分,您可以访问 IMPORTS
列表中的任何文件,但是,这些文件通常应该用于读取配置文件、模型等。使用 the sample in the documentation 的轻微改编,您可以执行类似的操作这个:
create function ReadImportedFile(file_name varchar)
returns varchar
language java
imports = ('@my_stage/my_path/my_config_file_1.txt',
'@my_stage/my_path/my_config_file_2.txt')
handler = 'my_package.TestReadRelativeFile.readFile' as $$
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
class TestReadRelativeFile {
// Reads the text file with the specified name, which must be part of the
// IMPORTS list.
public String readFile(String fileName) throws IOException {
StringBuilder contentBuilder = new StringBuilder();
String importDirectory = System.getProperty("com.snowflake.import_directory");
String fPath = importDirectory + fileName;
Stream<String> stream = Files.lines(Paths.get(fPath), StandardCharsets.UTF_8);
stream.forEach(s -> contentBuilder.append(s).append("\n"));
return contentBuilder.toString();
}
}
$$;
-- This reads the contents of my_config_file_1.txt, which was named in the
-- IMPORTS list in the function declaration.
select ReadImportedFile('my_config_file_1.txt');
如果您在使用此模式读取文件时遇到问题,最好在您的问题中说明问题所在。
我创建了一个 java UDF 来从我的外部阶段读取 word 文档。我在导入所有外部阶段文件并在我的 java UDF 中使用它们时使用了 imports 子句。它看起来不适合我。 还有其他方法可以动态地从舞台上读取文件吗?
Snowflake 支持使用标准 SQL 查询位于内部(即 Snowflake)阶段或命名为外部(Amazon S3、Google 云存储或 Microsoft Azure)阶段的数据文件。这对于 inspecting/viewing 暂存文件的内容很有用,尤其是在加载数据之前或卸载数据之后
https://docs.snowflake.com/en/user-guide/querying-stage.html
尚不支持使用 Java UDF 从阶段动态读取文件,如 Snowflake 峰会所示。作为执行的一部分,您可以访问 IMPORTS
列表中的任何文件,但是,这些文件通常应该用于读取配置文件、模型等。使用 the sample in the documentation 的轻微改编,您可以执行类似的操作这个:
create function ReadImportedFile(file_name varchar)
returns varchar
language java
imports = ('@my_stage/my_path/my_config_file_1.txt',
'@my_stage/my_path/my_config_file_2.txt')
handler = 'my_package.TestReadRelativeFile.readFile' as $$
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
class TestReadRelativeFile {
// Reads the text file with the specified name, which must be part of the
// IMPORTS list.
public String readFile(String fileName) throws IOException {
StringBuilder contentBuilder = new StringBuilder();
String importDirectory = System.getProperty("com.snowflake.import_directory");
String fPath = importDirectory + fileName;
Stream<String> stream = Files.lines(Paths.get(fPath), StandardCharsets.UTF_8);
stream.forEach(s -> contentBuilder.append(s).append("\n"));
return contentBuilder.toString();
}
}
$$;
-- This reads the contents of my_config_file_1.txt, which was named in the
-- IMPORTS list in the function declaration.
select ReadImportedFile('my_config_file_1.txt');
如果您在使用此模式读取文件时遇到问题,最好在您的问题中说明问题所在。