小编Rag*_*ena的帖子

使用Java Spark逐行读取大文本文件

我正在尝试读取一个大文本文件(2 到 3 GB)。我需要逐行读取文本文件并将每一行转换为 Json 对象。我尝试使用 .collect() 和 .toLocalIterator() 来通读文本文件。collect() 适用于小文件,但不适用于大文件。我知道 .toLocalIterator() 将分散在集群周围的数据收集到一个集群中。根据文档 .toLocalIterator() 在处理大型 RDD 时无效,因为它会遇到内存问题。有没有一种有效的方法来读取多节点集群中的大文本文件?

下面是我尝试读取文件并将每一行转换为 json 的各种尝试的方法。

public static void jsonConversion() {
    JavaRDD<String> lines = sc.textFile(path);
    String newrows = lines.first(); //<--- This reads the first line of the text file


    // Reading through with
    // tolocaliterator--------------------------------------------
     Iterator<String> newstuff = lines.toLocalIterator();
     System.out.println("line 1 " + newstuff.next());
     System.out.println("line 2 " + newstuff.next());

    // Inserting lines in a list.
    // Note: .collect() is appropriate for small files
    // only.-------------------------
    List<String> …
Run Code Online (Sandbox Code Playgroud)

java apache-spark

5
推荐指数
1
解决办法
1061
查看次数

Java代码跳过第一个if条件并跳过其他条件

我正在编写一个简单的程序,当年龄小于13,13到18和18之间时会产生一个特定的输出.我的代码不会读取第一个if语句,我不知道我做错了什么.

import java.io.*;
import java.util.*;
public class Person {
public  int age;    

public Person(int initialAge) {
    // Add some more code to run some checks on initialAge
    if (initialAge>-1){
        age=initialAge;
    }
    else 
        System.out.println("Age is not valid, setting age to 0. ");
        age=0;
}

public void amIOld() {
    // Write code determining if this person's age is old and print the correct statement:
    if(age<13)
     System.out.println("You are young.");   


    else if(age>=13&&age<18)
        System.out.println("You are a teenager."); 

   else 
    System.out.println("You are old.");


}

public void yearPasses() …
Run Code Online (Sandbox Code Playgroud)

java

0
推荐指数
1
解决办法
744
查看次数

标签 统计

java ×2

apache-spark ×1