2016-08-25 13 views
21

Tôi đang cố gắng nhập spark.implicits._ Rõ ràng, đây là một đối tượng bên trong một lớp trong scala. khi tôi nhập nó trong một phương pháp như vậy:Nhập spark.implicits._ vào scala

def f() = { 
    val spark = SparkSession().... 
    import spark.implicits._ 
} 

Nó hoạt động tốt, tuy nhiên tôi đang viết một lớp thử nghiệm và tôi muốn thực hiện nhập khẩu này có sẵn cho tất cả các xét nghiệm Tôi đã thử:

class SomeSpec extends FlatSpec with BeforeAndAfter { 
    var spark:SparkSession = _ 

    //This won't compile 
    import spark.implicits._ 

    before { 
    spark = SparkSession().... 
    //This won't either 
    import spark.implicits._ 
    } 

    "a test" should "run" in { 
    //Even this won't compile (although it already looks bad here) 
    import spark.implicits._ 

    //This was the only way i could make it work 
    val spark = this.spark 
    import spark.implicits._ 
    } 
} 

Điều này không chỉ trông xấu, tôi không muốn làm điều đó cho mọi thử nghiệm Cách "thực hiện" chính xác là gì?

+0

Tại sao không ở đầu tệp? Thông thường, tất cả các hàng nhập khẩu đều có – spiffman

+2

Cố gắng cũng quên viết mã, nhưng dường như không thể vì "implicits" là một đối tượng bên trong lớp "spark" và nó cần được khởi tạo trước – ShinySpiderdude

Trả lời

8

Bạn có thể làm điều gì đó tương tự như những gì được thực hiện trong các bộ thử nghiệm Spark. Ví dụ này sẽ làm việc (lấy cảm hứng từ SQLTestData):

class SomeSpec extends FlatSpec with BeforeAndAfter { self => 

    var spark: SparkSession = _ 

    private object testImplicits extends SQLImplicits { 
    protected override def _sqlContext: SQLContext = self.spark.sqlContext 
    } 
    import testImplicits._ 

    before { 
    spark = SparkSession.builder().master("local").getOrCreate() 
    } 

    "a test" should "run" in { 
    // implicits are working 
    val df = spark.sparkContext.parallelize(List(1,2,3)).toDF() 
    } 
} 

Hoặc bạn có thể sử dụng giống như SharedSQLContext trực tiếp, cung cấp một testImplicits: SQLImplicits, ví dụ:

class SomeSpec extends FlatSpec with SharedSQLContext { 
    import testImplicits._ 

    // ... 

} 
8

Tôi nghĩ rằng mã GitHub trong SparkSession.scala tập thể cung cấp cho bạn gợi ý tốt:

 /** 
     * :: Experimental :: 
     * (Scala-specific) Implicit methods available in Scala for converting 
     * common Scala objects into [[DataFrame]]s. 
     * 
     * {{{ 
     * val sparkSession = SparkSession.builder.getOrCreate() 
     * import sparkSession.implicits._ 
     * }}} 
     * 
     * @since 2.0.0 
     */ 
     @Experimental 
     object implicits extends SQLImplicits with Serializable { 
     protected override def _sqlContext: SQLContext = SparkSession.this.sqlContext 
     } 

đây "spark" trong "spark.implicits._" chỉ là sparkSessi trên đối tượng chúng tôi đã tạo.

Here là một tham chiếu khác!

1

Tôi vừa khởi tạo SparkSession và trước khi sử dụng, "nhập liên quan".

@transient lazy val spark = SparkSession 
    .builder() 
    .master("spark://master:7777") 
    .getOrCreate() 

    import spark.implicits._