Java - Spark SQL DataFrame map function is not working

后端 未结 6 838
说谎
说谎 2021-01-01 01:46

In Spark SQL when I tried to use map function on DataFrame then I am getting below error.

The method map(Function1, ClassTag) in the type DataFrame is not applicab

6条回答
  •  执念已碎
    2021-01-01 02:27

    No need to convert to RDD, its delays the execution it can be done as below

    `public static void mapMethod() { // Read the data from file, where the file is in the classpath. Dataset df = sparkSession.read().json("file1.json");

    // Prior to java 1.8 
    Encoder encoder = Encoders.STRING();
        List rowsList = df.map((new MapFunction() {
            private static final long serialVersionUID = 1L;
    
            @Override
            public String call(Row row) throws Exception {
                return "string:>" + row.getString(0).toString() + "<";
            }
        }), encoder).collectAsList();
    
    // from java 1.8 onwards
    List rowsList1 = df.map((row -> "string >" + row.getString(0) + "<" ), encoder).collectAsList();
    System.out.println(">>> " + rowsList);
    System.out.println(">>> " + rowsList1);
    

    }`

提交回复
热议问题