@@ -2,6 +2,7 @@ package recommendation
2
2
3
3
import scala .util .Random
4
4
import org .slf4j .LoggerFactory
5
+ import org .apache .hadoop .fs .{FileSystem , Path }
5
6
import org .apache .spark ._
6
7
import org .apache .spark .storage .StorageLevel
7
8
import org .apache .spark .rdd .RDD
@@ -15,7 +16,8 @@ case class Config(
15
16
algorithm : String = " " ,
16
17
similarityMethod : String = " " ,
17
18
recommendMethod : String = " " ,
18
- numRecommendations : Int = 30 )
19
+ numRecommendations : Int = 30 ,
20
+ outputPath : String = " " )
19
21
20
22
object MainClass {
21
23
@@ -34,8 +36,10 @@ object MainClass {
34
36
opt[String ]('r' , " recommend-method" ) action { (x, c) =>
35
37
c.copy(recommendMethod = x) } text(" recommend method" )
36
38
opt[Int ]('n' , " num-recommendations" ) action { (x, c) =>
37
- c.copy(numRecommendations = x) } text (" number of recommendations" )
38
- help(" help" ) text (" prints this usage text" )
39
+ c.copy(numRecommendations = x) } text(" number of recommendations" )
40
+ opt[String ]('o' , " output-path" ) action { (x, c) =>
41
+ c.copy(outputPath = x) } text(" output path" )
42
+ help(" help" ) text(" prints this usage text" )
39
43
}
40
44
41
45
val config = parser.parse(args, Config ()) match {
@@ -46,6 +50,15 @@ object MainClass {
46
50
val conf = new SparkConf ().setAppName(" Recommendation" ).setIfMissing(" spark.master" , " local[4]" )
47
51
val sc = new SparkContext (conf)
48
52
53
+ if (config.outputPath.nonEmpty) {
54
+ logger.info(" Delete output path " + config.outputPath)
55
+ try {
56
+ FileSystem .get(sc.hadoopConfiguration).delete(new Path (config.outputPath), true )
57
+ } catch {
58
+ case _ : Exception =>
59
+ }
60
+ }
61
+
49
62
// read logs - logs are pre-grouped by user-item
50
63
val logs = sc.textFile(config.inputPath).flatMap { line =>
51
64
try {
@@ -79,7 +92,8 @@ object MainClass {
79
92
80
93
val commonParams = Map (
81
94
" numNeighbours" -> 50 ,
82
- " numRecommendations" -> config.numRecommendations
95
+ " numRecommendations" -> config.numRecommendations,
96
+ " outputPath" -> config.outputPath
83
97
)
84
98
85
99
// model
@@ -102,6 +116,16 @@ object MainClass {
102
116
103
117
userRecomm.cache()
104
118
119
+ if (config.outputPath.nonEmpty) {
120
+ val userRecommPath = new Path (config.outputPath, " user_recommendations" )
121
+ logger.info(" Writing user recommendations into " + userRecommPath.toString)
122
+ userRecomm.flatMap { case (user, products) =>
123
+ products.map { case Rating (user, product, rating) =>
124
+ Seq [Any ](user, product, rating).mkString(" \t " )
125
+ }
126
+ }.saveAsTextFile(userRecommPath.toString)
127
+ }
128
+
105
129
val (precision, recall, f1) = evaluatePrecision(testingSet, userRecomm)
106
130
val coverage = evaluateCoverage(trainingSet, userRecomm)
107
131
val popularity = evaluatePopularity(trainingSet, userRecomm)
0 commit comments