我正在用c ++开发应用程序,我想使用solr索引sqlite数据库,我在网上搜索,我发现我需要使用JNI: http://randr.svbtle.com/experiment-with-embedded-solr-in-java-and-c 但我找到了JARS的许多问题(我使用了\ solr-4.9.0 \ dist \ solrj-lib和\ solr-4.9.0 \ dist和\ solr-4.9.0 \ example \ lib \ ext下的jar) 我知道我也可以使用JCC,我的问题是什么是最好的解决方案,如果是JNI,你可以向我详细说明如何配置从c ++应用程序调用solr以及我需要的罐子
答案 0 :(得分:0)
我之前从未使用过Solr,但这是使用JavaCPP和Maven进行此操作的唯一方法。首先,创建此pom.xml
构建文件:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>solrtest</artifactId>
<version>0</version>
<dependencies>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacpp</artifactId>
<version>0.9</version>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-core</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.bytedeco</groupId>
<artifactId>javacpp</artifactId>
<configuration>
<header>true</header>
<includePath>.</includePath>
<linkPath>.</linkPath>
<preloadPath>.</preloadPath>
</configuration>
<executions>
<execution>
<id>process-classes</id>
<phase>process-classes</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<artifactId>solr-core</artifactId>
<groupId>org.apache.solr</groupId>
<version>1.4.1</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
以及此src/main/java/SolrTest.java
源文件:
import java.io.*;
import org.apache.solr.core.*;
import org.apache.solr.client.solrj.*;
import org.apache.solr.client.solrj.embedded.*;
import org.bytedeco.javacpp.*;
import org.bytedeco.javacpp.annotation.*;
public class SolrTest {
static SolrServer createServer(String coreName) throws Exception {
CoreContainer coreContainer = new CoreContainer();
CoreDescriptor discriptor = new CoreDescriptor(coreContainer, coreName, new File("/some/path/", coreName).getAbsolutePath());
SolrCore solrCore = coreContainer.create(discriptor);
coreContainer.register(solrCore, false);
return new EmbeddedSolrServer(coreContainer, coreName);
}
static class Query extends FunctionPointer {
public @Name("query") String call(String string) throws Exception {
createServer(string);
return string;
}
}
}
然后我们可以执行mvn package
,在它下载了大量内容并为我们构建内容之后,我们将最终得到target/solrtest-0.jar
以及头文件和target/classes
子目录中的本机库文件。现在,我们可以用C ++这样的程序将所有这些链接在一起:
#include <iostream>
#include "target/classes/jniSolrTest.h"
int main() {
const char *argv[] = { "-Djava.class.path=target/solrtest-0.jar" };
JavaCPP_init(1, argv);
try {
query("test");
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
JavaCPP_uninit();
}
例如,我们可以在Linux x86_64下以这种方式编译和运行,但我们可以在其他平台上执行相同的操作:
$ g++ -I/usr/lib/jvm/java/include/ -I/usr/lib/jvm/java/include/linux/ target/classes/linux-x86_64/libjniSolrTest.so query.cpp -o query
$ ./query
我们从Solr得到一个正当的例外:
[main] INFO org.apache.solr.core.SolrResourceLoader - JNDI not configured for solr (NoInitialContextEx)
[main] INFO org.apache.solr.core.SolrResourceLoader - solr home defaulted to 'solr/' (could not find system property or JNDI)
[main] INFO org.apache.solr.core.SolrResourceLoader - Solr home set to '/some/path/test/'
java.lang.RuntimeException: Can't find resource 'solrconfig.xml' in classpath or '/some/path/test/conf/', cwd=/home/saudet/solrtest
我们可以通过为Solr安装东西来修复...