복붙노트

[SCALA] 다양한는 스파크의 유형은 무엇 조인입니까?

SCALA

다양한는 스파크의 유형은 무엇 조인입니까?

나는 문서를 보았고, 그 다음은 유형이 지원됩니다 가입 말합니다 :

나는 대답의 최고 커플이 중 일부는 예를 들어, 위에서 조인 언급하지 않는 조인 및 SQL에 StackOverflow의 응답 보았다 left_semi 및 left_anti. 그들은 불꽃에서 무엇을 의미합니까?

해결법

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

    1.다음은 간단한 설명 실험은 다음과 같습니다

    다음은 간단한 설명 실험은 다음과 같습니다

    import org.apache.spark.sql._
    
    object SparkSandbox extends App {
      private[this] implicit val spark = SparkSession.builder().master("local[*]").getOrCreate()
      import spark.implicits._
      spark.sparkContext.setLogLevel("ERROR")
    
      val left = Seq((1, "A1"), (2, "A2"), (3, "A3"), (4, "A4")).toDF("id", "value")
      val right = Seq((3, "A3"), (4, "A4"), (4, "A4_1"), (5, "A5"), (6, "A6")).toDF("id", "value")
    
      println("LEFT")
      left.orderBy("id").show()
    
      println("RIGHT")
      right.orderBy("id").show()
    
      val joinTypes = Seq("inner", "outer", "full", "full_outer", "left", "left_outer", "right", "right_outer", "left_semi", "left_anti")
    
      joinTypes foreach { joinType =>
        println(s"${joinType.toUpperCase()} JOIN")
        left.join(right = right, usingColumns = Seq("id"), joinType = joinType).orderBy("id").show()
      }
    }
    

    산출

    LEFT
    +---+-----+
    | id|value|
    +---+-----+
    |  1|   A1|
    |  2|   A2|
    |  3|   A3|
    |  4|   A4|
    +---+-----+
    
    RIGHT
    +---+-----+
    | id|value|
    +---+-----+
    |  3|   A3|
    |  4|   A4|
    |  4| A4_1|
    |  5|   A5|
    |  6|   A6|
    +---+-----+
    
    INNER JOIN
    +---+-----+-----+
    | id|value|value|
    +---+-----+-----+
    |  3|   A3|   A3|
    |  4|   A4| A4_1|
    |  4|   A4|   A4|
    +---+-----+-----+
    
    OUTER JOIN
    +---+-----+-----+
    | id|value|value|
    +---+-----+-----+
    |  1|   A1| null|
    |  2|   A2| null|
    |  3|   A3|   A3|
    |  4|   A4|   A4|
    |  4|   A4| A4_1|
    |  5| null|   A5|
    |  6| null|   A6|
    +---+-----+-----+
    
    FULL JOIN
    +---+-----+-----+
    | id|value|value|
    +---+-----+-----+
    |  1|   A1| null|
    |  2|   A2| null|
    |  3|   A3|   A3|
    |  4|   A4|   A4|
    |  4|   A4| A4_1|
    |  5| null|   A5|
    |  6| null|   A6|
    +---+-----+-----+
    
    FULL_OUTER JOIN
    +---+-----+-----+
    | id|value|value|
    +---+-----+-----+
    |  1|   A1| null|
    |  2|   A2| null|
    |  3|   A3|   A3|
    |  4|   A4|   A4|
    |  4|   A4| A4_1|
    |  5| null|   A5|
    |  6| null|   A6|
    +---+-----+-----+
    
    LEFT JOIN
    +---+-----+-----+
    | id|value|value|
    +---+-----+-----+
    |  1|   A1| null|
    |  2|   A2| null|
    |  3|   A3|   A3|
    |  4|   A4| A4_1|
    |  4|   A4|   A4|
    +---+-----+-----+
    
    LEFT_OUTER JOIN
    +---+-----+-----+
    | id|value|value|
    +---+-----+-----+
    |  1|   A1| null|
    |  2|   A2| null|
    |  3|   A3|   A3|
    |  4|   A4| A4_1|
    |  4|   A4|   A4|
    +---+-----+-----+
    
    RIGHT JOIN
    +---+-----+-----+
    | id|value|value|
    +---+-----+-----+
    |  3|   A3|   A3|
    |  4|   A4| A4_1|
    |  4|   A4|   A4|
    |  5| null|   A5|
    |  6| null|   A6|
    +---+-----+-----+
    
    RIGHT_OUTER JOIN
    +---+-----+-----+
    | id|value|value|
    +---+-----+-----+
    |  3|   A3|   A3|
    |  4|   A4|   A4|
    |  4|   A4| A4_1|
    |  5| null|   A5|
    |  6| null|   A6|
    +---+-----+-----+
    
    LEFT_SEMI JOIN
    +---+-----+
    | id|value|
    +---+-----+
    |  3|   A3|
    |  4|   A4|
    +---+-----+
    
    LEFT_ANTI JOIN
    +---+-----+
    | id|value|
    +---+-----+
    |  1|   A1|
    |  2|   A2|
    +---+-----+
    
  2. ==============================

    2.Pathikrit의 모범을 사랑했다. 여기에 크로스 조인을 포함하여 스파크 v2 및 dataframes를 사용하여 Java에서 가능한 번역입니다.

    Pathikrit의 모범을 사랑했다. 여기에 크로스 조인을 포함하여 스파크 v2 및 dataframes를 사용하여 Java에서 가능한 번역입니다.

    package net.jgp.books.sparkInAction.ch12.lab940AllJoins;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.apache.spark.sql.Dataset;
    import org.apache.spark.sql.Row;
    import org.apache.spark.sql.RowFactory;
    import org.apache.spark.sql.SparkSession;
    import org.apache.spark.sql.types.DataTypes;
    import org.apache.spark.sql.types.StructField;
    import org.apache.spark.sql.types.StructType;
    
    /**
     * All joins in a single app, inspired by
     * https://stackoverflow.com/questions/45990633/what-are-the-various-join-types-in-spark.
     * 
     * Used in Spark in Action 2e, http://jgp.net/sia
     * 
     * @author jgp
     */
    public class AllJoinsApp {
    
      /**
       * main() is your entry point to the application.
       * 
       * @param args
       */
      public static void main(String[] args) {
        AllJoinsApp app = new AllJoinsApp();
        app.start();
      }
    
      /**
       * The processing code.
       */
      private void start() {
        // Creates a session on a local master
        SparkSession spark = SparkSession.builder()
            .appName("Processing of invoices")
            .master("local")
            .getOrCreate();
    
        StructType schema = DataTypes.createStructType(new StructField[] {
            DataTypes.createStructField(
                "id",
                DataTypes.IntegerType,
                false),
            DataTypes.createStructField(
                "value",
                DataTypes.StringType,
                false) });
    
        List<Row> rows = new ArrayList<Row>();
        rows.add(RowFactory.create(1, "A1"));
        rows.add(RowFactory.create(2, "A2"));
        rows.add(RowFactory.create(3, "A3"));
        rows.add(RowFactory.create(4, "A4"));
        Dataset<Row> dfLeft = spark.createDataFrame(rows, schema);
        dfLeft.show();
    
        rows = new ArrayList<Row>();
        rows.add(RowFactory.create(3, "A3"));
        rows.add(RowFactory.create(4, "A4"));
        rows.add(RowFactory.create(4, "A4_1"));
        rows.add(RowFactory.create(5, "A5"));
        rows.add(RowFactory.create(6, "A6"));
        Dataset<Row> dfRight = spark.createDataFrame(rows, schema);
        dfRight.show();
    
        String[] joinTypes = new String[] { 
            "inner", // v2.0.0. default
            "cross", // v2.2.0
            "outer", // v2.0.0
            "full", // v2.1.1
            "full_outer", // v2.1.1
            "left", // v2.1.1
            "left_outer", // v2.0.0
            "right", // v2.1.1
            "right_outer", // v2.0.0
            "left_semi", // v2.0.0, was leftsemi before v2.1.1
            "left_anti" // v2.1.1
            };
    
        for (String joinType : joinTypes) {
          System.out.println(joinType.toUpperCase() + " JOIN");
          Dataset<Row> df = dfLeft.join(
              dfRight, 
              dfLeft.col("id").equalTo(dfRight.col("id")), 
              joinType);
          df.orderBy(dfLeft.col("id")).show();
        }
      }
    }
    

    나는 액션, 2E의 제 12 장 저장소에 불꽃이 예를 넣을 수 있습니다.

  3. ==============================

    3.

    Spark data frame support following types of joins between two dataframes.
    Please find the list of joins and joining string with respect to join types along with scala syntax.
    We can use following joining values used for specify the join type in Scala- Spark code. 
    ***Mathod:*** Leftdataframe.join(Rightdataframe, join_conditions, joinStringName)
    
    Join Name : Join String name in scala -Spark code
    
    1. inner : 'inner'
    2. cross: 'cross'
    3. outer: 'outer'
    4. full: 'full'
    5. full outer: 'fullouter'
    6. left : 'left'
    7. left outer : 'leftouter'
    8. right : 'right'
    9. right outer : 'rightouter'
    10. left semi: 'leftsemi'
    11. left anti: 'leftanti'
    
    example: 1. Left Semi join: 
    Leftdataframe.join(Rightdataframe, join_conditions, "leftsemi");
    2. inner Join Example:
    Leftdataframe.join(Rightdataframe, join_conditions, "inner");
    
    Its tested and working well.
    
  4. ==============================

    4.(가) 키를 가입 왼쪽 세미 반환 행은 두 테이블에서 발견되지만, 그것은 단지 왼쪽 테이블의 필드를 포함한다.

    (가) 키를 가입 왼쪽 세미 반환 행은 두 테이블에서 발견되지만, 그것은 단지 왼쪽 테이블의 필드를 포함한다.

    왼쪽 반은 키에 가입 행에만 왼쪽 테이블에서 발견된다 반환합니다.

    다른 좋은 설명 조인 유형 : https://www.cloudera.com/documentation/enterprise/latest/topics/impala_joins.html

  5. from https://stackoverflow.com/questions/45990633/what-are-the-various-join-types-in-spark by cc-by-sa and MIT license