负载星火CSV文件 [英] Load CSV file with Spark

查看:138
本文介绍了负载星火CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的火花,我试图从星火文件中读取CSV数据。
下面是我在做什么:

I'm new to Spark and I'm trying to read CSV data from a file with Spark. Here's what I am doing :

sc.textFile('file.csv')
    .map(lambda line: (line.split(',')[0], line.split(',')[1]))
    .collect()

我希望这个电话给我我的两个文件的第一列的列表,但我得到这个错误:

I would expect this call to give me a list of the two first columns of my file but I'm getting this error :

File "<ipython-input-60-73ea98550983>", line 1, in <lambda>
IndexError: list index out of range

虽然我的CSV文件为多个列。

although my CSV file as more than one column.

推荐答案

您确定的所有的行至少有2列?你可以尝试类似,只是为了检查:

Are you sure that all the lines have at least 2 columns? Can you try something like, just to check?:

sc.textFile("file.csv")
    .map(lambda line: line.split(","))
    .filter(lambda line: len(line)>1)
    .map(lambda line: (line[0],line[1]))
    .collect()

另外,你可以打印罪魁祸首(如果有的话):

Alternatively, you could print the culprit (if any):

sc.textFile("file.csv")
    .map(lambda line: line.split(","))
    .filter(lambda line: len(line)<=1)
    .collect()

这篇关于负载星火CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆