使用JavaSpark逐行读取大型文本文件

使用JavaSpark逐行读取大型文本文件,java,apache-spark,Java,Apache Spark,我试图读取一个大的文本文件(2到3 gb)。我需要逐行读取文本文件,并将每一行转换为Json对象。我已经尝试使用.collect()和.tolocaterator()来读取文本文件。collect()适用于小文件,但不适用于大文件。我知道.toLocalIterator()将分散在集群中的数据收集到单个集群中。根据文档,ToLocalitator()在处理大型RDD时无效,因为它会遇到内存问题。有没有一种有效的方法可以读取多节点集群中的大型文本文件 下面是一个方法,我尝试读取文件并将每一行转换为

我试图读取一个大的文本文件(2到3 gb)。我需要逐行读取文本文件,并将每一行转换为Json对象。我已经尝试使用.collect()和.tolocaterator()来读取文本文件。collect()适用于小文件,但不适用于大文件。我知道.toLocalIterator()将分散在集群中的数据收集到单个集群中。根据文档,ToLocalitator()在处理大型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> rows = lines.collect();

    // Sets loop limit based on the number on lines in text file.
    int count = (int) lines.count();
    System.out.println("Number of lines are " + count);

    // Using google's library to create a Json builder.
    GsonBuilder gsonBuilder = new GsonBuilder();
    Gson gson = new GsonBuilder().setLenient().create();

    // Created an array list to insert json objects.
    ArrayList<String> jsonList = new ArrayList<>();

    // Converting each line of the text file into a Json formatted string and
    // inserting into the array list 'jsonList'
    for (int i = 0; i <= count - 1; i++) {
        String JSONObject = gson.toJson(rows.get(i));
        Gson prettyGson = new GsonBuilder().setPrettyPrinting().create();
        String prettyJson = prettyGson.toJson(rows.get(i));
        jsonList.add(prettyJson);
    }

    // For printing out the all the json objects
    int lineNumber = 1;
    for (int i = 0; i <= count - 1; i++) {
        System.out.println("line " + lineNumber + "-->" + jsonList.get(i));
        lineNumber++;
    }

}

您可以尝试在RDD上使用map函数,而不是收集所有结果

JavaRDD<String> lines = sc.textFile(path);
JavaRDD<String> jsonList = lines.map(line -> <<all your json transformations>>)
javarddlines=sc.textFile(路径);

JavaRDD jsonList=lines.map(行->为什么要处理列表而不是RDD?RDD可以为您提供分发。在RDD上,您可以应用map方法,这样,您就可以逐行处理。@AlexStrong我是新手,不知道从哪里开始。我将尝试应用map方法,谢谢。@AlexStrong您能告诉我如何或在哪里可以找到这些方法吗举几个例子?将RDD转换为DF有什么意义?为什么我们不能只使用saveAsTextFile?@Alex Strong JavaRDD无法直接保存为JSON格式,所以最好的方法是将RDD转换为DataFram,然后保存为JSON格式。JavaRDD将包含字符串(看起来像JSON)使用saveAsTextFile方法保存它的结果与将转换后的rdd保存为df的结果相同。同意,文件内容为JSON。但是saveAsTextFile无法将文件格式保存为xxx.JSON!您可以先尝试。
JavaRDD<String> lines = sc.textFile(path);
JavaRDD<String> jsonList = lines.map(line -> <<all your json transformations>>)
JavaRDD<String> lines = sc.textFile(path);

JavaRDD<String> jsonList = lines.map(line ->line.split("/"))
   JavaRDD<String> jsonList = lines.map(line ->{
   String newline = line.replace("","")
   return newline ;
dfTobeSaved.write.format("json").save("/root/data.json")