我试图加载一个文件,这是一个单行,没有新的行章程在整个文件,所以技术单行大小是文件的大小。我试着用下面的代码来加载数据。
val data= spark.sparkContext.textFile("location")
data.count
它不能返回任何值。
尝试用以下代码将文件读取为字符串,尝试用java代码编写。
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.Path
import org.apache.hadoop.fs.FileSystem
val inputPath = new Path("File")
val conf = spark.sparkContext.hadoopConfiguration
val fs = FileSystem.get(conf)
val inputStream = fs.open(inputPath)
import java.io.{BufferedReader, InputStreamReader}
val readLines = new BufferedReader(new InputStreamReader(inputStream)).readLine()
jvm正在退出,出现以下错误。
ava hotspot(tm)64位服务器vm警告:信息:操作系统::提交内存(0x00007fcb6ba00000,2148532224,0)失败;error='cannot allocate memory'(错误号=12)
内存不足,java运行时环境无法继续。本机内存分配(mmap)Map2148532224字节以提交保留内存失败。
问题是整个数据在一行中,spark使用\n标识新记录(新行)。因为有\n它试图加载到一行中,这会造成内存问题
我可以根据长度拆分长字符串,每200个字符(0200)的第一行添加新行字符(200400)是第二行。
样本输入
This is Achyuth This is ychyath This is Mansoor ... .... this line size is more than 4 gigs.
输出
This is Achyuth
This is ychyath
This is Mansoor
.
.
.
2条答案
按热度按时间vlf7wbxs1#
如果文件大小是拆分大小的倍数,并且字符编码是固定长度(ascii、utf-16、utf-32,utf-8中没有超过127的代码点或类似的…),则此方法有效。
给定文件
输出:
hc2pp10m2#
spark没有为文本文件设置eol分隔符的选项。
对我来说,最好的方法是使用设置textinputformat.record.delimiter在spark中你会得到很多选项。