복붙노트

[SCALA] 왼쪽 안티 스파크에 참여?

SCALA

왼쪽 안티 스파크에 참여?

나는이 같은 두 테이블을 정의했습니다 :

 val tableName = "table1"
    val tableName2 = "table2"

    val format = new SimpleDateFormat("yyyy-MM-dd")
      val data = List(
        List("mike", 26, true),
        List("susan", 26, false),
        List("john", 33, true)
      )
    val data2 = List(
        List("mike", "grade1", 45, "baseball", new java.sql.Date(format.parse("1957-12-10").getTime)),
        List("john", "grade2", 33, "soccer", new java.sql.Date(format.parse("1978-06-07").getTime)),
        List("john", "grade2", 32, "golf", new java.sql.Date(format.parse("1978-06-07").getTime)),
        List("mike", "grade2", 26, "basketball", new java.sql.Date(format.parse("1978-06-07").getTime)),
        List("lena", "grade2", 23, "baseball", new java.sql.Date(format.parse("1978-06-07").getTime))
      )

      val rdd = sparkContext.parallelize(data).map(Row.fromSeq(_))
      val rdd2 = sparkContext.parallelize(data2).map(Row.fromSeq(_))
      val schema = StructType(Array(
        StructField("name", StringType, true),
        StructField("age", IntegerType, true),
        StructField("isBoy", BooleanType, false)
      ))
    val schema2 = StructType(Array(
        StructField("name", StringType, true),
        StructField("grade", StringType, true),
        StructField("howold", IntegerType, true),
        StructField("hobby", StringType, true),
        StructField("birthday", DateType, false)
      ))

      val df = sqlContext.createDataFrame(rdd, schema)
      val df2 = sqlContext.createDataFrame(rdd2, schema2)
      df.createOrReplaceTempView(tableName)
      df2.createOrReplaceTempView(tableName2)

나는 표 2에 일치하는 행이없는 표에서 행을 반환하는 빌드 쿼리에 노력하고있어. 나는이 쿼리를 사용하여 그것을 할 시도했다 :

Select * from table1 LEFT JOIN table2 ON table1.name = table2.name AND table1.age = table2.howold AND table2.name IS NULL AND table2.howold IS NULL

그러나 이것은 단지 나에게 표에서 모든 행을 제공합니다 :

효율적으로 스파크에 참여의 방법이 유형을 만드는 방법?

내가 다른 권장 질문에서 이루어집니다처럼 단지 행 단위로 비교하지, 두 테이블 사이에 비교하는 열을 지정 할 수 있어야합니다 때문에 SQL 쿼리를 찾고 있어요. 빼기를 사용하여처럼 등을 제외시켰다

해결법

  1. ==============================

    1.당신은이 "반"왼쪽 조인 유형을 사용할 수 있습니다 - DataFrame의 API 또는 SQL (모든 포함 SQL 지원, 당신은 필요 조건을 조인 DataFrame API가 지원하는 모든 것을)와 중 하나

    당신은이 "반"왼쪽 조인 유형을 사용할 수 있습니다 - DataFrame의 API 또는 SQL (모든 포함 SQL 지원, 당신은 필요 조건을 조인 DataFrame API가 지원하는 모든 것을)와 중 하나

    DataFrame의 API :

    df.as("table1").join(
      df2.as("table2"),
      $"table1.name" === $"table2.name" && $"table1.age" === $"table2.howold",
      "leftanti"
    )
    

    SQL :

    sqlContext.sql(
      """SELECT table1.* FROM table1
        | LEFT ANTI JOIN table2
        | ON table1.name = table2.name AND table1.age = table2.howold
      """.stripMargin)
    

    참고 : 또한 필요한 자동으로 추론 된 스키마를 "고정"다음, 별도로 스키마를 지정하지 않고 샘플 데이터를 생성 튜플과 암시 toDF 방법을 사용하여, 그리고 짧은, 더 간결한 방법이 있음을 주목할 필요가있다 :

    import spark.implicits._
    val df = List(
      ("mike", 26, true),
      ("susan", 26, false),
      ("john", 33, true)
    ).toDF("name", "age", "isBoy")
    
    val df2 = List(
      ("mike", "grade1", 45, "baseball", new java.sql.Date(format.parse("1957-12-10").getTime)),
      ("john", "grade2", 33, "soccer", new java.sql.Date(format.parse("1978-06-07").getTime)),
      ("john", "grade2", 32, "golf", new java.sql.Date(format.parse("1978-06-07").getTime)),
      ("mike", "grade2", 26, "basketball", new java.sql.Date(format.parse("1978-06-07").getTime)),
      ("lena", "grade2", 23, "baseball", new java.sql.Date(format.parse("1978-06-07").getTime))
    ).toDF("name", "grade", "howold", "hobby", "birthday").withColumn("birthday", $"birthday".cast(DateType))
    
  2. ==============================

    2.당신은 제외 기능을 내장 함께 할 수 있습니다 (:( 당신이 제공 한 코드를 사용하는 것입니다,하지만 당신은 그래서 난 그냥 C / P를 할 수 없었다, 수입을 포함하지 않았다)

    당신은 제외 기능을 내장 함께 할 수 있습니다 (:( 당신이 제공 한 코드를 사용하는 것입니다,하지만 당신은 그래서 난 그냥 C / P를 할 수 없었다, 수입을 포함하지 않았다)

    val a = sc.parallelize(Seq((1,"a",123),(2,"b",456))).toDF("col1","col2","col3")
    val b= sc.parallelize(Seq((4,"a",432),(2,"t",431),(2,"b",456))).toDF("col1","col2","col3")
    
    scala> a.show()
    +----+----+----+
    |col1|col2|col3|
    +----+----+----+
    |   1|   a| 123|
    |   2|   b| 456|
    +----+----+----+
    
    
    scala> b.show()
    +----+----+----+
    |col1|col2|col3|
    +----+----+----+
    |   4|   a| 432|
    |   2|   t| 431|
    |   2|   b| 456|
    +----+----+----+
    
    scala> a.except(b).show()
    +----+----+----+
    |col1|col2|col3|
    +----+----+----+
    |   1|   a| 123|
    +----+----+----+
    
  3. ==============================

    3.SQL에서, 당신이 할 수있는 간단하게 아래로 쿼리 (확실하지가 SPARK에서 작동하는 경우)

    SQL에서, 당신이 할 수있는 간단하게 아래로 쿼리 (확실하지가 SPARK에서 작동하는 경우)

    Select * from table1 LEFT JOIN table2 ON table1.name = table2.name AND table1.age = table2.howold where table2.name IS NULL 
    

    이 결합이 실패하는 표 1의 모든 행을 반환합니다

  4. ==============================

    4.당신은 왼쪽 반을 사용할 수 있습니다.

    당신은 왼쪽 반을 사용할 수 있습니다.

    dfRcc20.as("a").join(dfClientesDuplicados.as("b")
      ,col("a.eteerccdiid")===col("b.eteerccdiid")&&
        col("a.eteerccdinr")===col("b.eteerccdinr")
      ,"left_anti")
    
  5. from https://stackoverflow.com/questions/43186888/left-anti-join-in-spark by cc-by-sa and MIT license