2013-06-03 52 views
1

希望你能幫助我!複雜的MapReduce查詢與RavenDB

我正在收集tweets,它有一個created_at日期(DataPublicacao)和一些Hashtags。每條推文指廣播者(redeId)和節目(programaId)。 我想在某個時間段內查詢數據庫中20個最常用的主題標籤。

我必須映射每個哈希標籤,當它被使用時,以及它指向哪個廣播公司和電視節目。

然後,我需要能夠計算在某個時期(我不知道如何)每個哈希標籤的發生。

public class Tweet : IModelo 
{ 
    public string Id { get; set; } 
    public string RedeId { get; set; } 
    public string ProgramaId { get; set; } 
    public DateTime DataPublicacao { get; set; } 
    public string Conteudo { get; set; } 
    public string Aplicacao { get; set; } 
    public Autor Autor { get; set; } 
    public Twitter.Monitor.Dominio.Modelo.TweetJson.Geo LocalizacaoGeo { get; set; } 
    public Twitter.Monitor.Dominio.Modelo.TweetJson.Place Localizacao { get; set; } 
    public Twitter.Monitor.Dominio.Modelo.TweetJson.Entities Entidades { get; set; } 
    public string Imagem { get; set; } 
    public Autor Para_Usuario { get; set; } 
    public string Retweet_Para_Status_Id { get; set; } 
} 

而「實體」是主題標籤,用戶名和網址。

我試圖通過廣播公司,電視節目和文本對標籤進行分組,並列出出現的日期。然後,我必須改變結果,所以我可以統計那個時期的事件。

public class EntityResult 
    { 
     public string hashtagText { get; set; } 
     public string progId { get; set; } 
     public string redeId { get; set; } 
     public int listCount { get; set; } 
    } 

    public class HashtagsIndex : AbstractIndexCreationTask<Tweet, HashtagsIndex.ReduceResults> 
    { 
     public class ReduceResults 
     { 
      public string hashtagText { get; set; } 
      public DateTime createdAt { get; set; } 
      public string progId { get; set; } 
      public string redeId { get; set; } 
      public List<DateTime> datesList { get; set; } 
     } 

     public HashtagsIndex() 
     { 
      Map = tweets => from tweet in tweets 
          from hts in tweet.Entidades.hashtags 
          where tweet.Entidades != null 
          select new 
          { 
           createdAt = tweet.DataPublicacao, 
           progId = tweet.ProgramaId, 
           redeId = tweet.RedeId, 
           hashtagText = hts.text, 
           datesList = new List<DateTime>(new DateTime[] { tweet.DataPublicacao }) 
          }; 

      Reduce = results => from result in results 
           group result by new { result.progId, result.redeId, result.hashtagText } 
            into g 
            select new 
            { 
             createdAt = DateTime.MinValue, 
             progId = g.Key.progId, 
             redeId = g.Key.redeId, 
             hashtagText = g.Key.hashtagText, 
             datesList = g.ToList().Select(t => t.createdAt).ToList() 
            }; 
     } 
    } 

我迄今所取得的查詢是:

    var hashtags2 = session.Query<dynamic, HashtagsIndex>().Customize(t => t.TransformResults((query, results) => 
         results.Cast<dynamic>().Select(g => 
         { 
          Expression<Func<DateTime, bool>> exp = o => o >= dtInit && o <= dtEnd; 

          int count = g.Where(exp); 
          return new EntityResult 
          { 
           redeId = g.redeId, 
           progId = g.progId, 
           hashtagText = g.hashtagText, 
           listCount = count 
          }; 
         }))).Take(20).ToList(); 

現在我需要OrderByDescending(T => t.count),所以我不能就這一時期(20)最常用的井號標籤。

我該怎麼做?

+0

請張貼一些代碼來顯示您問的問題。你的問題目前沒有太多意義。 –

+0

嗨馬特。它更清楚嗎?如果沒有,請告訴我,我會編輯我的問題。 –

回答

1

是否可以在mapreduce過程之前過濾項目?

map/reduce索引就像任何其他索引一樣。所有文檔都會通過所有索引進行處理。所以當你像「之前」這樣表達時,答案顯然是「不」。

但我認爲你是期間索引中過濾項目有興趣,並且很容易在地圖上完成:

Map = items => from item in items 
       where item.foo == whatever // this is how you filter 
       select new 
       { 
       // whatever you want to map 
       } 

該指數將處理所有的文件,但最終得到的指數將只包含與您在where子句中指定的過濾器相匹配的項目。

是否有可能隨後由功能組,如用戶按年齡,再由區

分組在減少步驟中完成。這就是地圖/縮小的全部內容。

我向你提出的建議(我的意思是沒有不尊重這個),是在你嘗試跑步之前走路。構建一個簡單的原型或一組單元測試,並首先嚐試基本的存儲和檢索。然後嘗試基本的索引和查詢。然後嘗試一個簡單地圖減少,如計算你所有的推文。只有這樣,你才能嘗試使用其他分組進行高級地圖/縮小。如果你遇到麻煩,那麼你將有代碼,你可以在這裏發佈求助。

這可能嗎?

當然。一切皆有可能。 :)

+0

也許我沒有說清楚。我的英語不太好= /。我有一個以前不知道的狀況。我必須按日期過濾(例如,item.created_at

+0

這不是map/reduce的情況。你會爲你的'created_at'字段創建一個索引。您可以指定where子句以在* query *過程中過濾來自此索引的數據。你可以閱讀這裏[這裏](http://ravendb.net/docs/2.0/client-api/querying/using-linq-to-query-ravendb)。 –

+0

只是爲了澄清 - 根據hashtag,url等聚合推文的數量,*是* map/reduce索引。爲了通過'DateTime'進行過濾,您必須決定合理的時間間隔來存儲數據 - 這也是您的索引的一部分。但是通過諸如DateTime.UtcNow之類的參數來過濾該索引是在查詢時完成的。 –