spark read avro file from hdfs example

To load avro data in spark we need few additional jars and in the below example we are using the libraries from the com.databricks. If we are using maven to build our project we can use the below pom.xml file.

<project xmlns="" xmlns:xsi=""






<version>1.0.0</version> </dependency> -->



<!-- Maven shade plug-in that creates uber JARs -->

Once we have all the dependent jars available in the classpath we can run the below code to load the avro data using spark dataframe api. If the avro data file name does not have .avro extension then we need to add below code which will make sure spark will process the data without .avro extension.

jsc.hadoopConfiguration().set("avro.mapred.ignore.inputs.without.extension", "false");

import org.apache.spark.SparkConf;
import org.apache.spark.sql.DataFrame;
import org.apache.spark.sql.SQLContext;

public class LoadAvroData {

public static void main(String[] args) {

args = new String[] { "Input Path" };

SparkConf sparkConf = new SparkConf().setAppName("test").setMaster("local");

JavaSparkContext jsc = new JavaSparkContext(sparkConf);

jsc.hadoopConfiguration().set("avro.mapred.ignore.inputs.without.extension", "false");

SQLContext sql = new SQLContext(jsc);

DataFrame uii_inventory ="com.databricks.spark.avro").load(args[0]);;



1 thought on “spark read avro file from hdfs example”

  1. Hi,
    This blog is really helpful to understand the things in detail manner with practical examples. Please do notify me for upcoming blogs .

Leave a Reply

Your email address will not be published. Required fields are marked *