João_testeSW João_testeSW - 3 months ago 22
Scala Question

(Spark - Scala) error: constructor cannot be instantiated to expected type;

I'm doing a Link Analysis project on Retail Industry using Spark Mllib. My schema is:

ID - Long Chain - Int Dept - int Category - Int Company - Long Brand - Long Date - Date ProductSize - Int ProductMeasure - Chararray PurchaseQuantity - Int PurchaseAmount - Double

And the code that I'm using is:

spark-shell
import org.apache.spark._
import org.apache.spark.rdd.RDD
import org.apache.spark.util.IntParam
import org.apache.spark.graphx._
import org.apache.spark.graphx.util.GraphGenerators

case class Transactions(ID:Long,Chain:String,Dept:String,Category:String,Company:String,Brand:String,Date:String,ProductSize:String,ProductMeasure:String,PurchaseQuantity:String,PurchaseAmount:String)

def parseTransactions(str:String): Transactions = {
val line = str.split(",")
Transactions(line(0).toLong,line(1),line(2),line(3),line(4),line(5),line(6),line(7),line(8),line(9),line(10))
}

val textRDD = sc.textFile("/user/cloudera/transactions.csv")

val transactionsRDD = textRDD.map(parseTransactions).cache()

val products = transactionsRDD.map(Transactions => (Transactions.ID,Transactions.Chain,Transactions.Dept,Transactions.Category,Transactions.Company,Transactions.Brand)).distinct

products.take(1)

val productMap = products.map { case ((ID), name) => (ID -> name) }.collect.toList.toMap


I'm getting the folloiwng error:

<console>:46: error: constructor cannot be instantiated to expected type;
found : (T1, T2)
required: (Long, String, String, String, String, String)
val productMap = products.map { case ((ID), name) => (ID -> name) }.collect.toList.toMap
^
<console>:46: error: not found: value ID
val productMap = products.map { case ((ID), name) => (ID -> name) }.collect.toList.toMap
^
<console>:46: error: value toList is not a member of Array[Nothing]
val productMap = products.map { case ((ID), name) => (ID -> name) }.collect.toList.toMap
^


Anyone knows what I'm doing wrong?

Many thanks!

Answer
// this produces a collection of tuples of type (Long, String, String, String, String, String)
val products = transactionsRDD.map(Transactions => (Transactions.ID,Transactions.Chain,Transactions.Dept,Transactions.Category,Transactions.Company,Transactions.Brand)).distinct

// since products is a collection of sextuples, the pattern match should match the tuple length
val productMap = products.map { case (ID, chain, dept, cat, comp, brand) => (ID -> chain) }.collect.toList.toMap
Comments