客户端项目依赖:
<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(); } }
相关推荐
在Windows上,可以通过启动`bin/solr start`命令来启动Solr服务器。在Linux或Mac系统上,使用`bin/solr start -e cloud`可以启动带有示例配置的SolrCloud模式。配置文件主要位于`conf`目录下,如`solrconfig.xml`...
《Solr.3.1.Cookbook》是一本专注于Apache Solr搜索引擎的实践指南,而提供的"Solr.3.1.Cookbook_Code"压缩包则包含了书中所讲解的源代码示例。Solr是基于Lucene的全文搜索服务器,它提供了一个高效、可扩展的搜索...
Solr in action 源代码。Solr实战源码。Solr实战源码。Solr实战源码。Solr实战源码
要启动Solr 7.4.0,你需要先设置`JAVA_HOME`环境变量,然后进入解压后的目录,执行`bin/solr start`(在Windows上是`bin\solr.cmd start`)。这将启动一个单节点的Solr服务器。如果你想要配置多节点的SolrCloud模式...
apache-solr-quick-start-and-demo
这个目录下的`start.jar`是用于启动Solr服务器的命令行工具。 3. **dist 目录**:包含Solr的JAR文件和其他库,这些是运行Solr服务所必需的。 4. **example 目录**:提供了一个预配置的Solr实例,用于快速入门和...
在命令行中运行`./solr start`(Linux/Mac)或`bin\solr.cmd start`(Windows)启动Solr服务器。 3. **创建核心(Core)**:Solr中的核心是搜索服务的基本单元,每个核心可以独立配置。首次启动时,Solr可能没有...
(2)使用内置的Jetty来启动Solr服务器只需要在example目录下,执行start.jar程序即可,我们可以直接执行命令:java –jar start.jar。 (3)当服务启动后,默认发布在8983端口,所以可以访问该端口来访问Solr服务,...
### Solr 4.7 服务搭建详细指南 #### 一、环境准备 为了搭建 Solr 4.7 服务,我们需要确保以下环境已经准备好: 1. **Java Development Kit (JDK) 1.7**:Solr 需要 Java 运行环境支持,这里我们选择 JDK 1.7 ...
3. **启动Solr服务器**:进入解压后的目录,找到`bin`文件夹,然后执行`./solr start`(对于Windows环境,执行`solr.cmd start`)启动Solr服务器。 4. **配置Solr核心**:默认情况下,Solr会创建一个名为`...
2. 进入`bin`目录,根据操作系统运行相应的启动脚本(如`bin\solr start`或`bin\solr start -e cloud`)来启动Solr服务器。 3. 使用Web浏览器访问`http://localhost:8983/solr`,如果一切正常,你应该能看到Solr管理...
这些脚本会加载`solr.in.sh`或`solr.in.cmd`环境变量,并调用Java运行时执行`start.jar`。Solr的核心配置文件是`solrconfig.xml`和`schema.xml`,它们定义了索引的结构和处理查询的方式。 当你遇到404错误,首先...
solr.warsolr.war包solr.war包solr.war包solr.war包solr.war包solr.war包solr.war包solr.war包solr.war包solr.war包solr.war包solr.war包solr.war包solr.war包solr.war包solr.war包solr.war包solr.war包solr.war包...
- 使用 `bin/solr start` 命令启动 Solr 服务器。如果你已经将 Solr 集成到 Tomcat,那么 Solr 会随着 Tomcat 的启动而自动启动。 5. **操作 Solr**: - 通过浏览器访问 `http://your_server:port/solr/admin` 来...
Solr 安装部署文档 Solr 是一个基于 Lucene 的搜索服务器,提供了强大的全文搜索功能。下面是基于 Solr 5.5.5 的安装部署文档,包含 Solr 导入 Oracle 数据库的步骤。 Solr 安装部署 1. 下载 Solr 5.5.5,地址:...
对于Jetty部署,只需解压缩Solr的发行包,进入bin目录,然后运行solr.cmd start命令即可启动Solr服务器。你可以在浏览器中访问`http://localhost:8983/solr`来查看Solr的管理界面。 若选择Tomcat作为容器,你需要将...
Solr是Apache软件基金会开发的一款开源全文搜索引擎,它基于Java平台,是Lucene的一个扩展,提供了更为方便和强大的搜索功能。在Solr 6.2.0版本中,这个强大的分布式搜索引擎引入了许多新特性和改进,使其在处理大...
Solr服务器是Apache Lucene项目的一个子项目,是一款开源的企业级搜索平台,专门用于处理大量文本数据的全文检索、搜索和分析。它基于Java开发,能够处理多种数据源,包括XML、JSON、CSV等,提供了高效、可扩展的...
4. **启动SOLR**:在SOLR的根目录下运行`bin/solr start`命令(Windows上可能为`bin\solr.cmd start`),这会启动SOLR服务器。默认情况下,SOLR会监听8983端口。 5. **创建核心(Core)**:SOLR的核心是数据处理的...