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,109 @@
1
+ require "spec_helper"
2
+
3
+ RSpec::shared_examples "a groupping by key" do |workers|
4
+ it "with #{workers || 'default'} worker" do
5
+ expect(rdd_result(workers)).to eql(result)
6
+ end
7
+ end
8
+
9
+ RSpec::shared_examples "a cogroupping by key" do |workers|
10
+ context "with #{workers || 'default'} worker" do
11
+ it ".group_with" do
12
+ rdd = rdd_1(workers).group_with(rdd_2(workers))
13
+ expect(rdd.collect_as_hash).to eql(result_12)
14
+ end
15
+
16
+ it ".cogroup" do
17
+ rdd = rdd_1(workers).cogroup(rdd_2(workers), rdd_3(workers))
18
+ expect(rdd.collect_as_hash).to eql(result_123)
19
+ end
20
+ end
21
+ end
22
+
23
+ RSpec::shared_examples "a groupping by" do |workers|
24
+ it "with #{workers || 'default'} worker" do
25
+ rdd = rdd_numbers(workers)
26
+ rdd = rdd.group_by(key_function1)
27
+
28
+ expect(rdd.collect_as_hash).to eql(numbers.group_by(&key_function1))
29
+
30
+ rdd = rdd_words(workers)
31
+ rdd = rdd.group_by(key_function2)
32
+
33
+ expect(rdd.collect_as_hash).to eql(words.group_by(&key_function2))
34
+ end
35
+ end
36
+
37
+ RSpec::describe "Spark::RDD" do
38
+
39
+ def make_result(*hashes)
40
+ _result = {}
41
+ hashes.each do |data|
42
+ data.each do |key, value|
43
+ _result[key] ||= []
44
+ _result[key] << value
45
+ end
46
+ end
47
+ _result
48
+ end
49
+
50
+ context ".group_by_key" do
51
+ let(:hash) { Generator.hash }
52
+ let(:result) { make_result(hash) }
53
+
54
+ def rdd_result(workers)
55
+ rdd = $sc.parallelize(hash)
56
+ rdd.group_by_key.collect_as_hash
57
+ end
58
+
59
+ it_behaves_like "a groupping by key", nil
60
+ it_behaves_like "a groupping by key", 1
61
+ it_behaves_like "a groupping by key", rand(2..10)
62
+ end
63
+
64
+ context "cogroup" do
65
+ let(:hash1) { Generator.hash }
66
+ let(:hash2) { Generator.hash }
67
+ let(:hash3) { Generator.hash }
68
+
69
+ let(:result_12) { make_result(hash1, hash2) }
70
+ let(:result_123) { make_result(hash1, hash2, hash3) }
71
+
72
+ def rdd_1(workers)
73
+ $sc.parallelize(hash1)
74
+ end
75
+
76
+ def rdd_2(workers)
77
+ $sc.parallelize(hash2)
78
+ end
79
+
80
+ def rdd_3(workers)
81
+ $sc.parallelize(hash3)
82
+ end
83
+
84
+ it_behaves_like "a cogroupping by key", nil
85
+ it_behaves_like "a cogroupping by key", 1
86
+ it_behaves_like "a cogroupping by key", rand(2..10)
87
+ end
88
+
89
+ context "group_by" do
90
+ let(:key_function1) { lambda{|x| x%2} }
91
+ let(:key_function2) { lambda{|x| x.size} }
92
+
93
+ let(:numbers) { Generator.numbers }
94
+ let(:words) { Generator.words }
95
+
96
+ def rdd_numbers(workers)
97
+ $sc.parallelize(numbers)
98
+ end
99
+
100
+ def rdd_words(workers)
101
+ $sc.parallelize(words)
102
+ end
103
+
104
+ it_behaves_like "a groupping by", nil
105
+ it_behaves_like "a groupping by", 1
106
+ it_behaves_like "a groupping by", rand(2..10)
107
+ end
108
+
109
+ end
@@ -0,0 +1,19 @@
1
+ require "spec_helper"
2
+
3
+ RSpec::configure do |c|
4
+ c.include Spark::Helper::Parser
5
+ end
6
+
7
+ RSpec::describe Spark::Helper do
8
+
9
+ it "memory size" do
10
+ expect(to_memory_size("512mb")).to eql(524288.0)
11
+ expect(to_memory_size("1586 mb")).to eql(1624064.0)
12
+ expect(to_memory_size("3 MB")).to eql(3072.0)
13
+ expect(to_memory_size("9gb")).to eql(9437184.0)
14
+ expect(to_memory_size("9gb", "mb")).to eql(9216.0)
15
+ expect(to_memory_size("9mb", "gb")).to eql(0.01)
16
+ expect(to_memory_size("6652548796kb", "mb")).to eql(6496629.68)
17
+ end
18
+
19
+ end
@@ -0,0 +1,41 @@
1
+ require "spec_helper"
2
+
3
+ RSpec::shared_examples "a keying by" do |workers|
4
+ it "with #{workers || 'default'} worker" do
5
+ rdd = rdd_numbers(workers)
6
+ rdd = rdd.key_by(key_function1)
7
+
8
+ result = numbers.map{|item| [key_function1.call(item), item]}
9
+ expect(rdd.collect).to eql(result)
10
+
11
+ rdd = rdd_words(workers)
12
+ rdd = rdd.key_by(key_function2)
13
+
14
+ result = words.map{|item| [key_function2.call(item), item]}
15
+ expect(rdd.collect).to eql(result)
16
+ end
17
+ end
18
+
19
+ RSpec::describe "Spark::RDD" do
20
+
21
+ context "key_by" do
22
+ let(:key_function1) { lambda{|x| x.even?} }
23
+ let(:key_function2) { lambda{|x| x.include?("a")} }
24
+
25
+ let(:numbers) { Generator.numbers }
26
+ let(:words) { Generator.words }
27
+
28
+ def rdd_numbers(workers)
29
+ $sc.parallelize(numbers)
30
+ end
31
+
32
+ def rdd_words(workers)
33
+ $sc.parallelize(words)
34
+ end
35
+
36
+ it_behaves_like "a keying by", nil
37
+ it_behaves_like "a keying by", 1
38
+ it_behaves_like "a keying by", rand(2..10)
39
+ end
40
+
41
+ end
@@ -0,0 +1,114 @@
1
+ require "spec_helper"
2
+
3
+ RSpec::describe "Spark::RDD" do
4
+ let(:numbers) { 1..100 }
5
+ let(:rand_numbers) { Generator.numbers }
6
+
7
+ it ".glom" do
8
+ rdd = $sc.parallelize(numbers, 1).glom
9
+ expect(rdd.collect).to eql([numbers.to_a])
10
+
11
+ rdd = $sc.parallelize(numbers, 5, batch_size: 1).glom
12
+ expect(rdd.collect).to eql(numbers.each_slice(20).to_a)
13
+ end
14
+
15
+ it ".coalesce" do
16
+ rdd = $sc.parallelize(numbers, 5)
17
+
18
+ rdd2 = rdd.glom
19
+ expect(rdd2.collect.size).to eql(5)
20
+
21
+ rdd3 = rdd.coalesce(4).glom
22
+ expect(rdd3.collect.size).to eql(4)
23
+ end
24
+
25
+ it ".distinct" do
26
+ rdd = $sc.parallelize(rand_numbers, 5)
27
+ rdd = rdd.distinct
28
+ expect(rdd.collect.sort).to eql(rand_numbers.uniq.sort)
29
+
30
+ rdd = $sc.parallelize(numbers, 5)
31
+ rdd = rdd.map(lambda{|x| 1})
32
+ rdd = rdd.distinct
33
+ expect(rdd.collect).to eql([1])
34
+ end
35
+
36
+ context ".union" do
37
+ it "classic method" do
38
+ rdd = $sc.parallelize(numbers, 5)
39
+ rdd = rdd.union(rdd).collect
40
+
41
+ expect(rdd.collect.sort).to eql((numbers.to_a+numbers.to_a).sort)
42
+ end
43
+
44
+ it "with a different serializer" do
45
+ rdd1 = $sc.parallelize(numbers, 1, serializer: "marshal")
46
+ rdd2 = $sc.parallelize(numbers, 1, serializer: "oj")
47
+
48
+ expect { rdd1.union(rdd2).collect }.to_not raise_error
49
+ end
50
+
51
+ it "as operator" do
52
+ rdd1 = $sc.parallelize(numbers)
53
+ rdd2 = $sc.parallelize(rand_numbers)
54
+
55
+ expect((rdd1+rdd2).sum).to eql((numbers.to_a+rand_numbers).reduce(:+))
56
+ end
57
+ end
58
+
59
+ it ".compact" do
60
+ data = [nil, nil , 0, 0, 1, 2, nil, 6]
61
+ result = data.compact
62
+
63
+ rdd = $sc.parallelize(data, 1).compact
64
+ expect(rdd.collect).to eql(result)
65
+
66
+ rdd = $sc.parallelize(data, 5, batch_size: 1).compact
67
+ expect(rdd.collect).to eql(result)
68
+
69
+ rdd = $sc.parallelize(data, 1, batch_size: 1).compact
70
+ expect(rdd.collect).to eql(result)
71
+ end
72
+
73
+ it ".intersection" do
74
+ data1 = [0,1,2,3,4,5,6,7,8,9,10]
75
+ data2 = [5,6,7,8,9,10,11,12,13,14,15]
76
+
77
+ rdd1 = $sc.parallelize(data1)
78
+ rdd2 = $sc.parallelize(data2)
79
+
80
+ expect(rdd1.intersection(rdd2).collect.sort).to eql(data1 & data2)
81
+ end
82
+
83
+ it ".shuffle" do
84
+ data = Generator.numbers
85
+ rdd = $sc.parallelize(data)
86
+
87
+ expect(rdd.shuffle.collect).to_not eql(data)
88
+ end
89
+
90
+ context ".cartesian" do
91
+ let(:data1) { Generator.numbers(100) }
92
+ let(:data2) { Generator.numbers(100) }
93
+ let(:result) { data1.product(data2).map(&:to_s).sort }
94
+
95
+ it "unbatched" do
96
+ rdd1 = $sc.parallelize(data1, 2, batch_size: 1)
97
+ rdd2 = $sc.parallelize(data2, 2, batch_size: 1)
98
+
99
+ rdd = rdd1.cartesian(rdd2).map(lambda{|x| x.to_s})
100
+
101
+ expect(rdd.collect.sort).to eql(result)
102
+ end
103
+
104
+ it "batched" do
105
+ rdd1 = $sc.parallelize(data1, 2, batch_size: rand(4..10))
106
+ rdd2 = $sc.parallelize(data2, 2, batch_size: rand(4..10))
107
+
108
+ rdd = rdd1.cartesian(rdd2).map(lambda{|x| x.to_s})
109
+
110
+ expect(rdd.collect.sort).to eql(result)
111
+ end
112
+ end
113
+
114
+ end
@@ -0,0 +1,87 @@
1
+ require "spec_helper"
2
+
3
+ def func3(x)
4
+ x.map(&:to_i).reduce(:+)
5
+ end
6
+
7
+ def func4_with_index(data, index)
8
+ {
9
+ index => data.map(&:to_i).reduce(:*)
10
+ }
11
+ end
12
+
13
+ RSpec::shared_examples "a map partitions" do |workers|
14
+ context "with #{workers || 'default'} worker" do
15
+ it "without index" do
16
+ rdd2 = rdd(workers).map_partitions(func1)
17
+ result = func1.call(numbers)
18
+
19
+ expect(func1.call(rdd2.collect)).to eql(result)
20
+
21
+ rdd3 = rdd(workers)
22
+ rdd3 = rdd3.map_partitions(func1)
23
+ rdd3 = rdd3.map_partitions(func2)
24
+ rdd3 = rdd3.map_partitions(method(:func3))
25
+ result = func3(func2.call(func1.call(numbers)))
26
+
27
+ # Not same number of workers
28
+ expect(rdd3.collect.size).to be >= 1
29
+
30
+ rdd4 = rdd(workers)
31
+ rdd4 = rdd4.map_partitions(func1)
32
+ rdd4 = rdd4.map_partitions(func2)
33
+ rdd4 = rdd4.map_partitions(method(:func3))
34
+
35
+ expect(rdd4.collect).to eql(rdd3.collect)
36
+ end
37
+
38
+ it "with index" do
39
+ rdd2 = rdd(workers).map_partitions_with_index(method(:func4_with_index))
40
+ result = rdd2.collect
41
+
42
+ expect(result).to be_a(Array)
43
+
44
+ result.each do |x|
45
+ expect(x).to be_a(Hash)
46
+ end
47
+
48
+ # Multiply by 0
49
+ # Some values are 0 because of batched serialization
50
+ expect(result.map(&:values).flatten.compact.uniq.first).to eql(0)
51
+ end
52
+ end
53
+ end
54
+
55
+ RSpec::describe "Spark::RDD.map_partitions(_with_index)" do
56
+ let(:func1) { lambda{|x| x.map(&:to_i)} }
57
+ let(:func2) {
58
+ lambda{|x|
59
+ x.map{|y| y*2}
60
+ }
61
+ }
62
+
63
+ context "throught parallelize" do
64
+ let(:numbers) { 0..1000 }
65
+
66
+ def rdd(workers)
67
+ $sc.parallelize(numbers, workers)
68
+ end
69
+
70
+ it_behaves_like "a map partitions", nil
71
+ it_behaves_like "a map partitions", 1
72
+ it_behaves_like "a map partitions", rand(2..10)
73
+ end
74
+
75
+ context "throught text_file" do
76
+ let(:file) { File.join("spec", "inputs", "numbers_0_100.txt") }
77
+ let(:numbers) { File.readlines(file).map(&:strip) }
78
+
79
+ def rdd(workers)
80
+ $sc.text_file(file, workers)
81
+ end
82
+
83
+ it_behaves_like "a map partitions", nil
84
+ it_behaves_like "a map partitions", 1
85
+ it_behaves_like "a map partitions", rand(2..10)
86
+ end
87
+ end
@@ -0,0 +1,91 @@
1
+ require "spec_helper"
2
+
3
+ RSpec::shared_examples "a mapping" do |workers|
4
+ it "with #{workers || 'default'} worker" do
5
+ rdd2 = rdd(workers).map(func1)
6
+ result = numbers.map(&func1)
7
+
8
+ expect(rdd2.collect).to eql(result)
9
+
10
+ rdd3 = rdd(workers)
11
+ rdd3 = rdd3.map(func1)
12
+ rdd3 = rdd3.map(func2)
13
+ rdd3 = rdd3.map(func3)
14
+ result = numbers.map(&func1).map(&func2).map(&func3)
15
+
16
+ expect(rdd3.collect).to eql(result)
17
+
18
+ rdd4 = rdd(workers)
19
+ rdd4 = rdd4.map(func3)
20
+ rdd4 = rdd4.map(func2)
21
+ rdd4 = rdd4.map(func1)
22
+
23
+ expect(rdd4.collect).to eql(rdd3.collect)
24
+ end
25
+ end
26
+
27
+ RSpec::shared_examples "a mapping values" do |workers|
28
+ it "with #{workers || 'default'} worker" do
29
+ rdd2 = rdd(workers).map_values(func1)
30
+ result = hash.map{|key, value| [key, func1.call(value)]}
31
+
32
+ expect(rdd2.collect).to eql(result)
33
+
34
+ rdd3 = rdd(workers)
35
+ rdd3 = rdd3.map_values(func1)
36
+ rdd3 = rdd3.map_values(func2)
37
+ rdd3 = rdd3.map_values(func3)
38
+ result = hash.map{|key, value| [key, func1.call(value)]}
39
+ .map{|key, value| [key, func2.call(value)]}
40
+ .map{|key, value| [key, func3.call(value)]}
41
+
42
+ expect(rdd3.collect).to eql(result)
43
+ end
44
+ end
45
+
46
+ RSpec::describe "Spark::RDD" do
47
+ let(:func1) { lambda{|x| x*2} }
48
+ let(:func2) { lambda{|x| x*3} }
49
+ let(:func3) { lambda{|x| x*4} }
50
+
51
+ context "throught parallelize" do
52
+ context ".map" do
53
+ let(:numbers) { Generator.numbers }
54
+
55
+ def rdd(workers)
56
+ $sc.parallelize(numbers, workers)
57
+ end
58
+
59
+ it_behaves_like "a mapping", nil
60
+ it_behaves_like "a mapping", 1
61
+ it_behaves_like "a mapping", rand(2..10)
62
+ end
63
+
64
+ context ".map_values" do
65
+ let!(:hash) { Generator.hash }
66
+
67
+ def rdd(workers)
68
+ $sc.parallelize(hash, workers)
69
+ end
70
+
71
+ it_behaves_like "a mapping values", nil
72
+ it_behaves_like "a mapping values", 1
73
+ it_behaves_like "a mapping values", rand(2..10)
74
+ end
75
+ end
76
+
77
+ context "throught text_file" do
78
+ context ".map" do
79
+ let(:file) { File.join("spec", "inputs", "numbers_0_100.txt") }
80
+ let(:numbers) { File.readlines(file).map(&:strip) }
81
+
82
+ def rdd(workers)
83
+ $sc.text_file(file, workers)
84
+ end
85
+
86
+ it_behaves_like "a mapping", nil
87
+ it_behaves_like "a mapping", 1
88
+ it_behaves_like "a mapping", rand(2..10)
89
+ end
90
+ end
91
+ end