ruby-spark 1.1.0.1-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (180) 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 +252 -0
  7. data/Rakefile +35 -0
  8. data/TODO.md +6 -0
  9. data/benchmark/aggregate.rb +33 -0
  10. data/benchmark/bisect.rb +88 -0
  11. data/benchmark/comparison/prepare.sh +18 -0
  12. data/benchmark/comparison/python.py +156 -0
  13. data/benchmark/comparison/r.r +69 -0
  14. data/benchmark/comparison/ruby.rb +167 -0
  15. data/benchmark/comparison/run-all.sh +160 -0
  16. data/benchmark/comparison/scala.scala +181 -0
  17. data/benchmark/custom_marshal.rb +94 -0
  18. data/benchmark/digest.rb +150 -0
  19. data/benchmark/enumerator.rb +88 -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/example/website_search.rb +83 -0
  27. data/ext/ruby_c/extconf.rb +3 -0
  28. data/ext/ruby_c/murmur.c +158 -0
  29. data/ext/ruby_c/murmur.h +9 -0
  30. data/ext/ruby_c/ruby-spark.c +18 -0
  31. data/ext/ruby_java/Digest.java +36 -0
  32. data/ext/ruby_java/Murmur2.java +98 -0
  33. data/ext/ruby_java/RubySparkExtService.java +28 -0
  34. data/ext/ruby_java/extconf.rb +3 -0
  35. data/ext/spark/build.sbt +73 -0
  36. data/ext/spark/project/plugins.sbt +9 -0
  37. data/ext/spark/sbt/sbt +34 -0
  38. data/ext/spark/src/main/scala/Exec.scala +91 -0
  39. data/ext/spark/src/main/scala/MLLibAPI.scala +4 -0
  40. data/ext/spark/src/main/scala/Marshal.scala +52 -0
  41. data/ext/spark/src/main/scala/MarshalDump.scala +113 -0
  42. data/ext/spark/src/main/scala/MarshalLoad.scala +220 -0
  43. data/ext/spark/src/main/scala/RubyAccumulatorParam.scala +69 -0
  44. data/ext/spark/src/main/scala/RubyBroadcast.scala +13 -0
  45. data/ext/spark/src/main/scala/RubyConstant.scala +13 -0
  46. data/ext/spark/src/main/scala/RubyMLLibAPI.scala +55 -0
  47. data/ext/spark/src/main/scala/RubyMLLibUtilAPI.scala +21 -0
  48. data/ext/spark/src/main/scala/RubyPage.scala +34 -0
  49. data/ext/spark/src/main/scala/RubyRDD.scala +392 -0
  50. data/ext/spark/src/main/scala/RubySerializer.scala +14 -0
  51. data/ext/spark/src/main/scala/RubyTab.scala +11 -0
  52. data/ext/spark/src/main/scala/RubyUtils.scala +15 -0
  53. data/ext/spark/src/main/scala/RubyWorker.scala +257 -0
  54. data/ext/spark/src/test/scala/MarshalSpec.scala +84 -0
  55. data/lib/ruby-spark.rb +1 -0
  56. data/lib/spark.rb +198 -0
  57. data/lib/spark/accumulator.rb +260 -0
  58. data/lib/spark/broadcast.rb +98 -0
  59. data/lib/spark/build.rb +43 -0
  60. data/lib/spark/cli.rb +169 -0
  61. data/lib/spark/command.rb +86 -0
  62. data/lib/spark/command/base.rb +158 -0
  63. data/lib/spark/command/basic.rb +345 -0
  64. data/lib/spark/command/pair.rb +124 -0
  65. data/lib/spark/command/sort.rb +51 -0
  66. data/lib/spark/command/statistic.rb +144 -0
  67. data/lib/spark/command_builder.rb +141 -0
  68. data/lib/spark/command_validator.rb +34 -0
  69. data/lib/spark/config.rb +238 -0
  70. data/lib/spark/constant.rb +14 -0
  71. data/lib/spark/context.rb +322 -0
  72. data/lib/spark/error.rb +50 -0
  73. data/lib/spark/ext/hash.rb +41 -0
  74. data/lib/spark/ext/integer.rb +25 -0
  75. data/lib/spark/ext/io.rb +67 -0
  76. data/lib/spark/ext/ip_socket.rb +29 -0
  77. data/lib/spark/ext/module.rb +58 -0
  78. data/lib/spark/ext/object.rb +24 -0
  79. data/lib/spark/ext/string.rb +24 -0
  80. data/lib/spark/helper.rb +10 -0
  81. data/lib/spark/helper/logger.rb +40 -0
  82. data/lib/spark/helper/parser.rb +85 -0
  83. data/lib/spark/helper/serialize.rb +71 -0
  84. data/lib/spark/helper/statistic.rb +93 -0
  85. data/lib/spark/helper/system.rb +42 -0
  86. data/lib/spark/java_bridge.rb +19 -0
  87. data/lib/spark/java_bridge/base.rb +203 -0
  88. data/lib/spark/java_bridge/jruby.rb +23 -0
  89. data/lib/spark/java_bridge/rjb.rb +41 -0
  90. data/lib/spark/logger.rb +76 -0
  91. data/lib/spark/mllib.rb +100 -0
  92. data/lib/spark/mllib/classification/common.rb +31 -0
  93. data/lib/spark/mllib/classification/logistic_regression.rb +223 -0
  94. data/lib/spark/mllib/classification/naive_bayes.rb +97 -0
  95. data/lib/spark/mllib/classification/svm.rb +135 -0
  96. data/lib/spark/mllib/clustering/gaussian_mixture.rb +82 -0
  97. data/lib/spark/mllib/clustering/kmeans.rb +118 -0
  98. data/lib/spark/mllib/matrix.rb +120 -0
  99. data/lib/spark/mllib/regression/common.rb +73 -0
  100. data/lib/spark/mllib/regression/labeled_point.rb +41 -0
  101. data/lib/spark/mllib/regression/lasso.rb +100 -0
  102. data/lib/spark/mllib/regression/linear.rb +124 -0
  103. data/lib/spark/mllib/regression/ridge.rb +97 -0
  104. data/lib/spark/mllib/ruby_matrix/matrix_adapter.rb +53 -0
  105. data/lib/spark/mllib/ruby_matrix/vector_adapter.rb +57 -0
  106. data/lib/spark/mllib/stat/distribution.rb +12 -0
  107. data/lib/spark/mllib/vector.rb +185 -0
  108. data/lib/spark/rdd.rb +1377 -0
  109. data/lib/spark/sampler.rb +92 -0
  110. data/lib/spark/serializer.rb +79 -0
  111. data/lib/spark/serializer/auto_batched.rb +59 -0
  112. data/lib/spark/serializer/base.rb +63 -0
  113. data/lib/spark/serializer/batched.rb +84 -0
  114. data/lib/spark/serializer/cartesian.rb +13 -0
  115. data/lib/spark/serializer/compressed.rb +27 -0
  116. data/lib/spark/serializer/marshal.rb +17 -0
  117. data/lib/spark/serializer/message_pack.rb +23 -0
  118. data/lib/spark/serializer/oj.rb +23 -0
  119. data/lib/spark/serializer/pair.rb +41 -0
  120. data/lib/spark/serializer/text.rb +25 -0
  121. data/lib/spark/sort.rb +189 -0
  122. data/lib/spark/stat_counter.rb +125 -0
  123. data/lib/spark/storage_level.rb +39 -0
  124. data/lib/spark/version.rb +3 -0
  125. data/lib/spark/worker/master.rb +144 -0
  126. data/lib/spark/worker/spark_files.rb +15 -0
  127. data/lib/spark/worker/worker.rb +200 -0
  128. data/ruby-spark.gemspec +47 -0
  129. data/spec/generator.rb +37 -0
  130. data/spec/inputs/lorem_300.txt +316 -0
  131. data/spec/inputs/numbers/1.txt +50 -0
  132. data/spec/inputs/numbers/10.txt +50 -0
  133. data/spec/inputs/numbers/11.txt +50 -0
  134. data/spec/inputs/numbers/12.txt +50 -0
  135. data/spec/inputs/numbers/13.txt +50 -0
  136. data/spec/inputs/numbers/14.txt +50 -0
  137. data/spec/inputs/numbers/15.txt +50 -0
  138. data/spec/inputs/numbers/16.txt +50 -0
  139. data/spec/inputs/numbers/17.txt +50 -0
  140. data/spec/inputs/numbers/18.txt +50 -0
  141. data/spec/inputs/numbers/19.txt +50 -0
  142. data/spec/inputs/numbers/2.txt +50 -0
  143. data/spec/inputs/numbers/20.txt +50 -0
  144. data/spec/inputs/numbers/3.txt +50 -0
  145. data/spec/inputs/numbers/4.txt +50 -0
  146. data/spec/inputs/numbers/5.txt +50 -0
  147. data/spec/inputs/numbers/6.txt +50 -0
  148. data/spec/inputs/numbers/7.txt +50 -0
  149. data/spec/inputs/numbers/8.txt +50 -0
  150. data/spec/inputs/numbers/9.txt +50 -0
  151. data/spec/inputs/numbers_0_100.txt +101 -0
  152. data/spec/inputs/numbers_1_100.txt +100 -0
  153. data/spec/lib/collect_spec.rb +42 -0
  154. data/spec/lib/command_spec.rb +68 -0
  155. data/spec/lib/config_spec.rb +64 -0
  156. data/spec/lib/context_spec.rb +165 -0
  157. data/spec/lib/ext_spec.rb +72 -0
  158. data/spec/lib/external_apps_spec.rb +45 -0
  159. data/spec/lib/filter_spec.rb +80 -0
  160. data/spec/lib/flat_map_spec.rb +100 -0
  161. data/spec/lib/group_spec.rb +109 -0
  162. data/spec/lib/helper_spec.rb +19 -0
  163. data/spec/lib/key_spec.rb +41 -0
  164. data/spec/lib/manipulation_spec.rb +122 -0
  165. data/spec/lib/map_partitions_spec.rb +87 -0
  166. data/spec/lib/map_spec.rb +91 -0
  167. data/spec/lib/mllib/classification_spec.rb +54 -0
  168. data/spec/lib/mllib/clustering_spec.rb +35 -0
  169. data/spec/lib/mllib/matrix_spec.rb +32 -0
  170. data/spec/lib/mllib/regression_spec.rb +116 -0
  171. data/spec/lib/mllib/vector_spec.rb +77 -0
  172. data/spec/lib/reduce_by_key_spec.rb +118 -0
  173. data/spec/lib/reduce_spec.rb +131 -0
  174. data/spec/lib/sample_spec.rb +46 -0
  175. data/spec/lib/serializer_spec.rb +88 -0
  176. data/spec/lib/sort_spec.rb +58 -0
  177. data/spec/lib/statistic_spec.rb +170 -0
  178. data/spec/lib/whole_text_files_spec.rb +33 -0
  179. data/spec/spec_helper.rb +38 -0
  180. metadata +389 -0
@@ -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
@@ -0,0 +1,46 @@
1
+ require "spec_helper"
2
+
3
+ # Sample method can not be tested because of random generator
4
+ # Just test it for raising error
5
+
6
+ RSpec::shared_examples "a sampler" do |workers|
7
+ context "with #{workers || 'default'} worker" do
8
+
9
+ context ".sample" do
10
+ it "with replacement" do
11
+ rdd2 = rdd(workers).sample(true, rand)
12
+ expect { rdd2.collect }.to_not raise_error
13
+ end
14
+
15
+ it "without replacement" do
16
+ rdd2 = rdd(workers).sample(false, rand)
17
+ expect { rdd2.collect }.to_not raise_error
18
+ end
19
+ end
20
+
21
+ context ".take_sample" do
22
+ it "with replacement" do
23
+ size = rand(10..999)
24
+ expect(rdd(workers).take_sample(true, size).size).to eql(size)
25
+ end
26
+
27
+ it "without replacement" do
28
+ size = rand(10..999)
29
+ expect(rdd(workers).take_sample(false, size).size).to eql(size)
30
+ end
31
+ end
32
+
33
+ end
34
+ end
35
+
36
+ RSpec::describe "Spark::RDD" do
37
+ let(:numbers) { Generator.numbers(1000) }
38
+
39
+ def rdd(workers)
40
+ $sc.parallelize(numbers, workers)
41
+ end
42
+
43
+ it_behaves_like "a sampler", nil
44
+ it_behaves_like "a sampler", 1
45
+ it_behaves_like "a sampler", rand(2..10)
46
+ end
@@ -0,0 +1,88 @@
1
+ require 'spec_helper'
2
+ require 'zlib'
3
+
4
+ RSpec.describe Spark::Serializer do
5
+ let(:data) { [1, 'test', 2.0, [3], {key: 'value'}, :test, String] }
6
+
7
+ it 'find' do
8
+ expect(described_class.find('not_existed_class')).to eql(nil)
9
+
10
+ expect(described_class.find('Marshal')).to eq(described_class::Marshal)
11
+ expect(described_class.find('marshal')).to eq(described_class::Marshal)
12
+ expect(described_class.find(:marshal)).to eq(described_class::Marshal)
13
+ expect(described_class.find('batched')).to eq(described_class::Batched)
14
+ end
15
+
16
+ it 'find!' do
17
+ expect { expect(described_class.find!('not_existed_class')) }.to raise_error(Spark::SerializeError)
18
+ expect { expect(described_class.find!('marshal')) }.to_not raise_error
19
+ expect { expect(described_class.find!('batched')) }.to_not raise_error
20
+ end
21
+
22
+ it 'register' do
23
+ NewSerializer = Class.new
24
+
25
+ expect(described_class.find('new_serializer_1')).to eql(nil)
26
+ expect(described_class.find('new_serializer_2')).to eql(nil)
27
+ expect(described_class.find('new_serializer_3')).to eql(nil)
28
+
29
+ described_class.register('new_serializer_1', 'new_serializer_2', 'new_serializer_3', NewSerializer)
30
+
31
+ expect(described_class.find('new_serializer_1')).to eql(NewSerializer)
32
+ expect(described_class.find('new_serializer_2')).to eql(NewSerializer)
33
+ expect(described_class.find('new_serializer_3')).to eql(NewSerializer)
34
+ end
35
+
36
+ it '==' do
37
+ # One class
38
+ marshal1 = described_class::Marshal.new
39
+ marshal2 = described_class::Marshal.new
40
+
41
+ expect(marshal1).to eq(marshal1)
42
+ expect(marshal1).to eq(marshal2)
43
+
44
+ # Two classes
45
+ compressed1 = described_class::Compressed.new(marshal1)
46
+ compressed2 = described_class::Compressed.new(marshal2)
47
+
48
+ expect(compressed1).to eq(compressed1)
49
+ expect(compressed1).to eq(compressed2)
50
+
51
+ # Three classes
52
+ batched1 = described_class::Batched.new(compressed1, 1)
53
+ batched2 = described_class::Batched.new(compressed2, 1)
54
+ batched3 = described_class::Batched.new(compressed1, 2)
55
+
56
+ expect(batched1).to eq(batched2)
57
+ expect(batched1).to_not eq(batched3)
58
+ end
59
+
60
+ context 'build' do
61
+ let(:marshal1) { described_class::Marshal.new }
62
+ let(:compressed1) { described_class::Compressed.new(marshal1) }
63
+ let(:batched1) { described_class::Batched.new(compressed1, 1) }
64
+
65
+ it 'block' do
66
+ expect(described_class.build{ marshal }).to eq(marshal1)
67
+ expect(described_class.build{ marshal }).to eq(described_class.build{ __marshal__ })
68
+ expect(described_class.build{ compressed(marshal) }).to eq(compressed1)
69
+ expect(described_class.build{ batched(compressed(marshal), 1) }).to eq(batched1)
70
+ end
71
+
72
+ it 'text' do
73
+ expect(described_class.build('marshal')).to eq(marshal1)
74
+ expect(described_class.build('compressed(marshal)')).to eq(compressed1)
75
+ expect(described_class.build('batched(compressed(marshal), 1)')).to eq(batched1)
76
+ end
77
+ end
78
+
79
+ it 'serialization' do
80
+ marshal1 = described_class.build{ marshal }
81
+ compressed1 = described_class.build{ compressed(marshal) }
82
+
83
+ expect(marshal1.dump(data)).to eq(Marshal.dump(data))
84
+ expect(compressed1.dump(data)).to eq(
85
+ Zlib::Deflate.deflate(Marshal.dump(data))
86
+ )
87
+ end
88
+ end
@@ -0,0 +1,58 @@
1
+ require "spec_helper"
2
+
3
+ RSpec::shared_examples "a sorting" do |workers|
4
+ it "with #{workers || 'default'} worker" do
5
+ rdd2 = rdd(workers)
6
+
7
+ rdd2 = rdd2.flat_map(split)
8
+ result = lines.flat_map(&split)
9
+
10
+ # Sort by self
11
+ rdd3 = rdd2.map(map).sort_by_key
12
+ result2 = result.map(&map).sort_by{|(key, _)| key}
13
+
14
+ expect(rdd3.collect).to eql(result2)
15
+
16
+ # Sort by len
17
+ rdd3 = rdd2.map(len_map).sort_by_key
18
+ result2 = result.map(&len_map).sort_by{|(key, _)| key}
19
+
20
+ expect(rdd3.collect).to eql(result2)
21
+ end
22
+ end
23
+
24
+
25
+ RSpec::describe "Spark::RDD" do
26
+ let(:split) { lambda{|x| x.split} }
27
+ let(:map) { lambda{|x| [x.to_s, 1]} }
28
+ let(:len_map) { lambda{|x| [x.size, x]} }
29
+
30
+ context "throught parallelize" do
31
+ context ".map" do
32
+ let(:lines) { Generator.lines }
33
+
34
+ def rdd(workers)
35
+ $sc.parallelize(lines, workers)
36
+ end
37
+
38
+ it_behaves_like "a sorting", nil
39
+ it_behaves_like "a sorting", 1
40
+ it_behaves_like "a sorting", rand(2..10)
41
+ end
42
+ end
43
+
44
+ context "throught text_file" do
45
+ context ".map" do
46
+ let(:file) { File.join("spec", "inputs", "lorem_300.txt") }
47
+ let(:lines) { File.readlines(file).map(&:strip) }
48
+
49
+ def rdd(workers)
50
+ $sc.text_file(file, workers)
51
+ end
52
+
53
+ it_behaves_like "a sorting", nil
54
+ it_behaves_like "a sorting", 1
55
+ it_behaves_like "a sorting", rand(2..10)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,170 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.shared_examples 'a stats' do |workers|
4
+ let(:numbers) { [1.0, 1.0, 2.0, 3.0, 5.0, 8.0] }
5
+
6
+ context "with #{workers || 'default'} worker" do
7
+ it 'stats class' do
8
+ stats = $sc.parallelize(numbers, workers).stats
9
+
10
+ expect(stats.sum).to be_within(0.1).of(20)
11
+ expect(stats.mean).to be_within(0.1).of(20/6.0)
12
+ expect(stats.max).to be_within(0.1).of(8.0)
13
+ expect(stats.min).to be_within(0.1).of(1.0)
14
+ expect(stats.variance).to be_within(0.1).of(6.22222)
15
+ expect(stats.sample_variance).to be_within(0.1).of(7.46667)
16
+ expect(stats.stdev).to be_within(0.1).of(2.49444)
17
+ expect(stats.sample_stdev).to be_within(0.1).of(2.73252)
18
+ end
19
+
20
+ it 'rdd methods' do
21
+ rdd = $sc.parallelize([1, 2, 3], workers)
22
+
23
+ expect(rdd.mean).to be_within(0.1).of(2.0)
24
+ expect(rdd.variance).to be_within(0.1).of(0.666)
25
+ expect(rdd.stdev).to be_within(0.1).of(0.816)
26
+ expect(rdd.sample_stdev).to be_within(0.1).of(1.0)
27
+ expect(rdd.sample_variance).to be_within(0.1).of(1.0)
28
+ end
29
+ end
30
+ end
31
+
32
+ RSpec.shared_examples 'a histogram' do |workers|
33
+
34
+ context "with #{workers || 'default'} worker" do
35
+ it 'empty' do
36
+ rdd = $sc.parallelize([], workers, ser)
37
+
38
+ expect( rdd.histogram([0, 10])[1] ).to eq([0])
39
+ expect( rdd.histogram([0, 4, 10])[1] ).to eq([0, 0])
40
+ end
41
+
42
+ it 'validation' do
43
+ rdd = $sc.parallelize([], workers, ser)
44
+ expect { rdd.histogram(0) }.to raise_error(ArgumentError)
45
+ end
46
+
47
+ it 'double' do
48
+ rdd = $sc.parallelize([1.0, 2.0, 3.0, 4.0], workers, ser)
49
+ buckets, counts = rdd.histogram(2)
50
+
51
+ expect(buckets).to eq([1.0, 2.5, 4.0])
52
+ expect(counts).to eq([2, 2])
53
+ end
54
+
55
+ it 'out of range' do
56
+ rdd = $sc.parallelize([10.01, -0.01], workers, ser)
57
+
58
+ expect( rdd.histogram([0, 10])[1] ).to eq([0])
59
+ expect( rdd.histogram([0, 4, 10])[1] ).to eq([0, 0])
60
+ end
61
+
62
+ it 'in range with one bucket' do
63
+ rdd = $sc.parallelize([1, 2, 3, 4], workers, ser)
64
+
65
+ expect( rdd.histogram([0, 10])[1] ).to eq([4])
66
+ expect( rdd.histogram([0, 4, 10])[1] ).to eq([3, 1])
67
+ end
68
+
69
+ it 'in range with one bucket exact match' do
70
+ rdd = $sc.parallelize([1, 2, 3, 4], workers, ser)
71
+ expect( rdd.histogram([1, 4])[1] ).to eq([4])
72
+ end
73
+
74
+ it 'out of range with two buckets' do
75
+ rdd = $sc.parallelize([10.01, -0.01], workers, ser)
76
+ expect( rdd.histogram([0, 5, 10])[1] ).to eq([0, 0])
77
+ end
78
+
79
+ it 'out of range with two uneven buckets' do
80
+ rdd = $sc.parallelize([10.01, -0.01], workers, ser)
81
+ expect( rdd.histogram([0, 4, 10])[1] ).to eq([0, 0])
82
+ end
83
+
84
+ it 'in range with two buckets' do
85
+ rdd = $sc.parallelize([1, 2, 3, 5, 6], workers, ser)
86
+ expect( rdd.histogram([0, 5, 10])[1] ).to eq([3, 2])
87
+ end
88
+
89
+ it 'in range with two bucket and nil' do
90
+ rdd = $sc.parallelize([1, 2, 3, 5, 6, nil, Float::NAN], workers, ser)
91
+ expect( rdd.histogram([0, 5, 10])[1] ).to eq([3, 2])
92
+ end
93
+
94
+ it 'in range with two uneven buckets' do
95
+ rdd = $sc.parallelize([1, 2, 3, 5, 6], workers, ser)
96
+ expect( rdd.histogram([0, 5, 11])[1] ).to eq([3, 2])
97
+ end
98
+
99
+ it 'mixed range with two uneven buckets' do
100
+ rdd = $sc.parallelize([-0.01, 0.0, 1, 2, 3, 5, 6, 11.0, 11.01], workers, ser)
101
+ expect( rdd.histogram([0, 5, 11])[1] ).to eq([4, 3])
102
+ end
103
+
104
+ it 'mixed range with four uneven buckets' do
105
+ rdd = $sc.parallelize([-0.01, 0.0, 1, 2, 3, 5, 6, 11.01, 12.0, 199.0, 200.0, 200.1], workers, ser)
106
+ expect( rdd.histogram([0.0, 5.0, 11.0, 12.0, 200.0])[1] ).to eq([4, 2, 1, 3])
107
+ end
108
+
109
+ it 'mixed range with uneven buckets and NaN' do
110
+ rdd = $sc.parallelize([-0.01, 0.0, 1, 2, 3, 5, 6, 11.01, 12.0, 199.0, 200.0, 200.1, nil, Float::NAN], workers, ser)
111
+ expect( rdd.histogram([0.0, 5.0, 11.0, 12.0, 200.0])[1] ).to eq([4, 2, 1, 3])
112
+ end
113
+
114
+ it 'out of range with infinite buckets' do
115
+ rdd = $sc.parallelize([10.01, -0.01, Float::NAN, Float::INFINITY], workers, ser)
116
+ expect( rdd.histogram([-Float::INFINITY, 0, Float::INFINITY])[1] ).to eq([1, 1])
117
+ end
118
+
119
+ it 'without buckets' do
120
+ rdd = $sc.parallelize([1, 2, 3, 4], workers, ser)
121
+ expect( rdd.histogram(1) ).to eq([[1, 4], [4]])
122
+ end
123
+
124
+ it 'without buckets single element' do
125
+ rdd = $sc.parallelize([1], workers, ser)
126
+ expect( rdd.histogram(1) ).to eq([[1, 1], [1]])
127
+ end
128
+
129
+ it 'without bucket no range' do
130
+ rdd = $sc.parallelize([1, 1, 1, 1], workers, ser)
131
+ expect( rdd.histogram(1) ).to eq([[1, 1], [4]])
132
+ end
133
+
134
+ it 'without buckets basic two' do
135
+ rdd = $sc.parallelize([1, 2, 3, 4], workers, ser)
136
+ expect( rdd.histogram(2) ).to eq([[1, 2.5, 4], [2, 2]])
137
+ end
138
+
139
+ it 'without buckets with more requested than elements' do
140
+ rdd = $sc.parallelize([1, 2], workers, ser)
141
+ buckets = [1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0]
142
+ hist = [1, 0, 0, 0, 0, 0, 0, 0, 0, 1]
143
+
144
+ expect( rdd.histogram(10) ).to eq([buckets, hist])
145
+ end
146
+
147
+ it 'string' do
148
+ rdd = $sc.parallelize(['ab', 'ac', 'b', 'bd', 'ef'], workers, ser)
149
+
150
+ expect( rdd.histogram(['a', 'b', 'c'])[1] ).to eq([2, 2])
151
+ expect( rdd.histogram(1) ).to eq([['ab', 'ef'], [5]])
152
+ expect { rdd.histogram(2) }.to raise_error(Spark::RDDError)
153
+ end
154
+
155
+ end
156
+ end
157
+
158
+ RSpec.describe Spark::RDD do
159
+ let(:ser) { Spark::Serializer.build { __batched__(__marshal__, 1) } }
160
+
161
+ context '.stats' do
162
+ it_behaves_like 'a stats', 1
163
+ it_behaves_like 'a stats', rand(2..5)
164
+ end
165
+
166
+ context '.histogram' do
167
+ it_behaves_like 'a histogram', 1
168
+ it_behaves_like 'a histogram', rand(2..5)
169
+ end
170
+ end