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,163 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Spark::Context do
4
+
5
+ it '.run_job' do
6
+ workers = 5
7
+ numbers = (0...100).to_a
8
+ func = lambda{|part| part.size}
9
+
10
+ rdd = $sc.parallelize(numbers, workers, batch_size: 1)
11
+
12
+ rdd_result = $sc.run_job(rdd, func)
13
+ result = numbers.each_slice(numbers.size/workers).map(&func)
14
+ expect(rdd_result).to eql(result)
15
+
16
+ parts = [0, 2]
17
+ func = lambda{|part| part.to_s}
18
+
19
+ rdd_result = $sc.run_job(rdd, func, parts)
20
+ result = []
21
+ sliced_numbers = numbers.each_slice(numbers.size/workers).to_a
22
+ parts.each do |part|
23
+ result << func.call(sliced_numbers[part])
24
+ end
25
+
26
+ expect(rdd_result).to eql(result)
27
+ end
28
+
29
+ it '.broadcast' do
30
+ workers = rand(1..5)
31
+
32
+ values1 = [1,2,3]
33
+ values2 = [4,5,6]
34
+
35
+ broadcast1 = $sc.broadcast(values1)
36
+ broadcast2 = $sc.broadcast(values2)
37
+
38
+ rdd = $sc.parallelize(0..5, workers)
39
+ rdd = rdd.bind(broadcast1: broadcast1, broadcast2: broadcast2)
40
+ rdd = rdd.map_partitions(lambda{|_| broadcast1.value + broadcast2.value })
41
+
42
+ expect(rdd.sum).to eql(
43
+ (values1 + values2).reduce(:+) * workers
44
+ )
45
+ end
46
+
47
+ context '.accumulator' do
48
+ it 'test' do
49
+ accum1 = $sc.accumulator(0,)
50
+ accum2 = $sc.accumulator(1, :*, 1)
51
+ accum3 = $sc.accumulator(0, lambda{|max, val| val > max ? val : max})
52
+
53
+ accum1 += 1
54
+
55
+ accum2.add(2)
56
+ accum2.add(2)
57
+ accum2.add(2)
58
+
59
+ accum3.add(9)
60
+ accum3.add(6)
61
+ accum3.add(7)
62
+
63
+ expect(accum1.value).to eql(1)
64
+ expect(accum2.value).to eql(8)
65
+ expect(accum3.value).to eql(9)
66
+
67
+ func = Proc.new do |_, index|
68
+ accum1.add(1)
69
+ accum2.add(2)
70
+ accum3.add(index * 10)
71
+ end
72
+
73
+ rdd = $sc.parallelize(0..4, 4)
74
+ rdd = rdd.bind(accum1: accum1, accum2: accum2, accum3: accum3)
75
+ rdd = rdd.map_partitions_with_index(func)
76
+ rdd.collect
77
+
78
+ # http://stackoverflow.com/questions/28560133/ruby-server-java-scala-client-deadlock
79
+ sleep(1)
80
+
81
+ expect(accum1.value).to eql(5)
82
+ expect(accum2.value).to eql(128)
83
+ expect(accum3.value).to eql(30)
84
+ end
85
+
86
+ context 'accum param' do
87
+ it 'symbol' do
88
+ accum1 = $sc.accumulator(1, :+, 0)
89
+ accum2 = $sc.accumulator(5, :-, 3)
90
+ accum3 = $sc.accumulator(1, :*, 1)
91
+ accum4 = $sc.accumulator(1.0, :/, 1.0)
92
+ accum5 = $sc.accumulator(2, :**, 2)
93
+
94
+ func = Proc.new do |_|
95
+ accum1.add(1)
96
+ accum2.add(1)
97
+ accum3.add(2)
98
+ accum4.add(2)
99
+ accum5.add(2)
100
+ end
101
+
102
+ rdd = $sc.parallelize(0..4, 2)
103
+ rdd = rdd.bind(accum1: accum1, accum2: accum2, accum3: accum3, accum4: accum4, accum5: accum5)
104
+ rdd = rdd.map_partitions(func)
105
+ rdd.collect
106
+
107
+ # http://stackoverflow.com/questions/28560133/ruby-server-java-scala-client-deadlock
108
+ sleep(1)
109
+
110
+ expect(accum1.value).to eq(3)
111
+ expect(accum2.value).to eq(1)
112
+ expect(accum3.value).to eq(4)
113
+ expect(accum4.value).to eq(4)
114
+ expect(accum5.value).to eq(65536)
115
+ end
116
+
117
+ it 'proc' do
118
+ accum1 = $sc.accumulator(1, lambda{|mem, val| mem + val}, 0)
119
+ accum2 = $sc.accumulator('a', lambda{|mem, val| mem + val}, '')
120
+ accum3 = $sc.accumulator([], lambda{|mem, val| mem << val}, [])
121
+
122
+ func = Proc.new do |_|
123
+ accum1.add(1)
124
+ accum2.add('a')
125
+ accum3.add(1)
126
+ end
127
+
128
+ rdd = $sc.parallelize(0..4, 2)
129
+ rdd = rdd.bind(accum1: accum1, accum2: accum2, accum3: accum3)
130
+ rdd = rdd.map_partitions(func)
131
+ rdd.collect
132
+
133
+ # http://stackoverflow.com/questions/28560133/ruby-server-java-scala-client-deadlock
134
+ sleep(1)
135
+
136
+ expect(accum1.value).to eq(3)
137
+ expect(accum2.value).to eq('aaa')
138
+ expect(accum3.value).to eq([[1], [1]])
139
+ end
140
+
141
+ it 'string' do
142
+ expect { $sc.accumulator(1, '0') }.to raise_error(Spark::SerializeError)
143
+
144
+ accum = $sc.accumulator(1, 'lambda{|mem, val| mem + val}', 0)
145
+
146
+ func = Proc.new do |_|
147
+ accum.add(1)
148
+ end
149
+
150
+ rdd = $sc.parallelize(0..4, 2)
151
+ rdd = rdd.bind(accum: accum)
152
+ rdd = rdd.map_partitions(func)
153
+ rdd.collect
154
+
155
+ # http://stackoverflow.com/questions/28560133/ruby-server-java-scala-client-deadlock
156
+ sleep(1)
157
+
158
+ expect(accum.value).to eq(3)
159
+ end
160
+ end
161
+ end
162
+
163
+ end
@@ -0,0 +1,72 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe Array do
4
+
5
+ it ".deep_copy" do
6
+ data = ["a", "b", "c"]
7
+ new_data = data.dup
8
+
9
+ data[0] << "a"
10
+
11
+ expect(data).to eql(new_data)
12
+
13
+ new_data = data.deep_copy
14
+
15
+ data[1] << "b"
16
+
17
+ expect(data).to_not eql(new_data)
18
+ end
19
+
20
+ end
21
+
22
+ RSpec.describe Hash do
23
+
24
+ it ".stringify_keys!" do
25
+ data = {
26
+ a: "a",
27
+ b: "b",
28
+ c: "c"
29
+ }
30
+
31
+ data.stringify_keys!
32
+
33
+ expect(data).to eql({
34
+ "a" => "a",
35
+ "b" => "b",
36
+ "c" => "c"
37
+ })
38
+ end
39
+
40
+ end
41
+
42
+ RSpec.describe String do
43
+
44
+ it ".camelize" do
45
+ data = "aaa_bbb_ccc".camelize
46
+ expect(data).to eql("AaaBbbCcc")
47
+ end
48
+
49
+ end
50
+
51
+ RSpec.describe IO do
52
+
53
+ it 'serialize' do
54
+ file = Tempfile.new('serialize')
55
+ file.binmode
56
+
57
+ file.write_int(1)
58
+ file.write_long(2)
59
+ file.write_string("3")
60
+ file.write_data([4])
61
+
62
+ file.rewind
63
+
64
+ expect(file.read_int).to eq(1)
65
+ expect(file.read_long).to eq(2)
66
+ expect(file.read_string).to eq("3")
67
+ expect(file.read_data).to eq([4])
68
+
69
+ file.unlink
70
+ end
71
+
72
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec::describe Spark::RDD do
4
+
5
+ context '.pipe' do
6
+ let(:words) { Generator.words }
7
+ let(:numbers) { Generator.numbers }
8
+
9
+ it 'single program' do
10
+ skip if windows?
11
+
12
+ rdd = $sc.parallelize(words, 1)
13
+ rdd = rdd.pipe('tr a b')
14
+
15
+ result = words.dup
16
+ result.map! do |x|
17
+ x.gsub('a', 'b')
18
+ end
19
+
20
+ expect(rdd.collect).to eql(result)
21
+ end
22
+
23
+ it 'multiple program' do
24
+ skip if windows?
25
+
26
+ rdd = $sc.parallelize(numbers, 1)
27
+ rdd = rdd.pipe("tr 1 5", "awk '{print $1*10}'")
28
+ rdd = rdd.map(lambda{|x| x.to_i * 100})
29
+
30
+ result = numbers.dup
31
+ result.map! do |x|
32
+ x.to_s.gsub('1', '5')
33
+ end
34
+ result.map! do |x|
35
+ x.to_i * 10
36
+ end
37
+ result.map! do |x|
38
+ x * 100
39
+ end
40
+
41
+ expect(rdd.collect).to eql(result)
42
+ end
43
+ end
44
+
45
+ end
@@ -0,0 +1,80 @@
1
+ require "spec_helper"
2
+
3
+ def func4(item)
4
+ item.start_with?("a") && item.size > 3 && item[1].to_s.ord > 106
5
+ end
6
+
7
+ RSpec::shared_examples "a filtering" do |workers|
8
+ context "with #{workers || 'default'} worker" do
9
+ it "when numbers" do
10
+ rdd2 = rdd_numbers(workers)
11
+ rdd2 = rdd2.filter(func1)
12
+ result = numbers.select(&func1)
13
+
14
+ expect(rdd2.collect).to eql(result)
15
+
16
+ rdd3 = rdd_numbers(workers)
17
+ rdd3 = rdd3.filter(func1)
18
+ rdd3 = rdd3.filter(func2)
19
+
20
+ expect(rdd3.collect).to eql([])
21
+ end
22
+
23
+ it "when words" do
24
+ rdd2 = rdd_words(workers)
25
+ rdd2 = rdd2.filter(func3)
26
+ result = words.select{|x| func3.call(x)}
27
+
28
+ expect(rdd2.collect).to eql(result)
29
+
30
+ rdd3 = rdd_words(workers)
31
+ rdd3 = rdd3.filter(method(:func4))
32
+ result = words.select{|x| func4(x)}
33
+
34
+ expect(rdd3.collect).to eql(result)
35
+ end
36
+ end
37
+ end
38
+
39
+ RSpec::describe "Spark::RDD.filter" do
40
+ let(:func1) { lambda{|x| x.to_i.even?} }
41
+ let(:func2) { lambda{|x| x.to_i.odd?} }
42
+ let(:func3) { lambda{|x| x.to_s.start_with?("b")} }
43
+
44
+ context "throught parallelize" do
45
+ let(:numbers) { Generator.numbers_with_zero }
46
+ let(:words) { Generator.words }
47
+
48
+ def rdd_numbers(workers)
49
+ $sc.parallelize(numbers, workers)
50
+ end
51
+
52
+ def rdd_words(workers)
53
+ $sc.parallelize(words, workers)
54
+ end
55
+
56
+ it_behaves_like "a filtering", nil
57
+ it_behaves_like "a filtering", 1
58
+ it_behaves_like "a filtering", rand(2..10)
59
+ end
60
+
61
+ context "throught text_file" do
62
+ let(:file_numbers) { File.join("spec", "inputs", "numbers_0_100.txt") }
63
+ let(:file_words) { File.join("spec", "inputs", "lorem_300.txt") }
64
+
65
+ let(:numbers) { File.readlines(file_numbers).map(&:strip) }
66
+ let(:words) { File.readlines(file_words).map(&:strip) }
67
+
68
+ def rdd_numbers(workers)
69
+ $sc.text_file(file_numbers, workers)
70
+ end
71
+
72
+ def rdd_words(workers)
73
+ $sc.text_file(file_words, workers)
74
+ end
75
+
76
+ it_behaves_like "a filtering", nil
77
+ it_behaves_like "a filtering", 1
78
+ it_behaves_like "a filtering", rand(2..10)
79
+ end
80
+ end
@@ -0,0 +1,100 @@
1
+ require "spec_helper"
2
+
3
+ RSpec::shared_examples "a flat mapping" do |workers|
4
+ it "with #{workers || 'default'} worker" do
5
+ rdd2 = rdd(workers).map(func1)
6
+ result = numbers.flat_map(&func1)
7
+
8
+ expect(rdd2.collect).to eql(result)
9
+
10
+ rdd3 = rdd(workers)
11
+ rdd3 = rdd3.flat_map(func1)
12
+ rdd3 = rdd3.flat_map(func2)
13
+ rdd3 = rdd3.flat_map(func3)
14
+ result = numbers.flat_map(&func1).flat_map(&func2).flat_map(&func3)
15
+
16
+ expect(rdd3.collect).to eql(result)
17
+
18
+ rdd4 = rdd(workers)
19
+ rdd4 = rdd4.flat_map(func1)
20
+ rdd4 = rdd4.flat_map(func2)
21
+ rdd4 = rdd4.flat_map(func3)
22
+
23
+ expect(rdd4.collect).to eql(rdd3.collect)
24
+ end
25
+ end
26
+
27
+ RSpec::shared_examples "a flat mapping values" do |workers|
28
+ it "with #{workers || 'default'} worker" do
29
+ rdd2 = rdd(workers).flat_map_values(func1)
30
+ result = []
31
+ hash_with_values.each do |(key, values)|
32
+ values = func1.call(values).flatten
33
+ values.each do |value|
34
+ result << [key, value]
35
+ end
36
+ end
37
+
38
+ expect(rdd2.collect).to eql(result)
39
+
40
+ rdd2 = rdd(workers).flat_map_values(func2)
41
+ result = []
42
+ hash_with_values.each do |(key, values)|
43
+ values = func2.call(values).flatten
44
+ values.each do |value|
45
+ result << [key, value]
46
+ end
47
+ end
48
+
49
+ expect(rdd2.collect).to eql(result)
50
+ end
51
+ end
52
+
53
+ RSpec::describe "Spark::RDD" do
54
+ let(:func1) { lambda{|x| x*2} }
55
+ let(:func2) { lambda{|x| [x*3, 1, 1]} }
56
+ let(:func3) { lambda{|x| [x*4, 2, 2]} }
57
+
58
+ context "throught parallelize" do
59
+ context ".flat_map" do
60
+ let(:numbers) { Generator.numbers_with_zero }
61
+
62
+ def rdd(workers)
63
+ $sc.parallelize(numbers, workers)
64
+ end
65
+
66
+ it_behaves_like "a flat mapping", nil
67
+ it_behaves_like "a flat mapping", 1
68
+ it_behaves_like "a flat mapping", rand(2..10)
69
+ end
70
+
71
+ context ".flat_map_values" do
72
+ let(:func1) { lambda{|x| x*2} }
73
+ let(:func2) { lambda{|x| [x.first]} }
74
+ let(:hash_with_values) { Generator.hash_with_values }
75
+
76
+ def rdd(workers)
77
+ $sc.parallelize(hash_with_values, workers)
78
+ end
79
+
80
+ it_behaves_like "a flat mapping values", nil
81
+ it_behaves_like "a flat mapping values", 1
82
+ it_behaves_like "a flat mapping values", rand(2..10)
83
+ end
84
+ end
85
+
86
+ context "throught text_file" do
87
+ context ".flat_map" do
88
+ let(:file) { File.join("spec", "inputs", "numbers_0_100.txt") }
89
+ let(:numbers) { File.readlines(file).map(&:strip) }
90
+
91
+ def rdd(workers)
92
+ $sc.text_file(file, workers)
93
+ end
94
+
95
+ it_behaves_like "a flat mapping", nil
96
+ it_behaves_like "a flat mapping", 1
97
+ it_behaves_like "a flat mapping", rand(2..10)
98
+ end
99
+ end
100
+ end