Environment for Big Data practice (7)

Source: Internet
Author: User
Tags cassandra solr

In this section, I will test Solr in the Linux environment and check whether the Solr environment is OK by adding, deleting, modifying, and querying the instance demo. I remember some of my friends left a message asking me that I only saw Solr and Cassandra being built in a separate environment, but they didn't see their collaboration. I would like to say that there is no inevitable relationship between the two. In the DSE version of Cassandra, A Solr environment is inherited within Cassandra. You only need to enable the switch, you can automatically synchronize data to Solr. However, this embedded version has poor performance and cannot be used. How can they be linked? Because cassandra's secondary index query speed is not satisfactory, and the query syntax is limited, we introduce Solr, use Solr to query the primary key, and then use Cassandra to query the desired speed based on the primary key, in summary, Solr stores some fields for query, while Cassandra stores all the data to be used. Solr is responsible for query, and Cassandra cluster is responsible for data.


I encountered a problem when using Solr these two days, because my Schema definition is as follows:

650) this. width = 650; "style =" width: 730px; height: 161px "title =" QQ20130623142934.png "src =" http://www.bkjia.com/uploads/allimg/131228/16125UF0-0.png "width =" 711 "height =" 151 "/>

At that time, I did not define a uniqueKey, and an error was returned. However, after I added it and restarted Tomcat, I still reported this error, but it was not solved. The next day, I found myself ready, so I looked at the bin directory of Tomcat, there was no shutdown. sh, and only startup. sh. I thought about whether there was no shutdown. I used the kill process to shut down tomcat and restart it. I found that it was because tomcat was not closed.

[root@bogon ~]# ps -ef |grep tomcatroot      5079     1  3 23:02 ?        00:00:20 /usr/java/jdk1.7.0_21/bin/java -Djava.util.logging.config.file=/usr/apache-tomcat-7.0.40/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/apache-tomcat-7.0.40/endorsed -classpath /usr/apache-tomcat-7.0.40/bin/bootstrap.jar:/usr/apache-tomcat-7.0.40/bin/tomcat-juli.jar -Dcatalina.base=/usr/apache-tomcat-7.0.40 -Dcatalina.home=/usr/apache-tomcat-7.0.40 -Djava.io.tmpdir=/usr/apache-tomcat-7.0.40/temp org.apache.catalina.startup.Bootstrap startroot      6128  6093  0 23:12 pts/0    00:00:00 grep tomcat[root@bogon ~]# kill -9 5079[root@bogon ~]# cd /usr[root@bogon usr]# cd apache-tomcat-7.0.40[root@bogon apache-tomcat-7.0.40]# cd bin[root@bogon bin]# sh startup.shUsing CATALINA_BASE:   /usr/apache-tomcat-7.0.40Using CATALINA_HOME:   /usr/apache-tomcat-7.0.40Using CATALINA_TMPDIR: /usr/apache-tomcat-7.0.40/tempUsing JRE_HOME:        /usr/java/jdk1.7.0_21Using CLASSPATH:       /usr/apache-tomcat-7.0.40/bin/bootstrap.jar:/usr/apache-tomcat-7.0.40/bin/tomcat-juli.jar

OK. Now that there is no problem, go to the topic and modify the content in SolrConfig. xml under the Sol instance directory.

<cores adminPath="/admin/cores" defaultCoreName="MyTest.UserInfo" host="${host:}" hostPort="${jetty.port:8080}" hostContext="${hostContext:solr}" zkClientTimeout="${zkClientTimeout:15000}"><core name="MyTest.UserInfo" instanceDir="Test" /></cores>

Set Core Name to MyTest. UserInfo.

OK. Restart Solr. Open the browser as follows:

650) this. width = 650; "title =" qq201306231420.7.png "src =" http://www.bkjia.com/uploads/allimg/131228/16125W352-1.png "/>

Our Instance name has become the name set just now (MyTest. UserInfo ).


Next we should look at our program

650) this. width = 650; "title =" QQ20130623144224.png "src =" http://www.bkjia.com/uploads/allimg/131228/16125V1D-2.png "/>

The following is the Solr Entity we define in Entity.

namespace SolrCURDDemo{using SolrNet;using SolrNet.Attributes;public class UserInfoEntity{public UserInfoEntity() { }[SolrUniqueKey("UserNO")]public string UserNO { get; set; }[SolrField("Name")]public string Name { get; set; }[SolrField("Age")]public int? Age { get; set; }[SolrField("IsMarried")]public bool? IsMarried { get; set; }}}

It is consistent with the Solr Schema we just defined. Then the Reference below is the dll we want to Reference, SolrNet. dll and Microsoft. Practises. ServiceLocation. dl. The SolrNet I downloaded is 0.4.0.2002. You can download it online.

Next is our SolrConfig configuration file, which is a custom xml file.

<?xml version="1.0" encoding="utf-8" ?><SolrConfigCollection><SolrConfigs baseAddress="Http://192.168.192.128:8080/MyTest."><Schema name="UserInfo" entityName="SolrCURDDemo.UserInfoEntity"></Schema></SolrConfigs><!--<SolrConfigs baseAddress="Http://192.168.192.128:8080/MyOtherTest."><Schema name="UserInfo"></Schema></SolrConfigs>--></SolrConfigCollection>

What does it mean? Here I mean that an address can have multiple schemas or addresses. If I have such a requirement, a Solr Service is responsible for the student's basic information, one Solr Service is responsible for the student's score information, so we configure two SolrConfigs.

Next we will use Utility, mainly SolrHelper and SolrCollection classes.

namespace SolrCURDDemo{using SolrNet;using Microsoft.Practices.ServiceLocation;using System.Reflection;using SolrNet.Commands.Parameters;using System.Configuration;public class SolrHelper{private static bool isRegister;private static SolrHelper solrHelper = new SolrHelper();private SolrHelper(){ }private static int MaxQueryCount{get{return int.Parse(ConfigurationManager.AppSettings["MaxQueryCount"].ToString());}}public static SolrHelper GetInstance(){if (!isRegister){RegisterSolrInstance();isRegister = true;}return solrHelper;}private static void RegisterSolrInstance(){string path = AppDomain.CurrentDomain.BaseDirectory + @"\SolrConfig\SolrConfig.xml";SolrCollection solrCollection = Deserialize<SolrCollection>(path);Type startUpType = typeof(Startup);foreach (SolrConfigs solrConfig in solrCollection.SolrConfigs){foreach (SchemaInfo schema in solrConfig.Schemas){MethodInfo method = startUpType.GetMethod("Init", new Type[] { typeof(string) }).MakeGenericMethod(Type.GetType(schema.EntityType));method.Invoke(null, new object[] { solrConfig.BaseAddress + schema.ServiceName });}}}private static T Deserialize<T>(string path) where T : class,new(){XmlReader xmlReader = XmlReader.Create(path);XmlSerializer xmlSer = new XmlSerializer(typeof(SolrCollection));object deSerializeObj = xmlSer.Deserialize(xmlReader);xmlReader.Close();return deSerializeObj as T;}private ISolrOperations<T> GetSolrOperation<T>(){return ServiceLocator.Current.GetInstance<ISolrOperations<T>>();}#region solr operations#region querypublic List<T> QueryByFilter<T>(string filter){try{ISolrOperations<T> solrOperation = GetSolrOperation<T>();SolrQueryResults<T> result = solrOperation.Query(filter);return result.ToList();}catch (Exception ex){throw ex;}}public List<T> QueryByFilter<T>(ISolrQuery solrQuery){try{ISolrOperations<T> solrOperation = GetSolrOperation<T>();SolrQueryResults<T> result = solrOperation.Query(solrQuery);return result.ToList();}catch (Exception ex){throw ex;}}public List<T> QueryByFilter<T>(string filter, Dictionary<string, string> sortDictionary){try{ISolrOperations<T> solrOperation = GetSolrOperation<T>();List<SortOrder> sortOrderList = new List<SortOrder>();foreach (KeyValuePair<string, string> sortPair in sortDictionary){SortOrder sortOrder = new SortOrder(sortPair.Key, (Order)Enum.Parse(typeof(Order), sortPair.Value));sortOrderList.Add(sortOrder);}SolrQueryResults<T> result = solrOperation.Query(filter, sortOrderList);return result.ToList();}catch (Exception ex){throw ex;}}public List<T> QueryByFilter<T>(ISolrQuery solrQuery, Dictionary<string, string> sortDictionary){try{ISolrOperations<T> solrOperation = GetSolrOperation<T>();List<SortOrder> sortOrderList = new List<SortOrder>();foreach (KeyValuePair<string, string> sortPair in sortDictionary){SortOrder sortOrder = new SortOrder(sortPair.Key, (Order)Enum.Parse(typeof(Order), sortPair.Value));sortOrderList.Add(sortOrder);}SolrQueryResults<T> result = solrOperation.Query(solrQuery, sortOrderList);return result.ToList();}catch (Exception ex){throw ex;}}public List<T> QueryByFilter<T>(string filter, int pageIndex, int pageSize){try{ISolrOperations<T> solrOperation = GetSolrOperation<T>();QueryOptions queryOptions = new QueryOptions();queryOptions.Start = pageIndex;queryOptions.Rows = pageSize;if (pageSize > MaxQueryCount){queryOptions.Rows = MaxQueryCount;}SolrQueryResults<T> result = solrOperation.Query(filter, queryOptions);return result.ToList();}catch (Exception ex){throw ex;}}public List<T> QueryByFilter<T>(ISolrQuery solrQuery, int pageIndex, int pageSize){try{ISolrOperations<T> solrOperation = GetSolrOperation<T>();QueryOptions queryOptions = new QueryOptions();queryOptions.Start = pageIndex;queryOptions.Rows = pageSize;if (pageSize > MaxQueryCount){queryOptions.Rows = MaxQueryCount;}SolrQueryResults<T> result = solrOperation.Query(solrQuery, queryOptions);return result.ToList();}catch (Exception ex){throw ex;}}#endregion#region addpublic int AddEntity<T>(T entity){try{ISolrOperations<T> solrOperation = GetSolrOperation<T>();solrOperation.Add(entity);solrOperation.Commit();return 1;}catch (Exception ex){throw ex;}}public int AddEntityList<T>(List<T> entityList){try{ISolrOperations<T> solrOperation = GetSolrOperation<T>();solrOperation.AddRange(entityList);solrOperation.Commit();return 1;}catch (Exception ex){throw ex;}}#endregion#region deletepublic int DeleteByEntity<T>(T entity){try{ISolrOperations<T> solrOperation = GetSolrOperation<T>();solrOperation.Delete(entity);return 1;}catch (Exception ex){throw ex;}}public int DeleteByKey<T>(string key){try{ISolrOperations<T> solrOperation = GetSolrOperation<T>();solrOperation.Delete(key);return 1;}catch (Exception ex){throw ex;}}public int DeleteByEntityList<T>(List<T> entityList){try{ISolrOperations<T> solrOperation = GetSolrOperation<T>();solrOperation.Delete(entityList);return 1;}catch (Exception ex){throw ex;}}public int DeleteByKeyList<T>(List<string> keyList){try{ISolrOperations<T> solrOperation = GetSolrOperation<T>();solrOperation.Delete(keyList);return 1;}catch (Exception ex){throw ex;}}public int DeleteByFilter<T>(ISolrQuery query){try{ISolrOperations<T> solrOperation = GetSolrOperation<T>();solrOperation.Delete(query);return 1;}catch (Exception ex){throw ex;}}#endregion#endregion}}

This is mainly the addition, deletion, modification, and query method of Solr. The following method must be described:

private static void RegisterSolrInstance(){string path = AppDomain.CurrentDomain.BaseDirectory + @"\SolrConfig\SolrConfig.xml";SolrCollection solrCollection = Deserialize<SolrCollection>(path);Type startUpType = typeof(Startup);foreach (SolrConfigs solrConfig in solrCollection.SolrConfigs){foreach (SchemaInfo schema in solrConfig.Schemas){MethodInfo method = startUpType.GetMethod("Init", new Type[] { typeof(string) }).MakeGenericMethod(Type.GetType(schema.EntityType));method.Invoke(null, new object[] { solrConfig.BaseAddress + schema.ServiceName });}}}

This is the first time we use SolrHelper, We will register all the Solr instances in the configuration file. When using SolrHelper, we only need to get it through the following code.

private ISolrOperations<T> GetSolrOperation<T>(){return ServiceLocator.Current.GetInstance<ISolrOperations<T>>();}

Next, let's look at our SolrCollection class.

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml.Serialization;namespace SolrCURDDemo{[XmlRoot("SolrConfigCollection")]public class SolrCollection{[XmlElement("SolrConfigs")]public SolrConfigs[] SolrConfigs;}public class SolrConfigs{[XmlElement("Schema")]public SchemaInfo[] Schemas;[XmlAttribute("baseAddress")]public string BaseAddress;}public class SchemaInfo{[XmlAttribute("name")]public string ServiceName;[XmlAttribute("entityName")]public string EntityType;}}

Corresponding to our SolrConfig. xml file, used for deserialization, combined with the Desrialize method in SolrHelper, you can understand.

OK. Our form is as follows:

650) this. width = 650; "title =" QQ20130623151130.png "src =" http://www.bkjia.com/uploads/allimg/131228/16125TU7-3.png "/>

OK. Let's take a look at the simple saving code, as shown below:

UserInfoEntity userInfoEntity = new UserInfoEntity();           userInfoEntity.UserNo = userNo;           userInfoEntity.Name = name;           userInfoEntity.Age = age;           userInfoEntity.IsMarried = isMarried;           int suc = SolrHelper.GetInstance().AddEntity<UserInfoEntity>(userInfoEntity);

After the file is saved successfully, we first query it in Solr. The result is as follows:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/16125TA7-4.png "title =" QQ20130623180004.png "/>

Then we will query the code in the Demo as follows:

string filter = this.txtFilter.Text.Trim();            if (filter == string.Empty)            {                CommonMessage.ShowMessage(Resources.Msg_Input_Filter);                return;            }            try            {                List<UserInfoEntity> userInfoList = SolrHelper.GetInstance().QueryByFilter<UserInfoEntity>(filter);                this.dgvSearchResult.DataSource = userInfoList;                this.BuildRowNumber();            }            catch (Exception ex)            {                CommonMessage.ShowMessage(ex.Message);            }

Query results

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/16125UL5-5.png "title =" QQ20130623181206.png "/>

In this result, I have to say that the content of the SolrConfig configuration file above should be like this.

<?xml version="1.0" encoding="utf-8" ?><SolrConfigCollection>  <SolrConfigs baseAddress="Http://192.168.192.128:8080/solr/MyTest.">      <Schema name="UserInfo" entityName="SolrCURDDemo.UserInfoEntity"></Schema>  </SolrConfigs>  <!--<SolrConfigs baseAddress="Http://192.168.192.128:8080/solr/MyOtherTest.">      <Schema name="UserInfo"></Schema>  </SolrConfigs>--></SolrConfigCollection>

But I don't know what's going on. I want to change the above address because I am missing/solr. However, the 51cto editor cannot be modified in chrome and IE9, even if I want to delete it all, I am depressed and I will post it again.

Let's take a look at the deleted code.

string userNo = txtUserNo.Text.Trim();          if (userNo == string.Empty)          {              CommonMessage.ShowMessage(Resources.Msg_InputUserNo);              return;          }          int suc = SolrHelper.GetInstance().DeleteByKey<UserInfoEntity>(userNo);

OK. The deletion is based on the primary key, object deletion, and query condition deletion. Look at the methods in the SolrHelper class. Here we recommend several websites

Http://en.wikipedia.org/wiki/Apache_Solr

Http://wiki.apache.org/solr/

Let's learn it!

Finally I want to say is when you download solrNet do not go to the official website to download, do not know what is going on, the official website of the SolrNet-0.4.0.xx version has a problem, submit save data will report 400 error, however, if the data can be saved, you need to restart tomcat to view the data. Therefore, we recommend that you download it from GitHub. GitHub URL: https://github.com/mausch/solrnet.

Address: http://code.google.com/p/solrnet/

This article is from the "Microsoft technology" blog, please be sure to keep this source http://leelei.blog.51cto.com/856755/1227895

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.