in odps-console-dship/src/main/java/com/aliyun/odps/ship/common/OptionsBuilder.java [590:672]
private static void processArgs(String[] remains, CommandType type)
throws ODPSConsoleException {
if (remains.length == 3) {
String path = type == CommandType.download ? remains[2] : remains[1];
String desc = type == CommandType.download ? remains[1] : remains[2];
String project = null;
String schema = null;
String table;
String partition = null;
if (type == CommandType.download && desc.startsWith(Constants.TUNNEL_INSTANCE_PREFIX)) {
processInstanceArgs(desc);
} else {
// Description looks like <table name>/<partition spec>
// or <project name>.<table name>/<partition spec>
// or <schema name>.<table name>/<partition spec>
// or <project name>.<schema name>.<table name>/<partition spec>
String[] descSplit = desc.split("/");
Coordinate coordinate = Coordinate.getCoordinateABC(descSplit[0]);
coordinate.interpretByCtx(DshipContext.INSTANCE.getExecutionContext());
project = coordinate.getProjectName();
schema = coordinate.getSchemaName();
table = coordinate.getObjectName();
if (descSplit.length == 2) {
partition = new PartitionSpec(descSplit[1]).toString();
} else if (descSplit.length > 2) {
throw new IllegalArgumentException("Invalid table identifier: " + desc);
}
Odps odps = OdpsConnectionFactory.createOdps(DshipContext.INSTANCE.getExecutionContext());
odps.setCurrentSchema(schema);
setContextValue(Constants.TABLE_PROJECT, project);
setContextValue(Constants.SCHEMA, schema);
// Handle view
if (odps.tables().get(project, schema, table).isVirtualView()) {
// Cannot specify a partition of a view
if (partition != null) {
throw new IllegalArgumentException("Invalid view identifier: " + desc);
}
// Cannot upload to a view
if (type != CommandType.download) {
throw new IllegalArgumentException("Invalid operation: upload to a view");
}
try {
// flag=true, select * from a.b.c
// flag=false, select * from a.c
//todo schema check
String tableCoordinate = project + "." + schema + "." + table;
if (DshipContext.INSTANCE.getExecutionContext().isProjectMode()) {
tableCoordinate = project + "." + table;
}
String query = String.format("SELECT * FROM %s;", tableCoordinate);
Map<String, String> hints = new HashMap<>();
hints.put(ODPS_DEFAULT_SCHEMA, DshipContext.INSTANCE.get(Constants.SCHEMA));
hints.put(ODPSConsoleConstants.ODPS_NAMESPACE_SCHEMA,
String.valueOf(
DshipContext.INSTANCE.getExecutionContext().isOdpsNamespaceSchema()));
if (SetCommand.setMap.containsKey("odps.sql.allow.namespace.schema")) {
hints.put("odps.sql.allow.namespace.schema",
String.valueOf(
DshipContext.INSTANCE.getExecutionContext().isOdpsNamespaceSchema()));
}
hints.put(ODPSConsoleConstants.ODPS_NAMESPACE_SCHEMA,
String.valueOf(
DshipContext.INSTANCE.getExecutionContext().isOdpsNamespaceSchema()));
Instance instance = SQLTask.run(odps, odps.getDefaultProject(), query, hints, null);
instance.waitForSuccess();
processInstanceArgs(Constants.TUNNEL_INSTANCE_PREFIX + instance.getId());
} catch (OdpsException e) {
throw new ODPSConsoleException("Read from view failed", e);
}
} else {
setContextValue(Constants.TABLE, table);
setContextValue(Constants.PARTITION_SPEC, partition);
}
}
setContextValue(Constants.RESUME_PATH, FileUtil.expandUserHomeInPath(path));
} else {
throw new IllegalArgumentException("Unrecognized command\nType 'tunnel help "
+ type.name() + "' for usage.");
}
}