Repository: tajo Updated Branches: refs/heads/master 57be230e9 -> b4098353d
TAJO-1757: Add examples for TajoClient v2. Closes #689 Project: http://git-wip-us.apache.org/repos/asf/tajo/repo Commit: http://git-wip-us.apache.org/repos/asf/tajo/commit/b4098353 Tree: http://git-wip-us.apache.org/repos/asf/tajo/tree/b4098353 Diff: http://git-wip-us.apache.org/repos/asf/tajo/diff/b4098353 Branch: refs/heads/master Commit: b4098353d142c84d5fa881b25b8334650212ffed Parents: 57be230 Author: Hyunsik Choi <[email protected]> Authored: Wed Aug 19 13:03:01 2015 +0900 Committer: Hyunsik Choi <[email protected]> Committed: Wed Aug 19 13:03:01 2015 +0900 ---------------------------------------------------------------------- CHANGES | 2 + pom.xml | 1 + tajo-client-example/pom.xml | 217 +++++++++++++++++++ .../v2/example/TajoClientAsyncExample.java | 70 ++++++ .../client/v2/example/TajoClientExample.java | 65 ++++++ .../tajo/client/v2/LegacyClientDelegate.java | 4 +- .../org/apache/tajo/client/v2/QueryFuture.java | 4 +- .../org/apache/tajo/client/v2/TajoClient.java | 12 +- .../apache/tajo/exception/ErrorMessages.java | 15 +- .../apache/tajo/client/v2/TestTajoClientV2.java | 4 +- tajo-dist/pom.xml | 1 + tajo-project/pom.xml | 5 + 12 files changed, 390 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tajo/blob/b4098353/CHANGES ---------------------------------------------------------------------- diff --git a/CHANGES b/CHANGES index f2d4f64..bbd3386 100644 --- a/CHANGES +++ b/CHANGES @@ -34,6 +34,8 @@ Release 0.11.0 - unreleased IMPROVEMENT + TAJO-1757: Add examples for TajoClient v2. (hyunsik) + TAJO-1761: Separate an integration unit test kit into an independent module. (hyunsik) http://git-wip-us.apache.org/repos/asf/tajo/blob/b4098353/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index 605160a..ceba124 100644 --- a/pom.xml +++ b/pom.xml @@ -87,6 +87,7 @@ <module>tajo-rpc</module> <module>tajo-catalog</module> <module>tajo-client</module> + <module>tajo-client-example</module> <module>tajo-jdbc</module> <module>tajo-storage</module> <module>tajo-pullserver</module> http://git-wip-us.apache.org/repos/asf/tajo/blob/b4098353/tajo-client-example/pom.xml ---------------------------------------------------------------------- diff --git a/tajo-client-example/pom.xml b/tajo-client-example/pom.xml new file mode 100644 index 0000000..e81311d --- /dev/null +++ b/tajo-client-example/pom.xml @@ -0,0 +1,217 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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> + <artifactId>tajo-project</artifactId> + <groupId>org.apache.tajo</groupId> + <version>0.12.0-SNAPSHOT</version> + <relativePath>../tajo-project</relativePath> + </parent> + <artifactId>tajo-client-example</artifactId> + <packaging>jar</packaging> + <name>Tajo Client Example</name> + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> + <metrics.version>3.0.1</metrics.version> + </properties> + + <repositories> + <repository> + <id>repository.jboss.org</id> + <url>https://repository.jboss.org/nexus/content/repositories/releases/ + </url> + <snapshots> + <enabled>false</enabled> + </snapshots> + </repository> + </repositories> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + </plugin> + <plugin> + <groupId>org.apache.rat</groupId> + <artifactId>apache-rat-plugin</artifactId> + <executions> + <execution> + <phase>verify</phase> + <goals> + <goal>check</goal> + </goals> + </execution> + </executions> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <systemProperties> + <tajo.test>TRUE</tajo.test> + </systemProperties> + <argLine>-Xms512m -Xmx1024m -Dfile.encoding=UTF-8</argLine> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-dependency-plugin</artifactId> + <executions> + <execution> + <id>copy-dependencies</id> + <phase>package</phase> + <goals> + <goal>copy-dependencies</goal> + </goals> + <configuration> + <includeScope>runtime</includeScope> + <outputDirectory>${project.build.directory}/lib</outputDirectory> + <overWriteReleases>false</overWriteReleases> + <overWriteSnapshots>false</overWriteSnapshots> + <overWriteIfNewer>true</overWriteIfNewer> + </configuration> + </execution> + </executions> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-pmd-plugin</artifactId> + <version>2.7.1</version> + </plugin> + </plugins> + </build> + + + <dependencies> + <dependency> + <groupId>org.apache.tajo</groupId> + <artifactId>tajo-client</artifactId> + </dependency> + </dependencies> + + <profiles> + <profile> + <id>docs</id> + <activation> + <activeByDefault>false</activeByDefault> + </activation> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-javadoc-plugin</artifactId> + <executions> + <execution> + <!-- build javadoc jars per jar for publishing to maven --> + <id>module-javadocs</id> + <phase>package</phase> + <goals> + <goal>jar</goal> + </goals> + <configuration> + <destDir>${project.build.directory}</destDir> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> + </profile> + <profile> + <id>dist</id> + <activation> + <activeByDefault>false</activeByDefault> + <property> + <name>tar|rpm|deb</name> + </property> + </activation> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-antrun-plugin</artifactId> + <executions> + <execution> + <id>dist</id> + <phase>package</phase> + <goals> + <goal>run</goal> + </goals> + <configuration> + <target> + <echo file="${project.build.directory}/dist-layout-stitching.sh"> + run() { + echo "\$ ${@}" + "${@}" + res=$? + if [ $res != 0 ]; then + echo + echo "Failed!" + echo + exit $res + fi + } + + ROOT=`cd ${basedir}/..;pwd` + echo + echo "Current directory `pwd`" + echo + run rm -rf ${project.artifactId}-${project.version} + run mkdir ${project.artifactId}-${project.version} + run cd ${project.artifactId}-${project.version} + run cp -r ${basedir}/target/${project.artifactId}-${project.version}*.jar . + echo + echo "Tajo Client dist layout available at: ${project.build.directory}/${project.artifactId}-${project.version}" + echo + </echo> + <exec executable="sh" dir="${project.build.directory}" failonerror="true"> + <arg line="./dist-layout-stitching.sh" /> + </exec> + </target> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> + </profile> + </profiles> + + <reporting> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-project-info-reports-plugin</artifactId> + <version>2.4</version> + <configuration> + <dependencyLocationsEnabled>false</dependencyLocationsEnabled> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-report-plugin</artifactId> + </plugin> + </plugins> + </reporting> +</project> + http://git-wip-us.apache.org/repos/asf/tajo/blob/b4098353/tajo-client-example/src/main/java/org/apache/tajo/client/v2/example/TajoClientAsyncExample.java ---------------------------------------------------------------------- diff --git a/tajo-client-example/src/main/java/org/apache/tajo/client/v2/example/TajoClientAsyncExample.java b/tajo-client-example/src/main/java/org/apache/tajo/client/v2/example/TajoClientAsyncExample.java new file mode 100644 index 0000000..e2acbc5 --- /dev/null +++ b/tajo-client-example/src/main/java/org/apache/tajo/client/v2/example/TajoClientAsyncExample.java @@ -0,0 +1,70 @@ +/* + * 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.tajo.client.v2.example; + +import org.apache.tajo.client.ResultSetUtil; +import org.apache.tajo.client.v2.QueryFuture; +import org.apache.tajo.client.v2.TajoClient; +import org.apache.tajo.client.v2.exception.ClientUnableToConnectException; +import org.apache.tajo.exception.TajoException; + +import java.util.concurrent.ExecutionException; + +public class TajoClientAsyncExample { + + public static void run(String hostname, int port, String sql) throws ClientUnableToConnectException { + + try (TajoClient client = new TajoClient(hostname, port)) { + + try (QueryFuture future = client.executeQueryAsync(sql)) { + + while (!future.isDone()) { // isDone will be true if query state becomes success, failed, or killed. + System.out.println("progress: " + future.progress()); + } + + if (future.isSuccessful()) { + System.out.println(ResultSetUtil.prettyFormat(future.get())); + } + + } catch (TajoException e) { + // executeQueryAsync() directly throws a TajoException instance if a query syntax is wrong. + e.printStackTrace(); + } catch (ExecutionException e) { + // e.getCause() contains an TajoException caused by a running query. + System.err.println(e.getCause().getMessage()); + } catch (Throwable t) { + System.err.println(t.getMessage()); + } + } + } + + public static void main(String[] args) throws ClientUnableToConnectException { + if (args.length < 3) { + System.err.println("usage: java -cp [classpath] TajoClientAsyncExample [hostname] [port] sql"); + System.exit(-1); + } + + StringBuilder sqlBuilder = new StringBuilder(); + for (int i = 2; i < args.length; i++) { + sqlBuilder.append(args[i]).append(" "); + } + + run(args[0], Integer.parseInt(args[1]), sqlBuilder.toString()); + } +} http://git-wip-us.apache.org/repos/asf/tajo/blob/b4098353/tajo-client-example/src/main/java/org/apache/tajo/client/v2/example/TajoClientExample.java ---------------------------------------------------------------------- diff --git a/tajo-client-example/src/main/java/org/apache/tajo/client/v2/example/TajoClientExample.java b/tajo-client-example/src/main/java/org/apache/tajo/client/v2/example/TajoClientExample.java new file mode 100644 index 0000000..8a5222e --- /dev/null +++ b/tajo-client-example/src/main/java/org/apache/tajo/client/v2/example/TajoClientExample.java @@ -0,0 +1,65 @@ +/* + * 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.tajo.client.v2.example; + +import org.apache.tajo.client.ResultSetUtil; +import org.apache.tajo.client.v2.TajoClient; +import org.apache.tajo.client.v2.exception.ClientUnableToConnectException; +import org.apache.tajo.exception.QueryFailedException; +import org.apache.tajo.exception.QueryKilledException; +import org.apache.tajo.exception.TajoException; + +import java.sql.ResultSet; +import java.sql.SQLException; + +public class TajoClientExample { + + public static void run(String hostname, int port, String sql) throws ClientUnableToConnectException { + + try (TajoClient client = new TajoClient(hostname, port)) { + + try { + ResultSet result = client.executeQuery(sql); + System.out.println(ResultSetUtil.prettyFormat(result)); + + } catch (QueryFailedException e) { + System.err.println("query is failed."); + } catch (QueryKilledException e) { + System.err.println("query is killed."); + } catch (SQLException | TajoException e) { + e.printStackTrace(); + } + + } + } + + public static void main(String[] args) throws ClientUnableToConnectException { + if (args.length < 3) { + System.err.println("usage: java -cp [classpath] TajoClientExample [hostname] [port] sql"); + System.exit(-1); + } + + StringBuilder sqlBuilder = new StringBuilder(); + for (int i = 2; i < args.length; i++) { + sqlBuilder.append(args[i]).append(" "); + } + + run(args[0], Integer.parseInt(args[1]), sqlBuilder.toString()); + } +} http://git-wip-us.apache.org/repos/asf/tajo/blob/b4098353/tajo-client/src/main/java/org/apache/tajo/client/v2/LegacyClientDelegate.java ---------------------------------------------------------------------- diff --git a/tajo-client/src/main/java/org/apache/tajo/client/v2/LegacyClientDelegate.java b/tajo-client/src/main/java/org/apache/tajo/client/v2/LegacyClientDelegate.java index 697c1ac..12f8812 100644 --- a/tajo-client/src/main/java/org/apache/tajo/client/v2/LegacyClientDelegate.java +++ b/tajo-client/src/main/java/org/apache/tajo/client/v2/LegacyClientDelegate.java @@ -192,7 +192,7 @@ public class LegacyClientDelegate extends SessionConnection implements ClientDel } @Override - public void release() { + public void close() { queryClient.closeQuery(id); } @@ -332,7 +332,7 @@ public class LegacyClientDelegate extends SessionConnection implements ClientDel } @Override - public void release() { + public void close() { queryClient.closeQuery(queryId); } http://git-wip-us.apache.org/repos/asf/tajo/blob/b4098353/tajo-client/src/main/java/org/apache/tajo/client/v2/QueryFuture.java ---------------------------------------------------------------------- diff --git a/tajo-client/src/main/java/org/apache/tajo/client/v2/QueryFuture.java b/tajo-client/src/main/java/org/apache/tajo/client/v2/QueryFuture.java index f1604cd..d916e6d 100644 --- a/tajo-client/src/main/java/org/apache/tajo/client/v2/QueryFuture.java +++ b/tajo-client/src/main/java/org/apache/tajo/client/v2/QueryFuture.java @@ -26,7 +26,7 @@ import java.util.concurrent.Future; /** * */ -public interface QueryFuture extends Future<ResultSet> { +public interface QueryFuture extends Future<ResultSet>, AutoCloseable { /** * Get a query id * @@ -122,7 +122,7 @@ public interface QueryFuture extends Future<ResultSet> { /** * Release a query future. It will be automatically released after the session invalidation. */ - void release(); + void close(); /** * Add a listener which will be executed after this query is completed, error, failed or killed. http://git-wip-us.apache.org/repos/asf/tajo/blob/b4098353/tajo-client/src/main/java/org/apache/tajo/client/v2/TajoClient.java ---------------------------------------------------------------------- diff --git a/tajo-client/src/main/java/org/apache/tajo/client/v2/TajoClient.java b/tajo-client/src/main/java/org/apache/tajo/client/v2/TajoClient.java index f9401cb..2b4a150 100644 --- a/tajo-client/src/main/java/org/apache/tajo/client/v2/TajoClient.java +++ b/tajo-client/src/main/java/org/apache/tajo/client/v2/TajoClient.java @@ -18,6 +18,8 @@ package org.apache.tajo.client.v2; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.apache.tajo.exception.QueryFailedException; import org.apache.tajo.exception.UndefinedDatabaseException; import org.apache.tajo.client.v2.exception.ClientUnableToConnectException; @@ -29,6 +31,8 @@ import java.sql.ResultSet; import java.util.Map; public class TajoClient implements Closeable { + private static Log LOG = LogFactory.getLog(TajoClient.class); + /** * default client port number */ @@ -130,8 +134,12 @@ public class TajoClient implements Closeable { return delegate.executeSQLAsync(sql); } - public void close() throws IOException { - delegate.close(); + public void close() { + try { + delegate.close(); + } catch (IOException e) { + LOG.warn(e); + } } /** http://git-wip-us.apache.org/repos/asf/tajo/blob/b4098353/tajo-common/src/main/java/org/apache/tajo/exception/ErrorMessages.java ---------------------------------------------------------------------- diff --git a/tajo-common/src/main/java/org/apache/tajo/exception/ErrorMessages.java b/tajo-common/src/main/java/org/apache/tajo/exception/ErrorMessages.java index ad5776c..e617efa 100644 --- a/tajo-common/src/main/java/org/apache/tajo/exception/ErrorMessages.java +++ b/tajo-common/src/main/java/org/apache/tajo/exception/ErrorMessages.java @@ -21,7 +21,6 @@ package org.apache.tajo.exception; import com.google.common.collect.Maps; import org.apache.tajo.error.Errors.ResultCode; import org.apache.tajo.util.Pair; -import org.apache.tajo.util.StringUtils; import java.util.Map; @@ -135,6 +134,18 @@ public class ErrorMessages { } + public static String concat(String[] args) { + StringBuilder sb = new StringBuilder(); + boolean first = true; + for (String s : args) { + if (!first) { + sb.append(","); + } + sb.append(s); + } + return sb.toString(); + } + public static String getMessage(ResultCode code, String...args) { if (!MESSAGES.containsKey(code)) { throw new TajoInternalError("no error message for " + code); @@ -151,7 +162,7 @@ public class ErrorMessages { } } else { - throw new TajoInternalError("Argument mismatch: code=" + code.name() + ", args=" + StringUtils.join(args)); + throw new TajoInternalError("Argument mismatch: code=" + code.name() + ", args=" + concat(args)); } } } http://git-wip-us.apache.org/repos/asf/tajo/blob/b4098353/tajo-core-tests/src/test/java/org/apache/tajo/client/v2/TestTajoClientV2.java ---------------------------------------------------------------------- diff --git a/tajo-core-tests/src/test/java/org/apache/tajo/client/v2/TestTajoClientV2.java b/tajo-core-tests/src/test/java/org/apache/tajo/client/v2/TestTajoClientV2.java index e1fca63..6ba4854 100644 --- a/tajo-core-tests/src/test/java/org/apache/tajo/client/v2/TestTajoClientV2.java +++ b/tajo-core-tests/src/test/java/org/apache/tajo/client/v2/TestTajoClientV2.java @@ -219,7 +219,7 @@ public class TestTajoClientV2 extends QueryTestCaseBase { } catch (ExecutionException e) { throw e.getCause(); } finally { - future.release(); + future.close(); } } @@ -268,7 +268,7 @@ public class TestTajoClientV2 extends QueryTestCaseBase { } catch (ExecutionException e) { throw e.getCause(); } finally { - future.release(); + future.close(); } } } http://git-wip-us.apache.org/repos/asf/tajo/blob/b4098353/tajo-dist/pom.xml ---------------------------------------------------------------------- diff --git a/tajo-dist/pom.xml b/tajo-dist/pom.xml index 8592cba..b9d2343 100644 --- a/tajo-dist/pom.xml +++ b/tajo-dist/pom.xml @@ -138,6 +138,7 @@ run cp -r $ROOT/tajo-algebra/target/tajo-algebra-${project.version}/* . run cp -r $ROOT/tajo-plan/target/tajo-plan-${project.version}/* . run cp -r $ROOT/tajo-client/target/tajo-client-${project.version}/* . + run cp -r $ROOT/tajo-client-example/target/tajo-client-example-${project.version}/* . run cp -r $ROOT/tajo-cli/target/tajo-cli-${project.version}/* . run cp -r $ROOT/tajo-catalog/target/tajo-catalog-${project.version}/* . run cp -r $ROOT/tajo-storage/target/tajo-storage-${project.version}/* . http://git-wip-us.apache.org/repos/asf/tajo/blob/b4098353/tajo-project/pom.xml ---------------------------------------------------------------------- diff --git a/tajo-project/pom.xml b/tajo-project/pom.xml index 3303c2a..f486d7f 100644 --- a/tajo-project/pom.xml +++ b/tajo-project/pom.xml @@ -791,6 +791,11 @@ </dependency> <dependency> <groupId>org.apache.tajo</groupId> + <artifactId>tajo-client-example</artifactId> + <version>${tajo.version}</version> + </dependency> + <dependency> + <groupId>org.apache.tajo</groupId> <artifactId>tajo-cli</artifactId> <version>${tajo.version}</version> </dependency>
