将英语维基百科转储导入SQL Server [英] Import English Wikipedia dump into SQL Server

查看:146
本文介绍了将英语维基百科转储导入SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已从在这里,我正尝试将其导入SQL Server 2018。

I've downloaded the latest English Wikipedia dump (enwiki-latest-pages-articles-multistream.xml) from here, and I'm trying to import it to SQL Server 2018.

我看不到XML文件,因为它的重量超过75 GB,并且因此,我不知道在使用批量XML

I can’t see the XML file because it weighs over 75 GB, and thus I don't know what kind of tables I should create before I'm going to work with Bulk XML.

我该怎么做?我可以在 Python C#上编写一些脚本。

How can I do this? I can write some script on Python or C#. Thanks in advance!

推荐答案

使用以下

SQL查询创建数据库

SQL Query to create database

Create Database Feed
;
GO

USE [Feed]
drop table Doc
drop table Links
;
GO

CREATE TABLE [dbo].[Doc](
   DocID int primary key,
   Title [varchar](50) NULL,
   URL [varchar](50) NULL,
   Abstract [varchar](50) NULL
)

CREATE TABLE Links(
   DocID int, 
   LinkType [varchar](10) NULL,
   Anchor [varchar](50) NULL, 
   Link [varchar](50) NULL
   CONSTRAINT FK_DocID FOREIGN KEY (DocID)
        REFERENCES dbo.Doc (DocID)
)

c#代码从xml填充数据库

c# code to fill database from xml

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Data;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        const string CONNECTION_STRING = @"Server=.\SQLEXPRESS;Database=Feed;Trusted_Connection=True;";
        const string INSERT_DOC =
            "INSERT INTO [Feed].[dbo].[Doc] (" +
            "DocID, Title , URL, Abstract)" +
            " VALUES " +
            "(@DocID, @Title, @URL, @Abstract)";
        const string INSERT_LINK =
            "INSERT INTO [Feed].[dbo].[Links] (" +
            "DocID, LinkType , Anchor, Link)" +
            " VALUES " +
            "(@DocID, @Linktype, @Anchor, @Link)";

        static void Main(string[] args)
        {
            SqlConnection conn = new SqlConnection(CONNECTION_STRING);
            conn.Open();

            SqlCommand docCmd = new SqlCommand(INSERT_DOC, conn);

            docCmd.Parameters.Add("@DocID", SqlDbType.Int);
            docCmd.Parameters.Add("@Title", SqlDbType.VarChar, 50);
            docCmd.Parameters.Add("@URL", SqlDbType.VarChar, 50);
            docCmd.Parameters.Add("@Abstract", SqlDbType.VarChar, 50);

            SqlCommand linksCmd = new SqlCommand(INSERT_LINK, conn);

            linksCmd.Parameters.Add("@DocID", SqlDbType.Int);
            linksCmd.Parameters.Add("@LinkType", SqlDbType.VarChar, 10);
            linksCmd.Parameters.Add("@Anchor", SqlDbType.VarChar, 50);
            linksCmd.Parameters.Add("@Link", SqlDbType.VarChar, 50);

            XmlReader reader = XmlReader.Create(FILENAME);
            int id = 0;
            while (!reader.EOF)
            {
                if (reader.Name != "doc")
                {
                    reader.ReadToFollowing("doc");
                }
                if (!reader.EOF)
                {
                    XElement doc = (XElement)XElement.ReadFrom(reader);
                    id++;

                    docCmd.Parameters["@DocID"].Value = id;
                    docCmd.Parameters["@Title"].Value = (string)doc.Element("title");
                    docCmd.Parameters["@URL"].Value = (string)doc.Element("url");
                    docCmd.Parameters["@Abstract"].Value = (string)doc.Element("abstract");
                    int docRowsChanged = docCmd.ExecuteNonQuery();

                    foreach (XElement sublink in doc.Descendants("sublink"))
                    {
                        linksCmd.Parameters["@DocID"].Value = id;
                        linksCmd.Parameters["@LinkType"].Value = (string)sublink.Attribute("linktype");
                        linksCmd.Parameters["@Anchor"].Value = (string)sublink.Element("anchor");
                        linksCmd.Parameters["@Link"].Value = (string)sublink.Element("link");
                        int linksRowsChanged = linksCmd.ExecuteNonQuery();
                    }

                }
            }
        }
    }
}

这篇关于将英语维基百科转储导入SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆