0

I am trying to load data from a csv file to Hive. I am using JAVA API of spark for doing that. I want to know how I can load data in hive using spark dataframes.
Here is what I try to make it using JSON:

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.sql.SQLContext;
public class first {
public static void main (String[] args)
{
    String inputFileName = "samples/big.txt" ;
    String outputDirName = "output" ;

    SparkConf conf = new SparkConf().setAppName("org.sparkexample.WordCount").setMaster("local");
    JavaSparkContext context = new JavaSparkContext(conf);
    @SuppressWarnings("deprecation")
    SQLContext sc = new SQLContext(context);
    DataFrame input = sc.jsonFile(inputFileName);
    input.printSchema();
}
}

But don't know how to make it using csv. I have some idea about Spark-csv provided by databricks.
Kindly let me know how I can do it.

Jaffer Wilson
  • 7,029
  • 10
  • 62
  • 139

1 Answers1

1

On spark 2.x.x csv is built in (no need for package) Try to read like this:

SparkSession spark = SparkSession
.builder()
.appName("org.sparkexample.WordCount")
.master("local[*]") .
.enableHiveSupport()
.getOrCreate();
DataFrame input = spark.read.csv(inputFileName)

You can also add options for example:

DataFrame input = spark.read.option("header","true").csv(inputFileName)

will consider the first line to be a header and give the column names accordingly

Assaf Mendelson
  • 12,701
  • 5
  • 47
  • 56
  • 1
    Is this way I can write to hive? Actually reading was a problem now struggling with write. – Jaffer Wilson Feb 16 '17 at 09:44
  • you can try http://stackoverflow.com/questions/40122201/storing-a-dataframe-to-a-hive-partition-table-in-spark. I don't have hive configured so I can't check myself – Assaf Mendelson Feb 16 '17 at 10:17