mysql跟solr集成删除的数据怎么同步不了

mysql跟solr集成删除的数据怎么同步不了,第1张

1、创建core或collection,有两种方式创建

第一种是使用命令行,查看README.txt所知道的

bin/solr create -c collection

第二种使用访问链接创建

localhost:8983/solr/admin/cores?action=CREATE&name=collection&instanceDir=collection

默认创建的目录在solr-5.2.1/server/solr下

2、修改solr-5.2.1/server/solr/collection/conf/managed-schema文件为schema.xml

前面进入conf文件夹一看,傻了,居然没有4.6.1里面的schema.xml文件,这怎么设置?后来看到别人的一个文件说可以设置,难道我去4.6.1复制一个过来,再仔细一看有个managed-schema文件,于是试着打开一看,看到了下面的内容:

This is the Solr schema file. This file should be named “schema.xml” and should be in the conf directory under the solr home(i.e. ./solr/conf/schema.xml by default)

3、在schema.xml添加filed,因为我的mysql数据库当中只有id和name两个字段,而name这个filed在schema.xml已经存在,我只需要添加id就行了,如下:

<fields>

<field name="id" type="int" indexed="true" stored="true" required="true" />

</fields>

<uniqueKey>id</uniqueKey>

<defaultSearchField>name</defaultSearchField>

4、修改solr-5.2.1/server/solr/collection/conf/下的solrconfig.xml的配置文件,配置一下添加数据库数据的xml,如下:

<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">

<lst name="defaults">

<str name="config">data-config.xm

修改主方法

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()

}

}

}

你是solr 4以后的版本吧,使用了version的概念

文档数会不停的增加、或不变

只有达到某个合并的零界点的话,才会使文档数的跟你数据一致

删除:文档数不变化

更新/新增:文档数增大


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

原文地址: https://outofmemory.cn/sjk/9975529.html

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

发表评论

登录后才能评论

评论列表(0条)

保存