英文:
Unable to create Snowflake Java function
问题
根据Snowflake产品经理在YouTube上的演示,我创建了一个基于Java的函数,编译为JAR文件,上传到了Stage,并尝试创建函数,但一直出现SQL编译错误。
看起来Snowflake现在支持Java函数(而不仅仅是JavaScript和SQL函数),但我仍然无法使其正常工作。
我使用的是业务关键的Snowflake版本。
--函数
create or replace function calc(int x, int y, char op)
returns int
language java
import @stage_java/SnowflakeFunctions-1.0-SNAPSHOT.jar
classpath='Calculate';
---错误信息
[42000][1003] SQL编译错误:在位置4的第7行出现语法错误,意外的'@stage_java/SnowflakeFunctions-1.0-SNAPSHOT.jar'。
--Java代码
public class Calculate {
public static int compute(int x, int y, char op) {
int result = 0;
switch (op) {
case '+':
result = x + y;
break;
case '-':
result = x - y;
break;
case '/':
result = x / y;
break;
case '*':
result = x * y;
break;
}
return result;
}
}
英文:
Based on Snowflake product manager demo here at youtube -- https://www.youtube.com/watch?v=F5DWBdhFQF4
I created a java based function, compiled to jar, uploaded to stage and attempted to create function but it keep giving sql compilation error.
It looks like Snowflake now support Java function (beyond java script and SQL functions) but I unable to make it to work still.
I am on business critical snowflake edition.
--Function
create or replace function calc(int x, int y, char op)
returns int
language java
import @stage_java/SnowflakeFunctions-1.0-SNAPSHOT.jar
classpath='Calculate';
---Error
[42000][1003] SQL compilation error: syntax error line 4 at position 7 unexpected '@stage_java/SnowflakeFunctions-1.0-SNAPSHOT.jar'.
--Java Code
public class Calculate {
public static int compute(int x, int y, char op) {
int result = 0;
switch (op) {
case '+':
result = x + y;
break;
case '-':
result = x - y;
break;
case '/':
result = x / y;
break;
case '*':
result = x * y;
break;
}
return result;
}
}
答案1
得分: 1
以下是翻译好的部分:
"Following up, Java UDFs are in public preview in Snowflake. To build an example using your provided code:
create or replace function calc(x int, y int, op varchar)
returns int
language java
handler = 'Calculate.compute'
as $$
public class Calculate {
public static int compute(int x, int y, String op) {
int result = 0;
switch (op.charAt(0)) {
case '+':
result = x + y;
break;
case '-':
result = x - y;
break;
case '/':
result = x / y;
break;
case '*':
result = x * y;
break;
}
return result;
}
}
$$;
select calc(3, 5, '*');
+-----------------+
| CALC(3, 5, '*') |
|-----------------|
| 15 |
+-----------------+"
<details>
<summary>英文:</summary>
Following up, [Java UDFs are in public preview in Snowflake](https://docs.snowflake.com/en/developer-guide/udf/java/udf-java.html). To build an example using your provided code:
```sql
create or replace function calc(x int, y int, op varchar)
returns int
language java
handler = 'Calculate.compute'
as $$
public class Calculate {
public static int compute(int x, int y, String op) {
int result = 0;
switch (op.charAt(0)) {
case '+':
result = x + y;
break;
case '-':
result = x - y;
break;
case '/':
result = x / y;
break;
case '*':
result = x * y;
break;
}
return result;
}
}
$$;
select calc(3, 5, '*');
+-----------------+
| CALC(3, 5, '*') |
|-----------------|
| 15 |
+-----------------+
Note that I've used inline code for this example, meaning the JAR for the code is built when the query executes. Alternatively, you can compile the JAR yourself and then copy it to a stage using a PUT
command; see the documentation for the distinction between in-line and pre-compiled Java UDFs.
答案2
得分: 0
这是Java UDF功能的演示,目前尚未公开提供。
英文:
This is demo of the Java UDF feature which is yet to be available publicly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论