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

Add DataFrameWriter related stuff #24

Open
wants to merge 3 commits into
base: sql-dataframe
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
2 changes: 1 addition & 1 deletion lib/spark/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Build

DEFAULT_SCALA_VERSION = '2.10.4'
DEFAULT_CORE_VERSION = '2.10'
DEFAULT_SPARK_VERSION = '1.5.1'
DEFAULT_SPARK_VERSION = '1.5.2'
DEFAULT_HADOOP_VERSION = '1.0.4'

SBT = 'sbt/sbt'
Expand Down
1 change: 1 addition & 0 deletions lib/spark/sql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module SQL
autoload_without_import :DataType, 'spark/sql/data_type'
autoload_without_import :DataFrame, 'spark/sql/data_frame'
autoload_without_import :DataFrameReader, 'spark/sql/data_frame_reader'
autoload_without_import :DataFrameWriter, 'spark/sql/data_frame_writer'

autoload :Row, 'spark/sql/row'
autoload :Column, 'spark/sql/column'
Expand Down
4 changes: 4 additions & 0 deletions lib/spark/sql/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def read
DataFrameReader.new(self)
end

def sql query
DataFrame.new(jsql_context.sql(query).toDF, self)
end

end
end
end
9 changes: 8 additions & 1 deletion lib/spark/sql/data_frame.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,17 @@ def filter(condition)

# Limits the result count to the number specified.
def limit(num)
new_jdf = jdf.limit(mum)
new_jdf = jdf.limit(num)
DataFrame.new(new_jdf, sql_context)
end

def register_temp_table name
jdf.registerTempTable(name)
end

def write
DataFrameWriter.new(self)
end

alias_method :where, :filter

Expand Down
12 changes: 12 additions & 0 deletions lib/spark/sql/data_frame_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ def json(path, new_schema=nil)
load(path, 'org.apache.spark.sql.execution.datasources.json', new_schema)
end

def parquet(path, new_schema=nil)
load(path, 'org.apache.spark.sql.execution.datasources.parquet', new_schema)
end

def orc(path, new_schema=nil)
load(path, 'org.apache.spark.sql.execution.datasources.orc', new_schema)
end

def table name
df(jreader.table(name))
end

end
end
end
101 changes: 101 additions & 0 deletions lib/spark/sql/data_frame_writer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
module Spark
module SQL
class DataFrameWriter

attr_reader :sql_context, :jwriter

def initialize(df)
@sql_context = df.sql_context
@jwriter = df.jdf.write
end

# Specifies the input data source format.
# Parameter is name of the data source, e.g. 'json', 'parquet'.
def format(source)
jwriter.format(source)
self
end

# Adds an input option for the underlying data source.
def option(key, value)
jwriter.option(key, value.to_s)
self
end

# Adds input options for the underlying data source.
def options(options)
options.each do |key, value|
jwriter.option(key, value.to_s)
end
self
end

# Loads data from a data source and returns it as a :class`DataFrame`.
#
# == Parameters:
# path:: Optional string for file-system backed data sources.
# format:: Optional string for format of the data source. Default to 'parquet'.
# schema:: Optional {StructType} for the input schema.
# options:: All other string options.
#
def save(path=nil, new_format=nil, new_options=nil)
new_format && format(new_format)
new_options && options(new_options)

# TODO - jwrite returns nil, probably should catch exception and return true/false
if path.nil?
jwriter.save
else
jwriter.save(path)
end
end

# Saves DataFrame as a JSON file (one object per line)
#
# == Parameters:
# path:: string, path to the JSON dataset
#
# == Example:
# df = sql.writer.json('output.json')
#
def json(path)
# ClassNotFoundException: Failed to load class for data source: json
# df(jwriter.json(path))

save(path, 'org.apache.spark.sql.execution.datasources.json')
end

def parquet(path)
# ClassNotFoundException: Failed to load class for data source: parquet
# df(jwriter.parquet(path))

save(path, 'org.apache.spark.sql.execution.datasources.parquet')
end

def orc(path)
# ClassNotFoundException: Failed to load class for data source: json
# df(jwriter.json(path))

save(path, 'org.apache.spark.sql.execution.datasources.orc')
end

def insert_into table_name
jwriter.insertInto(table_name)
end

def save_as_table name
jwriter.saveAsTable(name)
end

def partition_by columns
jwriter.partitionBy(columns)
end

def mode name
jwriter.mode(name)
self
end

end
end
end