After learning from the previous two articles, using SOLR for data import and incremental indexing of MySQL should be all right.
(Children's shoes are not clear, please check the following blog posts to learn: http://blog.csdn.net/weijonathan/article/details/16962257, Http://blog.csdn.net/weijonathan /article/details/16961299)
Next we'll learn to read the data we want from SOLR. You can also verify with the SOLR Web interface to see if your query results are correct.
Environment Preparation:
Extract the following jar packages from the previously downloaded SOLR installer package
/dist:
Apache-solr-solrj-*.jar
/dist/solrj-lib:
Commons-codec-1.3.jar
Commons-httpclient-3.1.jar
Commons-io-1.4.jar
Jcl-over-slf4j-1.5.5.jar
Slf4j-api-1.5.5.jar
/lib:
Slf4j-jdk14-1.5.5.jar
Or if you are using Maven for Jar pack management. You can use the following MAVEN libraries to add the required jar packs
<dependency>
<artifactId>solr-solrj</artifactId>
<groupid>org.apache.solr</ groupid>
<version>1.4.0</version>
<type>jar</type>
<scope>compile </scope>
</dependency>
If you need to use the embeddedsolrserver, you need to import the core package.
<dependency>
<artifactId>solr-core</artifactId>
<groupid>org.apache.solr</ groupid>
<version>1.4.0</version>
<type>jar</type>
<scope> Compile</scope>
</dependency>
There are two more dependency packs.
<dependency>
<groupId>javax.servlet</groupId>
<artifactid>servlet-api</ artifactid>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactid>slf4j-simple</ artifactid>
<version>1.5.6</version>
</dependency>
Once the environment is ready, let's take a look at creating the connection using Httpsolrserver.
String url = "Http://${ip}:${port}";
/*
Httpsolrserver is thread-safe and if your are using the following constructor, you *must* re-use the same Instan
Ce for all requests. If instances are created
on the fly, it can cause a connection leak. The recommended practice is to keep a
static instance of Httpsolrserver/SOLR server URL and share it for all Reque Sts.
https://issues.apache.org/jira/browse/SOLR-861 for more details
*
/solrserver server = new Httpsolrserver ( URL);
You can also set some connection properties when creating a connection
String url = "Http://<span style=" font-family:arial, Helvetica, Sans-serif; " >${ip}:${port}</span><span style= "font-family:arial, Helvetica, Sans-serif;"
> "</span> httpsolrserver server = new Httpsolrserver (URL); Server.setmaxretries (1); Defaults to 0.
> 1 not recommended. Server.setconnectiontimeout (5000); 5 Seconds to establish TCP//Setting The XML response parser are only required for cross//version compatibility a
nd only when one side are 1.4.1 or//earlier and the other side is 3.1 or later. Server.setparser (New Xmlresponseparser ());
Binary parser is used by default//The following settings are provided this for completeness. They'll not normally is required, and should only is used//after consulting Javadocs to know whether They are TR
Uly required. Server.setsotimeout (1000);
Socket read Timeout server.setdefaultmaxconnectionsperhost (100);
Server.setmaxtotalconnections (100); Server.setfollowredirects (false);
Defaults to False//allowcompression defaults to False.
Server side must support gzip or deflate for this to have any effect. Server.setallowcompression (TRUE);
I think a lot of people are using the entity to receive the returned data, so easy to manage, then look at the SOLRJ inside how to define the entity.
In fact, the definition of entities in SOLRJ is not much different from normal. is a annotation annotation that is used to mark the corresponding to the SOLR entry attribute.
Import Org.apache.solr.client.solrj.beans.Field;
public class Item {
@Field
String ID;
@Field ("Cat")
string[] categories;
@Field
list<string> features;
}
In addition to being set on the field, we can also set on the set method.
@Field ("Cat") Public
void Setcategory (string[] c) {
this.categories = c;
}
Add Data:
First Get Solrserver
Solrserver Server = new Httpsolrserver ("Http://${ip}:${port}");
If you want to delete all the indexes,
Server.deletebyquery ("*:*");//Caution:deletes everything!
Inserting data from SOLR using our defined bean
Item item = new Item ();
Item.id = "one";
Item.categories = New string[] {"AAA", "BBB", "CCC"};
Server.addbean (item);
If you need to insert multiple words at once. Inserting a list<bean> can
list<item> beans;
Add Item objects to the list
Server.addbeans (beans);
You can change all of your indexes in an HTTP request in the following form. This is the most optimized way.
Httpsolrserver Server = new Httpsolrserver ();
iterator<solrinputdocument> iter = new iterator<solrinputdocument> () {public
Boolean hasnext () {
Boolean result;
Set to true false to say if your have more documensts return result
;
Public Solrinputdocument Next () {
solrinputdocument result = null;
Construct a new document here with set it to result return result
;
}
;
Server.add (ITER);
There will be separate posts for SOLRJ queries.