我正在嘗試做一些用於將度量標準發佈到Cloud Watch的聚合。在保存最終結果之前添加指標計數邏輯。基本上我試圖讓每列的值> 0的客戶數。這樣我可以得到數字和百分比。錯誤:value + =不是Long Scala的成員
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 = resultDF.map({
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
itemMetrics.accumulate(items)
val jsonString = JacksonUtil.toJson(items)
(customerId, jsonString)
}
})
publishMetrics(startDate, featureMetrics) ---- publishes metrics to cloud watch
resultValue.saveAsTextFile("S3:....")
但一直得到錯誤:
<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) {
我是新來斯卡拉/火花。有人能告訴我我在這裏做錯了什麼嗎?
用ItemData更新。有什麼辦法可以做到這一點? – Newbie
@Newbie你可以製作'totalRent'和'totalPurchases''var's,但你可能想要改變你的整個方法,根本不需要突變。 – sepp2k