Spark:java api讀取hdfs目錄下多個文件


需求:

由於一個大文件,在spark中加載性能比較差。於是把一個大文件拆分為多個小文件后上傳到hdfs,然而在spark2.2下如何加載某個目錄下多個文件呢?

public class SparkJob {
    public static void main(String[] args) {
        String filePath = args[0];
        // initialize spark session
        String appName = "Streaming-MRO-Load-Multiple-CSV-Files-Test";
        SparkSession sparkSession = SparkHelper.getInstance().getAndConfigureSparkSession(appName);

        // reader multiple csv files.
        try {
            Dataset<Row> rows = sparkSession.read().option("delimiter", "|").option("header", false)
                    .csv(filePath).toDF(getNCellSchema());
            rows.show(10);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        try {
            Dataset<String> rows = sparkSession.read().textFile(filePath);
            rows.show(10);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        SparkHelper.getInstance().dispose();
    }

    private static Seq<String> getNCellSchema() {
        List<String> ncellColumns = "m_id,m_eid,m_int_id,.....";

        List<String> columns = new ArrayList<String>();
        for (String column : ncellColumns) {
            columns.add(column);
        }

        Seq<String> columnsSet = JavaConversions.asScalaBuffer(columns);

        return columnsSet;
    }
}

測試結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM