|
|
@@ -0,0 +1,102 @@
|
|
|
+package com.aliyun.odps.spark.examples.makedata_recsys_r_rate
|
|
|
+
|
|
|
+import com.aliyun.odps.spark.examples.myUtils.{DataUtils, MyHdfsUtils, ParamUtils}
|
|
|
+import org.apache.hadoop.io.compress.GzipCodec
|
|
|
+import org.apache.spark.sql.SparkSession
|
|
|
+
|
|
|
+import scala.collection.JavaConversions._
|
|
|
+import scala.collection.mutable.ArrayBuffer
|
|
|
+import scala.io.Source
|
|
|
+
|
|
|
+
|
|
|
+object makedata_profile_gender_sample_20251114 {
|
|
|
+ def main(args: Array[String]): Unit = {
|
|
|
+ // 1. 读取参数
|
|
|
+ val param = ParamUtils.parseArgs(args)
|
|
|
+ val readPath = param.getOrElse("readPath", "/dw/recommend/model/user_profile/data/")
|
|
|
+ val year = param.getOrElse("year", "2025")
|
|
|
+ val suffixSet = param.getOrElse("suffix", "y,8,4,0,e,a,c,k,o,w,g,s,u,q,i,m").split(",").toSet
|
|
|
+ val whatLabel = param.getOrElse("whatLabel", "gender")
|
|
|
+ val classSet = param.getOrElse("class", "1,2").split(",").toSet
|
|
|
+ val featureFile = param.getOrElse("featureFile", "20241209_recsys_nor_name.txt")
|
|
|
+ val minCnt = param.getOrElse("minCnt", "10").toDouble
|
|
|
+ val repartition = param.getOrElse("repartition", "100").toInt
|
|
|
+ val savePath = param.getOrElse("savePath", "/dw/recommend/model/user_profile/gender/sample/")
|
|
|
+
|
|
|
+ // 2. content
|
|
|
+ val spark = SparkSession
|
|
|
+ .builder()
|
|
|
+ .appName(this.getClass.getName)
|
|
|
+ .getOrCreate()
|
|
|
+ val sc = spark.sparkContext
|
|
|
+
|
|
|
+ // 3. 处理数据
|
|
|
+ val loader = getClass.getClassLoader
|
|
|
+ val featureSet = loadFeatureNames(featureFile)
|
|
|
+ val featureBucketMap = DataUtils.loadUseFeatureBuckets(loader, 1, "")
|
|
|
+ val bucketsMap_br = sc.broadcast(featureBucketMap)
|
|
|
+ for (suffix <- suffixSet) {
|
|
|
+ val partition = "%s_%s".format(year, suffix)
|
|
|
+ println("开始执行:" + partition)
|
|
|
+ val data = sc.textFile(readPath + "/" + partition + "*").map(row => {
|
|
|
+ val cells = row.split("\t")
|
|
|
+ val mid = cells(0)
|
|
|
+ val labels = cells(1)
|
|
|
+ val featData = cells(2)
|
|
|
+ (mid, labels, featData)
|
|
|
+ })
|
|
|
+ .filter {
|
|
|
+ case (mid, labels, featData) =>
|
|
|
+ val label = DataUtils.parseLabel(labels, whatLabel)
|
|
|
+ classSet.contains(label)
|
|
|
+ }
|
|
|
+ .map {
|
|
|
+ case (mid, labels, featData) =>
|
|
|
+ var label = DataUtils.parseLabel(labels, whatLabel).toInt
|
|
|
+ val features = DataUtils.parseFeature(featData)
|
|
|
+ if (label > 0) {
|
|
|
+ label -= 1
|
|
|
+ }
|
|
|
+ (mid, label, features)
|
|
|
+ }
|
|
|
+ .filter {
|
|
|
+ case (mid, label, features) =>
|
|
|
+ val cnt = features.getOrElse("cnt", 0d)
|
|
|
+ cnt >= minCnt
|
|
|
+ }
|
|
|
+ .mapPartitions(row => {
|
|
|
+ val result = new ArrayBuffer[String]()
|
|
|
+ row.foreach {
|
|
|
+ case (mid, label, features) =>
|
|
|
+ val bucketsMap = bucketsMap_br.value
|
|
|
+ val featuresBucket = DataUtils.bucketFeature(featureSet, bucketsMap, features)
|
|
|
+ result.add(mid + "\t" + label + "\t" + featuresBucket.mkString("\t"))
|
|
|
+ }
|
|
|
+ result.iterator
|
|
|
+ })
|
|
|
+
|
|
|
+ // 4. 保存数据到hdfs
|
|
|
+ val hdfsPath = savePath + "/" + partition
|
|
|
+ if (hdfsPath.nonEmpty && hdfsPath.startsWith("/dw/recommend/model/")) {
|
|
|
+ println("删除路径并开始数据写入:" + hdfsPath)
|
|
|
+ MyHdfsUtils.delete_hdfs_path(hdfsPath)
|
|
|
+ data.repartition(repartition).saveAsTextFile(hdfsPath, classOf[GzipCodec])
|
|
|
+ } else {
|
|
|
+ println("路径不合法,无法写入:" + hdfsPath)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ def loadFeatureNames(nameFile: String): Set[String] = {
|
|
|
+ val buffer = Source.fromFile(nameFile)
|
|
|
+ val names = buffer.getLines().mkString("\n")
|
|
|
+ buffer.close()
|
|
|
+ val featSet = names.split("\n")
|
|
|
+ .map(r => r.replace(" ", "").replaceAll("\n", ""))
|
|
|
+ .filter(r => r.nonEmpty)
|
|
|
+ .toSet
|
|
|
+ println("featSet.size=" + featSet.size)
|
|
|
+ println(featSet)
|
|
|
+ featSet
|
|
|
+ }
|
|
|
+}
|