将 String RDD 转换为 Int RDD [英] Converting String RDD to Int RDD

查看:180
本文介绍了将 String RDD 转换为 Int RDD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Scala 的新手.我想知道在 Spark 中使用 Scala 处理大型数据集时是否可以读取为 int RDD 而不是 String RDD

I am new to scala..I want to know when processing large datasets with scala in spark is it possible to read as int RDD instead of String RDD

我尝试了以下方法:

val intArr = sc
              .textFile("Downloads/data/train.csv")
              .map(line=>line.split(","))
              .map(_.toInt)

但我收到错误:

错误:值 toInt 不是 Array[String]

error: value toInt is not a member of Array[String]

我需要转换为 int rdd 因为接下来我需要做下面的事情

I need to convert to int rdd because down the line i need to do the below

val vectors = intArr.map(p => Vectors.dense(p))

要求类型为整数

任何形式的帮助都非常感谢..提前致谢

Any kind of help is truly appreciated..thanks in advance

推荐答案

据我所知,一行应该创建一个向量,所以它应该是这样的:

As far as I understood, one line should create one vector, so it should goes like:

val result = sc
           .textFile("Downloads/data/train.csv")
           .map(line => line.split(","))
           .map(numbers => Vectors.dense(numbers.map(_.toInt)))

numbers.map(_.toInt) 将数组的每个元素映射到 int,所以结果类型将是 Array[Int]

numbers.map(_.toInt) will map every element of array to int, so result type will be Array[Int]

这篇关于将 String RDD 转换为 Int RDD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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