sql crawler

This commit is contained in:
insanity 2017-04-11 12:22:18 +09:00
commit ceb03cfb6b
9 changed files with 346 additions and 0 deletions

57
pom.xml Normal file
View File

@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.loafle</groupId>
<artifactId>maven_parent_jar</artifactId>
<version>1.0.0-RELEASE</version>
</parent>
<groupId>com.loafle.overflow</groupId>
<artifactId>sql</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>com.loafle.overflow.sql</name>
<dependencies>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.0.0</version>
</dependency>
<!-- 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>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.0.1.0</version>
<scope>system</scope>
<systemPath>${basedir}/src/lib/ojdbc7.jar</systemPath>
</dependency>
<dependency>
<groupId>com.loafle</groupId>
<artifactId>crawler_java</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

65
sql_crawler.iml Normal file
View File

@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:5.1.6" level="project" />
<orderEntry type="library" name="Maven: org.postgresql:postgresql:42.0.0" level="project" />
<orderEntry type="library" name="Maven: com.microsoft.sqlserver:mssql-jdbc:6.1.0.jre8" level="project" />
<orderEntry type="library" name="Maven: com.microsoft.azure:azure-keyvault:0.9.3" level="project" />
<orderEntry type="library" name="Maven: com.microsoft.azure:azure-core:0.9.3" level="project" />
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.10" level="project" />
<orderEntry type="library" name="Maven: commons-lang:commons-lang:2.6" level="project" />
<orderEntry type="library" name="Maven: javax.mail:mail:1.4.5" level="project" />
<orderEntry type="library" name="Maven: javax.activation:activation:1.1" level="project" />
<orderEntry type="library" name="Maven: com.sun.jersey:jersey-client:1.13" level="project" />
<orderEntry type="library" name="Maven: com.sun.jersey:jersey-core:1.13" level="project" />
<orderEntry type="library" name="Maven: com.sun.jersey:jersey-json:1.13" level="project" />
<orderEntry type="library" name="Maven: org.codehaus.jettison:jettison:1.1" level="project" />
<orderEntry type="library" name="Maven: stax:stax-api:1.0.1" level="project" />
<orderEntry type="library" name="Maven: com.sun.xml.bind:jaxb-impl:2.2.3-1" level="project" />
<orderEntry type="library" name="Maven: javax.xml.bind:jaxb-api:2.2.2" level="project" />
<orderEntry type="library" name="Maven: javax.xml.stream:stax-api:1.0-2" level="project" />
<orderEntry type="library" name="Maven: org.codehaus.jackson:jackson-core-asl:1.9.2" level="project" />
<orderEntry type="library" name="Maven: org.codehaus.jackson:jackson-mapper-asl:1.9.2" level="project" />
<orderEntry type="library" name="Maven: org.codehaus.jackson:jackson-jaxrs:1.9.2" level="project" />
<orderEntry type="library" name="Maven: org.codehaus.jackson:jackson-xc:1.9.2" level="project" />
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.3.6" level="project" />
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.3.3" level="project" />
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.1.3" level="project" />
<orderEntry type="library" name="Maven: javax.inject:javax.inject:1" level="project" />
<orderEntry type="library" name="Maven: com.microsoft.azure:adal4j:1.0.0" level="project" />
<orderEntry type="library" name="Maven: com.nimbusds:oauth2-oidc-sdk:4.5" level="project" />
<orderEntry type="library" name="Maven: net.jcip:jcip-annotations:1.0" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.3.1" level="project" />
<orderEntry type="library" name="Maven: net.minidev:json-smart:1.1.1" level="project" />
<orderEntry type="library" name="Maven: com.nimbusds:lang-tag:1.4" level="project" />
<orderEntry type="library" name="Maven: com.nimbusds:nimbus-jose-jwt:3.1.2" level="project" />
<orderEntry type="library" name="Maven: org.bouncycastle:bcprov-jdk15on:1.51" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.2.4" level="project" />
<orderEntry type="module-library">
<library name="Maven: com.oracle:ojdbc7:12.1.0.1.0">
<CLASSES>
<root url="jar://$MODULE_DIR$/src/lib/ojdbc7.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="library" name="Maven: com.loafle:crawler_java:1.0.0-SNAPSHOT" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.13" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: ch.qos.logback:logback-classic:1.1.3" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: ch.qos.logback:logback-core:1.1.3" level="project" />
</component>
</module>

BIN
src/lib/ojdbc7.jar Normal file

Binary file not shown.

View File

@ -0,0 +1,10 @@
package com.loafle.overflow.sql;
/**
* Created by insanity on 17. 4. 11.
*/
public class DBMetaInfo {
private String name;
public String getName() {return name;}
public void setName(String name) {this.name = name;}
}

View File

@ -0,0 +1,122 @@
package com.loafle.overflow.sql;
import com.loafle.overflow.crawler.Crawler;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by insanity on 17. 4. 11.
*/
public class SQLCrawler extends Crawler {
public SQLCrawler() {}
public Object get(String id) {
TargetInfo info = getTargetInfo(id);
return getMetrics(info);
}
private TargetInfo getTargetInfo(String id) {
TargetInfo info = new TargetInfo();
// //MySQL
info.setUrl("jdbc:mysql://192.168.1.215:3306");
info.setId("root");
info.setPw("qwe123");
info.setSSL(false);
info.setQuery("show session status");
// //MSSQL
// info.setUrl("jdbc:sqlserver://192.168.1.106:1433");
// info.setId("sa");
// info.setPw("qwe123");
// info.setSSL(false);
// info.setQuery("select * from master.dbo.sysprocesses");
// //PGSQL
// info.setUrl("jdbc:postgresql://192.168.1.107:5432/postgres");
// info.setId("postgres");
// info.setPw("!@#$qwer1234");
// info.setSSL(false);
// info.setQuery("select * from pg_stat_activity");
// //Oracle
// info.setUrl("jdbc:oracle:thin:@192.168.1.30:1521/oracle.loafle.com");
// info.setId("sys as sysdba");
// info.setPw("qwer5795QWER");
// info.setSSL(false);
// info.setQuery("select * from v$sysstat");
return info;
}
private List<Map<String, String>> getMetrics(TargetInfo info) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection(info.getUrl(),info.getId(), info.getPw());
stmt = conn.createStatement();
rs = stmt.executeQuery(info.getQuery());
ResultSetMetaData md = rs.getMetaData();
int cnt = md.getColumnCount();
List<DBMetaInfo> metaList = new ArrayList<DBMetaInfo>();
for (int i =0 ; i < cnt ; ++i) {
DBMetaInfo o = new DBMetaInfo();
o.setName(md.getColumnName(i+1));
metaList.add(o);
}
List<Map<String, String>> datas = new ArrayList<Map<String, String>>();
while(rs.next()){
Map<String, String> row = new HashMap<String, String>();
for (DBMetaInfo o : metaList) {
row.put(o.getName(), rs.getString(o.getName()));
}
datas.add(row);
}
return datas;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return null;
}
}

View File

@ -0,0 +1,53 @@
package com.loafle.overflow.sql;
/**
* Created by insanity on 17. 4. 11.
*/
public class TargetInfo {
private String url;
private String id;
private String pw;
private Boolean isSSL;
private String query;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPw() {
return pw;
}
public void setPw(String pw) {
this.pw = pw;
}
public Boolean getSSL() {
return isSSL;
}
public void setSSL(Boolean SSL) {
isSSL = SSL;
}
public String getQuery() {
return query;
}
public void setQuery(String query) {
this.query = query;
}
}

0
src/main/resources/_ Normal file
View File

View File

@ -0,0 +1,22 @@
package com.loafle.overflow;
import com.loafle.overflow.sql.SQLCrawler;
import org.junit.Test;
import java.util.List;
import java.util.Map;
public class AppTest {
@Test
public void testSQL() {
SQLCrawler sc = new SQLCrawler();
List<Map<String,String>> result = (List<Map<String,String>>)sc.get("1111");
for(Map<String, String> m : result) {
for (Map.Entry<String, String> entry : m.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
}
}
}

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="3 seconds">
<contextName>sql</contextName>
<!-- TRACE > DEBUG > INFO > WARN > ERROR -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{32} - %msg%n
</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
<logger name="com.loafle.overflow" level="ALL" />
</configuration>