我正在尝试将此bash脚本转换为powershell。我这样做是因为Linux的Windows子系统目前无法在Google Drive File System的已安装驱动器上运行,因此我需要使用PowerShell。
我需要在顶级父文件夹上运行此powershell脚本,以在~7000,000个深层嵌套文件夹中导入8700万个json文件。我愿意接受更好的方法。
#!/bin/sh
function import_from_start() {
echo "starting import from front"
find "$1" -name '*.json' | while read file; do
mongoimport --host=datalake7 --db=CA_facebook_copy --collection=test_import --type="json" --file="$file"
done
}
function import_from_end() {
echo "starting import from back end"
find "$1" -name '*.json' | sort -r | cut -f2 | while read file; do
mongoimport --host=datalake7 --db=CA_facebook_copy --collection=test_import --type="json" --file="$file"
done
}
import_from_start "$1" &
import_from_end "$1"
param (
[Parameter(Mandatory=$true)][string]$Src,
[Parameter(Mandatory=$true)][string]$Collection
)
$Extension = '*.json'
Get-ChildItem -Path $Src -Filter $Extension -Recurse | Where-Object {!$_.PsIsContainer} | ForEach-Object {
.\mongoimport.exe --host datalake7 --db CA_facebook_copy --collection $Collection --type json --file $_.FullName
}
我需要同时从文件列表的开头和结尾导入。我正在使用这种方法,因为创建和销毁每个文件的mongodb连接非常慢。
答案 0 :(得分:1)
现在排序可以基于任何列。因此,在您的情况下,您可以使用所有文件 fullname.length ,因为您使用了fullname并且可以按降序对其进行排序。
替换
Get-ChildItem -Path $Src -Filter $Extension -Recurse | Where-Object {!$_.PsIsContainer} | ForEach-Object {
.\mongoimport.exe --host datalake7 --db CA_facebook_copy --collection $Collection --type json --file $_.FullName
}
有了这个:
Get-ChildItem -Path $Src -Filter $Extension -Recurse | Where-Object {!$_.PsIsContainer} | ForEach-Object {
.\mongoimport.exe --host datalake7 --db CA_facebook_copy --collection $Collection --type json --file $_.FullName
}| Sort-Object @{expression = {$_.fullname.length}} -descending
我在foreach循环之后添加了sort对象部分。
希望它有所帮助。