mirror of
https://github.com/apache/sqoop.git
synced 2025-05-04 18:33:11 +08:00
SQOOP-502. Implement show connector command end-to-end.
(Bilung Lee via Jarek Jarcec Cecho) git-svn-id: https://svn.apache.org/repos/asf/sqoop/branches/sqoop2@1360620 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
498da2b799
commit
e19d63b625
@ -18,6 +18,7 @@
|
||||
package org.apache.sqoop.client.shell;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.cli.CommandLine;
|
||||
@ -25,6 +26,11 @@
|
||||
import org.apache.sqoop.client.core.Environment;
|
||||
import org.apache.sqoop.client.request.ConnectorRequest;
|
||||
import org.apache.sqoop.json.ConnectorBean;
|
||||
import org.apache.sqoop.model.MConnector;
|
||||
import org.apache.sqoop.model.MForm;
|
||||
import org.apache.sqoop.model.MInput;
|
||||
import org.apache.sqoop.model.MInputType;
|
||||
import org.apache.sqoop.model.MStringInput;
|
||||
import org.codehaus.groovy.tools.shell.IO;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
@ -79,36 +85,62 @@ private void showConnector(String cid) {
|
||||
}
|
||||
ConnectorBean connectorBean =
|
||||
conntectorRequest.doGet(Environment.getServerUrl(), cid);
|
||||
long[] ids = connectorBean.getIds();
|
||||
String[] names = connectorBean.getNames();
|
||||
String[] classes = connectorBean.getClasses();
|
||||
MConnector[] connectors = connectorBean.getConnectos();
|
||||
|
||||
if (cid == null) {
|
||||
io.out.println("@|bold Metadata for all connectors:|@");
|
||||
int size = ids.length;
|
||||
for (int i = 0; i < size; i++) {
|
||||
io.out.print("Connector ");
|
||||
io.out.print(i+1);
|
||||
io.out.println(":");
|
||||
io.out.println("@|bold " + connectors.length + " connector(s) to show: |@");
|
||||
for (int i = 0; i < connectors.length; i++) {
|
||||
MConnector connector = connectors[i];
|
||||
|
||||
io.out.print(" Id: ");
|
||||
io.out.println(ids[i]);
|
||||
io.out.print(" Name: ");
|
||||
io.out.println(names[i]);
|
||||
io.out.print(" Class: ");
|
||||
io.out.println(classes[i]);
|
||||
}
|
||||
io.out.print("Connector with id ");
|
||||
io.out.print(connector.getPersistenceId());
|
||||
io.out.println(":");
|
||||
|
||||
} else {
|
||||
io.out.println("@|bold Metadata for the connector:|@");
|
||||
io.out.print(" Id: ");
|
||||
io.out.println(ids[0]);
|
||||
io.out.print(" Name: ");
|
||||
io.out.println(names[0]);
|
||||
io.out.println(connector.getUniqueName());
|
||||
io.out.print(" Class: ");
|
||||
io.out.println(classes[0]);
|
||||
io.out.println(connector.getClassName());
|
||||
|
||||
displayForms(connector.getConnectionForms(), "Connection");
|
||||
displayForms(connector.getJobForms(), "Job");
|
||||
}
|
||||
|
||||
io.out.println();
|
||||
}
|
||||
|
||||
private void displayForms(List<MForm> forms, String type) {
|
||||
Iterator<MForm> fiter = forms.iterator();
|
||||
int findx = 1;
|
||||
while (fiter.hasNext()) {
|
||||
io.out.print(" ");
|
||||
io.out.print(type);
|
||||
io.out.print(" form ");
|
||||
io.out.print(findx++);
|
||||
io.out.println(":");
|
||||
|
||||
MForm form = fiter.next();
|
||||
io.out.print(" Name: ");
|
||||
io.out.println(form.getName());
|
||||
|
||||
List<MInput<?>> inputs = form.getInputs();
|
||||
Iterator<MInput<?>> iiter = inputs.iterator();
|
||||
int iindx = 1;
|
||||
while (iiter.hasNext()) {
|
||||
io.out.print(" Input ");
|
||||
io.out.print(iindx++);
|
||||
io.out.println(":");
|
||||
|
||||
MInput<?> input = iiter.next();
|
||||
io.out.print(" Name: ");
|
||||
io.out.println(input.getName());
|
||||
io.out.print(" Type: ");
|
||||
io.out.println(input.getType());
|
||||
if (input.getType() == MInputType.STRING) {
|
||||
io.out.print(" Mask: ");
|
||||
io.out.println(((MStringInput)input).isMasked());
|
||||
io.out.print(" Size: ");
|
||||
io.out.println(((MStringInput)input).getMaxLength());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -94,17 +94,17 @@ public Object execute(List<String> args) {
|
||||
private void showServer(boolean host, boolean port, boolean webapp,
|
||||
boolean version) {
|
||||
if (host) {
|
||||
io.out.print("@|bold Server host:|@ ");
|
||||
io.out.print("@|bold Server host:|@");
|
||||
io.out.println(Environment.getServerHost());
|
||||
}
|
||||
|
||||
if (port) {
|
||||
io.out.print("@|bold Server port:|@ ");
|
||||
io.out.print("@|bold Server port:|@");
|
||||
io.out.println(Environment.getServerPort());
|
||||
}
|
||||
|
||||
if (webapp) {
|
||||
io.out.print("@|bold Server webapp:|@ ");
|
||||
io.out.print("@|bold Server webapp:|@");
|
||||
io.out.println(Environment.getServerWebapp());
|
||||
}
|
||||
|
||||
|
@ -17,87 +17,175 @@
|
||||
*/
|
||||
package org.apache.sqoop.json;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.sqoop.model.MConnector;
|
||||
import org.apache.sqoop.model.MForm;
|
||||
import org.apache.sqoop.model.MFormType;
|
||||
import org.apache.sqoop.model.MInput;
|
||||
import org.apache.sqoop.model.MInputType;
|
||||
import org.apache.sqoop.model.MMapInput;
|
||||
import org.apache.sqoop.model.MStringInput;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
public class ConnectorBean implements JsonBean {
|
||||
|
||||
public static final String IDS = "ids";
|
||||
public static final String NAMES = "names";
|
||||
public static final String CLASSES = "classes";
|
||||
public static final String ID = "id";
|
||||
public static final String NAME = "name";
|
||||
public static final String CLASS = "class";
|
||||
public static final String CON_FORMS = "con_forms";
|
||||
public static final String JOB_FORMS = "job_forms";
|
||||
|
||||
private long[] ids;
|
||||
private String[] names;
|
||||
private String[] classes;
|
||||
public static final String FORM_NAME = "name";
|
||||
public static final String FORM_TYPE = "type";
|
||||
public static final String FORM_INPUTS = "inputs";
|
||||
public static final String FORM_INPUT_NAME = "name";
|
||||
public static final String FORM_INPUT_TYPE = "type";
|
||||
public static final String FORM_INPUT_MASK = "mask";
|
||||
public static final String FORM_INPUT_SIZE = "size";
|
||||
|
||||
private MConnector[] connectors;
|
||||
|
||||
// for "extract"
|
||||
public ConnectorBean(long[] ids, String[] names, String[] classes) {
|
||||
this.ids = new long[ids.length];
|
||||
System.arraycopy(ids, 0, this.ids, 0, ids.length);
|
||||
this.names = new String[names.length];
|
||||
System.arraycopy(names, 0, this.names, 0, names.length);
|
||||
this.classes = new String[classes.length];
|
||||
System.arraycopy(classes, 0, this.classes, 0, classes.length);
|
||||
public ConnectorBean(MConnector[] connectors) {
|
||||
this.connectors = new MConnector[connectors.length];
|
||||
System.arraycopy(connectors, 0, this.connectors, 0, connectors.length);
|
||||
}
|
||||
|
||||
// for "restore"
|
||||
public ConnectorBean() {
|
||||
}
|
||||
|
||||
public MConnector[] getConnectos() {
|
||||
return connectors;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public JSONObject extract() {
|
||||
JSONArray idArray = new JSONArray();
|
||||
JSONArray nameArray = new JSONArray();
|
||||
JSONArray classArray = new JSONArray();
|
||||
JSONArray conFormsArray = new JSONArray();
|
||||
JSONArray jobFormsArray = new JSONArray();
|
||||
|
||||
for (MConnector connector : connectors) {
|
||||
idArray.add(connector.getPersistenceId());
|
||||
nameArray.add(connector.getUniqueName());
|
||||
classArray.add(connector.getClassName());
|
||||
conFormsArray.add(extractForms(connector.getConnectionForms()));
|
||||
jobFormsArray.add(extractForms(connector.getJobForms()));
|
||||
}
|
||||
|
||||
JSONObject result = new JSONObject();
|
||||
JSONArray idsArray = new JSONArray();
|
||||
for (long id : ids) {
|
||||
idsArray.add(id);
|
||||
}
|
||||
result.put(IDS, idsArray);
|
||||
JSONArray namesArray = new JSONArray();
|
||||
for (String name : names) {
|
||||
namesArray.add(name);
|
||||
}
|
||||
result.put(NAMES, namesArray);
|
||||
JSONArray classesArray = new JSONArray();
|
||||
for (String clz : classes) {
|
||||
classesArray.add(clz);
|
||||
}
|
||||
result.put(CLASSES, classesArray);
|
||||
result.put(ID, idArray);
|
||||
result.put(NAME, nameArray);
|
||||
result.put(CLASS, classArray);
|
||||
result.put(CON_FORMS, conFormsArray);
|
||||
result.put(JOB_FORMS, jobFormsArray);
|
||||
return result;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private JSONArray extractForms(List<MForm> mForms) {
|
||||
JSONArray forms = new JSONArray();
|
||||
|
||||
for (MForm mForm : mForms) {
|
||||
forms.add(extractForm(mForm));
|
||||
}
|
||||
|
||||
return forms;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private JSONObject extractForm(MForm mForm) {
|
||||
JSONObject form = new JSONObject();
|
||||
form.put(FORM_NAME, mForm.getName());
|
||||
form.put(FORM_TYPE, MFormType.CONNECTION.toString());
|
||||
JSONArray mInputs = new JSONArray();
|
||||
form.put(FORM_INPUTS, mInputs);
|
||||
|
||||
for (MInput<?> mInput : mForm.getInputs()) {
|
||||
JSONObject input = new JSONObject();
|
||||
mInputs.add(input);
|
||||
|
||||
input.put(FORM_INPUT_NAME, mInput.getName());
|
||||
input.put(FORM_INPUT_TYPE, mInput.getType().toString());
|
||||
if (mInput.getType() == MInputType.STRING) {
|
||||
input.put(FORM_INPUT_MASK,
|
||||
((MStringInput)mInput).isMasked());
|
||||
input.put(FORM_INPUT_SIZE,
|
||||
((MStringInput)mInput).getMaxLength());
|
||||
}
|
||||
}
|
||||
|
||||
return form;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void restore(JSONObject jsonObject) {
|
||||
JSONArray idsArray = (JSONArray) jsonObject.get(IDS);
|
||||
int idsSize = idsArray.size();
|
||||
ids = new long[idsSize];
|
||||
for (int i = 0; i<idsSize; i++) {
|
||||
ids[i] = (Long) idsArray.get(i);
|
||||
}
|
||||
JSONArray namesArray = (JSONArray) jsonObject.get(NAMES);
|
||||
int namesSize = namesArray.size();
|
||||
names = new String[namesSize];
|
||||
for (int i = 0; i<namesSize; i++) {
|
||||
names[i] = (String) namesArray.get(i);
|
||||
}
|
||||
JSONArray classesArray = (JSONArray) jsonObject.get(CLASSES);
|
||||
int classeSize = classesArray.size();
|
||||
classes = new String[classeSize];
|
||||
for (int i = 0; i<classeSize; i++) {
|
||||
classes[i] = (String) classesArray.get(i);
|
||||
JSONArray idArray = (JSONArray) jsonObject.get(ID);
|
||||
JSONArray nameArray = (JSONArray) jsonObject.get(NAME);
|
||||
JSONArray classArray = (JSONArray) jsonObject.get(CLASS);
|
||||
JSONArray conFormsArray =
|
||||
(JSONArray) jsonObject.get(CON_FORMS);
|
||||
JSONArray jobFormsArray =
|
||||
(JSONArray) jsonObject.get(JOB_FORMS);
|
||||
|
||||
connectors = new MConnector[idArray.size()];
|
||||
for (int i = 0; i < connectors.length; i++) {
|
||||
long persistenceId = (Long) idArray.get(i);
|
||||
String uniqueName = (String) nameArray.get(i);
|
||||
String className = (String) classArray.get(i);
|
||||
|
||||
List<MForm> conMForms = restoreForms((JSONArray) conFormsArray.get(i));
|
||||
List<MForm> jobMForms = restoreForms((JSONArray) jobFormsArray.get(i));
|
||||
MConnector connector = new MConnector(uniqueName, className,
|
||||
conMForms, jobMForms);
|
||||
connector.setPersistenceId(persistenceId);
|
||||
connectors[i] = connector;
|
||||
}
|
||||
}
|
||||
|
||||
public long[] getIds() {
|
||||
return this.ids;
|
||||
private List<MForm> restoreForms(JSONArray forms) {
|
||||
List<MForm> mForms = new ArrayList<MForm>();
|
||||
|
||||
for (int i = 0; i < forms.size(); i++) {
|
||||
mForms.add(restoreForm((JSONObject) forms.get(i)));
|
||||
}
|
||||
|
||||
return mForms;
|
||||
}
|
||||
|
||||
public String[] getNames() {
|
||||
return this.names;
|
||||
}
|
||||
private MForm restoreForm(JSONObject form) {
|
||||
JSONArray inputs = (JSONArray) form.get(FORM_INPUTS);
|
||||
|
||||
public String[] getClasses() {
|
||||
return this.classes;
|
||||
}
|
||||
List<MInput<?>> mInputs = new ArrayList<MInput<?>>();
|
||||
for (int i = 0; i < inputs.size(); i++) {
|
||||
JSONObject input = (JSONObject) inputs.get(i);
|
||||
MInputType type =
|
||||
MInputType.valueOf((String) input.get(FORM_INPUT_TYPE));
|
||||
switch (type) {
|
||||
case STRING: {
|
||||
String name = (String) input.get(FORM_INPUT_NAME);
|
||||
boolean mask = (Boolean) input.get(FORM_INPUT_MASK);
|
||||
long size = (Long) input.get(FORM_INPUT_SIZE);
|
||||
MInput<String> mInput = new MStringInput(name, mask, (short)size);
|
||||
mInputs.add(mInput);
|
||||
break;
|
||||
}
|
||||
case MAP: {
|
||||
String name = (String) input.get(FORM_INPUT_NAME);
|
||||
MInput<Map<String, String>> mInput = new MMapInput(name);
|
||||
mInputs.add(mInput);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new MForm((String) form.get(FORM_NAME), mInputs);
|
||||
}
|
||||
}
|
||||
|
@ -58,8 +58,8 @@ public enum ConnectorError implements ErrorCode {
|
||||
+ "previously registered connector; or the connector metadata has "
|
||||
+ "changed since it was registered previously."),
|
||||
|
||||
/** A connector is found with an invalid id. */
|
||||
CONN_0010("A connector is found with an invalid id");
|
||||
/** A connector is not assigned with a valid id yet. */
|
||||
CONN_0010("A connector is not assigned with a valid id yet");
|
||||
|
||||
private final String message;
|
||||
|
||||
|
@ -44,12 +44,18 @@ public class ConnectorManager {
|
||||
private static Map<String, ConnectorHandler> handlerMap =
|
||||
new HashMap<String, ConnectorHandler>();
|
||||
|
||||
public static ConnectorHandler[] getHandlers() {
|
||||
return handlerMap.values().toArray(new ConnectorHandler[]{});
|
||||
public static MConnector[] getConnectors() {
|
||||
MConnector[] connectors = new MConnector[handlerMap.size()];
|
||||
int indx = 0;
|
||||
for (ConnectorHandler handler : handlerMap.values()) {
|
||||
connectors[indx++] = handler.getMetadata();
|
||||
}
|
||||
return connectors;
|
||||
}
|
||||
|
||||
public static ConnectorHandler getHandler(long connectorId) {
|
||||
return handlerMap.get(nameMap.get(connectorId));
|
||||
public static MConnector getConnector(long connectorId) {
|
||||
ConnectorHandler handler = handlerMap.get(nameMap.get(connectorId));
|
||||
return handler.getMetadata();
|
||||
}
|
||||
|
||||
public static synchronized void initialize() {
|
||||
|
@ -23,6 +23,7 @@
|
||||
import org.apache.sqoop.connector.ConnectorManager;
|
||||
import org.apache.sqoop.json.JsonBean;
|
||||
import org.apache.sqoop.json.ConnectorBean;
|
||||
import org.apache.sqoop.model.MConnector;
|
||||
import org.apache.sqoop.server.RequestContext;
|
||||
import org.apache.sqoop.server.RequestHandler;
|
||||
|
||||
@ -34,9 +35,6 @@ public class ConnectorRequestHandler implements RequestHandler {
|
||||
/** The API version supported by this server */
|
||||
public static final String PROTOCOL_V1 = "1";
|
||||
|
||||
|
||||
private ConnectorBean connectors;
|
||||
|
||||
public ConnectorRequestHandler() {
|
||||
LOG.info("ConnectorRequestHandler initialized");
|
||||
}
|
||||
@ -44,7 +42,7 @@ public ConnectorRequestHandler() {
|
||||
|
||||
@Override
|
||||
public JsonBean handleEvent(RequestContext ctx) throws SqoopException {
|
||||
ConnectorBean connectorBean;
|
||||
MConnector[] connectors;
|
||||
|
||||
String uri = ctx.getRequest().getRequestURI();
|
||||
int slash = uri.lastIndexOf("/");
|
||||
@ -52,30 +50,14 @@ public JsonBean handleEvent(RequestContext ctx) throws SqoopException {
|
||||
LOG.info("ConnectorRequestHandler handles cid: " + cid);
|
||||
if (cid.equals("all")) {
|
||||
// display all connectors
|
||||
if (connectors == null) {
|
||||
ConnectorHandler[] handlers = ConnectorManager.getHandlers();
|
||||
long[] ids = new long[handlers.length];
|
||||
String[] names = new String[handlers.length];
|
||||
String[] classes = new String[handlers.length];
|
||||
for (int i = 0; i < handlers.length; i++) {
|
||||
ids[i] = handlers[i].getMetadata().getPersistenceId();
|
||||
names[i] = handlers[i].getUniqueName();
|
||||
classes[i] = handlers[i].getConnectorClassName();
|
||||
}
|
||||
connectors = new ConnectorBean(ids, names, classes);
|
||||
}
|
||||
connectorBean = connectors;
|
||||
connectors = ConnectorManager.getConnectors();
|
||||
|
||||
} else {
|
||||
// display one connector
|
||||
ConnectorHandler handler =
|
||||
ConnectorManager.getHandler(Long.parseLong(cid));
|
||||
long[] ids = new long[] { handler.getMetadata().getPersistenceId() };
|
||||
String[] names = new String[] { handler.getUniqueName() };
|
||||
String[] classes = new String[] { handler.getConnectorClassName() };
|
||||
connectorBean = new ConnectorBean(ids, names, classes);
|
||||
connectors = new MConnector[] {
|
||||
ConnectorManager.getConnector(Long.parseLong(cid)) };
|
||||
}
|
||||
|
||||
return connectorBean;
|
||||
return new ConnectorBean(connectors);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user