客户端项目依赖:
<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> <groupId>com.petstore</groupId> <artifactId>dtrace</artifactId> <version>0.0.1-SNAPSHOT</version> <name>dtrace</name> <dependencies> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-core</artifactId> <version>4.6.0</version> </dependency> <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> <version>4.6.0</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> </dependencies> </project>
客户端测试代码:
package mytest; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer; import org.apache.solr.client.solrj.impl.BinaryRequestWriter; import org.apache.solr.client.solrj.impl.HttpSolrServer; import org.apache.solr.common.SolrInputDocument; import org.apache.solr.core.CoreContainer; public class SolrTest { public static void test1() throws Exception { HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/dtrace"); solrServer.setRequestWriter(new BinaryRequestWriter()); int size = 100; long begin = System.currentTimeMillis(); for (int i = 0; i < size; ++i) { SolrInputDocument doc1 = new SolrInputDocument(); doc1.addField("id", i); doc1.addField("rowkey", "100 p"); doc1.addField("cf", "[{30:50}]"); doc1.addField("timestamp", System.currentTimeMillis()); solrServer.add(doc1); } long end = System.currentTimeMillis(); System.out.println(" add cost:" + (end - begin) + "ms"); begin = System.currentTimeMillis(); solrServer.commit(); end = System.currentTimeMillis(); // System.out.println(" commit " + size + " cost:" + (end - begin) + " ms"); } static ExecutorService service = Executors.newFixedThreadPool(20); static CoreContainer container = new CoreContainer("/duitang/data/solr"); static { container.load(); } static EmbeddedSolrServer solrServer = new EmbeddedSolrServer(container, "dtrace"); public static void _test2(int round) throws Exception { int count = 10000; int size = count * round; final CountDownLatch latch = new CountDownLatch(count); final HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/dtrace"); solrServer.setRequestWriter(new BinaryRequestWriter()); long begin = System.currentTimeMillis(); for (int i = size - count; i < size; ++i) { final int id = i; service.submit(new Runnable() { public void run() { SolrInputDocument doc1 = new SolrInputDocument(); doc1.addField("id", id); doc1.addField("rowkey", "12345678901234567890 12345678901234567890 12345678901234567890 12345678901234567890 12345678901234567890"); doc1.addField( "cf", "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"); doc1.addField("timestamp", System.currentTimeMillis()); try { solrServer.add(doc1); latch.countDown(); } catch (Exception e) { e.printStackTrace(); } } }); } latch.await(); long end = System.currentTimeMillis(); System.out.println(" add[" + size + "] " + count + " cost:" + (end - begin) + "ms"); // begin = System.currentTimeMillis(); // solrServer.commit(false, false, true); // solrServer.commit(); // end = System.currentTimeMillis(); // System.out.println(" commit " + size + " cost:" + (end - begin) + "ms"); // service.shutdown(); } public static void test2() throws Exception { ExecutorService service = Executors.newFixedThreadPool(1); int count = 10000; final CountDownLatch latch = new CountDownLatch(count); long begin = System.currentTimeMillis(); for (int i = 7342; i <= count; ++i) { final int index = i; service.submit(new Runnable() { public void run() { try { _test2(index); latch.countDown(); } catch (Exception e) { e.printStackTrace(); } } }); } latch.await(); long end = System.currentTimeMillis(); System.out.println(" add finish " + count + " cost:" + (end - begin) + "ms"); } public static void test3() throws Exception { // CoreContainer container = new CoreContainer("/duitang/data/solr"); // container.load(); // EmbeddedSolrServer solrServer = new EmbeddedSolrServer(container, "dtrace"); SolrInputDocument doc1 = new SolrInputDocument(); doc1.addField("id", 1); doc1.addField("rowkey", "100 p"); doc1.addField("cf", "[{30:50}]"); doc1.addField("timestamp", System.currentTimeMillis()); solrServer.add(doc1); // solrServer.commit(); System.out.println("ok"); } public static void close() { solrServer.shutdown(); } public static void main(String[] args) throws Exception { test2(); // close(); // RAMDirectory rdir = new RAMDirectory(); // String fileList[] = rdir.listAll(); // for (int i = 0; i < fileList.length; i++) { // // } // FileSystem fs = FileSystem.get(null); // fs.startLocalOutput(fsOutputFile, tmpLocalFile) // FSDirectory.open(path) //IndexWriter indexWriter = new IndexWriter(); } }
下面是通过SolrCore的方式启动Solr
SolrResourceLoader.SetSolrHome("/duitang/dist/app/branches/mdrill/trunk/adhoc-core/solr"); SolrResourceLoader.SetSchemaHome("/duitang/dist/app/branches/mdrill/trunk/adhoc-core/solr"); CoreContainer.Initializer init = new CoreContainer.Initializer(); CoreContainer cores = init.initialize(); String corename = "dtrace"; SolrCore core = cores.getCore(corename); SolrConfig config = core.getSolrConfig(); SolrRequestParsers parser = new SolrRequestParsers(config); String path = "/select"; SolrRequestHandler handler = core.getRequestHandler(path); HttpServletRequest req = new HttpServletRequestMock(); parser.parse(core, path, req); handler = core.getRequestHandler(null); SolrQueryRequest solrReq = parser.parse(core, path, req); SolrQueryResponse solrRsp = new SolrQueryResponse(); core.execute(handler, solrReq, solrRsp); System.out.println(solrRsp.getValues()); System.out.println("================"); // OutputStreamWriter writer = new OutputStreamWriter(System.out); // XMLWriter.writeResponse(writer, solrReq, solrRsp); // writer.close(); OutputStreamWriter writer = new OutputStreamWriter(System.out); JSONResponseWriter JSONResponseWriter = new JSONResponseWriter(); JSONResponseWriter.write(writer, solrReq, solrRsp); writer.close(); System.exit(-1);
public class HttpServletRequestMock implements HttpServletRequest { @Override public Map getParameterMap() { // TODO Auto-generated method stub HashMap param = new HashMap(); param.put("q", new String[] { "*:*" }); return param; } @Override public String getMethod() { // TODO Auto-generated method stub return "GET"; } }
下面是通过JettySolrRunner跑
package mytest; import org.apache.solr.client.solrj.embedded.JettySolrRunner; import org.apache.solr.core.SolrResourceLoader; import com.alimama.mdrill.solr.realtime.ShardPartion; import com.alimama.mdrill.utils.IndexUtils; public class MySolrRunTester { public static void test1() throws Exception { // SolrResourceLoader.SetSolrHome("/duitang/dist/app/branches/mdrill/trunk/adhoc-core/solr/"); // SolrResourceLoader.SetSchemaHome("/duitang/dist/app/branches/mdrill/trunk/adhoc-core/solr/"); SolrResourceLoader .SetSolrHome("/duitang/dist/app/branches/mdrill/trunk/adhoc-mdrill/src/main/resources/solr/"); SolrResourceLoader .SetSchemaHome("/duitang/dist/app/branches/mdrill/trunk/adhoc-mdrill/src/main/resources/solr/"); // SolrResourceLoader.SetSolrHome("/duitang/dist/app/branches/mdrill/trunk/adhoc-core/solr/"); // SolrResourceLoader // .SetSchemaHome("/duitang/dist/app/branches/mdrill/trunk/adhoc-core/solr/"); // SolrConfig solrConfig = new SolrConfig( // "/duitang/dist/app/branches/mdrill/trunk/adhoc-core/solr/conf/solrconfig.xml"); // InputSource is = new InputSource(solrConfig.getResourceLoader().openSchema( // "/duitang/dist/app/branches/mdrill/trunk/adhoc-core/solr/conf/schema.xml")); ShardPartion.base = "/group/tbdp-etao-adhoc/p4padhoc/tabletest"; //必须的,如果不配置会有异常,SolrCore中会使用ShardPartion.getHdfsRealtimePath ShardPartion.taskIndex = 1; ShardPartion.index = IndexUtils.getHdfsForder(1); // String hdfsconf = "/duitang/dist/sys/hadoop-1.2.1/conf/"; //必须的,如果不配置会有异常,ReadOnlyDirectory.getConf() // HadoopUtil.setHdfsConfDir(hdfsconf); JettySolrRunner jetty = new JettySolrRunner("/solr", 1210); jetty.start(); while (true) { Thread.sleep(1000); } } public static void main(String[] args) throws Exception { test1(); } }