-
Notifications
You must be signed in to change notification settings - Fork 211
/
Copy pathPairRDDSamples.cs
526 lines (444 loc) · 20.3 KB
/
PairRDDSamples.cs
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Microsoft.Spark.CSharp.Core;
using NUnit.Framework;
namespace Microsoft.Spark.CSharp.Samples
{
class PairRDDSamples
{
[Sample]
internal static void PairRDDCollectAsMapSample()
{
var map = SparkCLRSamples.SparkContext.Parallelize(new[] { new Tuple<int, int>(1, 2), new Tuple<int, int>(3, 4) }, 1).CollectAsMap();
foreach (var kv in map)
Console.WriteLine(kv);
if (SparkCLRSamples.Configuration.IsValidationEnabled)
{
Assert.IsTrue(map.ContainsKey(1) && map[1] == 2);
Assert.IsTrue(map.ContainsKey(1) && map[3] == 4);
}
}
[Sample]
internal static void PairRDDKeysSample()
{
var keys = SparkCLRSamples.SparkContext.Parallelize(new[] { new Tuple<int, int>(1, 2), new Tuple<int, int>(3, 4) }, 1).Keys().Collect();
Console.WriteLine(keys[0]);
Console.WriteLine(keys[1]);
if (SparkCLRSamples.Configuration.IsValidationEnabled)
{
Assert.AreEqual(1, keys[0]);
Assert.AreEqual(3, keys[1]);
}
}
[Sample]
internal static void PairRDDValuesSample()
{
var values = SparkCLRSamples.SparkContext.Parallelize(new[] { new Tuple<int, int>(1, 2), new Tuple<int, int>(3, 4) }, 1).Values().Collect();
Console.WriteLine(values[0]);
Console.WriteLine(values[1]);
if (SparkCLRSamples.Configuration.IsValidationEnabled)
{
Assert.AreEqual(2, values[0]);
Assert.AreEqual(4, values[1]);
}
}
[Sample]
internal static void PairRDDReduceByKeySample()
{
var reduced = SparkCLRSamples.SparkContext.Parallelize(
new[]
{
new Tuple<string, int>("a", 1),
new Tuple<string, int>("b", 1),
new Tuple<string, int>("a", 1)
}, 2)
.ReduceByKey((x, y) => x + y).Collect();
foreach (var kv in reduced)
Console.WriteLine(kv);
if (SparkCLRSamples.Configuration.IsValidationEnabled)
{
Assert.IsTrue(reduced.Contains(new Tuple<string, int>("a", 2)));
Assert.IsTrue(reduced.Contains(new Tuple<string, int>("b", 1)));
}
}
[Sample]
internal static void PairRDDReduceByKeyLocallySample()
{
var reduced = SparkCLRSamples.SparkContext.Parallelize(
new[]
{
new Tuple<string, int>("a", 1),
new Tuple<string, int>("b", 1),
new Tuple<string, int>("a", 1)
}, 2)
.ReduceByKeyLocally((x, y) => x + y);
foreach (var kv in reduced)
Console.WriteLine(kv);
if (SparkCLRSamples.Configuration.IsValidationEnabled)
{
Assert.IsTrue(reduced.Contains(new KeyValuePair<string, int>("a", 2)));
Assert.IsTrue(reduced.Contains(new KeyValuePair<string, int>("b", 1)));
}
}
[Sample]
internal static void PairRDDCountByKeySample()
{
var countByKey = SparkCLRSamples.SparkContext.Parallelize(
new[]
{
new Tuple<string, int>("a", 1),
new Tuple<string, int>("b", 1),
new Tuple<string, int>("a", 1)
}, 2)
.CountByKey()
.ToDictionary(k => k.Item1, v => v.Item2);
foreach (var kv in countByKey)
Console.WriteLine(kv);
if (SparkCLRSamples.Configuration.IsValidationEnabled)
{
Assert.AreEqual(2, countByKey["a"]);
Assert.AreEqual(1, countByKey["b"]);
}
}
[Sample]
internal static void PairRDDJoinSample()
{
var l = SparkCLRSamples.SparkContext.Parallelize(
new[]
{
new Tuple<string, int>("a", 1),
new Tuple<string, int>("b", 4),
}, 1);
var r = SparkCLRSamples.SparkContext.Parallelize(
new[]
{
new Tuple<string, int>("a", 2),
new Tuple<string, int>("a", 3),
}, 1);
var joined = l.Join(r, 2).Collect();
foreach (var kv in joined)
Console.WriteLine(kv);
if (SparkCLRSamples.Configuration.IsValidationEnabled)
{
Assert.IsTrue(joined.Contains(new Tuple<string, Tuple<int, int>>("a", new Tuple<int, int>(1, 2))));
Assert.IsTrue(joined.Contains(new Tuple<string, Tuple<int, int>>("a", new Tuple<int, int>(1, 3))));
}
}
[Sample]
internal static void PairRDDLeftOuterJoinSample()
{
var l = SparkCLRSamples.SparkContext.Parallelize(
new[]
{
new Tuple<string, int>("a", 1),
new Tuple<string, int>("b", 4),
}, 2);
var r = SparkCLRSamples.SparkContext.Parallelize(
new[]
{
new Tuple<string, int>("a", 2),
}, 1);
var joined = l.LeftOuterJoin(r).Collect();
foreach (var kv in joined)
Console.WriteLine(kv);
if (SparkCLRSamples.Configuration.IsValidationEnabled)
{
Assert.IsTrue(joined.Any(kv => kv.Item1 == "a" && kv.Item2.Item1 == 1 && kv.Item2.Item2.IsDefined && kv.Item2.Item2.GetValue() == 2));
Assert.IsTrue(joined.Any(kv => kv.Item1 == "b" && kv.Item2.Item1 == 4 && !kv.Item2.Item2.IsDefined));
}
}
[Sample]
internal static void PairRDDRightOuterJoinSample()
{
var l = SparkCLRSamples.SparkContext.Parallelize(
new[]
{
new Tuple<string, int>("a", 2),
}, 1);
var r = SparkCLRSamples.SparkContext.Parallelize(
new[]
{
new Tuple<string, int>("a", 1),
new Tuple<string, int>("b", 4),
}, 2);
var joined = l.RightOuterJoin(r).Collect();
foreach (var kv in joined)
Console.WriteLine(kv);
if (SparkCLRSamples.Configuration.IsValidationEnabled)
{
Assert.IsTrue(joined.Any(kv => kv.Item1 == "a" && kv.Item2.Item1.IsDefined && kv.Item2.Item1.GetValue() == 2 && kv.Item2.Item2 == 1));
Assert.IsTrue(joined.Any(kv => kv.Item1 == "b" && !kv.Item2.Item1.IsDefined && kv.Item2.Item2 == 4));
}
}
[Sample]
internal static void PairRDDFullOuterJoinSample()
{
var l = SparkCLRSamples.SparkContext.Parallelize(
new[]
{
new Tuple<string, int>("a", 1),
new Tuple<string, int>("b", 4),
}, 2);
var r = SparkCLRSamples.SparkContext.Parallelize(
new[]
{
new Tuple<string, int>("a", 2),
new Tuple<string, int>("c", 8),
}, 2);
var joined = l.FullOuterJoin(r).Collect();
foreach (var kv in joined)
Console.WriteLine(kv);
if (SparkCLRSamples.Configuration.IsValidationEnabled)
{
Assert.IsTrue(joined.Any(kv => kv.Item1 == "a" && kv.Item2.Item1.IsDefined && kv.Item2.Item1.GetValue() == 1 &&
kv.Item2.Item2.IsDefined && kv.Item2.Item2.GetValue() == 2));
Assert.IsTrue(joined.Any(kv => kv.Item1 == "b" && kv.Item2.Item1.IsDefined && kv.Item2.Item1.GetValue() == 4 &&
!kv.Item2.Item2.IsDefined));
Assert.IsTrue(joined.Any(kv => kv.Item1 == "c" && !kv.Item2.Item1.IsDefined &&
kv.Item2.Item2.IsDefined && kv.Item2.Item2.GetValue() == 8));
}
}
[Sample]
internal static void PairRDDPartitionBySample()
{
Func<dynamic, int> partitionFunc = key =>
{
if (key < 3) return 1;
if (key >= 3 && key < 6) return 2;
else return 3;
};
var partitioned = SparkCLRSamples.SparkContext.Parallelize(new[] { 1, 2, 3, 4, 5, 6, 1 }, 3)
.Map(x => new Tuple<int, int>(x, x + 100))
.PartitionBy(3, partitionFunc)
.Glom()
.Collect();
foreach (var partition in partitioned)
{
foreach (var kv in partition)
{
Console.Write(kv + " ");
}
Console.WriteLine();
}
if (SparkCLRSamples.Configuration.IsValidationEnabled)
{
Assert.AreEqual(3, partitioned.Length);
// Assert that the partition distribution is correct with partitionFunc
Assert.IsTrue(partitioned.Count(p => p.All(key => key.Item1 < 3)) == 1);
Assert.IsTrue(partitioned.Count(p => p.All(key => key.Item1 >= 3 && key.Item1 < 6)) == 1);
Assert.IsTrue(partitioned.Count(p => p.All(key => key.Item1 >= 6)) == 1);
}
}
[Sample]
internal static void PairRDDCombineByKeySample()
{
var combineByKey = SparkCLRSamples.SparkContext.Parallelize(
new[]
{
new Tuple<string, int>("a", 1),
new Tuple<string, int>("b", 1),
new Tuple<string, int>("a", 1)
}, 2)
.CombineByKey(() => string.Empty, (x, y) => x + y.ToString(CultureInfo.InvariantCulture), (x, y) => x + y).Collect();
foreach (var kv in combineByKey)
Console.WriteLine(kv);
if (SparkCLRSamples.Configuration.IsValidationEnabled)
{
Assert.IsTrue(combineByKey.Contains(new Tuple<string, string>("a", "11")));
Assert.IsTrue(combineByKey.Contains(new Tuple<string, string>("b", "1")));
}
}
[Sample]
internal static void PairRDDAggregateByKeySample()
{
var aggregateByKey = SparkCLRSamples.SparkContext.Parallelize(
new[]
{
new Tuple<string, int>("a", 1),
new Tuple<string, int>("b", 1),
new Tuple<string, int>("a", 1)
}, 2)
.AggregateByKey(() => 0, (x, y) => x + y, (x, y) => x + y).Collect();
foreach (var kv in aggregateByKey)
Console.WriteLine(kv);
if (SparkCLRSamples.Configuration.IsValidationEnabled)
{
Assert.IsTrue(aggregateByKey.Contains(new Tuple<string, int>("a", 2)));
Assert.IsTrue(aggregateByKey.Contains(new Tuple<string, int>("b", 1)));
}
}
[Sample]
internal static void PairRDDFoldByKeySample()
{
var FoldByKey = SparkCLRSamples.SparkContext.Parallelize(
new[]
{
new Tuple<string, int>("a", 1),
new Tuple<string, int>("b", 1),
new Tuple<string, int>("a", 1)
}, 2)
.FoldByKey(() => 0, (x, y) => x + y).Collect();
foreach (var kv in FoldByKey)
Console.WriteLine(kv);
if (SparkCLRSamples.Configuration.IsValidationEnabled)
{
Assert.IsTrue(FoldByKey.Contains(new Tuple<string, int>("a", 2)));
Assert.IsTrue(FoldByKey.Contains(new Tuple<string, int>("b", 1)));
}
}
[Sample]
internal static void PairRDDGroupByKeySample()
{
var groupByKey = SparkCLRSamples.SparkContext.Parallelize(
new[]
{
new Tuple<string, int>("a", 1),
new Tuple<string, int>("b", 1),
new Tuple<string, int>("a", 1)
}, 2)
.GroupByKey().Collect();
foreach (var kv in groupByKey)
Console.WriteLine(kv.Item1 + ", " + "(" + string.Join(",", kv.Item2) + ")");
if (SparkCLRSamples.Configuration.IsValidationEnabled)
{
Assert.IsTrue(groupByKey.Any(kv => kv.Item1 == "a" && kv.Item2.Count == 2 && kv.Item2[0] == 1 && kv.Item2[1] == 1));
Assert.IsTrue(groupByKey.Any(kv => kv.Item1 == "b" && kv.Item2.Count == 1 && kv.Item2[0] == 1));
}
}
[Sample]
internal static void PairRDDMapValuesSample()
{
var mapValues = SparkCLRSamples.SparkContext.Parallelize(
new[]
{
new Tuple<string, string[]>("a", new[]{"apple", "banana", "lemon"}),
new Tuple<string, string[]>("b", new[]{"grapes"})
}, 2)
.MapValues(x => x.Length).Collect();
foreach (var kv in mapValues)
Console.WriteLine(kv);
if (SparkCLRSamples.Configuration.IsValidationEnabled)
{
Assert.IsTrue(mapValues.Any(kv => kv.Item1 == "a" && kv.Item2 == 3));
Assert.IsTrue(mapValues.Any(kv => kv.Item1 == "b" && kv.Item2 == 1));
}
}
[Sample]
internal static void PairRDDFlatMapValuesSample()
{
var flatMapValues = SparkCLRSamples.SparkContext.Parallelize(
new[]
{
new Tuple<string, string[]>("a", new[]{"x", "y", "z"}),
new Tuple<string, string[]>("b", new[]{"p", "r"})
}, 2)
.FlatMapValues(x => x).Collect();
foreach (var kv in flatMapValues)
Console.WriteLine(kv);
if (SparkCLRSamples.Configuration.IsValidationEnabled)
{
Assert.IsTrue(flatMapValues.Any(kv => kv.Item1 == "a" && kv.Item2 == "x"));
Assert.IsTrue(flatMapValues.Any(kv => kv.Item1 == "a" && kv.Item2 == "y"));
Assert.IsTrue(flatMapValues.Any(kv => kv.Item1 == "a" && kv.Item2 == "z"));
Assert.IsTrue(flatMapValues.Any(kv => kv.Item1 == "b" && kv.Item2 == "p"));
Assert.IsTrue(flatMapValues.Any(kv => kv.Item1 == "b" && kv.Item2 == "r"));
}
}
[Sample]
internal static void PairRDDGroupWithSample()
{
var x = SparkCLRSamples.SparkContext.Parallelize(new[] { new Tuple<string, int>("a", 1), new Tuple<string, int>("b", 4)}, 2);
var y = SparkCLRSamples.SparkContext.Parallelize(new[] { new Tuple<string, int>("a", 2)}, 1);
var groupWith = x.GroupWith(y).Collect();
foreach (var kv in groupWith)
Console.WriteLine(kv.Item1 + ", " + "(" + string.Join(",", kv.Item2) + ")");
if (SparkCLRSamples.Configuration.IsValidationEnabled)
{
Assert.IsTrue(groupWith.Any(kv => kv.Item1 == "a" && kv.Item2.Item1[0] == 1 && kv.Item2.Item2[0] == 2));
Assert.IsTrue(groupWith.Any(kv => kv.Item1 == "b" && kv.Item2.Item1[0] == 4 && !kv.Item2.Item2.Any()));
}
}
[Sample]
internal static void PairRDDGroupWithSample2()
{
var x = SparkCLRSamples.SparkContext.Parallelize(new[] { new Tuple<string, int>("a", 5), new Tuple<string, int>("b", 6) }, 2);
var y = SparkCLRSamples.SparkContext.Parallelize(new[] { new Tuple<string, int>("a", 1), new Tuple<string, int>("b", 4) }, 2);
var z = SparkCLRSamples.SparkContext.Parallelize(new[] { new Tuple<string, int>("a", 2) }, 1);
var groupWith = x.GroupWith(y, z).Collect();
foreach (var kv in groupWith)
Console.WriteLine(kv.Item1 + ", " + "(" + string.Join(",", kv.Item2) + ")");
if (SparkCLRSamples.Configuration.IsValidationEnabled)
{
Assert.IsTrue(groupWith.Any(kv => kv.Item1 == "a" && kv.Item2.Item1[0] == 5 && kv.Item2.Item2[0] == 1 && kv.Item2.Item3[0] == 2));
Assert.IsTrue(groupWith.Any(kv => kv.Item1 == "b" && kv.Item2.Item1[0] == 6 && kv.Item2.Item2[0] == 4 && !kv.Item2.Item3.Any()));
}
}
//TO DO: implement PairRDDFunctions.SampleByKey
//[Sample]
//internal static void PairRDDSampleByKeySample()
//{
// var fractions = new Dictionary<string, double> { { "a", 0.2 }, { "b", 0.1 } };
// var rdd = SparkCLRSamples.SparkContext.Parallelize(fractions.Keys.ToArray(), 2).Cartesian(SparkCLRSamples.SparkContext.Parallelize(Enumerable.Range(0, 1000), 2));
// var sample = rdd.Map(t => new Tuple<string, int>(t.Item1, t.Item2)).SampleByKey(false, fractions, 2).GroupByKey().Collect();
// Console.WriteLine(sample);
//}
[Sample]
internal static void PairRDDSubtractByKeySample()
{
var x = SparkCLRSamples.SparkContext.Parallelize(new[] { new Tuple<string, int?>("a", 1), new Tuple<string, int?>("b", 4), new Tuple<string, int?>("b", 5), new Tuple<string, int?>("a", 2) }, 2);
var y = SparkCLRSamples.SparkContext.Parallelize(new[] { new Tuple<string, int?>("a", 3), new Tuple<string, int?>("c", null) }, 2);
var subtractByKey = x.SubtractByKey(y).Collect();
foreach (var kv in subtractByKey)
Console.WriteLine(kv);
if (SparkCLRSamples.Configuration.IsValidationEnabled)
{
Assert.AreEqual(2, subtractByKey.Length);
subtractByKey.Contains(new Tuple<string, int?>("b", 4));
subtractByKey.Contains(new Tuple<string, int?>("b", 5));
}
}
[Sample]
internal static void PairRDDLookupSample()
{
var rdd = SparkCLRSamples.SparkContext.Parallelize(Enumerable.Range(0, 1000).Zip(Enumerable.Range(0, 1000), (x, y) => new Tuple<int, int>(x, y)), 10);
var lookup42 = rdd.Lookup(42);
var lookup1024 = rdd.Lookup(1024);
Console.WriteLine(string.Join(",", lookup42));
Console.WriteLine(string.Join(",", lookup1024));
if (SparkCLRSamples.Configuration.IsValidationEnabled)
{
CollectionAssert.AreEqual(new[] { 42 }, lookup42);
Assert.AreEqual(0, lookup1024.Length);
}
}
[Sample]
internal static void PairRDDSortByKeySample()
{
var rdd = SparkCLRSamples.SparkContext.Parallelize(new[] { new Tuple<string, int>("B", 2),
new Tuple<string, int>("a", 1), new Tuple<string, int>("c", 3),
new Tuple<string, int>("E", 5), new Tuple<string, int>("D", 4)}, 3);
var sortedRdd = rdd.SortByKey(true, 2);
var sortedInTotal = sortedRdd.Collect();
var sortedPartitions = sortedRdd.Glom().Collect();
if (SparkCLRSamples.Configuration.IsValidationEnabled)
{
Assert.AreEqual(2, sortedPartitions.Length);
// by default SortByKey is case sensitive
CollectionAssert.AreEqual(new[] { "B", "D", "E", "a", "c" }, sortedInTotal.Select(kv => kv.Item1).ToArray());
}
// convert the keys to lower case in order to sort with case insensitive
sortedRdd = rdd.SortByKey(true, 2, key => key.ToLowerInvariant());
sortedInTotal = sortedRdd.Collect();
sortedPartitions = sortedRdd.Glom().Collect();
if (SparkCLRSamples.Configuration.IsValidationEnabled)
{
Assert.AreEqual(2, sortedPartitions.Length);
CollectionAssert.AreEqual(new[] { "a", "B", "c", "D", "E" }, sortedInTotal.Select(kv => kv.Item1).ToArray());
}
}
}
}