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

使用PowerShell读取Excel/CSV生成单个TMSL分区刷新文件

批量生成包含所有分区的单个TMSL删除脚本

问题背景

现有Excel配置文件(CSV格式如下)记录需要删除的分区信息:

"DatabaseName","SchemaName","TableName","PartitionName"
"AdventureWorks2","fact","Internet Sales","Internet Sales - Current Month"
"AdventureWorks2","fact","Internet Sales","Internet Sales - M-1"
"AdventureWorks2","fact","Internet Sales","Internet Sales - M-2"

用于删除分区的TMSL模板格式如下:

{
  "refresh": {
    "type": "automatic",
    "objects": [
      {
        "database": "~DBName~",
        "table": "~TableName~",
        "partition": "~PartitionName~"
      }
    ]
  }
}

原PowerShell脚本仅能为每个分区生成独立TMSL文件,需修改为生成包含所有分区的单个TMSL文件。

修改后的PowerShell脚本

# 路径配置
$TemplatePath = "..\PartitionTemplate.tmsl"
$ConfigFile = "..\Delete-ParttionConfigFile.xlsx"
$TgtPath = "..\03_ProcessedTMSLFiles\"
$FinalTMSLFileName = "All_Partitions_Delete.tmsl"
$FinalDestinationTMSLPath = Join-Path -Path $TgtPath -ChildPath $FinalTMSLFileName

# 导入Excel配置数据(需提前安装Import-Excel模块)
$PartitionExtract = Import-Excel $ConfigFile

# 读取TMSL模板并解析为可操作的JSON对象
$tmslTemplate = Get-Content -Path $TemplatePath -Raw | ConvertFrom-Json

# 清空模板中原有的单个分区对象,准备批量添加
$tmslTemplate.refresh.objects = @()

# 遍历所有分区记录,生成TMSL格式的分区对象并添加到列表
foreach ($PartitionRecord in $PartitionExtract) {
    $partitionObject = [PSCustomObject]@{
        database   = $PartitionRecord.DatabaseName
        table      = "$($PartitionRecord.SchemaName).$($PartitionRecord.TableName)"  # TMSL要求表名包含Schema
        partition  = $PartitionRecord.PartitionName
    }
    $tmslTemplate.refresh.objects += $partitionObject
}

# 将整合后的JSON对象转换为格式化的TMSL文本并保存
$tmslTemplate | ConvertTo-Json -Depth 10 | Set-Content -Path $FinalDestinationTMSLPath -Encoding UTF8

Write-Host "单个TMSL删除文件已生成:$FinalDestinationTMSLPath"

核心修改说明

  1. 改用JSON对象操作:不再通过字符串替换修改模板,直接解析TMSL为PowerShell可操作的JSON对象,彻底避免手动字符串拼接导致的格式错误
  2. 批量填充分区列表:清空模板中默认的单个分区对象,遍历Excel配置里的每条记录,生成符合TMSL格式的分区对象并批量添加到objects数组
  3. 补全Schema表名:TMSL中指定表必须包含Schema(格式为SchemaName.TableName),脚本自动拼接配置中的Schema和TableName字段
  4. 单文件输出:最后将整合了所有分区的JSON对象转换为格式化的TMSL文本,直接保存为单个文件

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 12:05:42