-
Notifications
You must be signed in to change notification settings - Fork 28.5k
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
[SPARK-11622][MLLIB] Make LibSVMRelation extends HadoopFsRelation and… #9595
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9786a4e
[SPARK-11622][MLLIB] Make LibSVMRelation extends HadoopFsRelation and…
zjffdu 7cf79ef
minor change (remove Logging trait)
zjffdu 409f4d5
address review comments
zjffdu 75fcb50
minor code style fix
zjffdu 65cfb11
fix import ordering
zjffdu 6957dfe
fix code style
zjffdu 0d6d06d
fix code style issue
zjffdu 8a2c96f
code style issue
zjffdu 5bdf224
fix binary incompatibilities
zjffdu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,16 +17,21 @@ | |
|
||
package org.apache.spark.ml.source.libsvm | ||
|
||
import java.io.IOException | ||
|
||
import com.google.common.base.Objects | ||
import org.apache.hadoop.fs.{FileStatus, Path} | ||
import org.apache.hadoop.io.{NullWritable, Text} | ||
import org.apache.hadoop.mapreduce.{RecordWriter, TaskAttemptContext} | ||
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat | ||
|
||
import org.apache.spark.Logging | ||
import org.apache.spark.annotation.Since | ||
import org.apache.spark.mllib.linalg.{Vector, VectorUDT} | ||
import org.apache.spark.mllib.util.MLUtils | ||
import org.apache.spark.rdd.RDD | ||
import org.apache.spark.sql.{DataFrame, DataFrameReader, Row, SQLContext} | ||
import org.apache.spark.sql.sources._ | ||
import org.apache.spark.sql.types.{DoubleType, StructField, StructType} | ||
import org.apache.spark.sql.types._ | ||
|
||
/** | ||
* LibSVMRelation provides the DataFrame constructed from LibSVM format data. | ||
|
@@ -37,14 +42,10 @@ import org.apache.spark.sql.types.{DoubleType, StructField, StructType} | |
*/ | ||
private[libsvm] class LibSVMRelation(val path: String, val numFeatures: Int, val vectorType: String) | ||
(@transient val sqlContext: SQLContext) | ||
extends BaseRelation with TableScan with Logging with Serializable { | ||
|
||
override def schema: StructType = StructType( | ||
StructField("label", DoubleType, nullable = false) :: | ||
StructField("features", new VectorUDT(), nullable = false) :: Nil | ||
) | ||
extends HadoopFsRelation with Serializable { | ||
|
||
override def buildScan(): RDD[Row] = { | ||
override def buildScan(requiredColumns: Array[String], inputFiles: Array[FileStatus]) | ||
: RDD[Row] = { | ||
val sc = sqlContext.sparkContext | ||
val baseRdd = MLUtils.loadLibSVMFile(sc, path, numFeatures) | ||
val sparse = vectorType == "sparse" | ||
|
@@ -66,8 +67,63 @@ private[libsvm] class LibSVMRelation(val path: String, val numFeatures: Int, val | |
case _ => | ||
false | ||
} | ||
|
||
override def prepareJobForWrite(job: _root_.org.apache.hadoop.mapreduce.Job): | ||
_root_.org.apache.spark.sql.sources.OutputWriterFactory = { | ||
new OutputWriterFactory { | ||
override def newInstance( | ||
path: String, | ||
dataSchema: StructType, | ||
context: TaskAttemptContext): OutputWriter = { | ||
new LibSVMOutputWriter(path, dataSchema, context) | ||
} | ||
} | ||
} | ||
|
||
override def paths: Array[String] = Array(path) | ||
|
||
override def dataSchema: StructType = StructType( | ||
StructField("label", DoubleType, nullable = false) :: | ||
StructField("features", new VectorUDT(), nullable = false) :: Nil) | ||
} | ||
|
||
|
||
private[libsvm] class LibSVMOutputWriter( | ||
path: String, | ||
dataSchema: StructType, | ||
context: TaskAttemptContext) | ||
extends OutputWriter { | ||
|
||
private[this] val buffer = new Text() | ||
|
||
private val recordWriter: RecordWriter[NullWritable, Text] = { | ||
new TextOutputFormat[NullWritable, Text]() { | ||
override def getDefaultWorkFile(context: TaskAttemptContext, extension: String): Path = { | ||
val configuration = context.getConfiguration | ||
val uniqueWriteJobId = configuration.get("spark.sql.sources.writeJobUUID") | ||
val taskAttemptId = context.getTaskAttemptID | ||
val split = taskAttemptId.getTaskID.getId | ||
new Path(path, f"part-r-$split%05d-$uniqueWriteJobId$extension") | ||
} | ||
}.getRecordWriter(context) | ||
} | ||
|
||
override def write(row: Row): Unit = { | ||
val label = row.get(0) | ||
val vector = row.get(1).asInstanceOf[Vector] | ||
val sb = new StringBuilder(label.toString) | ||
vector.foreachActive { case (i, v) => | ||
sb += ' ' | ||
sb ++= s"${i + 1}:$v" | ||
} | ||
buffer.set(sb.mkString) | ||
recordWriter.write(NullWritable.get(), buffer) | ||
} | ||
|
||
override def close(): Unit = { | ||
recordWriter.close(context) | ||
} | ||
} | ||
/** | ||
* `libsvm` package implements Spark SQL data source API for loading LIBSVM data as [[DataFrame]]. | ||
* The loaded [[DataFrame]] has two columns: `label` containing labels stored as doubles and | ||
|
@@ -99,16 +155,32 @@ private[libsvm] class LibSVMRelation(val path: String, val numFeatures: Int, val | |
* @see [[https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/ LIBSVM datasets]] | ||
*/ | ||
@Since("1.6.0") | ||
class DefaultSource extends RelationProvider with DataSourceRegister { | ||
class DefaultSource extends HadoopFsRelationProvider with DataSourceRegister { | ||
|
||
@Since("1.6.0") | ||
override def shortName(): String = "libsvm" | ||
|
||
@Since("1.6.0") | ||
override def createRelation(sqlContext: SQLContext, parameters: Map[String, String]) | ||
: BaseRelation = { | ||
val path = parameters.getOrElse("path", | ||
throw new IllegalArgumentException("'path' must be specified")) | ||
private def verifySchema(dataSchema: StructType): Unit = { | ||
if (dataSchema.size != 2 || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It may be necessary to attache There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm sorry, it's private. Never mind. |
||
(!dataSchema(0).dataType.sameType(DataTypes.DoubleType) | ||
|| !dataSchema(1).dataType.sameType(new VectorUDT()))) { | ||
throw new IOException(s"Illegal schema for libsvm data, schema=${dataSchema}") | ||
} | ||
} | ||
|
||
override def createRelation( | ||
sqlContext: SQLContext, | ||
paths: Array[String], | ||
dataSchema: Option[StructType], | ||
partitionColumns: Option[StructType], | ||
parameters: Map[String, String]): HadoopFsRelation = { | ||
val path = if (paths.length == 1) paths(0) | ||
else if (paths.isEmpty) throw new IOException("No input path specified for libsvm data") | ||
else throw new IOException("Multiple input paths are not supported for libsvm data") | ||
if (partitionColumns.isDefined && !partitionColumns.get.isEmpty) { | ||
throw new IOException("Partition is not supported for libsvm data") | ||
} | ||
dataSchema.foreach(verifySchema(_)) | ||
val numFeatures = parameters.getOrElse("numFeatures", "-1").toInt | ||
val vectorType = parameters.getOrElse("vectorType", "sparse") | ||
new LibSVMRelation(path, numFeatures, vectorType)(sqlContext) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use just
OutputWriterFactory
because of importingorg.apache.spark.sql.sources._
?