1
votes

I am very new to scala. I have started using scala for my spark project. I am using some of java code. following line I am getting error.

         case class docDisplay( id :String,name :String,  session :String,
  time :String, docguid: scala.collection.immutable.List[String]

)

Code for my ParseDocumentGuid which is the java class is as follow.

public static List<String> ParseGuid(String Data, String type,String Name) {    
    boolean validJson=checkValidJson(Data);
    if(validJson==true)

    {
        try
        {
            JSONObject  json = null;
         json = new JSONObject (Data);
        List<String> singleDocGuidList= new ArrayList<String>();
        if(json!=null && json.getString("guid").equalsIgnoreCase("guid") )
        {
                    singleDocGuidList.add(json.getString("guid"));
        }
         return singleDocGuidList;
    }    
    catch(Exception e)
       {
           List<String> singleDocGuidList= new ArrayList<String>();
            singleDocGuidList.add(e.getMessage());
            return singleDocGuidList;
       }
    }
 else
    {
        List<String> singleDocGuidList= new ArrayList<String>();
        singleDocGuidList.add("unKnownDocumentGuid");
        return singleDocGuidList;
    }
}

Here is the code which is calling above method

def selectColumnsPerDocdisplayRows(row:Row):List[String]= {
    docDisplay(
        row.getAs[String]("id"),
        row.getAs[String]("name"),
        row.getAs[String]("session"),
        row.getAs[String]("time"),
    Utils.ParseDocumentGuid(row.getAs("Data"),Utils.DOCDISRELATED,row.getAs[String]("name"))
        );
}

So I am getting error on my method def selectColumnsPerDocdisplayRows() saying

type mismatch; found : java.util.List[String] required: List[String]

2

2 Answers

2
votes

Hello Thank you Brian Kent and Chris Shain for suggesion, I have removed List[String] from my def and it worked with

import collection.JavaConverters._

and .asScala.toList

 def selectColumnsPerDocdisplayRows(row:Row)= {
docDisplay(
    row.getAs[String]("id"),
    row.getAs[String]("name"),
    row.getAs[String]("session"),
    row.getAs[String]("time"),
Utils.ParseDocumentGuid(row.getAs("Data"),Utils.DOCDISRELATED,row.getAs[String]("name")).asScala.toList
    );

}

1
votes

Add the conversions:

import collection.JavaConverters._

and add .asScala to convert