Newbie Newbie - 1 year ago 152
Scala Question

ERROR : value += is not a member of Long Scala

I am trying to do some aggregates for publishing metrics to Cloud watch. I adding my metrics count logic when before saving the final result. Basically I am trying to get the count of customers with value > 0 for each column. So that I can get number and percentage.

case class ItemData(totalRent : Long, totalPurchase: Long, itemTypeCounts: Map[String, Int] ) extends Serializable

import scala.collection.JavaConversions._

class ItemDataMetrics(startDate: String) {

var totals: ItemData = _

def countNonZero(c: Long): Int = {if (c > 0) 1 else 0}

def accumulate(featureData: ItemData) {

totals.totalRent+= countNonZero( featureData.totalRent )
totals.totalPurchase += countNonZero( featureData.totalPurchase )

for (entry <- featureData.itemTypeCounts.entrySet) {

if (totals.itemTypeCounts.contains( entry.getKey )) {
totals.itemTypeCounts.updated( entry.getKey, entry.getValue + countNonZero( entry.getValue ) )
} else {
totals.itemTypeCounts.put( entry.getKey, countNonZero( entry.getValue ) )

var totalCustomer : Int = 0
val itemMetrics: ItemDataMetrics = new ItemDataMetrics(startDate)

val resultValue = {
r => {
val customerId = r.getAs[String]( "customerId" )

val totalRent = r.getAs[Long]( "totalRent" )
val totalPurchase = r.getAs[Long]( "totalPurchase" )

val itemTypeCounts = r.getAs[Map[String, Int]]( "itemType" )

val items = ItemData( totalRent, totalPurchase, itemTypeCounts)

totalCustomer = totalCustomer + 1


val jsonString = JacksonUtil.toJson( items)

(customerId, jsonString)
} )

publishMetrics(startDate, featureMetrics) ---- publishes metrics to cloud watch


But keep getting error :

<console>:26: error: value += is not a member of Long
totals.totalRent += countNonZero( itemData.totalRent )
<console>:27: error: value += is not a member of Long
totals.totalPurchase += countNonZero( itemData.totalPurchase )

<console>:36: error: value entrySet is not a member of Map[String,Int]
for (entry <- itemData.itemTypeCounts.entrySet) {

I am new to scala/spark . Can some tell me what I am doing wrong here ?

Answer Source

There are two conditions under which x += y is valid in Scala:

  1. x has a method named +=, which will be called with y as the argument, or
  2. x is a var and has a method named +. In that case x will be assigned the result of x + y

Now Long only has a + method, no += method. Therefore you can only use += on a Long if it's a var. Now you didn't show the definition of the ItemData class, but since you got an error, I assume that totals.totalRent is a val (or a def). Thus it can't be reassigned and you can't use += on it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download