import java.io.IOException; import java.net.MalformedURLException; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Types; import java.util.ArrayList; import java.util.Collection; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.impl.HttpSolrServer; import org.apache.solr.common.SolrInputDocument; public class Test { private static int fetchSize = 1000; private static String url = "http://localhost:8983/solr/core1/"; private static HttpSolrServer solrCore; public Test() throws MalformedURLException { solrCore = new HttpSolrServer(url); } public long addResultSet(ResultSet rs) throws SQLException, SolrServerException, IOException { long count = 0; int innerCount = 0; Collection docs = new ArrayList(); ResultSetMetaData rsm = rs.getMetaData(); int numColumns = rsm.getColumnCount(); String[] colNames = new String[numColumns + 1]; for (int i = 1; i < (numColumns + 1); i++) { colNames[i] = rsm.getColumnName(i); // //Example: // if (rsm.getColumnName(i) == "db_id") // { // colNames[i] = null; // } } while (rs.next()) { count++; innerCount++; SolrInputDocument doc = new SolrInputDocument(); // //Example: // doc.addField("solr_db_id", rs.getLong("db_id")); for (int j = 1; j < (numColumns + 1); j++) { if (colNames[j] != null) { Object f; switch (rsm.getColumnType(j)) { case Types.BIGINT: { f = rs.getLong(j); break; } case Types.INTEGER: { f = rs.getInt(j); break; } case Types.DATE: { f = rs.getDate(j); break; } case Types.FLOAT: { f = rs.getFloat(j); break; } case Types.DOUBLE: { f = rs.getDouble(j); break; } case Types.TIME: { f = rs.getDate(j); break; } case Types.BOOLEAN: { f = rs.getBoolean(j); break; } default: { f = rs.getString(j); } } doc.addField(colNames[j], f); } } docs.add(doc); if (innerCount == fetchSize) { solrCore.add(docs); docs.clear(); innerCount = 0; } } if (innerCount != 0) { solrCore.add(docs); } return count; } }