在Scala中创建Java对象 [英] Creating a Java Object in Scala

查看:49
本文介绍了在Scala中创建Java对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java类"Listings".我在Java MapReduce作业中使用它,如下所示:

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());
...
}

我想在Spark上运行相同的作业.因此,我现在正在Scala中编写此代码.我导入了Java类:

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

我想在Scala中创建一个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)

我得到一个错误:

值列表不是对象src.main.java.lists.Listings的成员

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

正确的方法是什么?

推荐答案

基于您的发言,我认为您可能会忘记Scala语法和Java语法之间的区别.

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

尝试一下:

val lists: Listings = new Listings(SomeString)

请注意,在Scala中指定类型是完全可选的.另外,如果您要更改列表的值,请使用var.

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.

您拥有它的方式,Scala试图通过其调用不带'.'的对象的方法/访问值的能力来解释它,因此您实际上是在告诉Scala:

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)

这篇关于在Scala中创建Java对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆