无法通过AWS lambda使用JAVA 8连接Snowflake。

问题描述 投票:0回答:1

试图使用Java 8从AWS lambda连接Snowflake。

  Class.forName("net.snowflake.client.jdbc.SnowflakeDriver"); // v3.9.1 as maven dependency

            final Properties snowflakProperties = new Properties();
            snowflakProperties.put("user", "username");
            snowflakProperties.put("password", "password");
            snowflakProperties.put("account", "account"); // accountname.regionname
            snowflakProperties.put("db", "dbname");
            snowflakProperties.put("schema", "schemaname");
            snowflakProperties.put("role", "rolename");

            DriverManager.getConnection("jdbc:snowflake://accountname.snowflakecomputing.com", 
                                                       snowflakProperties);

不幸的是,这是不能够连接和lambda去的时间.下面的试验已经采取了解决。

  1. 在AWS lambda中设置环境变量,如 SF_TEMPORARY_CREDENTIAL_CACHE_DIR。SF_OCSP_RESPONSE_CACHE_DIR。临时工.也试过JAVA_TOOL_OPTIONS为-Duser.home=tmp。

  2. 尝试的JDBC版本:3.11.1, 3.9.1, 3.8.4。

  3. 尝试将缓存目录设为home,结果抛出一个异常,消息为 触摸缓存文件失败:homeocsp_response_cache.json。

还是没有得到合适的解决方案。AWS lambda不允许使用临时目录获取Snowflake连接吗?

因此,期待尽快得到解决.

谢谢你。

java amazon-web-services jdbc aws-lambda snowflake-cloud-data-platform
1个回答
2
投票
I tested the Lambda fn with java .Change the values accordingly.
You can try this one below:

-----------------------------------------------------------------------------

package com.snowflake.LambdaTest;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class LambdaRequestHandler implements RequestHandler<Object, String> {
    public String handleRequest(Object input, Context context) {
        String data= input != null ? input.toString() : "{}";
        context.getLogger().log("Input: \n" + data);
        context.getLogger().log("Connecting to Snowflake DB ......\n");

        String connectionUrl = "jdbc:snowflake://<URL>/?account=<AccountName>&warehouse=<WareHouseName>&role=<RoleName>&db=<DBName>&schema=<SchemaName>&tracing=ALL";

        String user = "<UserName>";
        String passwd = "<Password>";

        Properties _connectionProperties = new Properties();
        _connectionProperties.put("user", user);
        _connectionProperties.put("password", passwd);

        try {
            Class.forName("net.snowflake.client.jdbc.SnowflakeDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        context.getLogger().log("Getting Conn...\n");
        Connection testConnection = null;

        try {
            testConnection = DriverManager.getConnection(connectionUrl, _connectionProperties);

            testConnection.setAutoCommit(false);
            Statement stat = testConnection.createStatement();
            ResultSet rs = stat.executeQuery("select * from <TableName>");
            while (rs.next()) {
                System.out.println(rs.getString(1));
            }

        } catch (SQLException e) {

            e.printStackTrace();
        }
        try {
            testConnection.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return "Success";

    }

}
----------------------------------------------------------------------
© www.soinside.com 2019 - 2024. All rights reserved.