gpt4 book ai didi

java - 单行文件大约 4G 加载到 Spark

转载 作者:行者123 更新时间:2023-12-02 09:56:37 25 4
gpt4 key购买 nike

我正在尝试加载一个单行文件,整个文件中没有新的换行符,因此技术上的单行大小就是文件的大小。我尝试使用下面的代码来加载数据。

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 位服务器虚拟机警告:信息:os::commit_memory(0x00007fcb6ba00000, 2148532224, 0) 失败; error='无法分配内存' (errno=12)

内存不足,Java 运行时环境无法继续。 native 内存分配 (mmap) 无法映射 2148532224 字节以提交保留内存。

问题是整个数据都在单行中,spark 使用\n 来标识新记录(新行)。由于\n 它试图加载到单行中,这会产生内存问题

我可以根据长度分割该长字符串,为第一行每 200 个字符 (0,200) 添加新行字符。 (200,400) 是第二行。

示例输入

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
.
.
.

最佳答案

如果文件大小是分割大小的倍数并且字符编码是固定长度(ASCII、UTF-16、UTF-32、UTF-8 或类似文件中没有高于 127 的代码点),则此方法有效。 )。

给定文件

This is AchyuthThis is ychyathThis is Mansoor
val rdd = spark
.sparkContext
.binaryRecords(path, 15)
.map(bytes => new String(bytes))
val df = spark.createDataset(rdd)
df.show()

输出:

+---------------+
| value|
+---------------+
|This is Achyuth|
|This is ychyath|
|This is Mansoor|
+---------------+

关于java - 单行文件大约 4G 加载到 Spark,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55955520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com