Creating a Java Object in Scala

最后都变了- 提交于 2021-01-27 05:51:46

问题


I have a Java class "Listings". I use this in my Java MapReduce job as below:

public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
Listings le = new Listings(value.toString());
...
}

I want to run the same job on Spark. So, I am writing this in Scala now. I imported the Java class:

import src.main.java.lists.Listings

I want to create a Listings object in Scala. I am doing this:

val file_le = sc.textFile("file// Path to file")
Listings lists = new Listings(file_le)

I get an error:

value lists is not a member of object src.main.java.lists.Listings

What is the right way to do this?


回答1:


Based on what you've said, I think you may be forgetting the differences between Scala syntax and Java syntax.

Try this:

val lists: Listings = new Listings(SomeString)

Please note that specifying the type in Scala is completely optional. Also, use a var if you're going to be changing the value of lists.

The way you have it, Scala is trying to interpret it by its ability to call methods/access values of an object without the '.', so you're actually telling Scala this:

Listings.lists = new Listings(SomeString)


来源:https://stackoverflow.com/questions/28948120/creating-a-java-object-in-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!