从Snowflake java UDF读取外部阶段文件 [英] Read external stage file from snowflake java UDF

查看:17
本文介绍了从Snowflake java UDF读取外部阶段文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个java UDF来从我的外部舞台读取Word文档。我使用IMPORTS子句导入所有外部阶段文件,并在java UDF中使用它们。它看起来不适合我。 是否有其他方法可以从舞台动态读取文件?

推荐答案

目前尚不支持使用JavaUDF从阶段动态读取文件,如雪花峰会所示。作为执行的一部分,您可以访问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("
"));
        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');

如果您在使用此模式读取文件时遇到问题,最好作为您的问题的一部分来澄清哪里出了问题。

这篇关于从Snowflake java UDF读取外部阶段文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆