5
0
mirror of https://github.com/apache/sqoop.git synced 2025-05-10 01:51:00 +08:00

SQOOP-643: Implement simple listings for connector and job objects rek

(Jarcec Cecho via Cheolsoo Park)
This commit is contained in:
Cheolsoo Park 2013-01-22 16:50:39 -08:00
parent 5be8eb6808
commit 03408d573e
6 changed files with 247 additions and 21 deletions

View File

@ -300,6 +300,20 @@ public class Constants {
"update.job";
public static final String RES_UPDATE_JOB_SUCCESSFUL =
"update.job_successful";
public static final String RES_TABLE_HEADER_ID =
"table.header.id";
public static final String RES_TABLE_HEADER_NAME =
"table.header.name";
public static final String RES_TABLE_HEADER_VERSION =
"table.header.version";
public static final String RES_TABLE_HEADER_CLASS =
"table.header.class";
public static final String RES_TABLE_HEADER_TYPE =
"table.header.type";
public static final String RES_TABLE_HEADER_CONNECTOR =
"table.header.connector";
private Constants() {
// Instantiation is prohibited
}

View File

@ -20,6 +20,7 @@
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.OptionBuilder;
import org.apache.sqoop.client.core.Constants;
import org.apache.sqoop.client.utils.TableDisplayer;
import org.apache.sqoop.json.ConnectionBean;
import org.apache.sqoop.model.MConnection;
import org.codehaus.groovy.tools.shell.IO;
@ -27,6 +28,7 @@
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.MessageFormat;
import java.util.LinkedList;
import java.util.List;
import static org.apache.sqoop.client.utils.FormDisplayer.*;
@ -63,23 +65,40 @@ public void printHelp(PrintWriter out) {
}
public Object execute(List<String> args) {
if (args.size() == 1) {
printHelp(io.out);
io.out.println();
return null;
}
CommandLine line = parseOptions(this, 1, args);
if (line.hasOption(Constants.OPT_ALL)) {
showConnection(null);
} else if (line.hasOption(Constants.OPT_XID)) {
showConnection(line.getOptionValue(Constants.OPT_XID));
} else {
showSummary();
}
return null;
}
private void showSummary() {
ConnectionBean connectionBean = readConnection(null);
List<MConnection> connections = connectionBean.getConnections();
List<String> header = new LinkedList<String>();
header.add(getResource().getString(Constants.RES_TABLE_HEADER_ID));
header.add(getResource().getString(Constants.RES_TABLE_HEADER_NAME));
header.add(getResource().getString(Constants.RES_TABLE_HEADER_CONNECTOR));
List<String> ids = new LinkedList<String>();
List<String> names = new LinkedList<String>();
List<String> connectors = new LinkedList<String>();
for(MConnection connection : connections) {
ids.add(String.valueOf(connection.getPersistenceId()));
names.add(connection.getName());
connectors.add(String.valueOf(connection.getConnectorId()));
}
TableDisplayer.display(io, header, ids, names, connectors);
}
private void showConnection(String xid) {
ConnectionBean connectionBean = readConnection(xid);

View File

@ -19,6 +19,7 @@
import java.io.PrintWriter;
import java.text.MessageFormat;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
@ -27,6 +28,7 @@
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.sqoop.client.core.Constants;
import org.apache.sqoop.client.utils.TableDisplayer;
import org.apache.sqoop.json.ConnectorBean;
import org.apache.sqoop.model.MConnector;
import org.codehaus.groovy.tools.shell.IO;
@ -63,23 +65,43 @@ public void printHelp(PrintWriter out) {
}
public Object execute(List<String> args) {
if (args.size() == 1) {
printHelp(io.out);
io.out.println();
return null;
}
CommandLine line = parseOptions(this, 1, args);
if (line.hasOption(Constants.OPT_ALL)) {
showConnector(null);
} else if (line.hasOption(Constants.OPT_CID)) {
showConnector(line.getOptionValue(Constants.OPT_CID));
} else {
showSummary();
}
return null;
}
private void showSummary() {
ConnectorBean connectorBean = readConnector(null);
List<MConnector> connectors = connectorBean.getConnectors();
List<String> header = new LinkedList<String>();
header.add(getResource().getString(Constants.RES_TABLE_HEADER_ID));
header.add(getResource().getString(Constants.RES_TABLE_HEADER_NAME));
header.add(getResource().getString(Constants.RES_TABLE_HEADER_VERSION));
header.add(getResource().getString(Constants.RES_TABLE_HEADER_CLASS));
List<String> ids = new LinkedList<String>();
List<String> uniqueNames = new LinkedList<String>();
List<String> versions = new LinkedList<String>();
List<String> classes = new LinkedList<String>();
for(MConnector connector : connectors) {
ids.add(String.valueOf(connector.getPersistenceId()));
uniqueNames.add(connector.getUniqueName());
versions.add(connector.getVersion());
classes.add(connector.getClassName());
}
TableDisplayer.display(io, header, ids, uniqueNames, versions, classes);
}
private void showConnector(String cid) {
ConnectorBean connectorBean = readConnector(cid);
List<MConnector> connectors = connectorBean.getConnectors();

View File

@ -20,6 +20,7 @@
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.OptionBuilder;
import org.apache.sqoop.client.core.Constants;
import org.apache.sqoop.client.utils.TableDisplayer;
import org.apache.sqoop.json.JobBean;
import org.apache.sqoop.model.MJob;
import org.codehaus.groovy.tools.shell.IO;
@ -27,6 +28,7 @@
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.MessageFormat;
import java.util.LinkedList;
import java.util.List;
import static org.apache.sqoop.client.utils.FormDisplayer.*;
@ -61,23 +63,43 @@ public void printHelp(PrintWriter out) {
}
public Object execute(List<String> args) {
if (args.size() == 1) {
printHelp(io.out);
io.out.println();
return null;
}
CommandLine line = parseOptions(this, 1, args);
if (line.hasOption(Constants.OPT_ALL)) {
showJob(null);
} else if (line.hasOption(Constants.OPT_JID)) {
showJob(line.getOptionValue(Constants.OPT_JID));
} else {
showSummary();
}
return null;
}
private void showSummary() {
JobBean jobBean = readJob(null);
List<MJob> jobs = jobBean.getJobs();
List<String> header = new LinkedList<String>();
header.add(getResource().getString(Constants.RES_TABLE_HEADER_ID));
header.add(getResource().getString(Constants.RES_TABLE_HEADER_NAME));
header.add(getResource().getString(Constants.RES_TABLE_HEADER_TYPE));
header.add(getResource().getString(Constants.RES_TABLE_HEADER_CONNECTOR));
List<String> ids = new LinkedList<String>();
List<String> names = new LinkedList<String>();
List<String> types = new LinkedList<String>();
List<String> connectors = new LinkedList<String>();
for(MJob job : jobs) {
ids.add(String.valueOf(job.getPersistenceId()));
names.add(job.getName());
types.add(job.getType().toString());
connectors.add(String.valueOf(job.getConnectorId()));
}
TableDisplayer.display(io, header, ids, names, types, connectors);
}
private void showJob(String jid) {
JobBean jobBean = readJob(jid);

View File

@ -0,0 +1,141 @@
/**
* 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.sqoop.client.utils;
import org.apache.commons.lang.StringUtils;
import org.codehaus.groovy.tools.shell.IO;
import java.util.LinkedList;
import java.util.List;
/**
* Display table based data
*/
public class TableDisplayer {
/**
* Display given columns in nice table structure to given IO object.
*
* @param io Shell's IO object
* @param headers List of headers
* @param columns Array of columns
*/
public static void display(IO io, List<String> headers, List<String> ...columns) {
assert io != null;
assert headers != null;
assert columns != null;
assert headers.size() == columns.length;
// Count of columns
int columnCount = headers.size();
// List of all maximal widths of each column
List<Integer> widths = new LinkedList<Integer>();
for(int i = 0; i < columnCount; i++) {
widths.add(getMaximalWidth(headers.get(i), columns[i]));
}
// First line is border
drawLine(io, widths);
// Print out header (text is centralised)
io.out.print("| ");
for(int i = 0 ; i < columnCount; i++) {
io.out.print(StringUtils.center(headers.get(i), widths.get(i), ' '));
io.out.print((i == columnCount -1) ? " |" : " | ");
}
io.out.println();
// End up header by border
drawLine(io, widths);
// Number of rows in the table
int rows = getMaximalRows(columns);
// Print out each row
for(int row = 0 ; row < rows; row++) {
io.out.print("| ");
for(int i = 0 ; i < columnCount; i++) {
io.out.print(StringUtils.rightPad(columns[i].get(row), widths.get(i), ' '));
io.out.print((i == columnCount -1) ? " |" : " | ");
}
io.out.println();
}
// End table by final border
drawLine(io, widths);
}
/**
* Draw border line
*
* @param io Shell's associated IO object
* @param widths List of widths of each column
*/
private static void drawLine(IO io, List<Integer> widths) {
int last = widths.size() - 1;
io.out.print("+-");
for(int i = 0; i < widths.size(); i++) {
io.out.print(StringUtils.repeat("-", widths.get(i)));
io.out.print((i == last) ? "-+" : "-+-");
}
io.out.println();
}
/**
* Get maximal width for given column with it's associated header.
*
* @param header Associated header
* @param column All column values
* @return Maximal
*/
private static int getMaximalWidth(String header, List<String> column) {
int max = header.length();
for(String value : column) {
if(value.length() > max) {
max = value.length();
}
}
return max;
}
/**
* Get maximal number of rows available in the column list
*
* @param columns Array with all column values
* @return
*/
private static int getMaximalRows(List<String>... columns) {
int max = 0;
for(List<String> column : columns) {
if(column.size() > max) {
max = column.size();
}
}
return max;
}
private TableDisplayer() {
// Instantiation is prohibited
}
}

View File

@ -161,3 +161,11 @@ sqoop.prompt_shell_loadrc = Loading resource file {0}
sqoop.prompt_shell_loadedrc = Resource file loaded.
submission.usage = Usage: submission {0}
# Various Table headers
table.header.id = Id
table.header.name = Name
table.header.version = Version
table.header.class = Class
table.header.type = Type
table.header.connector = Connector