|
@@ -1,4 +1,4 @@
|
|
-package feature.service;
|
|
|
|
|
|
+package feature.produce.service;
|
|
|
|
|
|
import com.aliyun.odps.Instance;
|
|
import com.aliyun.odps.Instance;
|
|
import com.aliyun.odps.Odps;
|
|
import com.aliyun.odps.Odps;
|
|
@@ -7,8 +7,11 @@ import com.aliyun.odps.account.Account;
|
|
import com.aliyun.odps.account.AliyunAccount;
|
|
import com.aliyun.odps.account.AliyunAccount;
|
|
import com.aliyun.odps.data.Record;
|
|
import com.aliyun.odps.data.Record;
|
|
import com.aliyun.odps.task.SQLTask;
|
|
import com.aliyun.odps.task.SQLTask;
|
|
|
|
+import com.aliyun.odps.utils.StringUtils;
|
|
import com.google.common.base.Joiner;
|
|
import com.google.common.base.Joiner;
|
|
-import feature.util.CommonCollectionUtils;
|
|
|
|
|
|
+import feature.produce.model.DTSConfig;
|
|
|
|
+import feature.produce.util.CommonCollectionUtils;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import java.util.Collections;
|
|
import java.util.Collections;
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
@@ -20,18 +23,44 @@ import java.util.Map;
|
|
*
|
|
*
|
|
* @author dyp
|
|
* @author dyp
|
|
*/
|
|
*/
|
|
|
|
+@Slf4j
|
|
public class ODPSService {
|
|
public class ODPSService {
|
|
private final String accessId = "LTAIWYUujJAm7CbH";
|
|
private final String accessId = "LTAIWYUujJAm7CbH";
|
|
private final String accessKey = "RfSjdiWwED1sGFlsjXv0DlfTnZTG1P";
|
|
private final String accessKey = "RfSjdiWwED1sGFlsjXv0DlfTnZTG1P";
|
|
private final String odpsUrl = "http://service.odps.aliyun.com/api";
|
|
private final String odpsUrl = "http://service.odps.aliyun.com/api";
|
|
- private final String sqlFormat = "select %s from %s where %s ;";
|
|
|
|
|
|
+ private final String sqlFormat = "select %s from %s where 1=1 %s ;";
|
|
|
|
|
|
|
|
|
|
- public List<Map<String, String>> read(String project,
|
|
|
|
- String table,
|
|
|
|
- List<String> colNames,
|
|
|
|
- String condition) {
|
|
|
|
- // ODPS
|
|
|
|
|
|
+ /**
|
|
|
|
+ * @return k: 列名 v:值
|
|
|
|
+ */
|
|
|
|
+ public List<Map<String, String>> read(DTSConfig config, Map<String, String> argMap) {
|
|
|
|
+
|
|
|
|
+ String project = argMap.get("project");
|
|
|
|
+ if (StringUtils.isBlank(project)) {
|
|
|
|
+ return Collections.emptyList();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String table = argMap.get("table");
|
|
|
|
+ if (StringUtils.isBlank(table)) {
|
|
|
|
+ return Collections.emptyList();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
|
+ for (String partition : config.getOdps().getPartition()) {
|
|
|
|
+ sb.append(" and ");
|
|
|
|
+ sb.append(partition);
|
|
|
|
+ sb.append(" = ");
|
|
|
|
+ sb.append(argMap.get(partition));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return read(project, table, config.getOdps().getCols(), sb.toString());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private List<Map<String, String>> read(String project,
|
|
|
|
+ String table,
|
|
|
|
+ List<String> colNames,
|
|
|
|
+ String condition) {
|
|
Account account = new AliyunAccount(accessId, accessKey);
|
|
Account account = new AliyunAccount(accessId, accessKey);
|
|
Odps odps = new Odps(account);
|
|
Odps odps = new Odps(account);
|
|
odps.setEndpoint(odpsUrl);
|
|
odps.setEndpoint(odpsUrl);
|
|
@@ -39,13 +68,14 @@ public class ODPSService {
|
|
|
|
|
|
String sql = String.format(sqlFormat, Joiner.on(",").join(colNames), table, condition);
|
|
String sql = String.format(sqlFormat, Joiner.on(",").join(colNames), table, condition);
|
|
|
|
|
|
- List<Record> records = Collections.emptyList();
|
|
|
|
|
|
+ List<Record> records;
|
|
try {
|
|
try {
|
|
Instance i = SQLTask.run(odps, sql);
|
|
Instance i = SQLTask.run(odps, sql);
|
|
i.waitForSuccess();
|
|
i.waitForSuccess();
|
|
records = SQLTask.getResult(i);
|
|
records = SQLTask.getResult(i);
|
|
} catch (OdpsException e) {
|
|
} catch (OdpsException e) {
|
|
- e.printStackTrace();
|
|
|
|
|
|
+ log.error("request odps error", e);
|
|
|
|
+ return Collections.emptyList();
|
|
}
|
|
}
|
|
|
|
|
|
List<Map<String, String>> fieldValues = CommonCollectionUtils.toList(records, r -> {
|
|
List<Map<String, String>> fieldValues = CommonCollectionUtils.toList(records, r -> {
|