zoukankan      html  css  js  c++  java
  • C# mongodb 简单自增排序 以及批量去重批量入库

    //取自动排序大小

    private Int64 SeqInt(int size)
    {
    var jobs = db.GetCollection<BsonDocument>("Counters");
    var query = Query.And(Query.EQ("_id", "TaoBaoItem"));
    var sortBy = SortBy.Descending("next");
    var update = Update.Inc("next", (Int64)size);//.Push("new", true).Push("upsert", true);
    var result = jobs.FindAndModify(query, sortBy, update, true, true);
    BsonDocument chosenJob = result.ModifiedDocument;
    return chosenJob.GetValue("next").AsInt64;
    }
    /// <summary>
    /// 批量查找存在的数据
    /// </summary>
    /// <param name="cacheitems"></param>
    /// <returns></returns>
    private MongoCursor<TaoBaoItem> QueryItems(List<TaoBaoItem> cacheitems)
    {
    Stopwatch sw = new Stopwatch();
    sw.Start();
    BsonArray dd;
    dd = new BsonArray();
    //批量插询
    foreach (var item in cacheitems)
    {
    dd.Add(item.Iid);
    }
    var query = Query.In("Iid", dd);
    IMongoQuery q = query;
    // var count = taoBaoItemRepo.Collection.Count(q);
    
    var sort = new SortByDocument { { "_id", -1 } };
    var x = taoBaoItemRepo.Collection.Find(q).SetSortOrder(sort);
    sw.Stop();
    WorkMsg("自写查询{0}条耗时:{1}
    ".FormatWith(cacheitems.Count, sw.ElapsedMilliseconds));
    return x;
    }

    业务部份

            /// <summary>
            /// 去重入库线程
            /// </summary>
            public void Work()
            {
    
                while (true)
                {
    
                    List<TaoBaoItem> cacheitems = ItemCache.GetLItem();
                    if (cacheitems != null)
                    {
    
                        Stopwatch sw = new Stopwatch();
                        sw.Start();
                        MongoCursor<TaoBaoItem> rz = QueryItems(cacheitems);
                        foreach (TaoBaoItem doc in rz)
                        {
    
                            HashCache.Add(doc.Iid);
                        }
    
                        int i = 0;
                        foreach (var item in cacheitems)
                        {
                            i++;
                            if (HashCache.Add(item.Iid))
                            { 
                                  
    
                                Items.Add(item);
                                //temp计数  并作大于100的判断
                                if (Items.Count >= 100 || (i == cacheitems.Count && ItemCache.Count() == 0))
                                {
                                   var seqid = SeqInt(Items.Count) ;
                                   seqid = seqid - Items.Count;
                                    foreach (var x in Items)
                                    {
                                        x.DocId = seqid;
                                        seqid++;                                  
                                    }
                                    
                                    Stopwatch sw1 = new Stopwatch();
                                    sw1.Start();
                                    taoBaoItemRepo.Add(Items);
                                    sw1.Stop();
                                    WorkMsg("插入{0}条宝贝耗时:{1}	库内共有宝贝{2}	".FormatWith(Items.Count, sw1.ElapsedMilliseconds, taoBaoItemRepo.Count()));
                                    Items.Clear();
                                }
                            }
                        }
                        sw.Stop();
                        WorkMsg("{0}条记录处理[去重+入库]总耗时:{1}
    ".FormatWith(cacheitems.Count, sw.ElapsedMilliseconds));
                    }
                    else
                    {
                        Thread.Sleep(5000);
                    }
                }
            }
  • 相关阅读:
    Linux环境下Nginx的安装
    Hibernate JPA 动态criteria语句针对null查询条件的特殊处理
    easyPOI使用
    Spring JdbcTemplate中关于RowMapper的使用实例
    十分钟了解分布式计算:Google Dataflow
    十分钟了解分布式计算:GraphX
    十分钟了解分布式计算:Petuum
    十分钟了解分布式计算:GraphLab
    文本深度表示模型Word2Vec
    Max Points on a Line
  • 原文地址:https://www.cnblogs.com/jizhongfong/p/3295161.html
Copyright © 2011-2022 走看看