Skip to content

Commit

Permalink
Modified UDAGG example and added new sample file and image files
Browse files Browse the repository at this point in the history
  • Loading branch information
Rukmani Gopalan committed Jun 21, 2016
1 parent 453ccfc commit 943c434
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
This script uses user defined operators and aggregates
*/

DECLARE @INPUT_FILE string = @"/Samples/Data/AmbulanceData/Drivers_shorter.txt";
DECLARE @INPUT_FILE string = @"/Samples/Data/AmbulanceData/Drivers.txt";

//Extract first and last names as separate columns using a custom extractor
@drivers =
Expand Down Expand Up @@ -45,13 +45,18 @@ OUTPUT @drivers_processed
TO @"/Samples/Output/drivers_processed.txt"
USING Outputters.Csv(quoting:false);

//Use a user defined aggregator to count the drivers from countries that you want to specify
@driver_count =
SELECT country,
AGG<Samples.SampleAggregate>(country) AS count
FROM @drivers
GROUP BY country;
//Use a user defined aggregator to calculate balance
//If the transaction type is "Debit", subtract from the balance, if the transaction type is "Credit", add to the balance
@transactions =
EXTRACT transaction string,
amount int
FROM @"/Samples/Data/transactions.txt"
USING Extractors.Csv();

OUTPUT @driver_count
TO @"/Samples/Output/driver_count.txt"
@balance =
SELECT AGG<Samples.SampleAggregate>(transaction, amount) AS balance
FROM @transactions;

OUTPUT @balance
TO @"/Samples/Output/balance.txt"
USING Outputters.Csv();
Original file line number Diff line number Diff line change
Expand Up @@ -86,29 +86,31 @@ public override IRow Process(IRow input, IUpdatableRow output)
}
}

//User defined aggregate to calculate count of drivers from each region (North America, South America, Europe)
public class SampleAggregate : IAggregate<string, int>
//User defined aggregate to calculate the total balance by adding or subtracting based on whether its credit or debit
public class SampleAggregate : IAggregate<string, int, int>
{
int count;
List<string> countries;

int balance;

public override void Init()
{
count = 0;
countries = new List<string>() { "Germany", "Mexico", "Argentina", "Spain", "UK" };
balance = 0;
}

public override void Accumulate(string country)
public override void Accumulate(string transaction, int amount)
{
if (countries.Contains(country))
if(transaction == "Credit")
{
balance += amount;
}
if(transaction == "Debit")
{
count += 1;
balance -= amount;
}
}

public override int Terminate()
{
return count;
return balance;
}

}
Expand Down
2 changes: 1 addition & 1 deletion Examples/Extensibility-Simple-Examples/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

This directory contains a sample project that shows how custom code can be used as inline C#, user defined functions (UDFs), user defined objects (UDOs) or user defined aggregates (UDAGGs) in U-SQL

The project operates on Drivers.txt found in \Samples\Data\AmbulanceDemos folder.
The project operates on Drivers.txt found in \Samples\Data\AmbulanceDemos folder and transactions.txt found in the \Samples\Data folder.
Binary file added Examples/Samples/Data/images/guard.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions Examples/Samples/Data/transactions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Debit,100
Credit,500
Credit,200
Credit,700
Debit,50
Debit,40
Credit,670
Debit,500
Credit,1000
Debit,100

0 comments on commit 943c434

Please sign in to comment.