|
@@ -3,15 +3,22 @@ package com.tzld.piaoquan.recommend.feature.produce.service;
|
|
import com.aliyun.odps.Instance;
|
|
import com.aliyun.odps.Instance;
|
|
import com.aliyun.odps.Odps;
|
|
import com.aliyun.odps.Odps;
|
|
import com.aliyun.odps.OdpsException;
|
|
import com.aliyun.odps.OdpsException;
|
|
|
|
+import com.aliyun.odps.TableSchema;
|
|
import com.aliyun.odps.account.Account;
|
|
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.data.SimpleJsonValue;
|
|
import com.aliyun.odps.task.SQLTask;
|
|
import com.aliyun.odps.task.SQLTask;
|
|
import com.aliyun.odps.utils.StringUtils;
|
|
import com.aliyun.odps.utils.StringUtils;
|
|
import com.google.common.base.Joiner;
|
|
import com.google.common.base.Joiner;
|
|
-import com.tzld.piaoquan.recommend.feature.produce.util.CommonCollectionUtils;
|
|
|
|
import com.tzld.piaoquan.recommend.feature.produce.model.DTSConfig;
|
|
import com.tzld.piaoquan.recommend.feature.produce.model.DTSConfig;
|
|
|
|
+import com.tzld.piaoquan.recommend.feature.produce.util.CommonCollectionUtils;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.apache.commons.lang3.math.NumberUtils;
|
|
|
|
+import org.apache.spark.aliyun.odps.OdpsOps;
|
|
|
|
+import org.apache.spark.api.java.JavaRDD;
|
|
|
|
+import org.apache.spark.api.java.JavaSparkContext;
|
|
|
|
+import org.apache.spark.api.java.function.Function2;
|
|
|
|
|
|
import java.util.Collections;
|
|
import java.util.Collections;
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
@@ -28,7 +35,96 @@ 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 tunnelUrl = "http://dt.cn-hangzhou.maxcompute.aliyun.com";
|
|
private final String sqlFormat = "select %s from %s where 1=1 %s ;";
|
|
private final String sqlFormat = "select %s from %s where 1=1 %s ;";
|
|
|
|
+ private final String countSqlFormat = "select count(1) as count from %s where 1=1 %s ;";
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public JavaRDD<Map<String, String>> read(JavaSparkContext jsc, DTSConfig config, Map<String, String> argMap) {
|
|
|
|
+ String project = argMap.get("project");
|
|
|
|
+ if (StringUtils.isBlank(project)) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String table = argMap.get("table");
|
|
|
|
+ if (StringUtils.isBlank(table)) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
|
+ for (String p : config.getOdps().getPartition()) {
|
|
|
|
+ sb.append(p);
|
|
|
|
+ sb.append(" = ");
|
|
|
|
+ sb.append(argMap.get(p));
|
|
|
|
+ sb.append(",");
|
|
|
|
+ }
|
|
|
|
+ String partition = sb.deleteCharAt(sb.length() - 1).toString();
|
|
|
|
+ int partitionNum = NumberUtils.toInt(argMap.get("partitionNum"), 10);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ OdpsOps odpsOps = new OdpsOps(jsc.sc(), accessId, accessKey, odpsUrl, tunnelUrl);
|
|
|
|
+
|
|
|
|
+ JavaRDD<Map<String, String>> readData = odpsOps.readTableWithJava(project, table, partition,
|
|
|
|
+ new RecordToMap(config.getOdps().getCols()), partitionNum);
|
|
|
|
+ return readData;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ static class RecordToMap implements Function2<Record, TableSchema, Map<String, String>> {
|
|
|
|
+ private List<String> cols;
|
|
|
|
+
|
|
|
|
+ public RecordToMap(List<String> cols) {
|
|
|
|
+ this.cols = cols;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Map<String, String> call(Record r, TableSchema schema) {
|
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
|
+ for (int i = 0; i < schema.getColumns().size(); i++) {
|
|
|
|
+ if (cols.contains(r.getColumns()[i].getName())) {
|
|
|
|
+ Object obj = r.get(i);
|
|
|
|
+ if (obj instanceof SimpleJsonValue) {
|
|
|
|
+ map.put(r.getColumns()[i].getName(), ((SimpleJsonValue) obj).toString());
|
|
|
|
+ } else {
|
|
|
|
+ map.put(r.getColumns()[i].getName(), r.getString(i));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return map;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public long count(DTSConfig config, Map<String, String> argMap) {
|
|
|
|
+ String project = argMap.get("project");
|
|
|
|
+ String table = argMap.get("table");
|
|
|
|
+
|
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
|
+ for (String partition : config.getOdps().getPartition()) {
|
|
|
|
+ sb.append(" and ");
|
|
|
|
+ sb.append(partition);
|
|
|
|
+ sb.append(" = ");
|
|
|
|
+ sb.append(argMap.get(partition));
|
|
|
|
+ }
|
|
|
|
+ String condition = sb.toString();
|
|
|
|
+
|
|
|
|
+ Account account = new AliyunAccount(accessId, accessKey);
|
|
|
|
+ Odps odps = new Odps(account);
|
|
|
|
+ odps.setEndpoint(odpsUrl);
|
|
|
|
+ odps.setDefaultProject(project);
|
|
|
|
+
|
|
|
|
+ String sql = String.format(countSqlFormat, table, condition);
|
|
|
|
+ List<Record> records;
|
|
|
|
+ try {
|
|
|
|
+ Instance i = SQLTask.run(odps, sql);
|
|
|
|
+ i.waitForSuccess();
|
|
|
|
+ records = SQLTask.getResult(i);
|
|
|
|
+ } catch (OdpsException e) {
|
|
|
|
+ log.error("request odps error", e);
|
|
|
|
+ return 0;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return Integer.valueOf(records.get(0).getString(0));
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|