Association spark table

I found a spark table join to write a lot simpler than flink, at least schema can be saved, the following is an example

public static void main(String[] args) {        

         SparkSession s= SparkSession.builder().appName("rec").getOrCreate();

         Dataset<Row> user=s.read().format("jdbc")
          .option("driver", "com.mysql.jdbc.Driver")
          .option("url", "jdbc:mysql://*")
          .option("dbtable", "user")
          .option("user", "1")
          .option("password", "1")
          .load();

         Dataset<Row> house=s.read().format("jdbc")
                  .option("driver", "com.mysql.jdbc.Driver")
                  .option("url", "jdbc:mysql://")
                  .option("dbtable", "house")
                  .option("user", "1")
                  .option("password", "1")
                  .load();

         user.cache();

         house.cache();

         user.createOrReplaceTempView("user");

         house.createOrReplaceTempView("house");

         Dataset<Row> temp= s.sql("select user.user_name, house.house_name from user inner join house where user.uid=house.uid ");

         temp.write().csv("/home/ziroom/house-user");

    }

Guess you like

Origin blog.51cto.com/12597095/2439789