|
@@ -0,0 +1,83 @@
|
|
|
|
|
+package com.tzld.piaoquan.recommend.feature.produce;
|
|
|
|
|
+
|
|
|
|
|
+import com.tzld.piaoquan.recommend.feature.produce.model.DTSConfig;
|
|
|
|
|
+import com.tzld.piaoquan.recommend.feature.produce.service.CMDService;
|
|
|
|
|
+import com.tzld.piaoquan.recommend.feature.produce.service.DTSConfigService;
|
|
|
|
|
+import com.tzld.piaoquan.recommend.feature.produce.service.ODPSService;
|
|
|
|
|
+import com.tzld.piaoquan.recommend.feature.produce.service.RedisService;
|
|
|
|
|
+import com.tzld.piaoquan.recommend.feature.produce.util.JSONUtils;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.apache.commons.collections.MapUtils;
|
|
|
|
|
+import org.apache.commons.lang3.math.NumberUtils;
|
|
|
|
|
+import org.apache.spark.SparkConf;
|
|
|
|
|
+import org.apache.spark.api.java.JavaRDD;
|
|
|
|
|
+import org.apache.spark.api.java.JavaSparkContext;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+public class FeatureRedisClean {
|
|
|
|
|
+
|
|
|
|
|
+ private static CMDService cmdService = new CMDService();
|
|
|
|
|
+ private static ODPSService odpsService = new ODPSService();
|
|
|
|
|
+
|
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
|
+ log.info("args {}", JSONUtils.toJson(args));
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, String> argMap = cmdService.parse(args);
|
|
|
|
|
+
|
|
|
|
|
+ if (MapUtils.isEmpty(argMap)) {
|
|
|
|
|
+ log.error("args is empty");
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ SparkConf sparkConf = new SparkConf()
|
|
|
|
|
+ .setAppName("odps sync to redis : " + argMap.get("table"));
|
|
|
|
|
+ for (Map.Entry<String, String> e : argMap.entrySet()) {
|
|
|
|
|
+ sparkConf.set(e.getKey(), e.getValue());
|
|
|
|
|
+ }
|
|
|
|
|
+ JavaSparkContext jsc = new JavaSparkContext(sparkConf);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ // ODPS
|
|
|
|
|
+ log.info("read odps");
|
|
|
|
|
+ String env = argMap.get("env");
|
|
|
|
|
+ DTSConfigService dtsConfigService = new DTSConfigService(env);
|
|
|
|
|
+ DTSConfig config = dtsConfigService.getV2DTSConfig(argMap);
|
|
|
|
|
+ if (config == null || !config.selfCheck()) {
|
|
|
|
|
+ log.error("dts config error");
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ long count = odpsService.count(config, argMap);
|
|
|
|
|
+ if (count == 0) {
|
|
|
|
|
+ log.error("table {} not data", argMap.get("table"));
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ JavaRDD<Map<String, String>> fieldValues = odpsService.read(jsc, config, argMap);
|
|
|
|
|
+ if (fieldValues == null) {
|
|
|
|
|
+ log.info("odps empty");
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // RDD
|
|
|
|
|
+ log.info("sync redis");
|
|
|
|
|
+ RedisService redisService = new RedisService(env);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ log.info("odps count {}", count);
|
|
|
|
|
+ long odpsBatchSize = NumberUtils.toLong(argMap.getOrDefault("odpsBatchSize", "200000"));
|
|
|
|
|
+ int calcPartationNum = (int) (count / odpsBatchSize + 1);
|
|
|
|
|
+
|
|
|
|
|
+ int maxPartitionNum = NumberUtils.toInt(argMap.get("maxPartitionNum"), 30);
|
|
|
|
|
+ int finalPartationNum = Math.min(maxPartitionNum, calcPartationNum);
|
|
|
|
|
+
|
|
|
|
|
+ log.info("argMap.maxPartitionNum: {} calcPartationNum: {}, finalPartationNum: {}", maxPartitionNum, calcPartationNum, finalPartationNum);
|
|
|
|
|
+ log.info("config: {}", config);
|
|
|
|
|
+ JavaRDD<Map<String, String>> repartitionedFieldValues = fieldValues.repartition(finalPartationNum);
|
|
|
|
|
+ repartitionedFieldValues.foreachPartition(iterator -> redisService.delete(iterator, config));
|
|
|
|
|
+
|
|
|
|
|
+ jsc.stop();
|
|
|
|
|
+ }
|
|
|
|
|
+}
|