Импортирует XML в SQL Server, но пытается сделать несколько записей, если существует несколько результатов для дочернего элемента

.equals() сравнивает данные в классе (при условии, что функция реализована). == сравнивает местоположения указателя (расположение объекта в памяти).

== возвращает true, если оба объекта (NOT TALKING OF PRIMITIVES) указывают на экземпляр SAME. .equals() возвращает true, если два объекта содержат одни и те же данные equals() Versus == в Java

Это может вам помочь.

1
задан James Z 10 March 2019 в 07:28
поделиться

2 ответа

Это должно увести тебя довольно далеко. Он полностью не проверен, поэтому, пожалуйста, прочитайте код, поймите его и внесите соответствующие изменения, чтобы заставить его работать.

Я удалил function и вставил весь код в цикл вместо этого, функция была слишком громоздкой на мой вкус. Теперь вы должны более четко видеть, что происходит.

Фактически это один и тот же код два раза, с небольшим дополнительным шагом, который добавляет собственные ссылки, чтобы вы могли запрашивать каждый продукт через его первичный идентификатор и его вторичные идентификаторы таким же образом, как обсуждалось в комментариях. ]

$connectionString = "Data Source=Apps2\Apps2;Initial Catalog=ICECAT;Integrated Security=SSPI"
$batchSize = 50000

# set up [files_index] datatable & read schema from DB
$files_index_table = New-Object System.Data.DataTable
$files_index_adapter = New-Object System.Data.SqlClient.SqlDataAdapter("SELECT * FROM files_index WHERE 0 = 1", $connectionString)
$files_index_adapter.Fill($files_index_table) | Out-Null
$files_index_bcp = New-Object SqlBulkCopy($connectionString)
$files_index_bcp.DestinationTableName = "dbo.files_index"
$files_index_count = 0

# set up [product_ids] datatable & read schema from DB
$product_ids_table = New-Object System.Data.DataTable
$product_ids_adapter = New-Object System.Data.SqlClient.SqlDataAdapter("SELECT * FROM product_ids WHERE 0 = 1", $connectionString)
$product_ids_adapter.Fill($product_ids_table) | Out-Null
$product_ids_bcp = New-Object System.Data.SqlClient.SqlBulkCopy($connectionString)
$product_ids_bcp.DestinationTableName = "dbo.product_ids"
$product_ids_count = 0

# main import loop
$xmlReader = New-Object System.Xml.XmlTextReader("C:\Scripts\icecat\files.index.xml")
while ($xmlReader.Read()) {
    # skip any XML nodes that aren't elements
    if ($xmlReader.NodeType -ne [System.Xml.XmlNodeType]::Element) { continue }

    # handle <file> elements
    if ($xmlReader.Name -eq "file") {
        $files_index_count++

        # remember current product ID, we'll need it when we hit the next <M_Prod_ID> element
        $curr_product_id = $xmlReader.GetAttribute("Product_ID")
        $is_new_file = $true

        $newRow = $files_index_table.NewRow()
        $newRow["Product_ID"] = $xmlReader.GetAttribute("Product_ID")
        $newRow["path"] = $xmlReader.GetAttribute("path")
        $newRow["Updated"] = $xmlReader.GetAttribute("Updated")
        $newRow["Quality"] = $xmlReader.GetAttribute("Quality")
        $newRow["Supplier_id"] = $xmlReader.GetAttribute("Supplier_id")
        $newRow["Prod_ID"] = $xmlReader.GetAttribute("Prod_ID")
        $newRow["Catid"] = $xmlReader.GetAttribute("Catid")
        $newRow["On_Market"] = $xmlReader.GetAttribute("On_Market")
        $newRow["Model_Name"] = $xmlReader.GetAttribute("Model_Name")
        $newRow["Product_View"] = $xmlReader.GetAttribute("Product_View")
        $newRow["HighPic"] = $xmlReader.GetAttribute("HighPic")
        $newRow["HighPicSize"] = $xmlReader.GetAttribute("HighPicSize")
        $newRow["HighPicWifiles_index_tableh"] = $xmlReader.GetAttribute("HighPicWifiles_index_tableh")
        $newRow["HighPicHeight"] = $xmlReader.GetAttribute("HighPicHeight")
        $newRow["Date_Added"] = $xmlReader.GetAttribute("Date_Added")
        $files_index_table.Rows.Add($newRow) | Out-Null

        if ($files_index_table.Rows.Count -eq $batchSize) {
            $files_index_bcp.WriteToServer($files_index_table)
            $files_index_table.Rows.Clear()
            Write-Host "$files_index_count <file> elements processed so far"
        }
    # handle <M_Prod_ID> elements
    } elseif ($xmlReader.Name -eq "M_Prod_ID") {
        $product_ids_count++

        # add self-reference row to the [product_ids] table
        # only for the first <M_Prod_ID> per <file> we need to do this
        if ($is_new_file) {
            $newRow = $product_ids_table.NewRow()
            $newRow["Product_ID"] = $curr_product_id  # from above
            $newRow["Alternative_ID"] = $curr_product_id
            $product_ids_table.Rows.Add($newRow) | Out-Null
            $is_new_file = $false
        }

        $newRow = $product_ids_table.NewRow()
        $newRow["Product_ID"] = $curr_product_id  # from above
        $newRow["Alternative_ID"] = $xmlReader.Value
        $product_ids_table.Rows.Add($newRow) | Out-Null

        if ($product_ids_table.Rows.Count -eq $batchSize) {
            $product_ids_bcp.WriteToServer($files_index_table)
            $product_ids_table.Rows.Clear()
            Write-Host "$product_ids_count <M_Prod_ID> elements processed so far"
        }
    }
}

# write any remaining rows to the server
if ($files_index_table.Rows.Count -gt 0) {
    $files_index_bcp.WriteToServer($files_index_table)
    $files_index_table.Rows.Clear()
}
Write-Host "$files_index_count <file> elements processed overall"

if ($product_ids_table.Rows.Count -gt 0) {
    $product_ids_bcp.WriteToServer($product_ids_table)
    $product_ids_table.Rows.Clear()
}
Write-Host "$product_ids_count <M_Prod_ID> elements processed overall"
0
ответ дан Tomalak 10 March 2019 в 07:28
поделиться

@Tomlak Мне удалось изменить ваш код и заставить его работать, большое спасибо за помощь, которую я не смог бы сделать без вашей помощи, и очень ценю вашу помощь и руководство, код, возможно, можно немного очистить, но Я не могу найти никаких недостатков в данных после нескольких дней тестирования. Я запустил его для файла объемом 3,6 ГБ, который произвел около 6,5 миллионов строк в таблице files_index и 7,4 миллиона строк в таблице product_ids, так что теперь у меня есть почти миллион потенциальных скусов, с которыми я потенциально могу сопоставить данные.

Я изменил его, чтобы он также добавил строку в таблицу product_ids, даже если нет дочернего узла M_Prod_ID с идентификатором продукта и Prod_ID, поэтому было проще создать представление, соответствующее данным. КОД НИЖЕ .......

> Blockquote$connectionString = "Data Source=Apps2\Apps2;Initial 
 Catalog=ICECATtesting;Integrated Security=SSPI"
$batchSize = 100000

 # set up [files_index] datatable & read schema from DB
$files_index_table = New-Object System.Data.DataTable;
$files_index_adapter = New-Object System.Data.SqlClient.SqlDataAdapter("SELECT * FROM 
files_index WHERE 0 = 1", $connectionString)
$files_index_adapter.Fill($files_index_table) | Out-Null;
$files_index_bcp = New-Object System.Data.SqlClient.SqlBulkCopy($connectionString)  
$files_index_bcp.DestinationTableName = "dbo.files_index"
$files_index_count = 0;

# set up [product_ids] datatable & read schema from DB
$product_ids_table = New-Object System.Data.DataTable
$product_ids_adapter = New-Object System.Data.SqlClient.SqlDataAdapter("SELECT * FROM 
product_ids WHERE 0 = 1", $connectionString)
$product_ids_adapter.Fill($product_ids_table) | Out-Null
$product_ids_bcp = New-Object System.Data.SqlClient.SqlBulkCopy($connectionString)
$product_ids_bcp.DestinationTableName = "dbo.product_ids"
$product_ids_count = 0

 # main import loop

$xmlReader = New-Object System.Xml.XmlTextReader("C:\Scripts\icecat\files.index.xml")
while ($xmlReader.Read()) {
# skip any XML nodes that aren't elements
if ($xmlReader.NodeType -ne [System.Xml.XmlNodeType]::Element) { continue }

# handle <file> elements
if ($xmlReader.Name -eq "file") {
    $files_index_count++

    # remember current product ID, we'll need it when we hit the next <M_Prod_ID> 
  element also add the Prod_ID from the file node
    $curr_product_id = $xmlReader.GetAttribute("Product_ID")
    $curr_prod_id = $xmlReader.GetAttribute("Prod_ID")
    $is_new_file = $false

    $newRow = $files_index_table.NewRow()
    $newRow["Product_ID"] = $xmlReader.GetAttribute("Product_ID")
    $newRow["path"] = $xmlReader.GetAttribute("path")
    $newRow["Updated"] = $xmlReader.GetAttribute("Updated")
    $newRow["Quality"] = $xmlReader.GetAttribute("Quality")
    $newRow["Supplier_id"] = $xmlReader.GetAttribute("Supplier_id")
    $newRow["Prod_ID"] = $xmlReader.GetAttribute("Prod_ID")
    $newRow["Catid"] = $xmlReader.GetAttribute("Catid")
    $newRow["On_Market"] = $xmlReader.GetAttribute("On_Market")
    $newRow["Model_Name"] = $xmlReader.GetAttribute("Model_Name")
    $newRow["Product_View"] = $xmlReader.GetAttribute("Product_View")
    $newRow["HighPic"] = $xmlReader.GetAttribute("HighPic")
    $newRow["HighPicSize"] = $xmlReader.GetAttribute("HighPicSize")
    $newRow["HighPicWidth"] = $xmlReader.GetAttribute("HighPicWidth")
    $newRow["HighPicHeight"] = $xmlReader.GetAttribute("HighPicHeight")
    $newRow["Date_Added"] = $xmlReader.GetAttribute("Date_Added")
    $Firstproduct_id = $xmlreader.GetAttribute("Product_ID")
    $Firstprod_id = $xmlreader.GetAttribute("Prod_ID")
    $newfilenode = $true

    $files_index_table.Rows.Add($newRow) | Out-Null
    $newRow = $product_ids_table.NewRow()
    $newRow["Product_ID"] = $curr_product_id  # from above
    $newRow["Alternative_ID"] = $curr_prod_id
    $product_ids_table.Rows.Add($newRow) | Out-Null


    if ($files_index_table.Rows.Count -eq $batchSize) {
        $files_index_bcp.WriteToServer($files_index_table)
        $files_index_table.Rows.Clear()
        Write-Host "$files_index_count <file> elements processed so far"

    }
    # handle <M_Prod_ID> elements
    } elseif ($xmlReader.Name -eq "M_Prod_ID") {
         $product_ids_count++

         # add self-reference row to the [product_ids] table also I added the Prod_ID 
from the file node so I can make a view to match all variants

    # only for the first <M_Prod_ID> per <file> we need to do this


        $xmlreader.read()
        $newRow = $product_ids_table.NewRow()
        $newRow["Product_ID"] = $curr_product_id  # from above
        $newRow["Alternative_ID"] = $xmlReader.Value
        $product_ids_table.Rows.Add($newRow) | Out-Null


    if ($product_ids_table.Rows.Count -eq $batchSize) {
        $product_ids_bcp.WriteToServer($product_ids_table)
        $product_ids_table.Rows.Clear()
        Write-Host "$product_ids_count <M_Prod_ID> elements processed so far"
    }
  }
 }

 # write any remaining rows to the server
if ($files_index_table.Rows.Count -gt 0) {
$files_index_bcp.WriteToServer($files_index_table)
$files_index_table.Rows.Clear()
}
Write-Host "$files_index_count <file> elements processed overall"

if ($product_ids_table.Rows.Count -gt 0) {
    $product_ids_bcp.WriteToServer($product_ids_table)
    $product_ids_table.Rows.Clear()
 }
 Write-Host "$product_ids_count <M_Prod_ID> elements processed overall"
0
ответ дан John Spencer 10 March 2019 в 07:28
поделиться
Другие вопросы по тегам:

Похожие вопросы: