Skip to content

[SPARK-4774] [SQL] Makes HiveFromSpark more portable #3628

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

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,26 @@

package org.apache.spark.examples.sql.hive

import com.google.common.io.{ByteStreams, Files}

import java.io.File

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.sql._
import org.apache.spark.sql.hive.HiveContext

object HiveFromSpark {
case class Record(key: Int, value: String)

// Copy kv1.txt file from classpath to temporary directory
val kv1Stream = HiveFromSpark.getClass.getResourceAsStream("/kv1.txt")
val kv1File = File.createTempFile("kv1", "txt")
kv1File.deleteOnExit()
ByteStreams.copy(kv1Stream, Files.newOutputStreamSupplier(kv1File))

def main(args: Array[String]) {
val sparkConf = new SparkConf().setAppName("HiveFromSpark")
val sc = new SparkContext(sparkConf)
val path = s"${System.getenv("SPARK_HOME")}/examples/src/main/resources/kv1.txt"

// A hive context adds support for finding tables in the MetaStore and writing queries
// using HiveQL. Users who do not have an existing Hive deployment can still create a
Expand All @@ -37,7 +46,7 @@ object HiveFromSpark {
import hiveContext._

sql("CREATE TABLE IF NOT EXISTS src (key INT, value STRING)")
sql(s"LOAD DATA LOCAL INPATH '$path' INTO TABLE src")
sql(s"LOAD DATA LOCAL INPATH '${kv1File.getAbsolutePath}' INTO TABLE src")

// Queries are expressed in HiveQL
println("Result of 'SELECT *': ")
Expand Down