Query SQL Server from Spark Scala - How to? - sql-server

Env: Spark 1.6 with Scala, Cloudera
SQL Server 2012, version 11.0
I am trying to query SQL Server from Spark.
object ConnTest extends App {
val conf = new SparkConf()
val sc = new SparkContext(conf.setAppName("Spark Ingestion").setMaster("local[*]"))
val sqlcontext = new SQLContext(sc)
val prop=new Properties()
val url2="jdbc:sqlserver://xxx.xxx.xxx:1511;user=username;password=mypassword;database=SessionMonitor"
prop.setProperty("user","username")
prop.setProperty("password","mypassword")
val test=sqlcontext.read.jdbc(url2,"Service",prop)
val dd = sqlcontext.sql("select count(*) as TOT from Service")
dd.foreach(println)
}
My pom.xml has dependencies-
<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.1.0.jre8</version>
</dependency>
I did not download any jar file; nor install jar to maven repository nor add jar to the class path. My Hadoop cluster does not have a connection to the internet. After creating the maven package, I tried to submit using
spark-submit --class ConnTest /Hadoopshare/tmp/sqldb-1.0-SNAPSHOT.jar
Error:
Exception in thread "main" java.sql.SQLException: No suitable driver

This should be added to your code:
prop.setProperty("driver" , "com.mysql.jdbc.Driver")
In my case i used this and it totally worked fine:
val jdbcDF = sqlContext.read
.format("jdbc")
.option("driver" , "com.mysql.jdbc.Driver")
.option("url", "jdbc:mysql://<<>Servername>:3306/<<DatabaseName>>")
.option("dbtable", "(SELECT id, name FROM partner) tmp")
.option("user", "username")
.option("password", "******")
.load()
Hope this should work.

Related

Mulesoft Anypoint Studio will not let me connect to SQL Server database

I continue to get a error saying:
Error trying to load driver: com.microsoft.sqlserver.jdbc.SQLServerDriver : Cannot load class 'com.microsoft.sqlserver.jdbc.SQLServerDriver':
Class 'com.microsoft.sqlserver.jdbc.SQLServerDriver' has no package mapping for region 'domain/default/app/s-claims'
I have the dependency for it in my pom.xml:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.2.2.jre8</version>
</dependency>
I also have the shared library in the pom:
<sharedLibrary>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
</sharedLibrary>
When trying to connect directly to SQL Server using the same credentials, I am successful.
I am fairly new to Mulesoft so It could be something simple I am missing. Does anyone have any ideas?
Apparently there was just a issue with the studio. I didn't change anything and tested my connection again and it worked after redeploying.

Apache Spark Connector Driver not suitable

Trying to connect to sqlserver using Azure Apache Spark connector and getting the following error
java.sql.SQLException: No suitable driver
Databricks cluster has com.microsoft.azure:spark-mssql-connector_2.12:1.2.0 for Apache Spark 3.1.2, Scala 2.12 as stated my documentation
That's the only library I have installed on the cluster.
Went through docs on
https://github.com/microsoft/sql-spark-connector
jdbcHostname = "server name"
jdbcPort = 1433
jdbcDatabase = "database name"
Table="tbl.name"
JDBC_URL='"jdbc:sqlserver://{0}:{1};database={2}"'.format(jdbcHostname,jdbcPort,jdbcDatabase)
username="user"
password="pass"
jdbcDF = spark.read \
.format("com.microsoft.sqlserver.jdbc.spark") \
.option("url", JDBC_URL) \
.option("dbtable", Table) \
.option("user", username) \
.option("password", password).load()

Spring Boot 2.1.2 Create dynamic DB connection just to run a query

In the Spring Boot 2.1.2 project, there is a default PostgreSQL db connection defined in application.properties:
spring.datasource.url=jdbc:postgresql://host:port/dbName
spring.datasource.username=username
spring.datasource.password=pass
Not any problem with Postgre DB, however, I need to connect another DB (MsSql) by JDBC connection rarely.
I just need to run a query, and process result list from a view of mssql table.
What I tried is;
Adding jdbc drivers to Pom.xml for Mssql:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>7.2.1.jre11</version>
<scope>test</scope>
</dependency>
Connect to sql-server and run query:
#GetMapping(path = "/getKonteynersFromSifirAtikServis/", produces = "application/json")
public ResponseEntity<ResponseStatusDto> getKonteynersFromSifirAtikServis() {
try {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
dataSource.setUrl("jdbc:mysql://host:1433/msDbName");
dataSource.setUsername("ms_user");
dataSource.setPassword("ms_pass");
JdbcTemplate template = new JdbcTemplate(dataSource);
SqlRowSet sqlRowSet = template.queryForRowSet("SELECT * FROM dbo.viewName;");
} catch (Exception e) {
return new ResponseEntity<ResponseStatusDto>(ResponseStatusDto.createErrorDto(e.getMessage(), e), HttpStatus.ACCEPTED);
}
}
This did not work. It throws:
Could not load JDBC driver class [com.microsoft.sqlserver.jdbc.SQLServerDriver]
I have been getting hard to load mssql-jdbc inside pom definition. It is resolved to make version 7.2.1.jre11.
Scope of the dependency is set as test.
test This scope indicates that the dependency is not required for
normal use of the application, and is only available for the test
compilation and execution phases. This scope is not transitive.
Remove the same and attempt a run

Tomcat not able to find Microsoft SQL driver class

Tomcat version - apache-tomcat-9.0.0.M19
I am trying to connect my spring application with microsoft sql database.
driverClassName=com.microsoft.sqlserver.jdbc.SQLserverDriver
Maven dependency -
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.1.0.jre8</version>
</dependency>
This jar is available in the generated war file and i have also placed the jar in the lib folder of the tomcat but i'm still getting the following error when i try to deploy my application on Tomcat -
Cannot load JDBC driver class 'com.microsoft.sqlserver.jdbc.SQLserverDriver'
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLserverDriver
What am i missing?
You have a typo in your config. The driver class is named
com.microsoft.sqlserver.jdbc.SQLServerDriver
not
com.microsoft.sqlserver.jdbc.SQLserverDriver
................................^...........

Connecting to MS SQL Server using Windows authentication and JTDS

I am unable to connect to SQL Server using Windows Authentification.
This is is the exception being thrown:
Caused by: java.io.IOException: SSO Failed: Native SSPI library not loaded. Check the java.library.path system property.
at net.sourceforge.jtds.jdbc.TdsCore.sendMSLoginPkt(TdsCore.java:1889)
at net.sourceforge.jtds.jdbc.TdsCore.login(TdsCore.java:587)
... 35 more
Here is the Spring configuration:
#Bean
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("net.sourceforge.jtds.jdbc.Driver");
dataSource.setUrl(...);
return dataSource;
}
The following VM argument is defined:
-Djava.library.path=C:\x64\SSO
This points to ntlmauth.dll (from jtds-1.3.1-dist.zip)
The following dependency is defined in the pom:
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>com.springsource.net.sourceforge.jtds</artifactId>
<version>1.2.2</version>
</dependency>
Have obviously omitted something - any help is very appreciated!

Resources