Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test Snowflake functions #5

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -734,6 +734,22 @@
<module>spark-shaded</module>
<module>flink</module>
<module>flink-shaded</module>
<module>snowflake</module>
</modules>
</profile>
<profile>
<id>snowflake</id>
<activation>
<property>
<name>snowflake</name>
<value>true</value>
</property>
<activeByDefault>false</activeByDefault>
</activation>
<modules>
<module>common</module>
<module>snowflake</module>
<module>snowflake-tester</module>
</modules>
</profile>
</profiles>
12 changes: 12 additions & 0 deletions snowflake-tester/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/target/
/.settings/
/.classpath
/.project
/dependency-reduced-pom.xml
/doc/
/.idea/
*.iml
/latest/
snowsql_rt.log*
local_test.sh
tmp/
59 changes: 59 additions & 0 deletions snowflake-tester/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.sedona</groupId>
<artifactId>sedona-parent</artifactId>
<version>1.5.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>sedona-snowflake-tester</artifactId>

<name>${project.groupId}:${project.artifactId}</name>
<description>A cluster computing system for processing large-scale spatial data: Snowflake tester programs.</description>
<url>http://sedona.apache.org/</url>

<properties>
<maven.deploy.skip>${skip.deploy.common.modules}</maven.deploy.skip>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.sedona</groupId>
<artifactId>sedona-snowflake</artifactId>
<version>1.5.1-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.snowflake</groupId>
<artifactId>snowflake-jdbc</artifactId>
<version>3.13.30</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.sedona.snowflake.snowsql;

import net.snowflake.client.jdbc.SnowflakeConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Objects;
import java.util.Properties;

public class SnowClient {

private Properties props = null;

private Connection conn = null;

private String connUrl = null;

private static Logger logger = LoggerFactory.getLogger(SnowClient.class);

public SnowClient(Properties props, String connUrl) throws SQLException {
this.props = props;
this.connUrl = connUrl;
this.initConnection();
}

public void initConnection() throws SQLException {
if (conn == null || conn.isClosed()) {
System.out.println("connect to " + connUrl);
conn = DriverManager.getConnection(connUrl, props);
}
}

public void close() throws SQLException {
try {
if (conn != null && !conn.isClosed()) {
conn.close();
}
} finally {
conn = null;
}
}

public ResultSet executeQuery(String query) throws SQLException {
logger.info("execute query " + query);
Objects.requireNonNull(conn);
ResultSet res = conn.createStatement().executeQuery(query);
return res;
}

public void uploadFile(String path, String stage) {
File jarFile = new File(path);
String[] pList = path.split("/");
String fileName = pList[pList.length - 1];
try {
System.out.printf("upload file %s to stage %s%n", path, stage);
logger.info(String.format("upload file %s to stage %s", path, stage));
FileInputStream fileInputStream = new FileInputStream(jarFile);
conn.unwrap(SnowflakeConnection.class).uploadStream(stage, "", fileInputStream, fileName, false);
logger.info("upload finished");
} catch (FileNotFoundException | SQLException e) {
throw new RuntimeException(e);
}
}

public static SnowClient newFromEnv() throws SQLException {
Properties prop = new Properties();
// check auth method
String authMethod = System.getenv("SNOWFLAKE_AUTH_METHOD");
if (authMethod.equals("BASIC")) {
prop.put("user", System.getenv("SNOWFLAKE_USER"));
prop.put("password", System.getenv("SNOWFLAKE_PASSWORD"));
}
prop.put("db", System.getenv("SNOWFLAKE_DB"));
prop.put("schema", System.getenv("SNOWFLAKE_SCHEMA"));
prop.put("warehouse", System.getenv("SNOWFLAKE_WAREHOUSE"));
prop.put("role", System.getenv("SNOWFLAKE_ROLE"));
String accountName = System.getenv("SNOWFLAKE_ACCOUNT");
String connectionUrl = String.format(
"jdbc:snowflake://%s.snowflakecomputing.com/?query_tag='ApacheSedona'",
accountName
);
return new SnowClient(prop, connectionUrl);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.sedona.snowflake.snowsql;

import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.InitializationError;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;


public class SnowTestRunner extends BlockJUnit4ClassRunner {

private TestBase testObject;

public SnowTestRunner(Class<TestBase> testClass) throws InitializationError, InstantiationException, IllegalAccessException, InvocationTargetException, SQLException {
super(testClass);
testObject = (TestBase) this.getTestClass().getOnlyConstructor().newInstance();
testObject.init();
}

@Override
protected Object createTest() {
return testObject;
}

@Override
public void run(RunNotifier notifier) {
super.run(notifier);
System.out.println("Closing SnowClient");
testObject.tearDown();
}
}
Loading