spark sql 连接使用mysql数据源

原创
2015/10/13 13:50
阅读数 4.4K

spark sql 可以通过标准的jdbc连接数据库,获得数据源

 

public class SparkSql {

    public static SimpleDateFormat sdf = new SimpleDateFormat("_yyyyMMdd_HH_mm_ss");

    private static final String appName = "spark sql test";
    private static final String master = "spark://192.168.1.21:7077";
    private static final String JDBCURL = "jdbc:mysql://192.168.1.18:3306/lng?user=root&password=123456";

    public static void main(String[] avgs){

        SparkContext context = new SparkContext(master, appName);

        SQLContext sqlContext = new SQLContext(context);

        // Creates a DataFrame based on a table named "people"
        // stored in a MySQL database.
        DataFrame df = sqlContext
                .read()
                .format("jdbc")
                .option("url", JDBCURL)
                .option("dbtable", "tsys_user")
                .load();

        // Looks the schema of this DataFrame.
        df.printSchema();

        // Counts people by age
        DataFrame countsByAge = df.groupBy("customStyle").count();
        countsByAge.show();

        // Saves countsByAge to S3 in the JSON format.
        countsByAge.write().format("json").save("hdfs://192.168.1.17:9000/administrator/sql-result" + sdf.format(new Date()));

    }

}

 

如果没有包含mysql的驱动程序,需要参考http://stackoverflow.com/questions/34764505/no-suitable-driver-found-for-jdbc-in-spark

  1. You might want to assembly you application with your build manager (Maven,SBT) thus you'll not need to add the dependecies in your spark-submit cli. (意思就是把mysql的驱动程序打包到提交到spark的jar包里)
  2. You can use the following option in your spark-submit cli :(改成下面,经测试,可行,或者加入export SPARK_CLASSPATH=$SPARK_CLASSPATH:/usr/local/spark-1.6.1-bin-hadoop2.6/conf/driverLib/mysql-connector-java-5.1.36.jar 到conf/spark-env.sh

    spark-submit --driver-class-path /usr/local/spark-1.6.1-bin-hadoop2.6/conf/driverLib/mysql-connector-java-5.1.36.jar --class com.xxx.SparkSql  /usr/local/spark.jar

    Explanation : Supposing that you have all your jars in a lib directory in your project root, this will read all the libraries and add them to the application submit.

  3. You can also try to configure these 2 variables : spark.driver.extraClassPath and spark.executor.extraClassPath in SPARK_HOME/conf/spark-default.conf file and specify the value of these variables as the path of the jar file. Ensure that the same path exists on workernodes.(经测,不行)

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部