导入StackOverflow 73G 数据到mongoDB

举报
Jet Ding 发表于 2021/07/23 10:11:51 2021/07/23
【摘要】 编程语言: c#数据库: MongoDB框架: ASP.Net Core 3.1关于.Net Core的安装,参考:https://dotnet.microsoft.com/download关于mongoDB安装,参考:http://3ms.huawei.com/km/blogs/details/7923543?l=zh-cnMongoDB在.Net Core中的配置参考:http://3m...

编程语言: c#

数据库: MongoDB
框架: ASP.Net Core 3.1

关于.Net Core的安装,参考:

关于mongoDB安装,参考:

MongoDB在.Net Core中的配置参考:

例子代码:


        private void FeedData()
        {
            var file = Path.Combine("D:/data/stackoverflow/stackoverflow.com-Posts",
                          """""Posts.xml");

            using (XmlReader reader = XmlReader.Create(file))
            {
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            Row row = new Row();
                            if (reader.HasAttributes)
                            {
                                Console.WriteLine("Attributes of <" + reader.Name + ">");
                                while (reader.MoveToNextAttribute())
                                {
                                    Console.WriteLine(" {0}={1}"reader.Namereader.Value);

                                    CreateRowAndInsert(rowreader.Namereader.Value); 
                                }
                                _stackService.Create(row);
                                // Move the reader back to the element node.

                                reader.MoveToElement();
                            }

                            break;
                        case XmlNodeType.Text:
                            Console.Write(reader.Value);
                            break;
                        case XmlNodeType.CDATA:
                            Console.Write("<![CDATA[{0}]]>"reader.Value);
                            break;
                        case XmlNodeType.ProcessingInstruction:
                            Console.Write("<?{0} {1}?>"reader.Namereader.Value);
                            break;
                        case XmlNodeType.Comment:
                            Console.Write("<!--{0}-->"reader.Value);
                            break;
                        case XmlNodeType.XmlDeclaration:
                            Console.Write("<?xml version='1.0'?>");
                            break;
                        case XmlNodeType.Document:
                            break;
                        case XmlNodeType.DocumentType:
                            Console.Write("<!DOCTYPE {0} [{1}]"reader.Namereader.Value);
                            break;
                        case XmlNodeType.EntityReference:
                            Console.Write(reader.Name);
                            break;
                        case XmlNodeType.EndElement:
                            Console.Write("</{0}>"reader.Name);
                            break;
                    }
                }
            }
        }

        private void CreateRowAndInsert(Row rowstring namestring value)
        {
            switch (name)
            {
                case "Id":
                    row.RowId = int.Parse(value);
                    break;
                case "PostTypeId":
                    row.PostTypeId = int.Parse(value);
                    break;
                case "AcceptedAnswerId":
                    row.AcceptedAnswerId = int.Parse(value);
                    break;
                case "CreationDate":
                    row.CreationDate = DateTime.Parse(value);
                    break;
                case "Score":
                    row.Score = int.Parse(value);
                    break;
                case "ViewCount":
                    row.ViewCount = long.Parse(value);
                    break;
                case "Body":
                    row.Body = value;
                    break;
                case "OwnerUserId":
                    row.OwnerUserId = long.Parse(value);
                    break;
                case "LastEditorUserId":
                    row.LastEditorUserId = long.Parse(value);
                    break;
                case "LastEditorDisplayName":
                    row.LastEditorDisplayName = value;
                    break;
                case "LastEditorDate":
                    row.LastEditorDate = DateTime.Parse(value);
                    break;
                case "LastActivityDate":
                    row.LastActivityDate = DateTime.Parse(value);
                    break;
                case "Title":
                    row.Title = value;
                    break;
                case "Tags":
                    row.Tags = value;
                    break;
                case "AnswerCount":
                    row.AnswerCount = int.Parse(value);
                    break;
                case "CommentCount":
                    row.CommentCount = int.Parse(value);
                    break;
                case "FavoriteCount":
                    row.FavoriteCount = int.Parse(value);
                    break;
                case "CommunityOwnedDate":
                    row.CommunityOwnedDate = DateTime.Parse(value);
                    break;
            }
        }


数据视图(数据导入中,已导入一百多万条...):


数据快照:
  1. _id
    :
    5e6ab141efc2046a34b39677
  2. RowId
    :
    4
  3. PostTypeId
    :
    1
  4. AcceptedAnswerId
    :
    7
  5. CreationDate
    :
    2008-08-01T04:42:52.667+00:00
  6. Score
    :
    658
  7. ViewCount
    :
    46852
  8. Body
    :
    "

    I want to use a <code>Track-Bar</code> to change a <code>Form</code..."

  9. OwnerUserId
    :
    8
  10. LastEditorUserId
    :
    12780337
  11. LastEditorDisplayName
    :
    "Rich B"
  12. LastEditorDate
    :
    0001-01-01T00:00:00.000+00:00
  13. LastActivityDate
    :
    2020-02-28T19:38:30.540+00:00
  14. Title
    :
    "Convert Decimal to Double"
  15. Tags
    :
    "<c#><floating-point><type-conversion><double><decimal>"
  16. AnswerCount
    :
    13
  17. CommentCount
    :
    5
  18. FavoriteCount
    :
    49
  19. CommunityOwnedDate
    :
    2012-10-31T23:42:47.213+00:00

对应的单个mongoDB document示例:
{
"_id": {
"$oid": "5e6ab141efc2046a34b39677"
},
"RowId": {
"$numberLong": "4"
},
"PostTypeId": {
"$numberInt": "1"
},
"AcceptedAnswerId": {
"$numberInt": "7"
},
"CreationDate": {
"$date": {
"$numberLong": "1217565772667"
}
},
"Score": {
"$numberInt": "658"
},
"ViewCount": {
"$numberLong": "46852"
},
"Body": "

I want to use a <code>Track-Bar</code> to change a <code>Form</code>'s opacity.

\n\n

This is my code:

\n\n<pre class=\"lang-cs prettyprint-override\"><code> decimal trans = trackBar1.Value / 5000;\n this.Opacity = trans;\n</code></pre>\n\n

When I build the application, it gives the following error:

\n\n<blockquote>\n

Cannot implicitly convert type <code>decimal</code> to <code>double</code>

\n</blockquote>\n\n

I have tried using <code>trans</code> and <code>double</code> but then the <code>Control</code> doesn't work. This code worked fine in a past <strong>VB.NET</strong> <code>Project</code>.

\n",
"OwnerUserId": {
"$numberLong": "8"
},
"LastEditorUserId": {
"$numberLong": "12780337"
},
"LastEditorDisplayName": "Rich B",
"LastEditorDate": {
"$date": {
"$numberLong": "-62135596800000"
}
},
"LastActivityDate": {
"$date": {
"$numberLong": "1582918710540"
}
},
"Title": "Convert Decimal to Double",
"Tags": "<c#><floating-point><type-conversion><double><decimal>",
"AnswerCount": {
"$numberInt": "13"
},
"CommentCount": {
"$numberInt": "5"
},
"FavoriteCount": {
"$numberInt": "49"
},
"CommunityOwnedDate": {
"$date": {
"$numberLong": "1351726967213"
}
}
}
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。