71 lines
2.8 KiB
C#
71 lines
2.8 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Npgsql;
|
|
using OpcPks.Core.Data;
|
|
|
|
namespace OpcPks.Core.Data
|
|
{
|
|
public class AssetLoader
|
|
{
|
|
public async Task ImportFullMapAsync(string csvPath)
|
|
{
|
|
Console.WriteLine($"🚀 CSV 데이터 로드 시작: {csvPath}");
|
|
|
|
if (!File.Exists(csvPath))
|
|
{
|
|
Console.WriteLine("❌ 파일을 찾을 수 없습니다.");
|
|
return;
|
|
}
|
|
|
|
// CSV 파일 읽기 (첫 줄 헤더 제외)
|
|
var lines = await File.ReadAllLinesAsync(csvPath);
|
|
Console.WriteLine($"📊 총 {lines.Length - 1}개의 라인을 처리합니다...");
|
|
|
|
using var conn = new NpgsqlConnection(DbConfig.ConnectionString);
|
|
await conn.OpenAsync();
|
|
|
|
// PostgreSQL Binary Copy 시작
|
|
// level, class, name, node_id, data_type 총 5개 필드
|
|
using (var writer = conn.BeginBinaryImport("COPY raw_node_map (level, class, name, node_id, data_type) FROM STDIN (FORMAT BINARY)"))
|
|
{
|
|
int count = 0;
|
|
for (int i = 1; i < lines.Length; i++) // i=1 부터 시작하여 헤더 스킵
|
|
{
|
|
var line = lines[i];
|
|
if (string.IsNullOrWhiteSpace(line)) continue;
|
|
|
|
var cols = line.Split(',');
|
|
if (cols.Length < 4) continue; // 최소 4개 필드(NodeId까지)는 있어야 함
|
|
|
|
try
|
|
{
|
|
writer.StartRow();
|
|
// 1. Level (int)
|
|
writer.Write(int.Parse(cols[0]), NpgsqlTypes.NpgsqlDbType.Integer);
|
|
// 2. Class (string)
|
|
writer.Write(cols[1], NpgsqlTypes.NpgsqlDbType.Text);
|
|
// 3. Name (string)
|
|
writer.Write(cols[2], NpgsqlTypes.NpgsqlDbType.Text);
|
|
// 4. NodeId (string)
|
|
writer.Write(cols[3], NpgsqlTypes.NpgsqlDbType.Text);
|
|
|
|
// 5. DataType (데이터가 없으면 "Unknown" 처리)
|
|
string dataType = cols.Length > 4 ? cols[4].Trim() : "Unknown";
|
|
writer.Write(dataType, NpgsqlTypes.NpgsqlDbType.Text);
|
|
|
|
count++;
|
|
if (count % 50000 == 0) Console.WriteLine($"... {count}개 처리 중");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"⚠️ 에러 발생 (라인 {i+1}): {ex.Message}");
|
|
}
|
|
}
|
|
await writer.CompleteAsync();
|
|
Console.WriteLine($"✅ 총 {count}개 노드 로드 완료!");
|
|
}
|
|
}
|
|
}
|
|
} |