Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将csv文件作为spark数据帧读取_Csv_Apache Spark_Dataframe - Fatal编程技术网

将csv文件作为spark数据帧读取

将csv文件作为spark数据帧读取,csv,apache-spark,dataframe,Csv,Apache Spark,Dataframe,我有一个CSV文件和一个头文件,它必须作为数据帧通过Spark(2.0.0和Scala 2.11.8)读取 csv数据示例: Item,No. of items,Place abc,5,xxx def,6,yyy ghi,7,zzz ......... 当我试图将spark中的csv数据作为数据帧读取时,我遇到了一个问题,因为标题包含具有特殊字符“”的列(项目数) 我尝试读取csv数据的代码是: val spark = SparkSession.builder().appName("Spark

我有一个CSV文件和一个头文件,它必须作为数据帧通过Spark(2.0.0和Scala 2.11.8)读取

csv数据示例:

Item,No. of items,Place
abc,5,xxx
def,6,yyy
ghi,7,zzz
.........
当我试图将spark中的csv数据作为数据帧读取时,我遇到了一个问题,因为标题包含具有特殊字符“”的列(项目数)

我尝试读取csv数据的代码是:

val spark = SparkSession.builder().appName("SparkExample")
import spark.implicits._    
val df = spark.read.option("header", "true").csv("file:///INPUT_FILENAME")
我面临的错误:

Exception in thread "main" org.apache.spark.sql.AnalysisException: Unable to resolve No. of items given [Item,No. of items,Place];
如果我从标题中删除
,我不会得到任何错误。甚至尝试转义字符,但它甚至从数据中转义所有
字符


是否有任何方法可以使用spark code仅从CSV头转义特殊字符“”?

我给您举了一个使用pyspark的示例,希望通过添加一些与语言相关的语法,同样适用于您

file =r'C:\Users\e5543130\Desktop\sampleCSV2.csv'   
conf = SparkConf().setAppName('FICBOutputGenerator')
sc = SparkContext(conf=conf)
sc.setLogLevel("ERROR")
sqlContext = SQLContext(sc)
df = sqlContext.read.options(delimiter=",", header="true").csv("cars.csv")   #Without deprecated API
df = sqlContext.read.format("com.databricks.spark.csv").option("header", "true").option("inferSchema", "true").option("delimiter", ",").load("cars.csv") 

@Pooja Nayak,不确定这是否解决了;为了社区的利益回答这个问题

sc: SparkContext
spark: SparkSession
sqlContext: SQLContext

// Read the raw file from localFS as-is.
val rdd_raw = sc.textFile("file:///home/xxxx/sample.csv")

// Drop the first line in first partition because it is the header.
val rdd = rdd_raw.mapPartitionsWithIndex{(idx,iter) => 
                      if(idx == 0) iter.drop(1) else iter
}

// A function to create schema dynamically.
def schemaCreator(header: String): StructType = {
  StructType(header
              .split(",")
              .map(field => StructField(field.trim, StringType, true))
  )
}

// Create the schema for the csv that was read and store it.
val csvSchema: StructType = schemaCreator(rdd_raw.first)

// As the input is CSV, split it at "," and trim away the whitespaces.
val rdd_curated = rdd.map(x => x.split(",").map(y => y.trim)).map(xy => Row(xy:_*))

// Create the DF from the RDD.
val df = sqlContext.createDataFrame(rdd_curated, csvSchema)
导入必要的
s

import org.apache.spark.sql.types._
import org.apache.spark.sql._
import org.apache.spark._

我已尝试使用您的解决方案,但无法解决问题,我仍然面临相同的错误。我尝试使用spark 2.2使用此代码
spark.read.format(“csv”).option(“header”、“true”).load(input).show()
。它显示良好。我在spark 2.0.0中尝试了您给定的代码,但仍然面临相同的问题。也许,错误不是在读取过程中,而是在以后的处理过程中?我同意@pasha701,您确定在读取部分得到了错误吗?请重新检查并确认。如果没有太多列,请跳过标题并单独提供架构。