使用 Spark SQL 从 SQL Server 读取数据 [英] Reading data from SQL Server using Spark SQL

查看:59
本文介绍了使用 Spark SQL 从 SQL Server 读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将 Microsoft Sql Server(以及 oracle、mysql 等)中的数据读取到 Spark 应用程序中的 rdd 中?还是我们需要在内存中创建一个集合并将其并行化为 RDD?

Is it possible to read data from Microsoft Sql Server (and oracle, mysql, etc.) into an rdd in a Spark application? Or do we need to create an in memory set and parallize that into an RDD?

推荐答案

从邮件列表中找到了解决方案.JdbcRDD 可用于完成此操作.我需要获取 MS Sql Server JDBC 驱动程序 jar 并将其添加到我的项目的库中.我想使用集成安全性,因此需要将 sqljdbc_auth.dll(在同一下载中可用)放在 java.library.path 可以看到的位置.然后,代码看起来像这样:

Found a solution to this from the mailing list. JdbcRDD can be used to accomplish this. I needed to get the MS Sql Server JDBC driver jar and add it to the lib for my project. I wanted to use integrated security, and so needed to put sqljdbc_auth.dll (available in the same download) in a location that java.library.path can see. Then, the code looks like this:

     val rdd = new JdbcRDD[Email](sc,
          () => {DriverManager.getConnection(
 "jdbc:sqlserver://omnimirror;databaseName=moneycorp;integratedSecurity=true;")},
          "SELECT * FROM TABLE_NAME Where ? < X and X < ?",
            1, 100000, 1000,
          (r:ResultSet) => { SomeClass(r.getString("Col1"), 
            r.getString("Col2"), r.getString("Col3")) } )

这给出了 SomeClass 的 Rdd.第二、第三和第四个参数是必需的,用于下限和上限,以及分区数.换句话说,该源数据需要可按 long 进行分区才能正常工作.

This gives an Rdd of SomeClass.The second, third and fourth parameters are required and are for lower and upper bounds, and number of partitions. In other words, that source data needs to be partitionable by longs for this to work.

这篇关于使用 Spark SQL 从 SQL Server 读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆