4

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?

2
  • Never worked on Scala. That being said, try changing the import to import lists.Listings Commented Mar 9, 2015 at 17:29
  • Thanks @bot . Import works fine. The next line is what I am talking about. Commented Mar 9, 2015 at 17:37

1 Answer 1

4

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)
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks @soong . Now I get this error: error: overloaded method constructor Listings with alternatives: (x$1: String)src.main.java.lists.Listings. cannot be applied to (org.apache.spark.rdd.RDD[String])
I'm afraid I'm not entirely certain how to fix that part, as my knowledge on using Java from within Scala is rather limited. Doing a bit of research, it sounds like it has something to do with the parameterized types in use. These links might help you, though you might want to ask a second question. stackoverflow.com/questions/8041105/… stackoverflow.com/questions/6735698/…
Thank you @soong. I did Listings lists = new Listings(file_le.toString). Now I get ArrayIndexOutOfBoundsException: 1. I get the same error when I try to pass any string. What could be wrong?
Yes. Not toString. Sorry. Used mkString. I get java.lang.ArrayIndexOutOfBoundsException: 1 error
I was splitting the input using a wrong character. I got it working! val lists: Listings = new Listings(SomeString) works now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.