in odps-console-dship/src/main/java/com/aliyun/odps/ship/download/TunnelDownloadSession.java [90:149]
public TunnelDownloadSession(String tableName, PartitionSpec ps)
throws OdpsException, ODPSConsoleException, IOException {
String tableProject = DshipContext.INSTANCE.get(Constants.TABLE_PROJECT);
String schemaName = DshipContext.INSTANCE.get(Constants.SCHEMA);
Odps odps = OdpsConnectionFactory.createOdps(DshipContext.INSTANCE.getExecutionContext());
TableTunnel tunnel = new TableTunnel(odps);
tunnel.getConfig().setQuotaName(DshipContext.INSTANCE.get(Constants.QUOTA_NAME));
if (DshipContext.INSTANCE.get(Constants.TUNNEL_ENDPOINT) != null) {
tunnel.setEndpoint(DshipContext.INSTANCE.get(Constants.TUNNEL_ENDPOINT));
} else if (StringUtils.isNotEmpty(
DshipContext.INSTANCE.getExecutionContext().getTunnelEndpoint())) {
tunnel.setEndpoint(DshipContext.INSTANCE.getExecutionContext().getTunnelEndpoint());
}
if (tableProject == null) {
tableProject = odps.getDefaultProject();
}
TableTunnel.DownloadSessionBuilder
builder =
tunnel.buildDownloadSession(tableProject, tableName).setSchemaName(schemaName)
.setAsyncMode(true).setWaitAsyncBuild(false);
if (ps == null) {
tableDownload = builder.build();
} else {
tableDownload = builder.setPartitionSpec(ps).build();
}
sessionHistory = SessionHistoryManager.createSessionHistory(tableDownload.getId());
String rapInstanceId = tableDownload.getRAPInstanceId();
if (rapInstanceId != null) {
while (rapInstanceId.isEmpty()) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
// get status will trigger reload
TableTunnel.DownloadStatus status = tableDownload.getStatus();
rapInstanceId = tableDownload.getRAPInstanceId();
if (status != TableTunnel.DownloadStatus.INITIATING) {
break;
}
}
if (rapInstanceId.isEmpty()) {
throw new OdpsException(
"Create TunnelDownloadSession error, failed to create RAP instance. session id: "
+ tableDownload.getId());
}
Instance sqlInstance = odps.instances().get(rapInstanceId);
log("\nDue to the presence of row-level permission rules on current table, tunnel download requires run SQL task. Task info:");
log("ID = " + rapInstanceId);
log("Log view:\n" + odps.logview().generateLogView(sqlInstance, 7 * 24) + "\n");
sqlInstance.waitForSuccess();
}
builder.wait(tableDownload, 2, 2 * 60);
totalLines = tableDownload.getRecordCount();
schema = tableDownload.getSchema();
downloadId = tableDownload.getId();
initSelectColumns();
}