How to add any new library like spark-csv in Apache Spark prebuilt version

后端 未结 6 661
死守一世寂寞
死守一世寂寞 2020-12-12 19:35

I have build the Spark-csv and able to use the same from pyspark shell using the following command

bin/spark-shell --packages com.databricks:spark-csv_2.10:1         


        
相关标签:
6条回答
  • 2020-12-12 20:06

    Instead of placing the jars in any specific folder a simple fix would be to start the pyspark shell with the following arguments:

    bin/pyspark --packages com.databricks:spark-csv_2.10:1.0.3
    

    This will automatically load the required spark-csv jars.

    Then do the following to read the csv file:

    from pyspark.sql import SQLContext
    sqlContext = SQLContext(sc)
    df = sqlContext.read.format('com.databricks.spark.csv').options(header='true').load('file.csv')
    df.show()
    
    0 讨论(0)
  • 2020-12-12 20:22

    Assuming the session/context hasn't been created yet:

    import os
    
    os.environ['PYSPARK_SUBMIT_ARGS'] = '--packages com.databricks:spark-csv_2.10:1.3.0 pyspark-shell'
    
    0 讨论(0)
  • 2020-12-12 20:24

    first find out the path of the spark. for example for pyspark

        which pyspark
    

    it will return you the path for example like this- /home/ubuntu/bin/pyspark

    then run this command by change the path as per your spark path general-: path --packages com.databricks:spark-csv_2.10:1.0.3

        /home/ubuntu/bin/pyspark --packages com.databricks:spark-csv_2.10:1.0.3
    
    0 讨论(0)
  • 2020-12-12 20:26

    Below command helped me -: With Scala 2.10 version

    /opt/mapr/spark/spark-1.5.2/bin/spark-shell --master local[*] --packages com.databricks:spark-csv_2.10:1.4.0
    

    Has below dependencies -:

    com.databricks#spark-csv_2.10;1.4.0!spark-csv_2.10.jar (2043ms)
    org.apache.commons#commons-csv;1.1!commons-csv.jar (419ms)
    com.univocity#univocity-parsers;1.5.1!univocity-parsers.jar (1481ms)
    
    0 讨论(0)
  • 2020-12-12 20:27

    At the time I used spark-csv, I also had to download commons-csv jar (not sure it is still relevant). Both jars where in the spark distribution folder.

    1. I downloaded the jars as follow:

      wget http://search.maven.org/remotecontent?filepath=org/apache/commons/commons-csv/1.1/commons-csv-1.1.jar -O commons-csv-1.1.jar<br/>    
      wget http://search.maven.org/remotecontent?filepath=com/databricks/spark-csv_2.10/1.0.0/spark-csv_2.10-1.0.0.jar -O spark-csv_2.10-1.0.0.jar
      
    2. then started the python spark shell with the arguments:

      ./bin/pyspark --jars "spark-csv_2.10-1.0.0.jar,commons-csv-1.1.jar"
      
    3. and read a spark dataframe from a csv file:

      from pyspark.sql import SQLContext
      sqlContext = SQLContext(sc)
      df = sqlContext.load(source="com.databricks.spark.csv", path = "/path/to/you/file.csv")
      df.show()
      
    0 讨论(0)
  • 2020-12-12 20:27

    Another option is to add the following to your spark-defaults.conf:

    spark.jars.packages com.databricks:spark-csv_2.11:1.2.0
    
    0 讨论(0)
提交回复
热议问题