forked from intel-analytics/ipex-llm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support convert rdd of pandas df to spark df with arrow (intel-analyt…
- Loading branch information
1 parent
9f1884c
commit 2fd9dfc
Showing
6 changed files
with
177 additions
and
28 deletions.
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
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
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
60 changes: 60 additions & 0 deletions
60
scala/orca/src/main/scala/com/intel/analytics/bigdl/orca/python/PythonSQLUtils.scala
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2016 The BigDL Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.sql | ||
|
||
import java.io.FileInputStream | ||
|
||
import com.intel.analytics.bigdl.dllib.tensor.TensorNumericMath.TensorNumeric | ||
import org.apache.spark.TaskContext | ||
import org.apache.spark.api.java.JavaRDD | ||
import org.apache.spark.sql.api.python.PythonSQLUtils | ||
import org.apache.spark.sql.execution.arrow.ArrowConverters | ||
import org.apache.spark.sql.types.{DataType, StructType} | ||
import org.apache.spark.util.{ShutdownHookManager, Utils} | ||
|
||
import java.io._ | ||
|
||
import scala.reflect.ClassTag | ||
|
||
object PythonOrcaSQLUtils { | ||
|
||
def ofFloat(): PythonOrcaSQLUtils[Float] = new PythonOrcaSQLUtils[Float]() | ||
|
||
def ofDouble(): PythonOrcaSQLUtils[Double] = new PythonOrcaSQLUtils[Double]() | ||
} | ||
|
||
class PythonOrcaSQLUtils[T: ClassTag](implicit ev: TensorNumeric[T]) { | ||
def orcaToDataFrame(jrdd: JavaRDD[String], schemaString: String, | ||
sqlContext: SQLContext): DataFrame = { | ||
val schema = DataType.fromJson(schemaString).asInstanceOf[StructType] | ||
val timeZoneId = sqlContext.sessionState.conf.sessionLocalTimeZone | ||
val rdd = jrdd.rdd.mapPartitions { iter => | ||
val context = TaskContext.get() | ||
val file = iter.next() | ||
val dir = new File(file) | ||
ShutdownHookManager.registerShutdownDeleteDir(dir) | ||
|
||
Utils.tryWithResource(new FileInputStream(file)) { fileStream => | ||
// Create array to consume iterator so that we can safely close the file | ||
val batches = ArrowConverters.getBatchesFromStream(fileStream.getChannel) | ||
ArrowConverters.fromBatchIterator(batches, | ||
DataType.fromJson(schemaString).asInstanceOf[StructType], timeZoneId, context) | ||
} | ||
} | ||
sqlContext.internalCreateDataFrame(rdd.setName("arrow"), schema) | ||
} | ||
} |