Skip to content

Commit 8eab133

Browse files
committed
prepare for 0.51.7-public
1 parent e58ee04 commit 8eab133

File tree

15 files changed

+583
-63
lines changed

15 files changed

+583
-63
lines changed

odps-sdk/odps-sdk-commons/src/main/java/com/aliyun/odps/type/NestedTypeInfo.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.aliyun.odps.type;
22

3+
34
/**
45
* @author dingxin ([email protected])
56
*/

odps-sdk/odps-sdk-core/src/main/java/com/aliyun/odps/Project.java

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.List;
3030
import java.util.Map;
3131
import java.util.Map.Entry;
32+
import java.util.concurrent.Callable;
3233

3334
import com.aliyun.odps.commons.transport.Headers;
3435
import com.aliyun.odps.commons.transport.Response;
@@ -91,7 +92,12 @@ public static enum ProjectType {
9192
/**
9293
* 映射到 Odps 的外部项目,例如 hive
9394
*/
94-
external
95+
external,
96+
97+
/**
98+
* 映射到 Odps 的外部项目 V2,后续将和 external 合并
99+
*/
100+
external_v2
95101
}
96102

97103
/**
@@ -428,12 +434,78 @@ public void reload() throws OdpsException {
428434

429435
properties = model.properties;
430436
clusters = model.clusters;
437+
438+
if (isExternalV2(model.name)) {
439+
model.type = ProjectType.external_v2.name();
440+
}
431441
} catch (Exception e) {
432442
throw new OdpsException("Can't bind xml to " + ProjectModel.class, e);
433443
}
434444
setLoaded(true);
435445
}
436446

447+
/**
448+
* judge whether external epv2
449+
*
450+
* @param projectName
451+
* @throws OdpsException
452+
*/
453+
private boolean isExternalV2(String projectName) throws OdpsException {
454+
if (projectName.equalsIgnoreCase("system_catalog")) {
455+
return false;
456+
}
457+
// get isEx from api-worker and put in cache
458+
String propertyStr = properties.get("external_project_properties");
459+
if (propertyStr != null) {
460+
//judge isExternalCatalogBound=true whether in external_project_properties of properties
461+
return propertyStr.contains("\"isExternalCatalogBound\":true");
462+
} else {
463+
return false;
464+
}
465+
}
466+
467+
/**
468+
* 首先检查当前项目是否为 epv2(但检查时可能会报错)。
469+
* 如果是 epv2,执行 ifEpv2。
470+
* 如果不是 epv2,或者检查时报错,则执行 ifNotEpv2。
471+
* 当执行 ifNotEpv2 时,如果报错且检查 project 时也报错,则将两个报错合成一个,抛出。
472+
*/
473+
public <T> T executeIfEpv2(Callable<T> ifEpv2, Callable<T> ifNotEpv2) throws OdpsException {
474+
Exception readProjectException = null;
475+
boolean isEpv2;
476+
try {
477+
isEpv2 = getType() == ProjectType.external_v2;
478+
} catch (Exception e) {
479+
readProjectException = e;
480+
isEpv2 = false;
481+
}
482+
try {
483+
if (isEpv2) {
484+
return ifEpv2.call();
485+
} else {
486+
return ifNotEpv2.call();
487+
}
488+
} catch (Exception e) {
489+
// 处理 ifNotEpv2 执行中的异常
490+
if (readProjectException != null) {
491+
// 如果项目检查时有异常,合成两个异常的消息抛出
492+
String errMsg = String.format(
493+
"%s, If any entities in the statement you execute are from external projects, the error may be caused by [%s]",
494+
e.getMessage(), readProjectException.getMessage());
495+
if (e instanceof NoSuchObjectException) {
496+
throw new NoSuchObjectException(errMsg, e);
497+
}
498+
throw new OdpsException(errMsg, e);
499+
} else {
500+
if (e instanceof OdpsException) {
501+
throw (OdpsException) e;
502+
} else {
503+
throw new OdpsException(e.getMessage(), e);
504+
}
505+
}
506+
}
507+
}
508+
437509
/**
438510
* 获取Project名称
439511
*
@@ -452,7 +524,6 @@ public ProjectType getType() throws OdpsException {
452524
if (model.type == null) {
453525
lazyLoad();
454526
}
455-
456527
if (model.type != null) {
457528
try {
458529
return ProjectType.valueOf(model.type.toLowerCase());

odps-sdk/odps-sdk-core/src/main/java/com/aliyun/odps/Projects.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public Project get() throws OdpsException {
9292
*
9393
* @param projectName {@link Project}名称
9494
* @return {@link Project}对象
95-
* @throws NoSuchObjectException Project不存在
95+
* @throws OdpsException No exception will throw, however for compatibility
9696
*/
9797
public Project get(String projectName) throws OdpsException {
9898
ProjectModel model = new ProjectModel();

odps-sdk/odps-sdk-core/src/main/java/com/aliyun/odps/Schemas.java

Lines changed: 79 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,28 @@
1919

2020
package com.aliyun.odps;
2121

22+
import static com.aliyun.odps.task.SQLTask.parseCsvRecord;
23+
24+
import java.io.InputStream;
2225
import java.util.ArrayList;
2326
import java.util.HashMap;
2427
import java.util.Iterator;
2528
import java.util.List;
2629
import java.util.Map;
30+
import java.util.Properties;
31+
import java.util.concurrent.atomic.AtomicReference;
2732

2833
import com.aliyun.odps.Schema.SchemaModel;
2934
import com.aliyun.odps.commons.transport.Headers;
35+
import com.aliyun.odps.data.Record;
3036
import com.aliyun.odps.rest.ResourceBuilder;
3137
import com.aliyun.odps.rest.RestClient;
3238
import com.aliyun.odps.rest.SimpleXmlUtils;
3339
import com.aliyun.odps.simpleframework.xml.Element;
3440
import com.aliyun.odps.simpleframework.xml.ElementList;
3541
import com.aliyun.odps.simpleframework.xml.Root;
3642
import com.aliyun.odps.simpleframework.xml.convert.Convert;
43+
import com.aliyun.odps.task.SQLTask;
3744
import com.aliyun.odps.utils.ExceptionUtils;
3845
import com.aliyun.odps.utils.StringUtils;
3946

@@ -289,9 +296,62 @@ public String getMarker() {
289296
return params.get("marker");
290297
}
291298

299+
300+
/**
301+
* getExternalProjectSchemaList , special for EPV2
302+
* @param projectName
303+
* @return
304+
* @throws OdpsException
305+
*/
306+
public List<Schema> getExternalProjectSchemaList(String projectName) throws OdpsException {
307+
//Temporary code for openlake Demo(YunQi big conference):
308+
// Long-term this code block should be converted to SQL for both internal and external projects,
309+
// and should not have any setting flag, except for opening the 3 layer model based on project attributes(user can choose use 2 or 3tier for 3 layer Project ).
310+
//warning filter not support in EPV2
311+
Map<String, String> queryHint = new HashMap<>();
312+
InputStream is = null;
313+
try {
314+
is = Schemas.class.getResourceAsStream("/com/aliyun/odps/core/base.conf");
315+
Properties properties = new Properties();
316+
properties.load(is);
317+
String majorVersion = properties.getProperty("epv2flighting");
318+
if (majorVersion != null && !majorVersion.isEmpty() && !"default".equals(majorVersion)) {
319+
queryHint.put("odps.task.major.version", majorVersion);
320+
}
321+
322+
} catch (Exception e) {
323+
} finally {
324+
org.apache.commons.io.IOUtils.closeQuietly(is);
325+
}
326+
queryHint.put("odps.sql.select.output.format", "csv");
327+
Instance i = SQLTask.run(odps, projectName, "show schemas;", queryHint, null);
328+
i.waitForSuccess();
329+
Instance.InstanceResultModel.TaskResult taskResult = i.getRawTaskResults().get(0);
330+
Instance.TaskStatus.Status taskStatus =
331+
Instance.TaskStatus.Status.valueOf(taskResult.status.toUpperCase());
332+
if (taskStatus != Instance.TaskStatus.Status.SUCCESS) {
333+
throw new RuntimeException("show schemas failed. instanceId:" + i.getId());
334+
}
335+
336+
String result = taskResult.result.getString();
337+
List<Record> schemalist = parseCsvRecord(result);
338+
if (schemalist == null || schemalist.isEmpty()) {
339+
return null;
340+
}
341+
ArrayList<Schema> schemas = new ArrayList<>();
342+
for (Record s : schemalist) {
343+
SchemaModel model = new SchemaModel();
344+
model.name = s.get(0).toString();
345+
Schema schema = new Schema(model, projectName, odps);
346+
schema.setLoaded(true);
347+
schemas.add(schema);
348+
}
349+
return schemas;
350+
}
351+
292352
@Override
293353
protected List<Schema> list() {
294-
ArrayList<Schema> schemas = new ArrayList<>();
354+
AtomicReference<List<Schema>> schemas = new AtomicReference<>(new ArrayList<>());
295355
params.put("expectmarker", "true");
296356
String lastMarker = params.get("marker");
297357
if (params.containsKey("marker") && StringUtils.isNullOrEmpty(lastMarker)) {
@@ -307,26 +367,28 @@ protected List<Schema> list() {
307367
params.put("owner", filter.getOwner());
308368
}
309369
}
310-
311370
String resource = ResourceBuilder.buildSchemaResource(projectName);
312-
313371
try {
314-
315-
ListSchemasResponse resp = client.request(
316-
ListSchemasResponse.class, resource, "GET", params);
317-
318-
for (SchemaModel model: resp.schemas) {
319-
Schema schema = new Schema(model, projectName, odps);
320-
schemas.add(schema);
321-
}
322-
323-
params.put("marker", resp.marker);
324-
372+
return odps.projects().get(projectName).executeIfEpv2(() -> {
373+
schemas.set(getExternalProjectSchemaList(projectName));
374+
if (schemas.get() == null || schemas.get().isEmpty()) {
375+
return null;
376+
}
377+
params.put("marker", "");
378+
return schemas.get();
379+
}, () -> {
380+
ListSchemasResponse resp = client.request(
381+
ListSchemasResponse.class, resource, "GET", params);
382+
for (SchemaModel model : resp.schemas) {
383+
Schema schema = new Schema(model, projectName, odps);
384+
schemas.get().add(schema);
385+
}
386+
params.put("marker", resp.marker);
387+
return schemas.get();
388+
});
325389
} catch (OdpsException e) {
326-
throw new RuntimeException(e.getMessage(), e);
390+
throw new UncheckedOdpsException(e);
327391
}
328-
329-
return schemas;
330392
}
331393
}
332394
}

0 commit comments

Comments
 (0)