ruby-spark 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (176) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +37 -0
  3. data/Gemfile +47 -0
  4. data/Guardfile +5 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +185 -0
  7. data/Rakefile +35 -0
  8. data/TODO.md +7 -0
  9. data/benchmark/aggregate.rb +33 -0
  10. data/benchmark/bisect.rb +88 -0
  11. data/benchmark/custom_marshal.rb +94 -0
  12. data/benchmark/digest.rb +150 -0
  13. data/benchmark/enumerator.rb +88 -0
  14. data/benchmark/performance/prepare.sh +18 -0
  15. data/benchmark/performance/python.py +156 -0
  16. data/benchmark/performance/r.r +69 -0
  17. data/benchmark/performance/ruby.rb +167 -0
  18. data/benchmark/performance/run-all.sh +160 -0
  19. data/benchmark/performance/scala.scala +181 -0
  20. data/benchmark/serializer.rb +82 -0
  21. data/benchmark/sort.rb +43 -0
  22. data/benchmark/sort2.rb +164 -0
  23. data/benchmark/take.rb +28 -0
  24. data/bin/ruby-spark +8 -0
  25. data/example/pi.rb +28 -0
  26. data/ext/ruby_c/extconf.rb +3 -0
  27. data/ext/ruby_c/murmur.c +158 -0
  28. data/ext/ruby_c/murmur.h +9 -0
  29. data/ext/ruby_c/ruby-spark.c +18 -0
  30. data/ext/ruby_java/Digest.java +36 -0
  31. data/ext/ruby_java/Murmur2.java +98 -0
  32. data/ext/ruby_java/RubySparkExtService.java +28 -0
  33. data/ext/ruby_java/extconf.rb +3 -0
  34. data/ext/spark/build.sbt +73 -0
  35. data/ext/spark/project/plugins.sbt +9 -0
  36. data/ext/spark/sbt/sbt +34 -0
  37. data/ext/spark/src/main/scala/Exec.scala +91 -0
  38. data/ext/spark/src/main/scala/MLLibAPI.scala +4 -0
  39. data/ext/spark/src/main/scala/Marshal.scala +52 -0
  40. data/ext/spark/src/main/scala/MarshalDump.scala +113 -0
  41. data/ext/spark/src/main/scala/MarshalLoad.scala +220 -0
  42. data/ext/spark/src/main/scala/RubyAccumulatorParam.scala +69 -0
  43. data/ext/spark/src/main/scala/RubyBroadcast.scala +13 -0
  44. data/ext/spark/src/main/scala/RubyConstant.scala +13 -0
  45. data/ext/spark/src/main/scala/RubyMLLibAPI.scala +55 -0
  46. data/ext/spark/src/main/scala/RubyMLLibUtilAPI.scala +21 -0
  47. data/ext/spark/src/main/scala/RubyPage.scala +34 -0
  48. data/ext/spark/src/main/scala/RubyRDD.scala +364 -0
  49. data/ext/spark/src/main/scala/RubySerializer.scala +14 -0
  50. data/ext/spark/src/main/scala/RubyTab.scala +11 -0
  51. data/ext/spark/src/main/scala/RubyUtils.scala +15 -0
  52. data/ext/spark/src/main/scala/RubyWorker.scala +257 -0
  53. data/ext/spark/src/test/scala/MarshalSpec.scala +84 -0
  54. data/lib/ruby-spark.rb +1 -0
  55. data/lib/spark.rb +198 -0
  56. data/lib/spark/accumulator.rb +260 -0
  57. data/lib/spark/broadcast.rb +98 -0
  58. data/lib/spark/build.rb +43 -0
  59. data/lib/spark/cli.rb +169 -0
  60. data/lib/spark/command.rb +86 -0
  61. data/lib/spark/command/base.rb +154 -0
  62. data/lib/spark/command/basic.rb +345 -0
  63. data/lib/spark/command/pair.rb +124 -0
  64. data/lib/spark/command/sort.rb +51 -0
  65. data/lib/spark/command/statistic.rb +144 -0
  66. data/lib/spark/command_builder.rb +141 -0
  67. data/lib/spark/command_validator.rb +34 -0
  68. data/lib/spark/config.rb +244 -0
  69. data/lib/spark/constant.rb +14 -0
  70. data/lib/spark/context.rb +304 -0
  71. data/lib/spark/error.rb +50 -0
  72. data/lib/spark/ext/hash.rb +41 -0
  73. data/lib/spark/ext/integer.rb +25 -0
  74. data/lib/spark/ext/io.rb +57 -0
  75. data/lib/spark/ext/ip_socket.rb +29 -0
  76. data/lib/spark/ext/module.rb +58 -0
  77. data/lib/spark/ext/object.rb +24 -0
  78. data/lib/spark/ext/string.rb +24 -0
  79. data/lib/spark/helper.rb +10 -0
  80. data/lib/spark/helper/logger.rb +40 -0
  81. data/lib/spark/helper/parser.rb +85 -0
  82. data/lib/spark/helper/serialize.rb +71 -0
  83. data/lib/spark/helper/statistic.rb +93 -0
  84. data/lib/spark/helper/system.rb +42 -0
  85. data/lib/spark/java_bridge.rb +19 -0
  86. data/lib/spark/java_bridge/base.rb +203 -0
  87. data/lib/spark/java_bridge/jruby.rb +23 -0
  88. data/lib/spark/java_bridge/rjb.rb +41 -0
  89. data/lib/spark/logger.rb +76 -0
  90. data/lib/spark/mllib.rb +100 -0
  91. data/lib/spark/mllib/classification/common.rb +31 -0
  92. data/lib/spark/mllib/classification/logistic_regression.rb +223 -0
  93. data/lib/spark/mllib/classification/naive_bayes.rb +97 -0
  94. data/lib/spark/mllib/classification/svm.rb +135 -0
  95. data/lib/spark/mllib/clustering/gaussian_mixture.rb +82 -0
  96. data/lib/spark/mllib/clustering/kmeans.rb +118 -0
  97. data/lib/spark/mllib/matrix.rb +120 -0
  98. data/lib/spark/mllib/regression/common.rb +73 -0
  99. data/lib/spark/mllib/regression/labeled_point.rb +41 -0
  100. data/lib/spark/mllib/regression/lasso.rb +100 -0
  101. data/lib/spark/mllib/regression/linear.rb +124 -0
  102. data/lib/spark/mllib/regression/ridge.rb +97 -0
  103. data/lib/spark/mllib/ruby_matrix/matrix_adapter.rb +53 -0
  104. data/lib/spark/mllib/ruby_matrix/vector_adapter.rb +57 -0
  105. data/lib/spark/mllib/stat/distribution.rb +12 -0
  106. data/lib/spark/mllib/vector.rb +185 -0
  107. data/lib/spark/rdd.rb +1328 -0
  108. data/lib/spark/sampler.rb +92 -0
  109. data/lib/spark/serializer.rb +24 -0
  110. data/lib/spark/serializer/base.rb +170 -0
  111. data/lib/spark/serializer/cartesian.rb +37 -0
  112. data/lib/spark/serializer/marshal.rb +19 -0
  113. data/lib/spark/serializer/message_pack.rb +25 -0
  114. data/lib/spark/serializer/oj.rb +25 -0
  115. data/lib/spark/serializer/pair.rb +27 -0
  116. data/lib/spark/serializer/utf8.rb +25 -0
  117. data/lib/spark/sort.rb +189 -0
  118. data/lib/spark/stat_counter.rb +125 -0
  119. data/lib/spark/storage_level.rb +39 -0
  120. data/lib/spark/version.rb +3 -0
  121. data/lib/spark/worker/master.rb +144 -0
  122. data/lib/spark/worker/spark_files.rb +15 -0
  123. data/lib/spark/worker/worker.rb +197 -0
  124. data/ruby-spark.gemspec +36 -0
  125. data/spec/generator.rb +37 -0
  126. data/spec/inputs/lorem_300.txt +316 -0
  127. data/spec/inputs/numbers/1.txt +50 -0
  128. data/spec/inputs/numbers/10.txt +50 -0
  129. data/spec/inputs/numbers/11.txt +50 -0
  130. data/spec/inputs/numbers/12.txt +50 -0
  131. data/spec/inputs/numbers/13.txt +50 -0
  132. data/spec/inputs/numbers/14.txt +50 -0
  133. data/spec/inputs/numbers/15.txt +50 -0
  134. data/spec/inputs/numbers/16.txt +50 -0
  135. data/spec/inputs/numbers/17.txt +50 -0
  136. data/spec/inputs/numbers/18.txt +50 -0
  137. data/spec/inputs/numbers/19.txt +50 -0
  138. data/spec/inputs/numbers/2.txt +50 -0
  139. data/spec/inputs/numbers/20.txt +50 -0
  140. data/spec/inputs/numbers/3.txt +50 -0
  141. data/spec/inputs/numbers/4.txt +50 -0
  142. data/spec/inputs/numbers/5.txt +50 -0
  143. data/spec/inputs/numbers/6.txt +50 -0
  144. data/spec/inputs/numbers/7.txt +50 -0
  145. data/spec/inputs/numbers/8.txt +50 -0
  146. data/spec/inputs/numbers/9.txt +50 -0
  147. data/spec/inputs/numbers_0_100.txt +101 -0
  148. data/spec/inputs/numbers_1_100.txt +100 -0
  149. data/spec/lib/collect_spec.rb +42 -0
  150. data/spec/lib/command_spec.rb +68 -0
  151. data/spec/lib/config_spec.rb +64 -0
  152. data/spec/lib/context_spec.rb +163 -0
  153. data/spec/lib/ext_spec.rb +72 -0
  154. data/spec/lib/external_apps_spec.rb +45 -0
  155. data/spec/lib/filter_spec.rb +80 -0
  156. data/spec/lib/flat_map_spec.rb +100 -0
  157. data/spec/lib/group_spec.rb +109 -0
  158. data/spec/lib/helper_spec.rb +19 -0
  159. data/spec/lib/key_spec.rb +41 -0
  160. data/spec/lib/manipulation_spec.rb +114 -0
  161. data/spec/lib/map_partitions_spec.rb +87 -0
  162. data/spec/lib/map_spec.rb +91 -0
  163. data/spec/lib/mllib/classification_spec.rb +54 -0
  164. data/spec/lib/mllib/clustering_spec.rb +35 -0
  165. data/spec/lib/mllib/matrix_spec.rb +32 -0
  166. data/spec/lib/mllib/regression_spec.rb +116 -0
  167. data/spec/lib/mllib/vector_spec.rb +77 -0
  168. data/spec/lib/reduce_by_key_spec.rb +118 -0
  169. data/spec/lib/reduce_spec.rb +131 -0
  170. data/spec/lib/sample_spec.rb +46 -0
  171. data/spec/lib/serializer_spec.rb +13 -0
  172. data/spec/lib/sort_spec.rb +58 -0
  173. data/spec/lib/statistic_spec.rb +168 -0
  174. data/spec/lib/whole_text_files_spec.rb +33 -0
  175. data/spec/spec_helper.rb +39 -0
  176. metadata +301 -0
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'Spark::Mllib classification' do
4
+
5
+ let(:data1) do
6
+ [
7
+ LabeledPoint.new(0.0, [1, 0, 0]),
8
+ LabeledPoint.new(1.0, [0, 1, 1]),
9
+ LabeledPoint.new(0.0, [2, 0, 0]),
10
+ LabeledPoint.new(1.0, [0, 2, 1])
11
+ ]
12
+ end
13
+
14
+ let(:values1) do
15
+ data1.map do |lp|
16
+ lp.features.values
17
+ end
18
+ end
19
+
20
+ let(:rdd1) { $sc.parallelize(data1) }
21
+
22
+ context 'logistic regression' do
23
+ it 'test' do
24
+ lrm = LogisticRegressionWithSGD.train(rdd1)
25
+
26
+ expect(lrm.predict(values1[0])).to be <= 0
27
+ expect(lrm.predict(values1[1])).to be > 0
28
+ expect(lrm.predict(values1[2])).to be <= 0
29
+ expect(lrm.predict(values1[3])).to be > 0
30
+ end
31
+ end
32
+
33
+ context 'svm' do
34
+ it 'test' do
35
+ lrm = SVMWithSGD.train(rdd1)
36
+
37
+ expect(lrm.predict(values1[0])).to be <= 0
38
+ expect(lrm.predict(values1[1])).to be > 0
39
+ expect(lrm.predict(values1[2])).to be <= 0
40
+ expect(lrm.predict(values1[3])).to be > 0
41
+ end
42
+ end
43
+
44
+ context 'naive bayes' do
45
+ it 'test' do
46
+ lrm = NaiveBayes.train(rdd1)
47
+
48
+ expect(lrm.predict(values1[0])).to be <= 0
49
+ expect(lrm.predict(values1[1])).to be > 0
50
+ expect(lrm.predict(values1[2])).to be <= 0
51
+ expect(lrm.predict(values1[3])).to be > 0
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'Spark::Mllib clustering' do
4
+ context 'kmeans' do
5
+ it 'test' do
6
+ data = [
7
+ DenseVector.new([0, 1.1]),
8
+ DenseVector.new([0, 1.2]),
9
+ DenseVector.new([1.1, 0]),
10
+ DenseVector.new([1.2, 0])
11
+ ]
12
+ model = KMeans.train($sc.parallelize(data), 2, initialization_mode: 'k-means||')
13
+
14
+ expect(model.predict(data[0])).to eq(model.predict(data[1]))
15
+ expect(model.predict(data[2])).to eq(model.predict(data[3]))
16
+ end
17
+
18
+ it 'deterministic' do
19
+ data = Array.new(10) do |i|
20
+ i *= 10
21
+ DenseVector.new([i, i])
22
+ end
23
+
24
+ clusters1 = KMeans.train($sc.parallelize(data), 3, initialization_mode: 'k-means||', seed: 42)
25
+ clusters2 = KMeans.train($sc.parallelize(data), 3, initialization_mode: 'k-means||', seed: 42)
26
+
27
+ centers1 = clusters1.centers.to_a
28
+ centers2 = clusters2.centers.to_a
29
+
30
+ centers1.zip(centers2).each do |c1, c2|
31
+ expect(c1).to eq(c2)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'Spark::Mllib::Matrix' do
4
+ context 'dense' do
5
+ it 'construct' do
6
+ values = [[1.0, 0.0, 4.0], [0.0, 3.0, 5.0], [2.0, 0.0, 6.0]]
7
+ matrix = DenseMatrix.new(3, 3, [[1.0, 0.0, 4.0], [0.0, 3.0, 5.0], [2.0, 0.0, 6.0]])
8
+
9
+ expect(matrix.shape).to eq([3, 3])
10
+ expect(matrix.values).to eq([[1.0, 0.0, 4.0], [0.0, 3.0, 5.0], [2.0, 0.0, 6.0]])
11
+ end
12
+ end
13
+
14
+ context 'sparse' do
15
+ it 'construct' do
16
+ values = [1.0, 2.0, 4.0, 5.0]
17
+ column_pointers = [0, 2, 2, 4, 4]
18
+ row_indices = [1, 2, 1, 2]
19
+
20
+ matrix = SparseMatrix.new(3, 4, column_pointers, row_indices, values)
21
+
22
+ expect(matrix.shape).to eq([3, 4])
23
+ expect(matrix.to_a).to eq(
24
+ [
25
+ [0.0, 0.0, 0.0, 0.0],
26
+ [1.0, 0.0, 4.0, 0.0],
27
+ [2.0, 0.0, 5.0, 0.0]
28
+ ]
29
+ )
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,116 @@
1
+ require 'spec_helper'
2
+
3
+ # Mllib functions are tested on Spark
4
+ # This just test if ruby call proper methods
5
+
6
+ RSpec.describe 'Spark::Mllib regression' do
7
+
8
+ let(:data1) do
9
+ [
10
+ LabeledPoint.new(-1.0, [0, -1]),
11
+ LabeledPoint.new(1.0, [0, 1]),
12
+ LabeledPoint.new(-1.0, [0, -2]),
13
+ LabeledPoint.new(1.0, [0, 2])
14
+ ]
15
+ end
16
+
17
+ let(:values1) do
18
+ data1.map do |lp|
19
+ lp.features.values
20
+ end
21
+ end
22
+
23
+ let(:rdd1) { $sc.parallelize(data1) }
24
+
25
+ context 'labeled point' do
26
+ let(:lp) { LabeledPoint.new(1, [1,2,3]) }
27
+
28
+ it 'from array' do
29
+ expect(lp.label).to eql(1.0)
30
+ expect(lp.features).to be_a(DenseVector)
31
+ end
32
+
33
+ it 'serialize' do
34
+ lp2 = Marshal.load(Marshal.dump(lp))
35
+
36
+ expect(lp2.label).to eql(lp.label)
37
+ expect(lp2.features.values).to eql(lp.features.values)
38
+ end
39
+ end
40
+
41
+ context 'linear regression' do
42
+ context 'test' do
43
+ let(:lrm) { LinearRegressionWithSGD.train(rdd1) }
44
+
45
+ it 'test' do
46
+ expect(lrm.predict(values1[0])).to be <= 0
47
+ expect(lrm.predict(values1[1])).to be > 0
48
+ expect(lrm.predict(values1[2])).to be <= 0
49
+ expect(lrm.predict(values1[3])).to be > 0
50
+ end
51
+
52
+ it 'test via rdd' do
53
+ rdd = $sc.parallelize(values1, 1)
54
+ rdd = rdd.map(lambda{|value| model.predict(value)})
55
+ rdd = rdd.bind(model: lrm)
56
+
57
+ result = rdd.collect
58
+
59
+ expect(result[0]).to be <= 0
60
+ expect(result[1]).to be > 0
61
+ expect(result[2]).to be <= 0
62
+ expect(result[3]).to be > 0
63
+ end
64
+ end
65
+
66
+ # Y = 3 + 10*X1 + 10*X2
67
+ it 'linear regression' do
68
+ data = Spark.jb.call(RubyMLLibUtilAPI, 'generateLinearInput', 3.0, ['10.0', '10.0'], 100, 42, 0.1)
69
+ rdd = $sc.parallelize(data)
70
+
71
+ lrm = LinearRegressionWithSGD.train(rdd, iterations: 1000, intercept: true, step: 1.0)
72
+
73
+ expect(lrm.intercept).to be_between(2.5, 3.5)
74
+ expect(lrm.weights.size).to eq(2)
75
+ expect(lrm.weights[0]).to be_between(9.0, 11.0)
76
+ expect(lrm.weights[1]).to be_between(9.0, 11.0)
77
+ end
78
+ end
79
+
80
+ context 'lasso' do
81
+ it 'test' do
82
+ lrm = LassoWithSGD.train(rdd1)
83
+
84
+ expect(lrm.predict(values1[0])).to be <= 0
85
+ expect(lrm.predict(values1[1])).to be > 0
86
+ expect(lrm.predict(values1[2])).to be <= 0
87
+ expect(lrm.predict(values1[3])).to be > 0
88
+ end
89
+
90
+ it 'local random SGD with initial weights' do
91
+ data = Spark.jb.call(RubyMLLibUtilAPI, 'generateLinearInput', 2.0, ['-1.5', '0.01'], 1000, 42, 0.1)
92
+ data.map! do |lp|
93
+ LabeledPoint.new(lp.label, [1.0] + lp.features.values)
94
+ end
95
+
96
+ rdd = $sc.parallelize(data);
97
+
98
+ lrm = LassoWithSGD.train(rdd, step: 1.0, reg_param: 0.01, iterations: 40, initial_weights: [-1.0, -1.0, -1.0])
99
+
100
+ expect(lrm.weights[0]).to be_between(1.9, 2.1)
101
+ expect(lrm.weights[1]).to be_between(-1.60, -1.40)
102
+ expect(lrm.weights[2]).to be_between(-1.0e-3, 1.0e-3)
103
+ end
104
+ end
105
+
106
+ context 'ridge' do
107
+ it 'test' do
108
+ lrm = RidgeRegressionWithSGD.train(rdd1)
109
+
110
+ expect(lrm.predict(values1[0])).to be <= 0
111
+ expect(lrm.predict(values1[1])).to be > 0
112
+ expect(lrm.predict(values1[2])).to be <= 0
113
+ expect(lrm.predict(values1[3])).to be > 0
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'Spark::Mllib::Vector' do
4
+
5
+ context 'parsing' do
6
+ it 'dense vector' do
7
+ dv = DenseVector.new([1.0, 2.0, 3.0, 4.0, 5.0])
8
+ dv2 = DenseVector.parse(dv.to_s)
9
+ dv3 = Vectors.parse(dv.to_s)
10
+
11
+ expect(dv.to_s).to eq("[1.0,2.0,3.0,4.0,5.0]")
12
+ expect(dv2.values).to eq(dv.values)
13
+ expect(dv3.values).to eq(dv.values)
14
+ end
15
+
16
+ it 'sparse vector' do
17
+ sv = SparseVector.new(5, {1 => 3, 4 => 5})
18
+ sv2 = SparseVector.parse(sv.to_s)
19
+ sv3 = Vectors.parse(sv.to_s)
20
+
21
+ expect(sv.to_s).to eq("(5,[1,4],[3,5])")
22
+ expect(sv2.size).to eq(sv.size)
23
+ expect(sv2.indices).to eq(sv.indices)
24
+ expect(sv2.values).to eq(sv.values)
25
+ expect(sv3.size).to eq(sv.size)
26
+ expect(sv3.indices).to eq(sv.indices)
27
+ expect(sv3.values).to eq(sv.values)
28
+ end
29
+ end
30
+
31
+ it 'dot' do
32
+ sv = SparseVector.new(4, {1 => 1, 3 => 2})
33
+ dv = DenseVector.new([1.0, 2.0, 3.0, 4.0])
34
+ lst = DenseVector.new([1, 2, 3, 4])
35
+
36
+ expect(sv.dot(dv)).to eq(10.0)
37
+ expect(dv.dot(dv)).to eq(30.0)
38
+ expect(lst.dot(dv)).to eq(30.0)
39
+ end
40
+
41
+ it 'squared distance' do
42
+ sv = SparseVector.new(4, {1 => 1, 3 => 2})
43
+ dv = DenseVector.new([1.0, 2.0, 3.0, 4.0])
44
+ lst = DenseVector.new([4, 3, 2, 1])
45
+
46
+ expect(sv.squared_distance(dv)).to eq(15)
47
+ expect(sv.squared_distance(lst)).to eq(25)
48
+ expect(dv.squared_distance(lst)).to eq(20)
49
+ expect(dv.squared_distance(sv)).to eq(15)
50
+ expect(lst.squared_distance(sv)).to eq(25)
51
+ expect(lst.squared_distance(dv)).to eq(20)
52
+ expect(sv.squared_distance(sv)).to eq(0)
53
+ expect(dv.squared_distance(dv)).to eq(0)
54
+ expect(lst.squared_distance(lst)).to eq(0)
55
+ end
56
+
57
+ it 'sparse vector indexing' do
58
+ sv1 = SparseVector.new(4, {1 => 1, 3 => 2})
59
+ sv2 = SparseVector.new(4, [1, 3], [1, 2])
60
+
61
+ expect(sv1[0]).to eq(0)
62
+ expect(sv1[3]).to eq(2)
63
+ expect(sv1[1]).to eq(1)
64
+ expect(sv1[2]).to eq(0)
65
+ expect(sv1[-1]).to eq(2)
66
+ expect(sv1[-2]).to eq(0)
67
+ expect(sv1[-4]).to eq(0)
68
+
69
+ expect(sv2[0]).to eq(0)
70
+ expect(sv2[3]).to eq(2)
71
+ expect(sv2[1]).to eq(1)
72
+ expect(sv2[2]).to eq(0)
73
+ expect(sv2[-1]).to eq(2)
74
+ expect(sv2[-2]).to eq(0)
75
+ expect(sv2[-4]).to eq(0)
76
+ end
77
+ end
@@ -0,0 +1,118 @@
1
+ require "spec_helper"
2
+
3
+ def flat_map(line)
4
+ line.split
5
+ end
6
+
7
+ def map(item)
8
+ [item, 1]
9
+ end
10
+
11
+ def reduce(x,y)
12
+ x+y
13
+ end
14
+
15
+ RSpec::shared_examples "a words counting" do |workers|
16
+ context "with #{workers || 'default'} worker" do
17
+ let(:result) do
18
+ keyyed = lines.flat_map{|x| x.split}.map{|x| [x,1]}
19
+ result = keyyed.reduce({}){|memo, item|
20
+ key = item[0]
21
+ value = item[1]
22
+
23
+ memo[key] ||= 0
24
+ memo[key] += value
25
+ memo
26
+ }
27
+ result
28
+ end
29
+
30
+ it "when lambda" do
31
+ rdd2 = rdd(workers)
32
+ rdd2 = rdd2.flat_map(lambda{|line| line.split})
33
+ rdd2 = rdd2.map(lambda{|word| [word, 1]})
34
+ rdd2 = rdd2.reduce_by_key(lambda{|x,y| x+y})
35
+
36
+ expect(rdd2.collect_as_hash).to eql(result)
37
+ end
38
+
39
+ it "when method" do
40
+ rdd2 = rdd(workers)
41
+ rdd2 = rdd2.flat_map(method(:flat_map))
42
+ rdd2 = rdd2.map(method(:map))
43
+ rdd2 = rdd2.reduce_by_key(method(:reduce))
44
+
45
+ expect(rdd2.collect_as_hash).to eql(result)
46
+ end
47
+
48
+ it "keys, values" do
49
+ rdd2 = rdd(workers)
50
+ rdd2 = rdd2.flat_map(method(:flat_map))
51
+ rdd2 = rdd2.map(method(:map))
52
+ rdd2 = rdd2.reduce_by_key(method(:reduce))
53
+
54
+ expect(rdd2.keys.collect.sort).to eql(result.keys.sort)
55
+ expect { rdd2.values.collect.reduce(:+) }.to_not raise_error
56
+ end
57
+ end
58
+ end
59
+
60
+ RSpec::describe "Spark::RDD" do
61
+ context ".reduce_by_key" do
62
+ context "throught parallelize" do
63
+ let(:lines) { Generator.lines }
64
+
65
+ def rdd(workers)
66
+ $sc.parallelize(lines, workers)
67
+ end
68
+
69
+ it_behaves_like "a words counting", nil
70
+ it_behaves_like "a words counting", 1
71
+ it_behaves_like "a words counting", rand(2..10)
72
+ end
73
+
74
+ context "throught text_file" do
75
+ let(:file) { File.join("spec", "inputs", "lorem_300.txt") }
76
+ let(:lines) { File.readlines(file).map(&:strip) }
77
+
78
+ def rdd(workers)
79
+ $sc.text_file(file, workers)
80
+ end
81
+
82
+ it_behaves_like "a words counting", nil
83
+ it_behaves_like "a words counting", 1
84
+ it_behaves_like "a words counting", rand(2..10)
85
+ end
86
+ end
87
+
88
+ context ".fold_by_key" do
89
+ let(:numbers) { Generator.numbers }
90
+ let(:zero_value) { 0 }
91
+ let(:rdd) { $sc.parallelize(numbers) }
92
+ let(:map) { lambda{|x| [x, 1]} }
93
+ let(:add) { lambda{|x,y| x+y} }
94
+
95
+ let(:result) do
96
+ _result = {}
97
+ numbers.map(&map).each do |key, value|
98
+ _result[key] ||= zero_value
99
+ _result[key] = add.call(_result[key], value)
100
+ end
101
+ _result
102
+ end
103
+
104
+ def fold_by_key(num_partitions=nil)
105
+ rdd.map(map).fold_by_key(zero_value, add, num_partitions).collect_as_hash
106
+ end
107
+
108
+ it "default num_partitions" do
109
+ expect(fold_by_key).to eq(result)
110
+ end
111
+
112
+ it "default num_partitions" do
113
+ expect(
114
+ fold_by_key rand(1..10)
115
+ ).to eq(result)
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,131 @@
1
+ require "spec_helper"
2
+
3
+ def longest_words(memo, word)
4
+ memo.length > word.length ? memo : word
5
+ end
6
+
7
+ RSpec::shared_examples "a reducing" do |workers|
8
+ context "with #{workers || 'default'} worker" do
9
+ it ".reduce" do
10
+ rdd2 = rdd_numbers(workers)
11
+ rdd2 = rdd2.map(to_i)
12
+ rdd2 = rdd2.reduce(func1)
13
+ result = numbers.map(&:to_i).reduce(&func1)
14
+
15
+ expect(rdd2).to eql(result)
16
+
17
+ rdd3 = rdd_numbers(workers)
18
+ rdd3 = rdd3.map(to_i)
19
+ rdd3 = rdd3.reduce(func2)
20
+ result = numbers.map(&:to_i).reduce(&func2)
21
+
22
+ expect(rdd3).to eql(result)
23
+
24
+ rdd4 = rdd_lines(workers)
25
+ rdd4 = rdd4.flat_map(split)
26
+ rdd4 = rdd4.reduce(method(:longest_words))
27
+
28
+ result = lines.flat_map(&split).reduce(&lambda(&method(:longest_words)))
29
+
30
+ expect(rdd4).to eql(result)
31
+ end
32
+
33
+ it ".fold" do
34
+ rdd2 = rdd_numbers(workers)
35
+ rdd2 = rdd2.map(to_i)
36
+ rdd_result = rdd2.fold(1, func1)
37
+
38
+ # all workers add 1 + last reducing phase
39
+ result = numbers.map(&:to_i).reduce(&func1) + rdd2.partitions_size + 1
40
+
41
+ expect(rdd_result).to eql(result)
42
+ end
43
+
44
+ it ".aggregate" do
45
+ rdd2 = rdd_numbers(workers)
46
+ rdd2 = rdd2.map(to_i)
47
+
48
+ # Sum of items + their count
49
+ seq = lambda{|x,y| [x[0] + y, x[1] + 1]}
50
+ com = lambda{|x,y| [x[0] + y[0], x[1] + y[1]]}
51
+ rdd_result = rdd2.aggregate([0,0], seq, com)
52
+
53
+ result = [numbers.reduce(:+), numbers.size]
54
+
55
+ expect(rdd_result).to eql(result)
56
+ end
57
+
58
+ it ".max" do
59
+ rdd2 = rdd_numbers(workers)
60
+ rdd2 = rdd2.map(to_i)
61
+
62
+ expect(rdd2.max).to eql(numbers.map(&:to_i).max)
63
+ end
64
+
65
+ it ".min" do
66
+ rdd2 = rdd_numbers(workers)
67
+ rdd2 = rdd2.map(to_i)
68
+
69
+ expect(rdd2.min).to eql(numbers.map(&:to_i).min)
70
+ end
71
+
72
+ it ".sum" do
73
+ rdd2 = rdd_numbers(workers)
74
+ rdd2 = rdd2.map(to_i)
75
+
76
+ expect(rdd2.sum).to eql(numbers.map(&:to_i).reduce(:+))
77
+ end
78
+
79
+ it ".count" do
80
+ rdd2 = rdd_numbers(workers)
81
+ rdd2 = rdd2.map(to_i)
82
+
83
+ expect(rdd2.count).to eql(numbers.size)
84
+ end
85
+ end
86
+ end
87
+
88
+ RSpec::describe "Spark::RDD" do
89
+ let(:func1) { lambda{|sum, x| sum+x} }
90
+ let(:func2) { lambda{|product, x| product*x} }
91
+
92
+ let(:to_i) { lambda{|item| item.to_i} }
93
+ let(:split) { lambda{|item| item.split} }
94
+
95
+ context "throught parallelize" do
96
+ let(:numbers) { Generator.numbers }
97
+ let(:lines) { Generator.lines }
98
+
99
+ def rdd_numbers(workers)
100
+ $sc.parallelize(numbers, workers)
101
+ end
102
+
103
+ def rdd_lines(workers)
104
+ $sc.parallelize(lines, workers)
105
+ end
106
+
107
+ it_behaves_like "a reducing", nil
108
+ it_behaves_like "a reducing", 1
109
+ it_behaves_like "a reducing", rand(2..10)
110
+ end
111
+
112
+ context "throught text_file" do
113
+ let(:file) { File.join("spec", "inputs", "numbers_0_100.txt") }
114
+ let(:file_lines) { File.join("spec", "inputs", "lorem_300.txt") }
115
+
116
+ let(:numbers) { File.readlines(file).map(&:strip).map(&:to_i) }
117
+ let(:lines) { File.readlines(file_lines).map(&:strip) }
118
+
119
+ def rdd_numbers(workers)
120
+ $sc.text_file(file, workers)
121
+ end
122
+
123
+ def rdd_lines(workers)
124
+ $sc.text_file(file_lines, workers)
125
+ end
126
+
127
+ it_behaves_like "a reducing", nil
128
+ it_behaves_like "a reducing", 1
129
+ it_behaves_like "a reducing", rand(2..10)
130
+ end
131
+ end