Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development for save_as_text_file #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/spark/rdd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,19 @@ def values
end


# Save the RDD as a text file
#
# == Example:
# rdd = $sc.parallelize([1,2,3,4,5])
# rdd.save_as_text_file(path)
#
def save_as_text_file(path)
jrdd.saveAsTextFile(path)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

end




# Aliases
alias_method :partitionsSize, :partitions_size
alias_method :defaultReducePartitions, :default_reduce_partitions
Expand Down
39 changes: 39 additions & 0 deletions spec/lib/save_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'spec_helper'

RSpec.describe 'Spark::RDD' do

context '.save_as_text_file' do
let(:file) { File.join('spec', 'inputs', 'numbers_0_100.txt') }

def serializer
Spark::Serializer.build { __batched__(__marshal__, 1) }
end

def file_rdd
$sc.text_file(file, 2, Encoding::UTF_8, serializer)
end

def par_rdd
$sc.parallelize( (0..5).collect { |i| i.to_s }, 2, serializer)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use (0..5).map(&:to_s)

end

it 'saves the file_rdd' do
Dir.mktmpdir do |tmpdir|
file_rdd.save_as_text_file(File.join(tmpdir,'file_rdd.txt'))
result = Dir.glob(File.join(tmpdir,'file_rdd.txt','*')).collect { |file| File.readlines(file).collect { |l| l.chomp } }.flatten
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really ugly line :-).


expect(result).to eq (0..100).collect { |i| i.to_s }
end
end

it 'saves the par_rdd' do
Dir.mktmpdir do |tmpdir|
par_rdd.save_as_text_file(File.join(tmpdir,'par_rdd.txt'))
result = Dir.glob(File.join(tmpdir,'par_rdd.txt','*')).collect { |file| File.readlines(file).collect { |l| l.chomp } }.flatten

expect(result).to eq (0..5).collect { |i| i.to_s }
end
end

end
end