From 5acc167944a7a6f05591b648b7365e0c80d0bed7 Mon Sep 17 00:00:00 2001 From: Hyunsik Choi Date: Thu, 13 Aug 2015 22:01:08 +0900 Subject: [PATCH 1/2] TAJO-1757: Add examples for TajoClient v2. --- 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 +- .../apache/tajo/client/v2/QueryFuture.java | 4 +- .../org/apache/tajo/client/v2/TajoClient.java | 12 +- .../apache/tajo/exception/ErrorMessages.java | 15 +- .../tajo/client/v2/TestTajoClientV2.java | 4 +- tajo-dist/pom.xml | 1 + tajo-project/pom.xml | 5 + 11 files changed, 388 insertions(+), 10 deletions(-) create mode 100644 tajo-client-example/pom.xml create mode 100644 tajo-client-example/src/main/java/org/apache/tajo/client/v2/example/TajoClientAsyncExample.java create mode 100644 tajo-client-example/src/main/java/org/apache/tajo/client/v2/example/TajoClientExample.java diff --git a/pom.xml b/pom.xml index 296830ab21..42746b6630 100644 --- a/pom.xml +++ b/pom.xml @@ -87,6 +87,7 @@ tajo-rpc tajo-catalog tajo-client + tajo-client-example tajo-jdbc tajo-storage tajo-pullserver diff --git a/tajo-client-example/pom.xml b/tajo-client-example/pom.xml new file mode 100644 index 0000000000..bfdac1500c --- /dev/null +++ b/tajo-client-example/pom.xml @@ -0,0 +1,217 @@ + + + + + 4.0.0 + + tajo-project + org.apache.tajo + 0.11.0-SNAPSHOT + ../tajo-project + + tajo-client-example + jar + Tajo Client Example + + UTF-8 + UTF-8 + 3.0.1 + + + + + repository.jboss.org + https://repository.jboss.org/nexus/content/repositories/releases/ + + + false + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + org.apache.rat + apache-rat-plugin + + + verify + + check + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + TRUE + + -Xms512m -Xmx1024m -Dfile.encoding=UTF-8 + + + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-dependencies + package + + copy-dependencies + + + runtime + ${project.build.directory}/lib + false + false + true + + + + + + org.apache.maven.plugins + maven-pmd-plugin + 2.7.1 + + + + + + + + org.apache.tajo + tajo-client + + + + + + docs + + false + + + + + org.apache.maven.plugins + maven-javadoc-plugin + + + + module-javadocs + package + + jar + + + ${project.build.directory} + + + + + + + + + dist + + false + + tar|rpm|deb + + + + + + org.apache.maven.plugins + maven-antrun-plugin + + + dist + package + + run + + + + + 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 + + + + + + + + + + + + + + + + + + org.apache.maven.plugins + maven-project-info-reports-plugin + 2.4 + + false + + + + org.apache.maven.plugins + maven-surefire-report-plugin + + + + + 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 0000000000..e2acbc5570 --- /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()); + } +} 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 0000000000..8a5222e2bb --- /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()); + } +} 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 697c1ac068..12f8812cb8 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 long finishTime() { } @Override - public void release() { + public void close() { queryClient.closeQuery(id); } @@ -332,7 +332,7 @@ public long finishTime() { } @Override - public void release() { + public void close() { queryClient.closeQuery(queryId); } 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 f1604cd53f..d916e6db8c 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 @@ /** * */ -public interface QueryFuture extends Future { +public interface QueryFuture extends Future, AutoCloseable { /** * Get a query id * @@ -122,7 +122,7 @@ public interface QueryFuture extends Future { /** * 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. 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 f9401cb9c3..2b4a15042e 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.util.Map; public class TajoClient implements Closeable { + private static Log LOG = LogFactory.getLog(TajoClient.class); + /** * default client port number */ @@ -130,8 +134,12 @@ public QueryFuture executeQueryAsync(String sql) throws TajoException { return delegate.executeSQLAsync(sql); } - public void close() throws IOException { - delegate.close(); + public void close() { + try { + delegate.close(); + } catch (IOException e) { + LOG.warn(e); + } } /** 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 ad5776cbea..e617efa8af 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 @@ 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 static String getInternalErrorMessage(Throwable t) { } + 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 static String getMessage(ResultCode code, String...args) { } } else { - throw new TajoInternalError("Argument mismatch: code=" + code.name() + ", args=" + StringUtils.join(args)); + throw new TajoInternalError("Argument mismatch: code=" + code.name() + ", args=" + concat(args)); } } } diff --git a/tajo-core/src/test/java/org/apache/tajo/client/v2/TestTajoClientV2.java b/tajo-core/src/test/java/org/apache/tajo/client/v2/TestTajoClientV2.java index e1fca63556..6ba4854de6 100644 --- a/tajo-core/src/test/java/org/apache/tajo/client/v2/TestTajoClientV2.java +++ b/tajo-core/src/test/java/org/apache/tajo/client/v2/TestTajoClientV2.java @@ -219,7 +219,7 @@ public void testQueryFutureKill() throws Throwable { } catch (ExecutionException e) { throw e.getCause(); } finally { - future.release(); + future.close(); } } @@ -268,7 +268,7 @@ public void testFailedExecuteQueryAsync() throws Throwable { } catch (ExecutionException e) { throw e.getCause(); } finally { - future.release(); + future.close(); } } } diff --git a/tajo-dist/pom.xml b/tajo-dist/pom.xml index 3b8f9470d3..ed71c31f59 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}/* . diff --git a/tajo-project/pom.xml b/tajo-project/pom.xml index 15a86ce906..8dd31cc367 100644 --- a/tajo-project/pom.xml +++ b/tajo-project/pom.xml @@ -790,6 +790,11 @@ tajo-client ${tajo.version} + + org.apache.tajo + tajo-client-example + ${tajo.version} + org.apache.tajo tajo-cli From 0ff48d0c7164f96674c1613624f31911994f0257 Mon Sep 17 00:00:00 2001 From: Hyunsik Choi Date: Wed, 19 Aug 2015 11:08:13 +0900 Subject: [PATCH 2/2] Fix version in pom.xml. --- tajo-client-example/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tajo-client-example/pom.xml b/tajo-client-example/pom.xml index bfdac1500c..e81311df6a 100644 --- a/tajo-client-example/pom.xml +++ b/tajo-client-example/pom.xml @@ -22,7 +22,7 @@ tajo-project org.apache.tajo - 0.11.0-SNAPSHOT + 0.12.0-SNAPSHOT ../tajo-project tajo-client-example