0

需要嵌套羣組幫助。非常新的火花和斯卡拉。感謝您的專家建議。星型聚合 - 嵌套羣組

我正在使用spark對mongo集合進行轉換。我正在與IntelliJ-Idea合作。這裏是收集細節:

{ 
_id: 
customer: 
product: 
location: 
date: 
transType: 
} 

用例:對於每個'產品'和每個位置誰是交易類型'訂購'的客戶。

//輸出類似這樣

 { 
     Product: ABCD 
      location: North america 
      customer: Cust 1, type: ordered 
       total: 200 
     } 
     { 
     Product: EFGH 
      location: North america 
      customer: Cust 2, type: Ordered 
       total: 300 
} 

這是我到目前爲止有:

val conf = new SparkConf().setAppName("PVL").setMaster("local"). 
     set("spark.mongodb.input.uri","mongodb://127.0.0.1:27017/product.transactionEvent"). 
     set("spark.mongodb.output.uri", "mongodb://127.0.0.1:27017/product.transctionResult") 
    val sc = new SparkContext(conf) 

val rdd = sc.loadFromMongoDB() 
val aggRdd = rdd.withPipeline(Seq(
     Document.parse("{$match: {transType: 'ordered'}}"), 
     Document.parse("""{ $group: {_id: {prodId: "$prodId", customer: "$customer", location: "$location", Transtype: "$Transtype"}, total: {$sum:1}}}"""), 
     Document.parse("""{$group: {_Id: {prodId: "$_id.prodId"}, details: {$addToSet: {customer: "$_id.customer", location: "$_id.location", transType: "$_id.transType", total: "$total"}}}}"""))) 

但這不工作的一些原因。錯誤是:

「未知組操作員「PRODID」在服務器上

首先,是有可能做這種火花築巢?如果是的話,我做錯了什麼? 任何幫助將不勝感激

+0

錯誤消息指出你的組字段'prodId'在你的'transactionEvent'集合中不存在。我會建議瞭解[MongoDB Aggregation](https://docs.mongodb.com/manual/aggregation/)。首先通過[mongo shell](https://docs.mongodb.com/manual/mongo/)測試你的聚合,以確保它的工作。 –

+0

謝謝萬。我**有**名爲prodId的字段。我只是沒有在這裏列出這個問題,但我確實收集了這個問題。我也在mongo shell中測試了它,並且拋出了相同的錯誤。我認爲這與我的$組嵌套有關。 – Vamsi

+0

您應該使用正確的文檔示例更新您的問題,以及您在mongo shell上測試的聚合示例。否則,如果沒有適當的環境,人們很難幫助你。 –

回答

0

我知道問題是什麼。在$ group語句之一中,我有_id大寫(如_Id)。一旦我刪除它,它工作正常。

因此簡而言之:像unknown group operator<field> should be inside the object這樣的錯誤意味着代碼無法識別組運算符/字段。原因可能是:

  1. 在組_id主要字段可以資本化
  2. 缺少逗號
  3. 沒有任何組織宣佈運營商爲一組現場
  4. 等。

所以請檢查您的代碼是否存在這些錯誤。

謝謝