-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPsMapReduxExample.ps1
More file actions
90 lines (72 loc) · 4.29 KB
/
PsMapReduxExample.ps1
File metadata and controls
90 lines (72 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<#
-------------------------------------------------------------------
An example of how to use the PsMapRedux PowerShell Module.
#>
cls
# Import our MapRedux module...
$Path = Split-Path -Path $MyInvocation.MyCommand.Path -Parent
Import-Module -name ($Path + "\PsMapRedux")
# Import the SQL Server Module.
Import-Module “sqlps” -DisableNameChecking
Set-Location SQLSERVER:\sql\mydbserver\default\databases\MyDb
$query = "SELECT TOP 5000 Key1, Date, Value1 FROM MonitoringHourlyBiogas ORDER BY Key1";
Write-Host "Query: $query"
$dataset = Invoke-SqlCmd -query $query
# Define the Map function
$MyMap =
{
Param
(
[PsObject] $dataset
)
Write-Host ($env:computername + "::Map");
$list = @{};
foreach($row in $dataset.Data)
{
if($list.ContainsKey($row.Key1) -eq $true)
{
$s = $list.Item($row.Key1);
$s.Sum += $row.Value1;
$s.Count++;
}
else
{
$s = New-Object PSObject;
$s | Add-Member -Type NoteProperty -Name Key1 -Value $row.Key1;
$s | Add-Member -type NoteProperty -Name Sum -Value $row.Value1;
$s | Add-Member -type NoteProperty -Name Count -Value 1;
$list.Add($row.Key1, $s);
}
}
Write-Output $list;
}
# Define the Reduce Function
$MyReduce =
{
Param
(
[object] $key,
[PSObject] $dataset
)
Write-Host ($env:computername + "::Reduce - Count: " + $dataset.Data.Count)
$redux = @{};
foreach($s in $dataset.Data)
{
$sum += $s.Sum;
$count += $s.Count;
}
# Reduce
$redux.Add($s.Key1, $sum / $count);
# Return
Write-Output $redux;
}
# Create the item data
$Mr = New-MapReduxItem "My Example MapReduce Job" $MyMap $MyReduce
$MyNodes = ("node1",
"node2",
"node3")
# Run the Map Reduce routine...
$MyMrResults = Invoke-MapRedux -MapReduceItem $Mr -ComputerName $MyNodes -DataSet $dataset -Verbose
# Show the results
Set-Location C:\
$MyMrResults | Out-GridView