【问题标题】:How to make aliases for the classes in scala如何为scala中的类创建别名
【发布时间】:2017-06-28 16:37:33
【问题描述】:

我正在使用以下代码:

val fs = FileSystem.get(new Configuration())
val status = fs.listStatus(new Path("wasb:///example/"))
status.foreach(x=> println(x.getPath)

来自这个问题:How to enumerate files in HDFS directory

我的问题是我不明白如何为类创建别名,如果没有它,代码就会失败。我找到了代码中提到的所有类,并且以下代码有效:

val fs = org.apache.hadoop.fs.FileSystem.get(new org.apache.hadoop.conf.Configuration())
val status = fs.listStatus(new org.apache.hadoop.fs.Path("wasb:///example/"))
status

所以问题是:如何为 scala 中的类创建别名?如何将 Path() 指向 org.apache.hadoop.fs.Path()?

我在 Stackoverflow 上尝试过这个问题:Class alias in scala,但没有找到与我的案例相关的问题。

【问题讨论】:

    标签: scala hadoop


    【解决方案1】:

    不确定您的术语别名。我想你想import。例如

    import org.apache.hadoop.fs.Path
    

    或更一般地

    import org.apache.hadoop.fs._
    

    请注意,您可以通过导入为别名,因此:

    import org.apache.hadoop.fs.{Path => MyPath}
    

    然后将Path 称为MyPath。这在编写导入 2 个同名但不同包的类的代码时特别有用,例如java.util.Datejava.sql.Date。别名可以让您解决这种困惑。

    【讨论】:

    • 你说得对。但是为什么org.apache.hadoop.fs.Path 起作用了?在 Python 中,无法使用未导入的类。
    • 在 Scala 和 Java 中,您可以指定完整的包名,但“导入”对您来说很方便。请注意,类 still 必须可通过类路径获得
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    • 2020-02-04
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    • 2016-10-25
    相关资源
    最近更新 更多