The Search service supports multiple programming languages and is fully compatible with open source Apache Solr APIs. This topic describes how to access the Search service by using Solr Java APIs.
Background information
1. Obtain the cluster connection address.
On the details page of the Search instance, click Database Connection
to view the client address information, as shown in the following figure.Client Address
Note: Public IP addresses are not supported for the Search service. If you want to access the service over the Internet, contact customer service in the ApsaraDB for HBase Q&A DingTalk group.
2. Configure the SDK dependency of the client. The Search service is compatible with the open source Solr protocol. Therefore, you can add the same dependency that is used in the open source client.
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>7.7.3</version>
</dependency>
3. Write code.
The following figure provides an example of how to write code. Query the index to
obtain the ID
of the document, convert the ID to the rowkey
of the HBase table, and then query the table to obtain the original data that is
required.
-
3.1 Query an index
/** // Use the HttpSolrClient method for local debugging when you access the Solr client over the Internet. // Prerequisite: The IP address of the host is added to the whitelist in the RAM console. String httpUrl = "http://***:8983/solr/" HttpSolrClient solrClient = new HttpSolrClient.Builder(httpUrl).build(); **/ // Use the CloudSolrClient method when you access the Solr client over an internal network. String zkHost = "ld-****-m1-se-proxy-zk.hbaseue.rds.aliyuncs.com:2181/solr"; CloudSolrClient solrClient = new CloudSolrClient.Builder(Collections.singletonList(zkHost), Optional.empty()).build(); // CloudSolrClient is thread-safe and can be shared by multiple threads. SolrQuery solrQuery = new SolrQuery("name_s:bobo AND age_i:18"); QueryResponse response = solrClient.query("your_index_name", solrQuery); SolrDocumentList documentList = response.getResults(); long hitCount = documentList.getNumFound(); // Query the number of documents that meet the specified condition. for(SolrDocument doc : documentList) { // Query the documents that meet the specified condition. String id = (String)doc.getFieldValue("id"); //do something } solrClient.close();
For more information, visit Reference.
-
3.2 Convert the ID to the rowkey
By default, the ID is a string. You can run the following code to convert the ID to the rowkey:
// String id = "xxxx"; org.apache.hadoop.hbase.util.Bytes.toBytes(docId)
-
3.3 Obtain the final data
After you obtain the rowkey that meets the specified condition, you can use the GET method to obtain the data. You can use the native Java APIs to obtain data. You can also use Thrift, which supports multiple programming languages, such as C#, Python, and Go, to obtain data.