Repository: tajo Updated Branches: refs/heads/master 1e149127d -> f7a3fe0d6
TAJO-1915: Add a TajoClient example for creating and getting tables. Closes #811 Project: http://git-wip-us.apache.org/repos/asf/tajo/repo Commit: http://git-wip-us.apache.org/repos/asf/tajo/commit/f7a3fe0d Tree: http://git-wip-us.apache.org/repos/asf/tajo/tree/f7a3fe0d Diff: http://git-wip-us.apache.org/repos/asf/tajo/diff/f7a3fe0d Branch: refs/heads/master Commit: f7a3fe0d642e6016a99b0731b1d8d56ef81eb1ec Parents: 1e14912 Author: Hyunsik Choi <[email protected]> Authored: Tue Oct 6 21:52:14 2015 -0700 Committer: Hyunsik Choi <[email protected]> Committed: Tue Oct 6 21:54:33 2015 -0700 ---------------------------------------------------------------------- CHANGES | 3 + .../tajo/client/v1/example/TableExample.java | 86 ++++++++++++++++++++ .../v2/example/TajoClientAsyncExample.java | 3 +- .../client/v2/example/TajoClientExample.java | 3 +- 4 files changed, 93 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tajo/blob/f7a3fe0d/CHANGES ---------------------------------------------------------------------- diff --git a/CHANGES b/CHANGES index 27fe2ce..63d9279 100644 --- a/CHANGES +++ b/CHANGES @@ -28,6 +28,9 @@ Release 0.12.0 - unreleased TASKS + TAJO-1915: Add a TajoClient example for creating and getting tables. + (hyunsik) + TAJO-1893: Remove unused import. (Contributed by Dongkyu Hwangbo, Committed by jihoon) http://git-wip-us.apache.org/repos/asf/tajo/blob/f7a3fe0d/tajo-client-example/src/main/java/org/apache/tajo/client/v1/example/TableExample.java ---------------------------------------------------------------------- diff --git a/tajo-client-example/src/main/java/org/apache/tajo/client/v1/example/TableExample.java b/tajo-client-example/src/main/java/org/apache/tajo/client/v1/example/TableExample.java new file mode 100644 index 0000000..e6a7368 --- /dev/null +++ b/tajo-client-example/src/main/java/org/apache/tajo/client/v1/example/TableExample.java @@ -0,0 +1,86 @@ +/* + * 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.v1.example; + +import org.apache.tajo.catalog.Column; +import org.apache.tajo.catalog.TableDesc; +import org.apache.tajo.client.TajoClient; +import org.apache.tajo.client.TajoClientImpl; +import org.apache.tajo.exception.DuplicateDatabaseException; +import org.apache.tajo.exception.TajoException; +import org.apache.tajo.exception.UndefinedDatabaseException; +import org.apache.tajo.exception.UndefinedTableException; +import org.apache.tajo.util.KeyValueSet; + +import java.net.InetSocketAddress; +import java.sql.SQLException; + +public class TableExample { + public static void run(String hostname, int port) throws SQLException, TajoException{ + TajoClient client = new TajoClientImpl(new InetSocketAddress(hostname, port), "default", new KeyValueSet()); + + try { + client.createDatabase("example"); + } catch (DuplicateDatabaseException e) { + throw new RuntimeException("database 'example' already exists"); + } + + if (!client.getAllDatabaseNames().contains("example")) { + throw new RuntimeException("Database creation was failed"); + } + + // select base database + try { + client.selectDatabase("example"); + } catch (UndefinedDatabaseException e) { + throw new RuntimeException("No such a database"); + } + + // It will create a table 'table' in 'example' database. + client.updateQuery("CREATE TABLE employee (name TEXT, age int, dept TEXT)"); + + TableDesc tableDesc; + try { + tableDesc = client.getTableDesc("example.employee"); + } catch (UndefinedTableException t) { + throw new RuntimeException("No such a table"); + } + + System.out.println("Table name: " + tableDesc.getName()); + System.out.println("Table uri: " + tableDesc.getUri().toASCIIString()); + System.out.println("Table schema: "); + + // for each column, print its name and data type + for (Column c: tableDesc.getSchema().getAllColumns()) { + System.out.println(String.format(" name: %s, type: %s", c.getSimpleName(), c.getDataType().getType().name())); + } + + client.close(); + } + + public static void main(String[] args) throws TajoException, SQLException { + if (args.length < 2) { + System.err.println(String.format("usage: java -cp [classpath] %s [hostname] [port]", + TableExample.class.getCanonicalName())); + System.exit(-1); + } + + run(args[0], Integer.parseInt(args[1])); + } +} http://git-wip-us.apache.org/repos/asf/tajo/blob/f7a3fe0d/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 index e2acbc5..f6313af 100644 --- 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 @@ -56,7 +56,8 @@ public class TajoClientAsyncExample { public static void main(String[] args) throws ClientUnableToConnectException { if (args.length < 3) { - System.err.println("usage: java -cp [classpath] TajoClientAsyncExample [hostname] [port] sql"); + System.err.println(String.format("usage: java -cp [classpath] %s [hostname] [port] sql", + TajoClientAsyncExample.class.getCanonicalName())); System.exit(-1); } http://git-wip-us.apache.org/repos/asf/tajo/blob/f7a3fe0d/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 index 8a5222e..8c5f28a 100644 --- 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 @@ -51,7 +51,8 @@ public class TajoClientExample { public static void main(String[] args) throws ClientUnableToConnectException { if (args.length < 3) { - System.err.println("usage: java -cp [classpath] TajoClientExample [hostname] [port] sql"); + System.err.println(String.format("usage: java -cp [classpath] %s [hostname] [port] sql", + TajoClientExample.class.getCanonicalName())); System.exit(-1); }
