如何保证solr跟数据库的数据一致性

如何保证solr跟数据库的数据一致性,第1张

关系型数据库有四个显著的特征,即安全性、完整性、并发性和监测性。数据库的安全性就是要保证数据库中数据的安全,防止未授权用户随意修改数据库中的数据,确保数据的安全。在大多数数据库管理系统中,主要是通过许可来保证数据库的安全性。完整性是数据库的一个重要特征,也是保证数据库中的数据切实有效、防止错误、实现商业规则的一种重要机制。在数据库中,区别所保存的数据是无用的垃圾还是有价值的信息,主要是依据数据库的完整性是否健全。在SQL Server 7.0中,数据的完整性是通过一系列逻辑来保障的,这些逻辑分为三个方面,即实体完整性、域完整性和参考完整性。对任何系统都可以这样说,没有监测,就没有优化。这句话用在数据库管理系统方面,也是切合实际的。只有通过对数据库进行全面的性能监测,也才能发现影响系统性能的因素和瓶颈,才能针对瓶颈因素,采取切合实际策略,解决问题,提高系统的性能。

修改主方法

public int saveContent(String enterpriseId, String enterpriseName, String lableType, String resouce, String pubDate,

String content) {

int state = 0

LBHttpSolrServer server = SolrUtil.getSolrServer(ap.getEnterprisenewSolrUrl())

SolrQuery query = new SolrQuery()

query.set("q", "enterpriseId:" + enterpriseId)

try {

QueryResponse qr = server.query(query)

List<EnterpriseContentBean>contentList = qr.getBeans(EnterpriseContentBean.class)

// 设置需要保存的文章信息

for (EnterpriseContentBean bean : contentList) {

bean.setEnterpriseId(enterpriseId)

bean.setEnterpriseName(enterpriseName)

List<String>contents = new ArrayList<String>()

contents.add(content)

bean.setContent(contents)

bean.setPubDate(pubDate)

System.out.println("pubDate======>" + pubDate)

List<String>lableTypes = Arrays.asList(lableType.split(","))

bean.setLableType(lableTypes)

bean.setResource(resouce)

bean.setIsVisited_s("1")

}

server.addBeans(contentList)

server.commit()

} catch (SolrServerException e) {

state = 1

System.out.println("修改solr数据报错")

e.printStackTrace()

} catch (IOException e) {

state = 1

System.out.println("修改solr数据报错")

e.printStackTrace()

}

return state

}

删除主方法

public int deletContent(String enterpriseId) {

LBHttpSolrServer server = SolrUtil.getSolrServer(ap.getEnterprisenewSolrUrl())

int state = 0

try {

server.deleteById(enterpriseId)

server.commit()

} catch (SolrServerException e) {

state = 1

System.out.println("删除solr数据报错")

e.printStackTrace()

} catch (IOException e) {

state = 1

System.out.println("删除solr数据报错")

e.printStackTrace()

}

return state

}

solr工具类

package com.dinfo.boc.utils

import java.io.IOException

import java.net.MalformedURLException

import java.util.ArrayList

import java.util.Arrays

import java.util.Collection

import java.util.List

import org.apache.solr.client.solrj.SolrQuery

import org.apache.solr.client.solrj.SolrServerException

import org.apache.solr.client.solrj.impl.LBHttpSolrServer

import org.apache.solr.client.solrj.response.QueryResponse

import org.apache.solr.common.SolrDocumentList

import org.apache.solr.common.SolrInputDocument

import com.dinfo.boc.enterprise.bean.EnterpriseContentBean

import com.dinfo.boc.enterprisenew.bean.SolrQueryResult

/**

* 与Solr服务器交互的工具类

* @author qiuyj

*

*/

public class SolrUtil {

/**

* 获取与指定Solr地址的连接

* @param solrUrl

* @return

*/

public static LBHttpSolrServer getSolrServer(String solrUrl){

final int ONE_HUNDRED_MS = 10000000

if(solrUrl == null || "".equals(solrUrl)){

throw new RuntimeException("Solr url can not be empty!")

}

LBHttpSolrServer solrServer = null

try {

solrServer = new LBHttpSolrServer(solrUrl)

solrServer.setConnectionTimeout(ONE_HUNDRED_MS)

} catch (MalformedURLException e) {

e.printStackTrace()

} //SolrUtil.getSolrServer(solrUrl)

//solrServer.setDefaultMaxConnectionsPerHost(100)

//solrServer.setMaxTotalConnections(100)

return solrServer

}

/**

* 向指定的Solr地址添加一条数据

* @param solrUrl

* @param doc

* @throws Exception

*/

public static void add(String solrUrl, SolrInputDocument doc) throws Exception {

if(doc == null){

throw new RuntimeException("SolrInputDocument object can not be null!")

}

LBHttpSolrServer solr = getSolrServer(solrUrl)

solr.add(doc)

solr.commit()

}

/**

* 向指定的Solr地址用JavaBean添加一条数据

* @param solrUrl

* @param obj

* @throws Exception

*/

public static void add(String solrUrl, Object obj) throws Exception {

if(obj == null){

throw new RuntimeException("Object to be inserted can not be null!")

}

LBHttpSolrServer solr = getSolrServer(solrUrl)

solr.addBean(obj)

solr.commit()

}

/**

* 向指定Solr地址批量添加数据

* @param solrUrl

* @param docs

* @throws Exception

*/

@SuppressWarnings("unchecked")

public static void addAll(String solrUrl, Collection<? extends Object>objs) throws Exception {

if(objs == null){

throw new RuntimeException("Object collection can not be null!")

}

if(objs.size() == 0){

return

}

LBHttpSolrServer solr = getSolrServer(solrUrl)

if(objs.iterator().next() instanceof SolrInputDocument){

solr.add((Collection<SolrInputDocument>)objs)

} else {

solr.addBeans(objs)

}

solr.commit()

}

/**

* 根据给定的id,从solr中删除对应信息

* @param solrUrl

* @param ids

*/

public static void deleteByIds(String solrUrl, String ... ids) throws Exception {

if(ids == null || ids.length == 0){

throw new RuntimeException("Ids can not be empty!")

}

LBHttpSolrServer solr = getSolrServer(solrUrl)

solr.deleteById(Arrays.asList(ids))

solr.commit()

}

public static void deleteByIds(String solrUrl, Integer ... ids) throws Exception {

if(ids == null || ids.length == 0){

throw new RuntimeException("Ids can not be empty!")

}

List<String>stringIdList = new ArrayList<>(ids.length)

for(Integer id : ids){

stringIdList.add("" + id)

}

LBHttpSolrServer solr = getSolrServer(solrUrl)

solr.deleteById(stringIdList)

solr.commit()

}

/**

* 删除指定Solr路径下符合指定查询条件的数据

* @param solrUrl

* @param condition

* @throws Exception

*/

public static void deleteByCondition(String solrUrl, String condition) throws Exception {

if(condition == null || "".equals(condition)){

throw new RuntimeException("Condition can not be empty!")

}

LBHttpSolrServer solr = getSolrServer(solrUrl)

solr.deleteByQuery(condition)

solr.commit()

}

/**

* 删除指定Solr路径下的所有数据

* @param solrUrl

* @throws Exception

*/

public static void deleteAll(String solrUrl) throws Exception {

deleteByCondition(solrUrl, "*:*")

}

/**

* 根据 指定查询条件从Solr中查询数据,并以SolrDocument的List形式返回

* @param solrUrl

* @param query

* @return

* @throws Exception

*/

public static SolrDocumentList queryAndGetSolrDocumentList(String solrUrl, SolrQuery query) throws Exception {

if(query == null){

throw new RuntimeException("SolrQuery object can not be null!")

}

LBHttpSolrServer solr = getSolrServer(solrUrl)

QueryResponse resp = solr.query(query)

return resp.getResults()

}

/**

* 根据 指定查询条件从Solr中查询数据,并以QueryResponse形式返回

* @param solrUrl

* @param query

* @return

* @throws Exception

*/

public static QueryResponse queryAndGetSolrQueryResponse(String solrUrl, SolrQuery query) throws Exception {

if(query == null){

throw new RuntimeException("SolrQuery object can not be null!")

}

LBHttpSolrServer solr = getSolrServer(solrUrl)

QueryResponse resp = solr.query(query)

return resp

}

/**

* 根据 指定查询条件从Solr中查询数据,并以Java Bean的List形式返回

* @param solrUrl

* @param query

* @param returnClass 返回的List集合的泛型

* @return

* @throws Exception

*/

public static <T>List<T>queryAndGetBeanList(String solrUrl, SolrQuery query, Class<T>returnClass) throws Exception {

if(query == null){

throw new RuntimeException("SolrQuery object can not be null!")

}

if(returnClass == null){

throw new RuntimeException("Return class can not be null!")

}

LBHttpSolrServer solr = getSolrServer(solrUrl)

QueryResponse resp = solr.query(query)

return resp.getBeans(returnClass)

}

/**

* 根据 指定查询条件从Solr中查询数据,并以SolrQueryResult对象的形式返回,其中包含List对象和totalCount

* @param solrUrl

* @param query

* @param returnClass 返回的List集合的泛型

* @return

* @throws Exception

*/

public static <T>SolrQueryResult<T>queryAndGetSolrQueryResult(String solrUrl, SolrQuery query, Class<T>returnClass) throws Exception {

SolrQueryResult<T>result = new SolrQueryResult<T>()

if(query == null){

throw new RuntimeException("SolrQuery object can not be null!")

}

if(returnClass == null){

throw new RuntimeException("Return class can not be null!")

}

LBHttpSolrServer solr = getSolrServer(solrUrl)

solr.setConnectionTimeout(10000)

QueryResponse resp = solr.query(query)

List<T>resultList = resp.getBeans(returnClass)

long totalCount = resp.getResults().getNumFound()

result.setResultList(resultList)

result.setTotalCount(totalCount)

return result

}

/**

* 根据 指定查询条件从Solr中查询数据,并以SolrQueryResult对象的形式返回,其中包含List对象和totalCount

* @param solrUrl

* @param query

* @param returnClass 返回的List集合的泛型

* @return

* @throws Exception

*/

public static <T>SolrQueryResult<T>queryAndGetSolrQueryResult(LBHttpSolrServer solr, SolrQuery query, Class<T>returnClass) throws Exception {

SolrQueryResult<T>result = new SolrQueryResult<T>()

if(query == null){

throw new RuntimeException("SolrQuery object can not be null!")

}

if(returnClass == null){

throw new RuntimeException("Return class can not be null!")

}

QueryResponse resp = solr.query(query)

List<T>resultList = resp.getBeans(returnClass)

long totalCount = resp.getResults().getNumFound()

result.setResultList(resultList)

result.setTotalCount(totalCount)

return result

}

/**

* 用以过滤一些影响Solr查询的特殊字符,如左右括号、星号等

* @param str

* @return

*/

public static String filterSpecialCharacters(String str){

if(str == null){

return str

}

str = str.replace("(", "\\(")

str = str.replace(")", "\\)")

str = str.replace("*", "\\*")

return str

}

public static void updateSolrById(LBHttpSolrServer server){

SolrQuery query = new SolrQuery()

String id="5d495a00a5c8118c03ef0bec0111dd8d"

int state=0

String name="新疆金风科技股份有限公司"

query.set("q", "enterpriseId:"+id)

try {

QueryResponse qr = server.query(query)

List<EnterpriseContentBean>contentList = qr.getBeans(EnterpriseContentBean.class)

//设置需要保存的文章信息

for(EnterpriseContentBean bean:contentList){

// bean.setEnterpriseId(enterpriseId)

bean.setEnterpriseName(name)

bean.setResource("东方财富网港股频道")

}

server.addBeans(contentList)

server.commit()

} catch (SolrServerException e) {

state = 1

e.printStackTrace()

} catch (IOException e) {

state = 1

e.printStackTrace()

}

}

public static void main(String[] args) {

try {

LBHttpSolrServerenterpriseServer=new LBHttpSolrServer("http://115.182.226.165:8008/solr/enterprisenew")

enterpriseServer.setConnectionTimeout(10000000)

updateSolrById(enterpriseServer)

System.out.println("over")

} catch (MalformedURLException e) {

// TODO Auto-generated catch block

e.printStackTrace()

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6821880.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-28
下一篇 2023-03-28

发表评论

登录后才能评论

评论列表(0条)

保存