You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过PowerShell将CSV数据导入Azure Data Explorer集群?

用PowerShell向Azure Data Explorer(ADX)集群插入CSV数据的方案

当然可以通过PowerShell向ADX集群插入CSV数据,下面是两种实用方案,适配不同场景:

方法一:使用官方Kusto.PowerShell模块(推荐)

这是微软官方提供的PowerShell模块,操作简单且稳定,适合大部分场景:

  1. 先安装模块(如果未安装):
Install-Module -Name Kusto.PowerShell -Scope CurrentUser -Force
  1. 连接ADX集群并执行插入操作:
# 替换为你的集群、数据库、表及CSV路径信息
$clusterUri = "https://<集群名称>.<区域>.kusto.windows.net"
$databaseName = "<数据库名称>"
$tableName = "<目标表名称>"
$csvFilePath = "C:\local\path\to\your\data.csv"

# 连接到ADX集群(会自动触发AAD身份验证)
Connect-AzKustoCluster -ClusterUri $clusterUri

# 执行CSV数据导入,ignoreFirstRecord=true适用于CSV包含表头的情况
$ingestQuery = ".ingest into table $tableName ('$csvFilePath') with (format='csv', ignoreFirstRecord=true)"
Invoke-KustoCommand -ClusterUri $clusterUri -DatabaseName $databaseName -Query $ingestQuery

方法二:使用Kusto .NET SDK(适配自定义场景)

如果遇到模块安装或加载异常(比如你之前碰到的DLL加载问题),可以直接使用Kusto .NET SDK实现:

  1. 先获取Kusto.Data和Kusto.Data.Net.Client的DLL文件(可通过NuGet包下载提取)
  2. 编写PowerShell脚本执行插入:
# 替换为你的DLL实际路径
Add-Type -Path "C:\packages\Kusto.Data\lib\netstandard2.0\Kusto.Data.dll"
Add-Type -Path "C:\packages\Kusto.Data.Net.Client\lib\netstandard2.0\Kusto.Data.Net.Client.dll"

# 定义连接信息
$clusterUri = "https://<集群名称>.<区域>.kusto.windows.net"
$databaseName = "<数据库名称>"
$tableName = "<目标表名称>"
$csvFilePath = "C:\local\path\to\your\data.csv"

# 构建AAD认证的连接字符串(若使用服务账号,可替换为应用ID/密钥/租户ID的格式)
$connectionString = "Data Source=$clusterUri;Initial Catalog=$databaseName;Fed=True"

# 创建管理客户端并执行导入命令
$adminClient = [Kusto.Data.Net.Client.KustoClientFactory]::CreateCslAdminProvider($connectionString)
$ingestCommand = ".ingest into table $tableName ('$csvFilePath') with (format='csv', ignoreFirstRecord=true)"
$adminClient.ExecuteControlCommand($databaseName, $ingestCommand)

关键注意事项

  • 权限要求:执行脚本的账号需要拥有ADX集群的Ingestor权限,或目标数据库的Admin权限,否则会触发权限不足错误。
  • 数据匹配:CSV文件的列顺序、数据类型必须与ADX目标表完全一致,否则会导致插入失败或数据失真。
  • 大文件处理:若CSV文件体积较大,建议启用ADX的批量导入机制,或使用.ingest async命令异步执行导入。

内容的提问来源于stack exchange,提问作者user9297554

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 11:48:23