Copyright © 2022-2025 aizws.net · 网站版本: v1.2.6·内部版本: v1.25.2·
页面加载耗时 0.00 毫秒·物理内存 180.7MB ·虚拟内存 1439.6MB
欢迎来到 AI 中文社区(简称 AI 中文社),这里是学习交流 AI 人工智能技术的中文社区。 为了更好的体验,本站推荐使用 Chrome 浏览器。
在本教程页面中,我们将讨论使用 Java 客户端 API 检索数据的方式。例如,我们有一个名为 sample.csv 的 .csv 文档,其中包含以下数据。
001, +148022337,
new
York, John 002, +148022338, Hawaii, Emma 003, +148022339, Florida, Sophia
我们现在可以使用 post 命令在名为 sample_Solr 的核心下索引这些数据。
[Hadoop@localhost bin]$ ./post-c Solr_sample.csv
下面给出的是 Java 程序,它被编码以将文档添加到 Apache Solr 索引。将代码保存在名为 RetrievingData.java 的文件中。
import java.io.IOException; import org.apache.Solr.client.Solrj.SolrClient; import org.apache.Solr.client.Solrj.SolrQuery; import org.apache.Solr.client.Solrj.SolrServerException; import org.apache.Solr.client.Solrj.impl.HttpSolrClient; import org.apache.Solr.client.Solrj.response.QueryResponse; import org.apache.Solr.common.SolrDocumentList;
public
class
RetrievingData {
public
static
void
main(String args[])
throws
SolrServerException, IOException {
//Preparing the Solr client
String urlString =
"http://localhost:8983/Solr/my_core"
;
SolrClient Solr =
new
HttpSolrClient.
Builder
(urlString).
build
();
//Preparing the Solr query
SolrQuery query =
new
SolrQuery();
query.
setQuery
(
"*:*"
);
//Adding the field that has to be retrieved
query.
addField
(
"*"
);
//Executing query
for
data
QueryResponse queryResponse = Solr.
query
(query);
//Saving the results of the query
SolrDocumentList docs = queryResponse.
getResults
();
System.
out.println
(docs);
System.
out.println
(docs.
get
(0));
System.
out.println
(docs.
get
(1));
System.
out.println
(docs.
get
(2));
//storing the operations
Solr.
commit
(); } }
在终端执行以下命令编译并运行上述代码-
[Hadoop@localhost bin]$ javac RetrievingData [Hadoop@localhost bin]$ java RetrievingData
运行上面给出的命令,我们将得到以下结果。
{numFound = 3, start = 0, docs = [SolrDocument{id=001, phone = [+148022337],
city = [California], first_name = [Olivia],
_version_ = 1547262806014820352}, SolrDocument { id = 002, phone = [+148022338],
city = [Hawaii], first_name = [Emma],
_version_ = 1547262806026354688}, SolrDocument { id = 003, phone = [+148022339],
city = [Florida], first_name = [Sophia],
_version_ = 1547262806029500416}]} SolrDocument {id = 004, phone = [+148022337], city = [Texas], first_name = [Emily],
_version_ = 1547262806014820352}, SolrDocument { id = 002, phone = [+148022338],
city = [Hawaii], first_name = [Emma],
_version_ = 1547262806026354688}, SolrDocument { id = 003, phone = [+148022339],
city = [Florida], first_name = [Sophia],
_version_ = 1547262806029500416}
Apache Solr 除了存储数据外,还提供查询数据的功能。 Solr 提供了一些参数,用户可以使用这些参数来查询其中存储的数据。在下表中,我们列出了 Apache Solr。参数说明q是Apache So ...