Donnerstag, 2. Juni 2011

MSSQL Solr Import Workaround

After a long while searching for a workaround for inserting data from MSSQL database into a Solr search server a finally did this java server page (jsp):

<html>
<head><title>Workaround the mssql java database driver </title></head>
<body>
<table>
<%@ page import="java.util.*"
import="java.sql.*"
import="javax.sql.*"
import="javax.sql.*"
import="java.io.*"
import="java.util.*"
%>

<%
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
BufferedOutputStream outPut = new BufferedOutputStream( new FileOutputStream("transferFile.tmp"));
Connection connection =
DriverManager.getConnection(
"jdbc:sqlserver://192.168.151.151;instanceName=testmssql", "sa","sa");
System.out.println("Connection Done");
connection.setCatalog("Test");
String sqlCmdString = "SELECT dokumentAspdf FROM Test";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sqlCmdString);
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
int columnCount = resultSetMetaData.getColumnCount();
int tstDocNum=0;
while (resultSet.next()) {
tstDocNum++;
outPut = new BufferedOutputStream( new FileOutputStream(""+resultSet.getString(0)));
outPut.write(resultSet.getBytes(0));
out.println("getColumnType:0:"+ resultSet.getString(0));
outPut.flush();
outPut.close();
String args = "/usr/bin/curl "+
"http://localhost:8080/solr/import/update/extract?map.content=dokumentpdf&map.stream_name=idrowid&commit=true " +
" -F " +
" file=@/usr/share/tomcat6/"+resultSet.getString(2);
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(args);
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
System.out.printf("exec: "+ args);
process.waitFor();
while ((line = br.readLine()) != null) {
System.out.println(line);
}
File f = new File("/usr/share/tomcat6/"+resultSet.getString(2));
f.delete();
}
resultSet.close();
connection.close();
System.out.println("Done");
} catch (SQLException exception) {
exception.printStackTrace();
} catch (Exception exception) {
exception.printStackTrace();
}
%>
</body>
</html>


Sure it is ugly. But it is triggerable via REST-request.

As you see:
  • it reads out the document from the database
  • write to a local file
  • insert it into solr via curl doing a POST request
But finally this fixed the stacktrace:

Keine Kommentare: