http://git-wip-us.apache.org/repos/asf/tajo/blob/c59baa3a/tajo-client/src/main/java/org/apache/tajo/cli/tsql/commands/TajoHAAdminCommand.java ---------------------------------------------------------------------- diff --git a/tajo-client/src/main/java/org/apache/tajo/cli/tsql/commands/TajoHAAdminCommand.java b/tajo-client/src/main/java/org/apache/tajo/cli/tsql/commands/TajoHAAdminCommand.java new file mode 100644 index 0000000..9010ccb --- /dev/null +++ b/tajo-client/src/main/java/org/apache/tajo/cli/tsql/commands/TajoHAAdminCommand.java @@ -0,0 +1,58 @@ +/** + * 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.cli.tsql.commands; + +import org.apache.tajo.cli.tsql.TajoCli; +import org.apache.tajo.cli.tools.TajoHAAdmin; + +public class TajoHAAdminCommand extends TajoShellCommand { + private TajoHAAdmin haAdmin; + + public TajoHAAdminCommand(TajoCli.TajoCliContext context) { + super(context); + haAdmin = new TajoHAAdmin(context.getConf(), context.getOutput(), context.getTajoClient()); + } + + @Override + public String getCommand() { + return "\\haadmin"; + } + + @Override + public void invoke(String[] command) throws Exception { + try { + String[] haAdminCommands = new String[command.length - 1]; + System.arraycopy(command, 1, haAdminCommands, 0, haAdminCommands.length); + + haAdmin.runCommand(haAdminCommands); + } catch (Exception e) { + context.getOutput().println("ERROR: " + e.getMessage()); + } + } + + @Override + public String getUsage() { + return "<command> [options]"; + } + + @Override + public String getDescription() { + return "execute a tajo haAdminF command."; + } +}
http://git-wip-us.apache.org/repos/asf/tajo/blob/c59baa3a/tajo-client/src/main/java/org/apache/tajo/cli/tsql/commands/TajoShellCommand.java ---------------------------------------------------------------------- diff --git a/tajo-client/src/main/java/org/apache/tajo/cli/tsql/commands/TajoShellCommand.java b/tajo-client/src/main/java/org/apache/tajo/cli/tsql/commands/TajoShellCommand.java new file mode 100644 index 0000000..361f04d --- /dev/null +++ b/tajo-client/src/main/java/org/apache/tajo/cli/tsql/commands/TajoShellCommand.java @@ -0,0 +1,129 @@ +/** + * 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.cli.tsql.commands; + +import org.apache.tajo.cli.tsql.TajoCli; +import org.apache.tajo.client.TajoClient; +import org.apache.tajo.conf.TajoConf; + +public abstract class TajoShellCommand { + public abstract String getCommand(); + public String [] getAliases() { + return new String[] {}; + } + public abstract void invoke(String [] command) throws Exception; + public abstract String getUsage(); + public abstract String getDescription(); + public void printHelp() { + context.getOutput().print(getCommand()); + context.getOutput().print(" - "); + context.getOutput().println(getDescription()); + } + + protected TajoCli.TajoCliContext context; + protected TajoClient client; + protected int maxColumn; + + public TajoShellCommand(TajoCli.TajoCliContext context) { + maxColumn = context.getConf().getIntVar(TajoConf.ConfVars.$CLI_MAX_COLUMN); + this.context = context; + client = context.getTajoClient(); + } + + protected void println() { + context.getOutput().println(); + } + + protected void printLeft(String message, int columnWidth) { + int messageLength = message.length(); + + if(messageLength >= columnWidth) { + context.getOutput().print(message.substring(0, columnWidth - 1)); + } else { + context.getOutput().print(message); + print(' ', columnWidth - messageLength - 1); + } + } + + protected void printCenter(String message, int columnWidth, boolean warp) { + int messageLength = message.length(); + + if(messageLength > columnWidth) { + context.getOutput().print(message.substring(0, columnWidth - 1)); + } else { + int numPadding = (columnWidth - messageLength)/2; + + print(' ', numPadding); + context.getOutput().print(message); + print(' ', numPadding); + } + if(warp) { + println(); + } + } + + protected void printCenter(String message) { + printCenter(message, maxColumn, true); + } + + protected void print(char c, int count) { + for(int i = 0; i < count; i++) { + context.getOutput().print(c); + } + } + + protected int[] printHeader(String[] headers, float[] columnWidthRates) { + int[] columnWidths = new int[columnWidthRates.length]; + + int columnWidthSum = 0; + for(int i = 0; i < columnWidths.length; i++) { + columnWidths[i] = (int)(maxColumn * columnWidthRates[i]); + if(i > 0) { + columnWidthSum += columnWidths[i - 1]; + } + } + + columnWidths[columnWidths.length - 1] = maxColumn - columnWidthSum; + + String prefix = ""; + for(int i = 0; i < headers.length; i++) { + context.getOutput().print(prefix); + printLeft(" " + headers[i], columnWidths[i]); + prefix = "|"; + } + println(); + + int index = 0; + int printPos = columnWidths[index] - 1; + for(int i = 0; i < maxColumn; i++) { + if(i == printPos) { + if(index < columnWidths.length - 1) { + print('+', 1); + index++; + printPos += columnWidths[index]; + } + } else { + print('-', 1); + } + } + + println(); + return columnWidths; + } +} http://git-wip-us.apache.org/repos/asf/tajo/blob/c59baa3a/tajo-client/src/main/java/org/apache/tajo/cli/tsql/commands/UnsetCommand.java ---------------------------------------------------------------------- diff --git a/tajo-client/src/main/java/org/apache/tajo/cli/tsql/commands/UnsetCommand.java b/tajo-client/src/main/java/org/apache/tajo/cli/tsql/commands/UnsetCommand.java new file mode 100644 index 0000000..b540ca1 --- /dev/null +++ b/tajo-client/src/main/java/org/apache/tajo/cli/tsql/commands/UnsetCommand.java @@ -0,0 +1,53 @@ +/** + * 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.cli.tsql.commands; + +import com.google.common.collect.Lists; +import org.apache.tajo.cli.tsql.TajoCli; + +public class UnsetCommand extends TajoShellCommand { + + public UnsetCommand(TajoCli.TajoCliContext context) { + super(context); + } + + @Override + public String getCommand() { + return "\\unset"; + } + + @Override + public void invoke(String[] cmd) throws Exception { + if (cmd.length == 2) { + client.unsetSessionVariables(Lists.newArrayList(cmd[1])); + } else { + context.getOutput().println("usage: \\unset NAME"); + } + } + + @Override + public String getUsage() { + return ""; + } + + @Override + public String getDescription() { + return "unset a session variable"; + } +} http://git-wip-us.apache.org/repos/asf/tajo/blob/c59baa3a/tajo-client/src/main/java/org/apache/tajo/cli/tsql/commands/VersionCommand.java ---------------------------------------------------------------------- diff --git a/tajo-client/src/main/java/org/apache/tajo/cli/tsql/commands/VersionCommand.java b/tajo-client/src/main/java/org/apache/tajo/cli/tsql/commands/VersionCommand.java new file mode 100644 index 0000000..9c4aa25 --- /dev/null +++ b/tajo-client/src/main/java/org/apache/tajo/cli/tsql/commands/VersionCommand.java @@ -0,0 +1,49 @@ +/** + * 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.cli.tsql.commands; + +import org.apache.tajo.cli.tsql.TajoCli; +import org.apache.tajo.util.VersionInfo; + +public class VersionCommand extends TajoShellCommand { + + public VersionCommand(TajoCli.TajoCliContext context) { + super(context); + } + + @Override + public String getCommand() { + return "\\version"; + } + + @Override + public void invoke(String[] cmd) throws Exception { + context.getOutput().println(VersionInfo.getDisplayVersion()); + } + + @Override + public String getUsage() { + return ""; + } + + @Override + public String getDescription() { + return "show Tajo version"; + } +} http://git-wip-us.apache.org/repos/asf/tajo/blob/c59baa3a/tajo-client/src/main/java/org/apache/tajo/client/TajoAdmin.java ---------------------------------------------------------------------- diff --git a/tajo-client/src/main/java/org/apache/tajo/client/TajoAdmin.java b/tajo-client/src/main/java/org/apache/tajo/client/TajoAdmin.java deleted file mode 100644 index 817a698..0000000 --- a/tajo-client/src/main/java/org/apache/tajo/client/TajoAdmin.java +++ /dev/null @@ -1,456 +0,0 @@ -/** - * 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; - -import com.google.protobuf.ServiceException; -import org.apache.commons.cli.*; -import org.apache.commons.lang.StringUtils; -import org.apache.tajo.QueryId; -import org.apache.tajo.TajoProtos; -import org.apache.tajo.conf.TajoConf; -import org.apache.tajo.ipc.ClientProtos.BriefQueryInfo; -import org.apache.tajo.ipc.ClientProtos.WorkerResourceInfo; -import org.apache.tajo.util.NetUtils; -import org.apache.tajo.util.HAServiceUtil; -import org.apache.tajo.util.TajoIdUtils; - -import java.io.IOException; -import java.io.PrintWriter; -import java.io.Writer; -import java.net.InetSocketAddress; -import java.sql.SQLException; -import java.text.DecimalFormat; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.List; - -public class TajoAdmin { - private static final org.apache.commons.cli.Options options; - private static DecimalFormat decimalF = new DecimalFormat("###.0"); - private enum WorkerStatus { - RUNNING, - LOST, - DECOMMISSIONED - } - - final static String DASHLINE_LEN5 = "-----"; - final static String DASHLINE_LEN10 = "----------"; - final static String DASHLINE_LEN12 = "------------"; - final static String DASHLINE_LEN25 = "-------------------------"; - final static String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; - - static { - options = new Options(); - options.addOption("h", "host", true, "Tajo server host"); - options.addOption("p", "port", true, "Tajo server port"); - options.addOption("list", null, false, "Show Tajo query list"); - options.addOption("cluster", null, false, "Show Cluster Info"); - options.addOption("showmasters", null, false, "gets list of tajomasters in the cluster"); - options.addOption("desc", null, false, "Show Query Description"); - options.addOption("kill", null, true, "Kill a running query"); - } - - private TajoConf tajoConf; - private TajoClient tajoClient; - private Writer writer; - - public TajoAdmin(TajoConf tajoConf, Writer writer) { - this(tajoConf, writer, null); - } - - public TajoAdmin(TajoConf tajoConf, Writer writer, TajoClient tajoClient) { - this.tajoConf = tajoConf; - this.writer = writer; - this.tajoClient = tajoClient; - } - - private void printUsage() { - HelpFormatter formatter = new HelpFormatter(); - formatter.printHelp( "admin [options]", options ); - } - - public void runCommand(String[] args) throws Exception { - CommandLineParser parser = new PosixParser(); - CommandLine cmd = parser.parse(options, args); - - String param = ""; - int cmdType = 0; - - String hostName = null; - Integer port = null; - if (cmd.hasOption("h")) { - hostName = cmd.getOptionValue("h"); - } - if (cmd.hasOption("p")) { - port = Integer.parseInt(cmd.getOptionValue("p")); - } - - String queryId = null; - - if (cmd.hasOption("list")) { - cmdType = 1; - } else if (cmd.hasOption("desc")) { - cmdType = 2; - } else if (cmd.hasOption("cluster")) { - cmdType = 3; - } else if (cmd.hasOption("kill")) { - cmdType = 4; - queryId = cmd.getOptionValue("kill"); - } else if (cmd.hasOption("showmasters")) { - cmdType = 5; - } - - // if there is no "-h" option, - if(hostName == null) { - if (tajoConf.getVar(TajoConf.ConfVars.TAJO_MASTER_CLIENT_RPC_ADDRESS) != null) { - // it checks if the client service address is given in configuration and distributed mode. - // if so, it sets entryAddr. - hostName = tajoConf.getVar(TajoConf.ConfVars.TAJO_MASTER_CLIENT_RPC_ADDRESS).split(":")[0]; - } - } - if (port == null) { - if (tajoConf.getVar(TajoConf.ConfVars.TAJO_MASTER_CLIENT_RPC_ADDRESS) != null) { - // it checks if the client service address is given in configuration and distributed mode. - // if so, it sets entryAddr. - port = Integer.parseInt(tajoConf.getVar(TajoConf.ConfVars.TAJO_MASTER_CLIENT_RPC_ADDRESS).split(":")[1]); - } - } - - if (cmdType == 0) { - printUsage(); - return; - } - - - if ((hostName == null) ^ (port == null)) { - System.err.println("ERROR: cannot find valid Tajo server address"); - return; - } else if (hostName != null && port != null) { - tajoConf.setVar(TajoConf.ConfVars.TAJO_MASTER_CLIENT_RPC_ADDRESS, hostName + ":" + port); - tajoClient = new TajoClientImpl(tajoConf); - } else if (hostName == null && port == null) { - tajoClient = new TajoClientImpl(tajoConf); - } - - switch (cmdType) { - case 1: - processList(writer); - break; - case 2: - processDesc(writer); - break; - case 3: - processCluster(writer); - break; - case 4: - processKill(writer, queryId); - break; - case 5: - processMasters(writer); - break; - default: - printUsage(); - break; - } - - writer.flush(); - } - - private void processDesc(Writer writer) throws ParseException, IOException, - ServiceException, SQLException { - tajoClient = TajoHAClientUtil.getTajoClient(tajoConf, tajoClient); - List<BriefQueryInfo> queryList = tajoClient.getRunningQueryList(); - SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT); - int id = 1; - for (BriefQueryInfo queryInfo : queryList) { - String queryId = String.format("q_%s_%04d", - queryInfo.getQueryId().getId(), - queryInfo.getQueryId().getSeq()); - - writer.write("Id: " + id); - writer.write("\n"); - id++; - writer.write("Query Id: " + queryId); - writer.write("\n"); - writer.write("Started Time: " + df.format(queryInfo.getStartTime())); - writer.write("\n"); - - writer.write("Query State: " + queryInfo.getState().name()); - writer.write("\n"); - long end = queryInfo.getFinishTime(); - long start = queryInfo.getStartTime(); - String executionTime = decimalF.format((end-start) / 1000) + " sec"; - if (TajoClientUtil.isQueryComplete(queryInfo.getState())) { - writer.write("Finished Time: " + df.format(queryInfo.getFinishTime())); - writer.write("\n"); - } - writer.write("Execution Time: " + executionTime); - writer.write("\n"); - writer.write("Query Progress: " + queryInfo.getProgress()); - writer.write("\n"); - writer.write("Query Statement:"); - writer.write("\n"); - writer.write(queryInfo.getQuery()); - writer.write("\n"); - writer.write("\n"); - } - } - - private void processCluster(Writer writer) throws ParseException, IOException, - ServiceException, SQLException { - tajoClient = TajoHAClientUtil.getTajoClient(tajoConf, tajoClient); - List<WorkerResourceInfo> workerList = tajoClient.getClusterInfo(); - - int runningQueryMasterTasks = 0; - - List<WorkerResourceInfo> liveWorkers = new ArrayList<WorkerResourceInfo>(); - List<WorkerResourceInfo> deadWorkers = new ArrayList<WorkerResourceInfo>(); - List<WorkerResourceInfo> decommissionWorkers = new ArrayList<WorkerResourceInfo>(); - - List<WorkerResourceInfo> liveQueryMasters = new ArrayList<WorkerResourceInfo>(); - List<WorkerResourceInfo> deadQueryMasters = new ArrayList<WorkerResourceInfo>(); - - for (WorkerResourceInfo eachWorker : workerList) { - if(eachWorker.getQueryMasterMode() == true) { - if(eachWorker.getWorkerStatus().equals(WorkerStatus.RUNNING.toString())) { - liveQueryMasters.add(eachWorker); - runningQueryMasterTasks += eachWorker.getNumQueryMasterTasks(); - } - if(eachWorker.getWorkerStatus().equals(WorkerStatus.LOST.toString())) { - deadQueryMasters.add(eachWorker); - } - } - - if(eachWorker.getTaskRunnerMode() == true) { - if(eachWorker.getWorkerStatus().equals(WorkerStatus.RUNNING.toString())) { - liveWorkers.add(eachWorker); - } else if(eachWorker.getWorkerStatus().equals(WorkerStatus.LOST.toString())) { - deadWorkers.add(eachWorker); - } else if(eachWorker.getWorkerStatus().equals(WorkerStatus.DECOMMISSIONED.toString())) { - decommissionWorkers.add(eachWorker); - } - } - } - - String fmtInfo = "%1$-5s %2$-5s %3$-5s%n"; - String infoLine = String.format(fmtInfo, "Live", "Dead", "Tasks"); - - writer.write("Query Master\n"); - writer.write("============\n\n"); - writer.write(infoLine); - String line = String.format(fmtInfo, DASHLINE_LEN5, DASHLINE_LEN5, DASHLINE_LEN5); - writer.write(line); - - line = String.format(fmtInfo, liveQueryMasters.size(), - deadQueryMasters.size(), runningQueryMasterTasks); - writer.write(line); - writer.write("\n"); - - writer.write("Live QueryMasters\n"); - writer.write("=================\n\n"); - - if (liveQueryMasters.isEmpty()) { - writer.write("No Live QueryMasters\n"); - } else { - String fmtQueryMasterLine = "%1$-25s %2$-5s %3$-5s %4$-10s %5$-10s%n"; - line = String.format(fmtQueryMasterLine, "QueryMaster", "Port", "Query", - "Heap", "Status"); - writer.write(line); - line = String.format(fmtQueryMasterLine, DASHLINE_LEN25, DASHLINE_LEN5, - DASHLINE_LEN5, DASHLINE_LEN10, DASHLINE_LEN10); - writer.write(line); - for (WorkerResourceInfo queryMaster : liveQueryMasters) { - TajoProtos.WorkerConnectionInfoProto connInfo = queryMaster.getConnectionInfo(); - String queryMasterHost = String.format("%s:%d", connInfo.getHost(), connInfo.getQueryMasterPort()); - String heap = String.format("%d MB", queryMaster.getMaxHeap() / 1024 / 1024); - line = String.format(fmtQueryMasterLine, - queryMasterHost, - connInfo.getClientPort(), - queryMaster.getNumQueryMasterTasks(), - heap, - queryMaster.getWorkerStatus()); - writer.write(line); - } - - writer.write("\n\n"); - } - - if (!deadQueryMasters.isEmpty()) { - writer.write("Dead QueryMasters\n"); - writer.write("=================\n\n"); - - String fmtQueryMasterLine = "%1$-25s %2$-5s %3$-10s%n"; - line = String.format(fmtQueryMasterLine, "QueryMaster", "Port", "Status"); - writer.write(line); - line = String.format(fmtQueryMasterLine, DASHLINE_LEN25, DASHLINE_LEN5, DASHLINE_LEN10); - writer.write(line); - - for (WorkerResourceInfo queryMaster : deadQueryMasters) { - TajoProtos.WorkerConnectionInfoProto connInfo = queryMaster.getConnectionInfo(); - String queryMasterHost = String.format("%s:%d", connInfo.getHost(), connInfo.getQueryMasterPort()); - line = String.format(fmtQueryMasterLine, - queryMasterHost, - connInfo.getClientPort(), - queryMaster.getWorkerStatus()); - writer.write(line); - } - - writer.write("\n\n"); - } - - writer.write("Worker\n"); - writer.write("======\n\n"); - - String fmtWorkerInfo = "%1$-5s %2$-5s%n"; - String workerInfoLine = String.format(fmtWorkerInfo, "Live", "Dead"); - writer.write(workerInfoLine); - line = String.format(fmtWorkerInfo, DASHLINE_LEN5, DASHLINE_LEN5); - writer.write(line); - - line = String.format(fmtWorkerInfo, liveWorkers.size(), deadWorkers.size()); - writer.write(line); - writer.write("\n"); - - writer.write("Live Workers\n"); - writer.write("============\n\n"); - if(liveWorkers.isEmpty()) { - writer.write("No Live Workers\n\n"); - } else { - writeWorkerInfo(writer, liveWorkers); - } - - writer.write("Dead Workers\n"); - writer.write("============\n\n"); - if(deadWorkers.isEmpty()) { - writer.write("No Dead Workers\n\n"); - } else { - writeWorkerInfo(writer, deadWorkers); - } - } - - private void writeWorkerInfo(Writer writer, List<WorkerResourceInfo> workers) throws ParseException, - IOException, ServiceException, SQLException { - String fmtWorkerLine = "%1$-25s %2$-5s %3$-5s %4$-10s %5$-10s %6$-12s %7$-10s%n"; - String line = String.format(fmtWorkerLine, - "Worker", "Port", "Tasks", - "Mem", "Disk", - "Heap", "Status"); - writer.write(line); - line = String.format(fmtWorkerLine, - DASHLINE_LEN25, DASHLINE_LEN5, DASHLINE_LEN5, - DASHLINE_LEN10, DASHLINE_LEN10, - DASHLINE_LEN12, DASHLINE_LEN10); - writer.write(line); - - for (WorkerResourceInfo worker : workers) { - TajoProtos.WorkerConnectionInfoProto connInfo = worker.getConnectionInfo(); - String workerHost = String.format("%s:%d", connInfo.getHost(), connInfo.getPeerRpcPort()); - String mem = String.format("%d/%d", worker.getUsedMemoryMB(), - worker.getMemoryMB()); - String disk = String.format("%.2f/%.2f", worker.getUsedDiskSlots(), - worker.getDiskSlots()); - String heap = String.format("%d/%d MB", worker.getFreeHeap()/1024/1024, - worker.getMaxHeap()/1024/1024); - - line = String.format(fmtWorkerLine, workerHost, - connInfo.getPullServerPort(), - worker.getNumRunningTasks(), - mem, disk, heap, worker.getWorkerStatus()); - writer.write(line); - } - writer.write("\n\n"); - } - - private void processList(Writer writer) throws ParseException, IOException, - ServiceException, SQLException { - tajoClient = TajoHAClientUtil.getTajoClient(tajoConf, tajoClient); - List<BriefQueryInfo> queryList = tajoClient.getRunningQueryList(); - SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT); - StringBuilder builder = new StringBuilder(); - - /* print title */ - builder.append(StringUtils.rightPad("QueryId", 21)); - builder.append(StringUtils.rightPad("State", 20)); - builder.append(StringUtils.rightPad("StartTime", 20)); - builder.append(StringUtils.rightPad("Query", 30)).append("\n"); - - builder.append(StringUtils.rightPad(StringUtils.repeat("-", 20), 21)); - builder.append(StringUtils.rightPad(StringUtils.repeat("-", 19), 20)); - builder.append(StringUtils.rightPad(StringUtils.repeat("-", 19), 20)); - builder.append(StringUtils.rightPad(StringUtils.repeat("-", 29), 30)).append("\n"); - writer.write(builder.toString()); - - builder = new StringBuilder(); - for (BriefQueryInfo queryInfo : queryList) { - builder.append(StringUtils.rightPad(new QueryId(queryInfo.getQueryId()).toString(), 21)); - builder.append(StringUtils.rightPad(queryInfo.getState().name(), 20)); - builder.append(StringUtils.rightPad(df.format(queryInfo.getStartTime()), 20)); - builder.append(StringUtils.abbreviate(queryInfo.getQuery(), 30)).append("\n"); - } - writer.write(builder.toString()); - } - - public void processKill(Writer writer, String queryIdStr) - throws IOException, ServiceException { - QueryStatus status = tajoClient.killQuery(TajoIdUtils.parseQueryId(queryIdStr)); - if (status.getState() == TajoProtos.QueryState.QUERY_KILLED) { - writer.write(queryIdStr + " is killed successfully.\n"); - } else if (status.getState() == TajoProtos.QueryState.QUERY_KILL_WAIT) { - writer.write(queryIdStr + " will be finished after a while.\n"); - } else { - writer.write("ERROR:" + status.getErrorMessage()); - } - } - - private void processMasters(Writer writer) throws ParseException, IOException, - ServiceException, SQLException { - tajoClient = TajoHAClientUtil.getTajoClient(tajoConf, tajoClient); - if (tajoConf.getBoolVar(TajoConf.ConfVars.TAJO_MASTER_HA_ENABLE)) { - - List<String> list = HAServiceUtil.getMasters(tajoConf); - int i = 0; - for (String master : list) { - if (i > 0) { - writer.write(" "); - } - writer.write(master); - i++; - } - writer.write("\n"); - } else { - String confMasterServiceAddr = tajoConf.getVar(TajoConf.ConfVars.TAJO_MASTER_UMBILICAL_RPC_ADDRESS); - InetSocketAddress masterAddress = NetUtils.createSocketAddr(confMasterServiceAddr); - writer.write(masterAddress.getHostName()); - writer.write("\n"); - } - } - - public static void main(String [] args) throws Exception { - TajoConf conf = new TajoConf(); - - Writer writer = new PrintWriter(System.out); - try { - TajoAdmin admin = new TajoAdmin(conf, writer); - admin.runCommand(args); - } finally { - writer.close(); - System.exit(0); - } - } -} http://git-wip-us.apache.org/repos/asf/tajo/blob/c59baa3a/tajo-client/src/main/java/org/apache/tajo/client/TajoDump.java ---------------------------------------------------------------------- diff --git a/tajo-client/src/main/java/org/apache/tajo/client/TajoDump.java b/tajo-client/src/main/java/org/apache/tajo/client/TajoDump.java deleted file mode 100644 index 540f54b..0000000 --- a/tajo-client/src/main/java/org/apache/tajo/client/TajoDump.java +++ /dev/null @@ -1,186 +0,0 @@ -/** - * 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; - -import com.google.protobuf.ServiceException; -import org.apache.commons.cli.*; -import org.apache.hadoop.security.UserGroupInformation; -import org.apache.tajo.catalog.CatalogUtil; -import org.apache.tajo.catalog.DDLBuilder; -import org.apache.tajo.catalog.TableDesc; -import org.apache.tajo.conf.TajoConf; -import org.apache.tajo.util.Pair; -import org.apache.tajo.util.TUtil; - -import java.io.IOException; -import java.io.PrintWriter; -import java.sql.SQLException; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Collections; -import java.util.List; - -public class TajoDump { - private static final org.apache.commons.cli.Options options; - - static { - options = new Options(); - options.addOption("h", "host", true, "Tajo server host"); - options.addOption("p", "port", true, "Tajo server port"); - options.addOption("a", "all", false, "dump all table DDLs"); - } - - private static void printUsage() { - HelpFormatter formatter = new HelpFormatter(); - formatter.printHelp( "tajo-dump [options] [database name]", options); - } - - private static Pair<String, Integer> getConnectionAddr(TajoConf conf, CommandLine cmd) { - String hostName = null; - Integer port = null; - if (cmd.hasOption("h")) { - hostName = cmd.getOptionValue("h"); - } - if (cmd.hasOption("p")) { - port = Integer.parseInt(cmd.getOptionValue("p")); - } - - if(hostName == null) { - if (conf.getVar(TajoConf.ConfVars.TAJO_MASTER_CLIENT_RPC_ADDRESS) != null) { - hostName = conf.getVar(TajoConf.ConfVars.TAJO_MASTER_CLIENT_RPC_ADDRESS).split(":")[0]; - } - } - if (port == null) { - if (conf.getVar(TajoConf.ConfVars.TAJO_MASTER_CLIENT_RPC_ADDRESS) != null) { - port = Integer.parseInt(conf.getVar(TajoConf.ConfVars.TAJO_MASTER_CLIENT_RPC_ADDRESS).split(":")[1]); - } - } - return new Pair<String, Integer>(hostName, port); - } - - public static void main(String [] args) throws ParseException, IOException, ServiceException, SQLException { - final TajoConf conf = new TajoConf(); - final CommandLineParser parser = new PosixParser(); - final CommandLine cmd = parser.parse(options, args); - final Pair<String, Integer> hostAndPort = getConnectionAddr(conf, cmd); - final String hostName = hostAndPort.getFirst(); - final Integer port = hostAndPort.getSecond(); - final UserGroupInformation userInfo = UserGroupInformation.getCurrentUser(); - - String baseDatabaseName = null; - if (cmd.getArgList().size() > 0) { - baseDatabaseName = (String) cmd.getArgList().get(0); - } - - boolean isDumpingAllDatabases = cmd.hasOption('a'); - - // Neither two choices - if (!isDumpingAllDatabases && baseDatabaseName == null) { - printUsage(); - System.exit(-1); - } - - TajoClient client = null; - if ((hostName == null) ^ (port == null)) { - System.err.println("ERROR: cannot find any TajoMaster rpc address in arguments and tajo-site.xml."); - System.exit(-1); - } else if (hostName != null && port != null) { - conf.setVar(TajoConf.ConfVars.TAJO_MASTER_CLIENT_RPC_ADDRESS, hostName+":"+port); - client = new TajoClientImpl(conf); - } else { - client = new TajoClientImpl(conf); - } - - PrintWriter writer = new PrintWriter(System.out); - dump(client, userInfo, baseDatabaseName, isDumpingAllDatabases, true, true, writer); - - System.exit(0); - } - - public static void dump(TajoClient client, UserGroupInformation userInfo, String baseDatabaseName, - boolean isDumpingAllDatabases, boolean includeUserName, boolean includeDate, PrintWriter out) - throws SQLException, ServiceException { - printHeader(out, userInfo, includeUserName, includeDate); - - if (isDumpingAllDatabases) { - // sort database names in an ascending lexicographic order of the names. - List<String> sorted = new ArrayList<String>(client.getAllDatabaseNames()); - Collections.sort(sorted); - - for (String databaseName : sorted) { - dumpDatabase(client, databaseName, out); - } - } else { - dumpDatabase(client, baseDatabaseName, out); - } - out.flush(); - } - - private static void printHeader(PrintWriter writer, UserGroupInformation userInfo, boolean includeUSerName, - boolean includeDate) { - writer.write("--\n"); - writer.write("-- Tajo database dump\n"); - if (includeUSerName) { - writer.write("--\n-- Dump user: " + userInfo.getUserName() + "\n"); - } - if (includeDate) { - writer.write("--\n-- Dump date: " + toDateString() + "\n"); - } - writer.write("--\n"); - writer.write("\n"); - } - - private static void dumpDatabase(TajoClient client, String databaseName, PrintWriter writer) - throws SQLException, ServiceException { - writer.write("\n"); - writer.write("--\n"); - writer.write(String.format("-- Database name: %s%n", CatalogUtil.denormalizeIdentifier(databaseName))); - writer.write("--\n"); - writer.write("\n"); - writer.write(String.format("CREATE DATABASE IF NOT EXISTS %s;", CatalogUtil.denormalizeIdentifier(databaseName))); - writer.write("\n\n"); - - // returned list is immutable. - List<String> tableNames = TUtil.newList(client.getTableList(databaseName)); - Collections.sort(tableNames); - for (String tableName : tableNames) { - try { - TableDesc table = client.getTableDesc(CatalogUtil.buildFQName(databaseName, tableName)); - if (table.isExternal()) { - writer.write(DDLBuilder.buildDDLForExternalTable(table)); - } else { - writer.write(DDLBuilder.buildDDLForBaseTable(table)); - } - writer.write("\n\n"); - } catch (Exception e) { - // dump for each table can throw any exception. We need to skip the exception case. - // here, the error message prints out via stderr. - System.err.println("ERROR:" + tableName + "," + e.getMessage()); - } - } - } - - private static String toDateString() { - DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); - java.util.Date today = Calendar.getInstance().getTime(); - return df.format(today); - } -} http://git-wip-us.apache.org/repos/asf/tajo/blob/c59baa3a/tajo-client/src/main/java/org/apache/tajo/client/TajoGetConf.java ---------------------------------------------------------------------- diff --git a/tajo-client/src/main/java/org/apache/tajo/client/TajoGetConf.java b/tajo-client/src/main/java/org/apache/tajo/client/TajoGetConf.java deleted file mode 100644 index 88ab491..0000000 --- a/tajo-client/src/main/java/org/apache/tajo/client/TajoGetConf.java +++ /dev/null @@ -1,159 +0,0 @@ -/** - * 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; - -import com.google.protobuf.ServiceException; -import org.apache.commons.cli.*; -import org.apache.tajo.conf.TajoConf; - -import java.io.IOException; -import java.io.PrintWriter; -import java.io.Writer; -import java.sql.SQLException; - -public class TajoGetConf { - private static final org.apache.commons.cli.Options options; - - static { - options = new Options(); - options.addOption("h", "host", true, "Tajo server host"); - options.addOption("p", "port", true, "Tajo server port"); - } - - private TajoConf tajoConf; - private TajoClient tajoClient; - private Writer writer; - - public final static String defaultLeftPad = " "; - public final static String defaultDescPad = " "; - - public TajoGetConf(TajoConf tajoConf, Writer writer) { - this(tajoConf, writer, null); - } - - public TajoGetConf(TajoConf tajoConf, Writer writer, TajoClient tajoClient) { - this.tajoConf = tajoConf; - this.writer = writer; - this.tajoClient = tajoClient; - } - - private void printUsage(boolean tsqlMode) { - if (!tsqlMode) { - HelpFormatter formatter = new HelpFormatter(); - formatter.printHelp( "getconf <key> [options]", options ); - } - System.out.println(defaultLeftPad + "key" + defaultDescPad + "gets a specific key from the configuration"); - } - - public void runCommand(String[] args) throws Exception { - runCommand(args, true); - } - - public void runCommand(String[] args, boolean tsqlMode) throws Exception { - CommandLineParser parser = new PosixParser(); - - if (args.length == 0) { - printUsage(tsqlMode); - return; - } - - CommandLine cmd = parser.parse(options, args); - - String hostName = null; - Integer port = null; - if (cmd.hasOption("h")) { - hostName = cmd.getOptionValue("h"); - } - if (cmd.hasOption("p")) { - port = Integer.parseInt(cmd.getOptionValue("p")); - } - - String param; - if (cmd.getArgs().length > 1) { - printUsage(tsqlMode); - return; - } else { - param = cmd.getArgs()[0]; - } - - // if there is no "-h" option, - if(hostName == null) { - if (tajoConf.getVar(TajoConf.ConfVars.TAJO_MASTER_CLIENT_RPC_ADDRESS) != null) { - // it checks if the client service address is given in configuration and distributed mode. - // if so, it sets entryAddr. - hostName = tajoConf.getVar(TajoConf.ConfVars.TAJO_MASTER_CLIENT_RPC_ADDRESS).split(":")[0]; - } - } - if (port == null) { - if (tajoConf.getVar(TajoConf.ConfVars.TAJO_MASTER_CLIENT_RPC_ADDRESS) != null) { - // it checks if the client service address is given in configuration and distributed mode. - // if so, it sets entryAddr. - port = Integer.parseInt(tajoConf.getVar(TajoConf.ConfVars.TAJO_MASTER_CLIENT_RPC_ADDRESS).split(":")[1]); - } - } - - if ((hostName == null) ^ (port == null)) { - return; - } else if (hostName != null && port != null) { - tajoConf.setVar(TajoConf.ConfVars.TAJO_MASTER_CLIENT_RPC_ADDRESS, hostName + ":" + port); - tajoClient = new TajoClientImpl(tajoConf); - } else if (hostName == null && port == null) { - tajoClient = new TajoClientImpl(tajoConf); - } - - processConfKey(writer, param); - writer.flush(); - } - - private void processConfKey(Writer writer, String param) throws ParseException, IOException, - ServiceException, SQLException { - String value = tajoConf.getTrimmed(param); - - // If there is no value in the configuration file, we need to find all ConfVars. - if (value == null) { - for(TajoConf.ConfVars vars : TajoConf.ConfVars.values()) { - if (vars.varname.equalsIgnoreCase(param)) { - value = tajoConf.getVar(vars); - break; - } - } - } - - if (value != null) { - writer.write(value); - } else { - writer.write("Configuration " + param + " is missing."); - } - - writer.write("\n"); - } - - public static void main(String [] args) throws Exception { - TajoConf conf = new TajoConf(); - - Writer writer = new PrintWriter(System.out); - try { - TajoGetConf admin = new TajoGetConf(conf, writer); - admin.runCommand(args, false); - } finally { - writer.close(); - System.exit(0); - } - } -} http://git-wip-us.apache.org/repos/asf/tajo/blob/c59baa3a/tajo-client/src/main/java/org/apache/tajo/client/TajoHAAdmin.java ---------------------------------------------------------------------- diff --git a/tajo-client/src/main/java/org/apache/tajo/client/TajoHAAdmin.java b/tajo-client/src/main/java/org/apache/tajo/client/TajoHAAdmin.java deleted file mode 100644 index 5d5cf71..0000000 --- a/tajo-client/src/main/java/org/apache/tajo/client/TajoHAAdmin.java +++ /dev/null @@ -1,210 +0,0 @@ -/** - * 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; - -import com.google.protobuf.ServiceException; -import org.apache.commons.cli.*; -import org.apache.tajo.conf.TajoConf; -import org.apache.tajo.ipc.ClientProtos; -import org.apache.tajo.util.HAServiceUtil; - -import java.io.IOException; -import java.io.PrintWriter; -import java.io.Writer; -import java.util.List; - -public class TajoHAAdmin { - private static final Options options; - - static { - options = new Options(); - options.addOption("h", "host", true, "Tajo server host"); - options.addOption("p", "port", true, "Tajo server port"); - options.addOption("transitionToActive", null, true, "Transitions the master into Active state"); - options.addOption("transitionToBackup", null, true, "Transitions the master into Backup state"); - options.addOption("getState", null, true, "Returns the state of the master"); - options.addOption("formatHA", null, false, "Format HA status on share storage"); - } - - private TajoConf tajoConf; - private TajoClient tajoClient; - private Writer writer; - - public TajoHAAdmin(TajoConf tajoConf, Writer writer) { - this(tajoConf, writer, null); - } - - public TajoHAAdmin(TajoConf tajoConf, Writer writer, TajoClient tajoClient) { - this.tajoConf = tajoConf; - this.writer = writer; - this.tajoClient = tajoClient; - } - - private void printUsage() { - HelpFormatter formatter = new HelpFormatter(); - formatter.printHelp( "haadmin [options]", options ); - } - - public void runCommand(String[] args) throws Exception { - if(args.length == 1 && - (args[0].equalsIgnoreCase("-transitionToActive") - || args[0].equalsIgnoreCase("-transitionToBackup") - || args[0].equalsIgnoreCase("-getState"))) { - writer.write("Not enough arguments: expected 1 but got 0\n"); - writer.flush(); - return; - } - - CommandLineParser parser = new PosixParser(); - CommandLine cmd = parser.parse(options, args); - - String param = ""; - int cmdType = 0; - - String hostName = null; - Integer port = null; - if (cmd.hasOption("h")) { - hostName = cmd.getOptionValue("h"); - } - if (cmd.hasOption("p")) { - port = Integer.parseInt(cmd.getOptionValue("p")); - } - - if (cmd.hasOption("transitionToActive")) { - cmdType = 1; - param = cmd.getOptionValue("transitionToActive"); - } else if (cmd.hasOption("transitionToBackup")) { - cmdType = 2; - param = cmd.getOptionValue("transitionToBackup"); - } else if (cmd.hasOption("getState")) { - cmdType = 3; - param = cmd.getOptionValue("getState"); - } else if (cmd.hasOption("formatHA")) { - cmdType = 4; - } - - // if there is no "-h" option, - if(hostName == null) { - if (tajoConf.getVar(TajoConf.ConfVars.TAJO_MASTER_CLIENT_RPC_ADDRESS) != null) { - // it checks if the client service address is given in configuration and distributed mode. - // if so, it sets entryAddr. - hostName = tajoConf.getVar(TajoConf.ConfVars.TAJO_MASTER_CLIENT_RPC_ADDRESS).split(":")[0]; - } - } - if (port == null) { - if (tajoConf.getVar(TajoConf.ConfVars.TAJO_MASTER_CLIENT_RPC_ADDRESS) != null) { - // it checks if the client service address is given in configuration and distributed mode. - // if so, it sets entryAddr. - port = Integer.parseInt(tajoConf.getVar(TajoConf.ConfVars.TAJO_MASTER_CLIENT_RPC_ADDRESS).split(":")[1]); - } - } - - if (cmdType == 0) { - printUsage(); - return; - } - - - if ((hostName == null) ^ (port == null)) { - System.err.println("ERROR: cannot find valid Tajo server address"); - return; - } else if (hostName != null && port != null) { - tajoConf.setVar(TajoConf.ConfVars.TAJO_MASTER_CLIENT_RPC_ADDRESS, hostName + ":" + port); - tajoClient = new TajoClientImpl(tajoConf); - } else if (hostName == null && port == null) { - tajoClient = new TajoClientImpl(tajoConf); - } - - if (!tajoConf.getBoolVar(TajoConf.ConfVars.TAJO_MASTER_HA_ENABLE)) { - writer.write("HA is not enabled for this tajo cluster."); - } else { - switch (cmdType) { - case 1: - writer.write("Not Yet Implemented\n"); - break; - case 2: - writer.write("Not Yet Implemented\n"); - break; - case 3: - getState(writer, param); - break; - case 4: - formatHA(writer); - break; - default: - printUsage(); - break; - } - } - - writer.flush(); - } - - private void getState(Writer writer, String param) throws ParseException, IOException, - ServiceException { - tajoClient = TajoHAClientUtil.getTajoClient(tajoConf, tajoClient); - int retValue = HAServiceUtil.getState(param, tajoConf); - - switch (retValue) { - case 1: - writer.write("The master is active.\n"); - break; - case 0: - writer.write("The master is backup.\n"); - break; - case -1: - writer.write("Finding failed. - master:" + param + "\n"); - break; - default: - writer.write("Cannot find the master. - master:" + param + "\n"); - break; - } - } - - private void formatHA(Writer writer) throws ParseException, IOException, - ServiceException { - int retValue = HAServiceUtil.formatHA(tajoConf); - - switch (retValue) { - case 1: - writer.write("Formatting finished successfully.\n"); - break; - case 0: - writer.write("If you want to format the ha information, you must shutdown tajo masters " - + " before formatting.\n"); - break; - default: - writer.write("Cannot format ha information.\n"); - break; - } - } - - public static void main(String [] args) throws Exception { - TajoConf conf = new TajoConf(); - - Writer writer = new PrintWriter(System.out); - try { - TajoHAAdmin admin = new TajoHAAdmin(conf, writer); - admin.runCommand(args); - } finally { - writer.close(); - System.exit(0); - } - } -} http://git-wip-us.apache.org/repos/asf/tajo/blob/c59baa3a/tajo-client/src/main/java/org/apache/tajo/client/TajoHAClientUtil.java ---------------------------------------------------------------------- diff --git a/tajo-client/src/main/java/org/apache/tajo/client/TajoHAClientUtil.java b/tajo-client/src/main/java/org/apache/tajo/client/TajoHAClientUtil.java index b93590c..b95fb35 100644 --- a/tajo-client/src/main/java/org/apache/tajo/client/TajoHAClientUtil.java +++ b/tajo-client/src/main/java/org/apache/tajo/client/TajoHAClientUtil.java @@ -37,7 +37,7 @@ package org.apache.tajo.client; import com.google.protobuf.ServiceException; -import org.apache.tajo.cli.TajoCli.TajoCliContext; +import org.apache.tajo.cli.tsql.TajoCli.TajoCliContext; import org.apache.tajo.conf.TajoConf; import org.apache.tajo.util.HAServiceUtil; http://git-wip-us.apache.org/repos/asf/tajo/blob/c59baa3a/tajo-common/src/main/java/org/apache/tajo/conf/TajoConf.java ---------------------------------------------------------------------- diff --git a/tajo-common/src/main/java/org/apache/tajo/conf/TajoConf.java b/tajo-common/src/main/java/org/apache/tajo/conf/TajoConf.java index 86f934e..62f5007 100644 --- a/tajo-common/src/main/java/org/apache/tajo/conf/TajoConf.java +++ b/tajo-common/src/main/java/org/apache/tajo/conf/TajoConf.java @@ -351,7 +351,7 @@ public class TajoConf extends Configuration { $CLI_PRINT_PAUSE_NUM_RECORDS("tajo.cli.print.pause.num.records", 100), $CLI_PRINT_PAUSE("tajo.cli.print.pause", true), $CLI_PRINT_ERROR_TRACE("tajo.cli.print.error.trace", true), - $CLI_OUTPUT_FORMATTER_CLASS("tajo.cli.output.formatter", "org.apache.tajo.cli.DefaultTajoCliOutputFormatter"), + $CLI_OUTPUT_FORMATTER_CLASS("tajo.cli.output.formatter", "org.apache.tajo.cli.tsql.DefaultTajoCliOutputFormatter"), $CLI_ERROR_STOP("tajo.cli.error.stop", false), // Timezone & Date ---------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tajo/blob/c59baa3a/tajo-core/src/test/java/org/apache/tajo/QueryTestCaseBase.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/test/java/org/apache/tajo/QueryTestCaseBase.java b/tajo-core/src/test/java/org/apache/tajo/QueryTestCaseBase.java index becb73e..604a56b 100644 --- a/tajo-core/src/test/java/org/apache/tajo/QueryTestCaseBase.java +++ b/tajo-core/src/test/java/org/apache/tajo/QueryTestCaseBase.java @@ -29,8 +29,8 @@ import org.apache.tajo.annotation.Nullable; import org.apache.tajo.catalog.CatalogService; import org.apache.tajo.catalog.CatalogUtil; import org.apache.tajo.catalog.TableDesc; -import org.apache.tajo.cli.ParsedResult; -import org.apache.tajo.cli.SimpleParser; +import org.apache.tajo.cli.tsql.ParsedResult; +import org.apache.tajo.cli.tsql.SimpleParser; import org.apache.tajo.client.TajoClient; import org.apache.tajo.client.TajoClientImpl; import org.apache.tajo.conf.TajoConf; http://git-wip-us.apache.org/repos/asf/tajo/blob/c59baa3a/tajo-core/src/test/java/org/apache/tajo/cli/TestDefaultCliOutputFormatter.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/test/java/org/apache/tajo/cli/TestDefaultCliOutputFormatter.java b/tajo-core/src/test/java/org/apache/tajo/cli/TestDefaultCliOutputFormatter.java deleted file mode 100644 index bd2b4e1..0000000 --- a/tajo-core/src/test/java/org/apache/tajo/cli/TestDefaultCliOutputFormatter.java +++ /dev/null @@ -1,178 +0,0 @@ -/** - * 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.cli; - -import org.apache.hadoop.fs.Path; -import org.apache.tajo.TajoTestingCluster; -import org.apache.tajo.TpchTestBase; -import org.apache.tajo.catalog.TableDesc; -import org.apache.tajo.catalog.statistics.TableStats; -import org.apache.tajo.common.TajoDataTypes; -import org.apache.tajo.conf.TajoConf; -import org.apache.tajo.datum.Float8Datum; -import org.apache.tajo.datum.Int4Datum; -import org.apache.tajo.datum.TextDatum; -import org.apache.tajo.jdbc.MetaDataTuple; -import org.apache.tajo.jdbc.TajoMetaDataResultSet; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import java.io.ByteArrayOutputStream; -import java.io.PrintWriter; -import java.io.StringWriter; -import java.net.URL; -import java.sql.ResultSet; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -public class TestDefaultCliOutputFormatter { - protected static final TpchTestBase testBase; - protected static final TajoTestingCluster cluster; - - /** the base path of result directories */ - protected static final Path resultBasePath; - static { - testBase = TpchTestBase.getInstance(); - cluster = testBase.getTestingCluster(); - URL resultBaseURL = ClassLoader.getSystemResource("results"); - resultBasePath = new Path(resultBaseURL.toString()); - } - - private TajoConf conf; - private TajoCli tajoCli; - private TajoCli.TajoCliContext cliContext; - - @Before - public void setUp() throws Exception { - conf = cluster.getConfiguration(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - tajoCli = new TajoCli(conf, new String[]{}, System.in, out); - cliContext = tajoCli.getContext(); - } - - @After - public void tearDown() { - if (tajoCli != null) { - tajoCli.close(); - } - } - - - @Test - public void testParseErrorMessage() { - String message = "java.sql.SQLException: ERROR: no such a table: table1"; - assertEquals("ERROR: no such a table: table1", DefaultTajoCliOutputFormatter.parseErrorMessage(message)); - - String multiLineMessage = - "ERROR: java.sql.SQLException: ERROR: no such a table: table1\n" + - "com.google.protobuf.ServiceException: java.sql.SQLException: ERROR: no such a table: table1\n" + - "\tat org.apache.tajo.rpc.ServerCallable.withRetries(ServerCallable.java:107)\n" + - "\tat org.apache.tajo.client.TajoClient.getTableDesc(TajoClient.java:777)\n" + - "\tat org.apache.tajo.cli.DescTableCommand.invoke(DescTableCommand.java:43)\n" + - "\tat org.apache.tajo.cli.TajoCli.executeMetaCommand(TajoCli.java:300)\n" + - "\tat org.apache.tajo.cli.TajoCli.executeParsedResults(TajoCli.java:280)\n" + - "\tat org.apache.tajo.cli.TajoCli.runShell(TajoCli.java:271)\n" + - "\tat org.apache.tajo.cli.TajoCli.main(TajoCli.java:420)\n" + - "Caused by: java.sql.SQLException: ERROR: no such a table: table1\n" + - "\tat org.apache.tajo.client.TajoClient$22.call(TajoClient.java:791)\n" + - "\tat org.apache.tajo.client.TajoClient$22.call(TajoClient.java:778)\n" + - "\tat org.apache.tajo.rpc.ServerCallable.withRetries(ServerCallable.java:97)\n" + - "\t... 6 more"; - - assertEquals("ERROR: no such a table: table1", DefaultTajoCliOutputFormatter.parseErrorMessage(multiLineMessage)); - } - - @Test - public void testPrintResultInsertStatement() throws Exception { - - - DefaultTajoCliOutputFormatter outputFormatter = new DefaultTajoCliOutputFormatter(); - outputFormatter.init(cliContext); - - float responseTime = 10.1f; - long numBytes = 102; - long numRows = 30; - - TableDesc tableDesc = new TableDesc(); - TableStats stats = new TableStats(); - stats.setNumBytes(102); - stats.setNumRows(numRows); - tableDesc.setStats(stats); - - StringWriter stringWriter = new StringWriter(); - PrintWriter writer = new PrintWriter(stringWriter); - outputFormatter.printResult(writer, null, tableDesc, responseTime, null); - - String expectedOutput = "(" + numRows + " rows, " + responseTime + " sec, " + numBytes + " B inserted)\n"; - assertEquals(expectedOutput, stringWriter.toString()); - } - - @Test - public void testPrintResultSelectStatement() throws Exception { - DefaultTajoCliOutputFormatter outputFormatter = new DefaultTajoCliOutputFormatter(); - outputFormatter.init(cliContext); - - float responseTime = 10.1f; - long numBytes = 102; - long numRows = 30; - - TableDesc tableDesc = new TableDesc(); - TableStats stats = new TableStats(); - stats.setNumBytes(102); - stats.setNumRows(numRows); - tableDesc.setStats(stats); - - final List<MetaDataTuple> resultTables = new ArrayList<MetaDataTuple>(); - - String expectedOutput = "col1, col2, col3\n"; - expectedOutput += "-------------------------------\n"; - - String prefix = ""; - for (int i = 0; i < numRows; i++) { - MetaDataTuple tuple = new MetaDataTuple(3); - - int index = 0; - - tuple.put(index++, new TextDatum("row_" + i)); - tuple.put(index++, new Int4Datum(i)); - tuple.put(index++, new Float8Datum(i)); - - expectedOutput += prefix + "row_" + i + ", " + (new Int4Datum(i)) + ", " + (new Float8Datum(i)); - prefix = "\n"; - resultTables.add(tuple); - } - expectedOutput += "\n(" + numRows + " rows, " + responseTime + " sec, " + numBytes + " B selected)\n"; - - ResultSet resultSet = new TajoMetaDataResultSet( - Arrays.asList("col1", "col2", "col3"), - Arrays.asList(TajoDataTypes.Type.TEXT, TajoDataTypes.Type.INT4, TajoDataTypes.Type.FLOAT8), - resultTables); - - StringWriter stringWriter = new StringWriter(); - PrintWriter writer = new PrintWriter(stringWriter); - outputFormatter.printResult(writer, null, tableDesc, responseTime, resultSet); - - assertEquals(expectedOutput, stringWriter.toString()); - } - -} http://git-wip-us.apache.org/repos/asf/tajo/blob/c59baa3a/tajo-core/src/test/java/org/apache/tajo/cli/TestExecExternalShellCommand.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/test/java/org/apache/tajo/cli/TestExecExternalShellCommand.java b/tajo-core/src/test/java/org/apache/tajo/cli/TestExecExternalShellCommand.java deleted file mode 100644 index 8017dc6..0000000 --- a/tajo-core/src/test/java/org/apache/tajo/cli/TestExecExternalShellCommand.java +++ /dev/null @@ -1,44 +0,0 @@ -/** - * 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.cli; - -import org.apache.tajo.TpchTestBase; -import org.apache.tajo.conf.TajoConf; -import org.junit.Test; - -import java.io.ByteArrayOutputStream; - -import static org.junit.Assert.assertEquals; - -public class TestExecExternalShellCommand { - @Test - public void testCommand() throws Exception { - TajoConf tajoConf = TpchTestBase.getInstance().getTestingCluster().getConfiguration(); - - ByteArrayOutputStream out = new ByteArrayOutputStream(); - - TajoCli cli = new TajoCli(tajoConf, new String[]{}, null, out); - - cli.executeMetaCommand("\\! echo \"this is test\""); - String consoleResult = new String(out.toByteArray()); - assertEquals("this is test\n", consoleResult); - - assertEquals(-1, cli.executeMetaCommand("\\! error_command")); - } -} http://git-wip-us.apache.org/repos/asf/tajo/blob/c59baa3a/tajo-core/src/test/java/org/apache/tajo/cli/TestHdfsCommand.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/test/java/org/apache/tajo/cli/TestHdfsCommand.java b/tajo-core/src/test/java/org/apache/tajo/cli/TestHdfsCommand.java deleted file mode 100644 index b51835f..0000000 --- a/tajo-core/src/test/java/org/apache/tajo/cli/TestHdfsCommand.java +++ /dev/null @@ -1,46 +0,0 @@ -/** - * 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.cli; - -import org.apache.tajo.TpchTestBase; -import org.apache.tajo.conf.TajoConf; -import org.junit.Test; - -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; - -import static org.junit.Assert.assertEquals; - -public class TestHdfsCommand { - @Test - public void testHdfCommand() throws Exception { - TajoConf tajoConf = TpchTestBase.getInstance().getTestingCluster().getConfiguration(); - - ByteArrayOutputStream out = new ByteArrayOutputStream(); - - System.setOut(new PrintStream(out)); - System.setErr(new PrintStream(out)); - TajoCli cli = new TajoCli(tajoConf, new String[]{}, null, out); - - cli.executeMetaCommand("\\dfs -test"); - String consoleResult = new String(out.toByteArray()); - assertEquals("-test: Not enough arguments: expected 1 but got 0\n" + - "Usage: hadoop fs [generic options] -test -[defsz] <path>\n", consoleResult); - } -} http://git-wip-us.apache.org/repos/asf/tajo/blob/c59baa3a/tajo-core/src/test/java/org/apache/tajo/cli/TestSimpleParser.java ---------------------------------------------------------------------- diff --git a/tajo-core/src/test/java/org/apache/tajo/cli/TestSimpleParser.java b/tajo-core/src/test/java/org/apache/tajo/cli/TestSimpleParser.java deleted file mode 100644 index 5b5057b..0000000 --- a/tajo-core/src/test/java/org/apache/tajo/cli/TestSimpleParser.java +++ /dev/null @@ -1,271 +0,0 @@ -/** - * 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.cli; - -import org.junit.Test; - -import java.util.List; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -public class TestSimpleParser { - - @Test - public final void testSpecialCases() throws InvalidStatementException { - List<ParsedResult> res1 = SimpleParser.parseScript(""); - assertEquals(0, res1.size()); - - List<ParsedResult> res2 = SimpleParser.parseScript("a"); - assertEquals(1, res2.size()); - - List<ParsedResult> res3 = SimpleParser.parseScript("?"); - assertEquals(0, res3.size()); - - List<ParsedResult> res4 = SimpleParser.parseScript("\\"); - assertEquals(1, res4.size()); - } - - @Test - public final void testMetaCommands() throws InvalidStatementException { - List<ParsedResult> res1 = SimpleParser.parseScript("\\d"); - assertEquals(1, res1.size()); - assertEquals(ParsedResult.StatementType.META, res1.get(0).getType()); - assertEquals("\\d", res1.get(0).getHistoryStatement()); - - List<ParsedResult> res2 = SimpleParser.parseScript("\\d;\\c;\\f;"); - assertEquals(3, res2.size()); - assertEquals(ParsedResult.StatementType.META, res2.get(0).getType()); - assertEquals("\\d", res2.get(0).getHistoryStatement()); - assertEquals(ParsedResult.StatementType.META, res2.get(1).getType()); - assertEquals("\\c", res2.get(1).getHistoryStatement()); - assertEquals(ParsedResult.StatementType.META, res2.get(2).getType()); - assertEquals("\\f", res2.get(2).getHistoryStatement()); - - List<ParsedResult> res3 = SimpleParser.parseScript("\n\t\t \\d;\n\\c;\t\t\\f ;"); - assertEquals(3, res3.size()); - assertEquals(ParsedResult.StatementType.META, res3.get(0).getType()); - assertEquals("\\d", res3.get(0).getHistoryStatement()); - assertEquals(ParsedResult.StatementType.META, res3.get(1).getType()); - assertEquals("\\c", res3.get(1).getHistoryStatement()); - assertEquals(ParsedResult.StatementType.META, res3.get(2).getType()); - assertEquals("\\f", res3.get(2).getHistoryStatement()); - - List<ParsedResult> res4 = SimpleParser.parseScript("\\\td;"); - assertEquals(1, res4.size()); - assertEquals("\\\td", res4.get(0).getHistoryStatement()); - } - - @Test - public final void testParseScript() throws InvalidStatementException { - List<ParsedResult> res1 = SimpleParser.parseScript("select * from test;"); - assertEquals(1, res1.size()); - assertEquals(ParsedResult.StatementType.STATEMENT, res1.get(0).getType()); - assertEquals("select * from test", res1.get(0).getStatement()); - assertEquals("select * from test", res1.get(0).getHistoryStatement()); - - List<ParsedResult> res2 = SimpleParser.parseScript("select * from test;"); - assertEquals(1, res2.size()); - assertEquals(ParsedResult.StatementType.STATEMENT, res2.get(0).getType()); - assertEquals("select * from test", res2.get(0).getStatement()); - assertEquals("select * from test", res2.get(0).getHistoryStatement()); - - List<ParsedResult> res3 = SimpleParser.parseScript("select * from test1;select * from test2;"); - assertEquals(2, res3.size()); - assertEquals(ParsedResult.StatementType.STATEMENT, res3.get(0).getType()); - assertEquals("select * from test1", res3.get(0).getStatement()); - assertEquals("select * from test1", res3.get(0).getHistoryStatement()); - assertEquals(ParsedResult.StatementType.STATEMENT, res3.get(1).getType()); - assertEquals("select * from test2", res3.get(1).getStatement()); - assertEquals("select * from test2", res3.get(1).getHistoryStatement()); - - List<ParsedResult> res4 = SimpleParser.parseScript("\t\t\n\rselect * from \ntest1;select * from test2\n;"); - assertEquals(2, res4.size()); - assertEquals(ParsedResult.StatementType.STATEMENT, res4.get(0).getType()); - assertEquals("select * from \ntest1", res4.get(0).getStatement()); - assertEquals("select * from test1", res4.get(0).getHistoryStatement()); - assertEquals(ParsedResult.StatementType.STATEMENT, res4.get(1).getType()); - assertEquals("select * from test2", res4.get(1).getStatement()); - assertEquals("select * from test2", res4.get(1).getHistoryStatement()); - - List<ParsedResult> res5 = - SimpleParser.parseScript("\t\t\n\rselect * from \ntest1;\\d test;select * from test2;\n\nselect 1;"); - assertEquals(4, res5.size()); - assertEquals(ParsedResult.StatementType.STATEMENT, res5.get(0).getType()); - assertEquals("select * from \ntest1", res5.get(0).getStatement()); - assertEquals("select * from test1", res5.get(0).getHistoryStatement()); - assertEquals(ParsedResult.StatementType.META, res5.get(1).getType()); - assertEquals("\\d test", res5.get(1).getStatement()); - assertEquals(ParsedResult.StatementType.STATEMENT, res5.get(2).getType()); - assertEquals("select * from test2", res5.get(2).getStatement()); - assertEquals("select * from test2", res5.get(2).getHistoryStatement()); - assertEquals(ParsedResult.StatementType.STATEMENT, res5.get(3).getType()); - assertEquals("select 1", res5.get(3).getStatement()); - assertEquals("select 1", res5.get(3).getHistoryStatement()); - - List<ParsedResult> res6 = - SimpleParser.parseScript("select * from \n--test1; select * from test2;\ntest3;"); - assertEquals(1, res6.size()); - assertEquals("select * from test3", res6.get(0).getHistoryStatement()); - assertEquals("select * from \n--test1; select * from test2;\ntest3", res6.get(0).getStatement()); - - List<ParsedResult> res7 = - SimpleParser.parseScript("select * from --test1; select * from test2;\ntest3;"); - assertEquals(1, res7.size()); - assertEquals("select * from test3", res7.get(0).getHistoryStatement()); - assertEquals("select * from --test1; select * from test2;\ntest3", res7.get(0).getStatement()); - - List<ParsedResult> res8 = SimpleParser.parseScript("\\d test\nselect * \n--from test1;\nfrom test2;\\d test2;"); - assertEquals(3, res8.size()); - assertEquals(ParsedResult.StatementType.META, res8.get(0).getType()); - assertEquals("\\d test", res8.get(0).getStatement()); - assertEquals("\\d test", res8.get(0).getHistoryStatement()); - assertEquals(ParsedResult.StatementType.STATEMENT, res8.get(1).getType()); - assertEquals("select * \n--from test1;\nfrom test2", res8.get(1).getStatement()); - assertEquals("select * from test2", res8.get(1).getHistoryStatement()); - assertEquals(ParsedResult.StatementType.META, res8.get(2).getType()); - assertEquals("\\d test2", res8.get(2).getStatement()); - assertEquals("\\d test2", res8.get(2).getHistoryStatement()); - } - - @Test - public final void testParseLines() throws InvalidStatementException { - SimpleParser simpleParser = new SimpleParser(); - List<ParsedResult> res1 = null; - - res1 = simpleParser.parseLines("select * from test1; select * from test2;"); - assertEquals(2, res1.size()); - assertEquals("select * from test1", res1.get(0).getStatement()); - assertEquals("select * from test2", res1.get(1).getStatement()); - assertEquals("select * from test1", res1.get(0).getHistoryStatement()); - assertEquals("select * from test2", res1.get(1).getHistoryStatement()); - - simpleParser = new SimpleParser(); - res1 = simpleParser.parseLines("select * from "); - assertEquals(0, res1.size()); - res1 = simpleParser.parseLines("test1; select * from test2;"); - assertEquals(2, res1.size()); - assertEquals("select * from \ntest1", res1.get(0).getStatement()); - assertEquals("select * from test2", res1.get(1).getStatement()); - assertEquals("select * from test1", res1.get(0).getHistoryStatement()); - assertEquals("select * from test2", res1.get(1).getHistoryStatement()); - - // select * from - // --test1; select * from test2; - // test3; - simpleParser = new SimpleParser(); - res1 = simpleParser.parseLines("select * from "); - assertEquals(0, res1.size()); - res1 = simpleParser.parseLines("--test1; select * from test2;"); - assertEquals(0, res1.size()); - res1 = simpleParser.parseLines("test3;"); - assertEquals(1, res1.size()); - assertEquals("select * from test3", res1.get(0).getHistoryStatement()); - assertEquals("select * from \n--test1; select * from test2;\ntest3", res1.get(0).getStatement()); - - - // select * from - // test1 --select * from test2; - // where col1 = '123'; - simpleParser = new SimpleParser(); - res1 = simpleParser.parseLines("select * from "); - assertEquals(0, res1.size()); - res1 = simpleParser.parseLines("test1 --select * from test2;"); - assertEquals(0, res1.size()); - res1 = simpleParser.parseLines("where col1 = '123';"); - assertEquals(1, res1.size()); - assertEquals("select * from test1 where col1 = '123'", res1.get(0).getHistoryStatement()); - assertEquals("select * from \ntest1 --select * from test2;\nwhere col1 = '123'", res1.get(0).getStatement()); - } - - @Test - public final void testQuoted() throws InvalidStatementException { - List<ParsedResult> res1 = SimpleParser.parseScript("select '\n;' from test;"); - assertEquals(1, res1.size()); - assertEquals(ParsedResult.StatementType.STATEMENT, res1.get(0).getType()); - assertEquals("select '\n;' from test", res1.get(0).getHistoryStatement()); - assertEquals("select '\n;' from test", res1.get(0).getStatement()); - - List<ParsedResult> res2 = SimpleParser.parseScript("select 'abc\nbbc\nddf' from test;"); - assertEquals(1, res2.size()); - assertEquals(ParsedResult.StatementType.STATEMENT, res2.get(0).getType()); - assertEquals("select 'abc\nbbc\nddf' from test", res2.get(0).getHistoryStatement()); - assertEquals("select 'abc\nbbc\nddf' from test", res2.get(0).getStatement()); - - List<ParsedResult> res3 = SimpleParser.parseScript("select '--test', \n'--test2' from test"); - assertEquals(1, res3.size()); - assertEquals(ParsedResult.StatementType.STATEMENT, res3.get(0).getType()); - assertEquals("select '--test', '--test2' from test", res3.get(0).getHistoryStatement()); - assertEquals("select '--test', \n'--test2' from test", res3.get(0).getStatement()); - - try { - SimpleParser.parseScript("select 'abc"); - assertTrue(false); - } catch (InvalidStatementException is) { - assertTrue(true); - } - } - - @Test - public final void testParseLines1() throws InvalidStatementException { - String [] lines = { - "select abc, ", - "bbc from test" - }; - SimpleParser parser = new SimpleParser(); - List<ParsedResult> result1 = parser.parseLines(lines[0]); - assertEquals(0, result1.size()); - List<ParsedResult> result2 = parser.parseLines(lines[1]); - assertEquals(0, result2.size()); - List<ParsedResult> result3 = parser.EOF(); - assertEquals(1, result3.size()); - assertEquals(lines[0] + lines[1], result3.get(0).getHistoryStatement()); - assertEquals(lines[0] + "\n" + lines[1], result3.get(0).getStatement()); - } - - @Test - public final void testParseLines2() throws InvalidStatementException { - String [] lines = { - "select abc, '", - "bbc' from test; select * from test3;" - }; - SimpleParser parser = new SimpleParser(); - List<ParsedResult> result1 = parser.parseLines(lines[0]); - assertEquals(0, result1.size()); - List<ParsedResult> result2 = parser.parseLines(lines[1]); - assertEquals(2, result2.size()); - assertEquals("select abc, 'bbc' from test", result2.get(0).getHistoryStatement()); - assertEquals("select * from test3", result2.get(1).getHistoryStatement()); - } - - @Test - public final void testParseLines3() throws InvalidStatementException { - String [] lines = { - "select abc, 'bbc", - "' from test; select * from test3;" - }; - SimpleParser parser = new SimpleParser(); - List<ParsedResult> result1 = parser.parseLines(lines[0]); - assertEquals(0, result1.size()); - List<ParsedResult> result2 = parser.parseLines(lines[1]); - assertEquals(2, result2.size()); - assertEquals("select abc, 'bbc' from test", result2.get(0).getHistoryStatement()); - assertEquals("select * from test3", result2.get(1).getHistoryStatement()); - } -}
