solr4.5笔记(三)—— solr导入mysql数据

这里修改core1来实现mysql的数据导入(所需jar第一篇文章已经加过了),这里只是示例具体请根据自己的需要稍微改一下

PS:如果修改core1库的名字 换为 test,需要把solrconfig.xml里面的${solr.core1.data.dir:} 全部改为${solr.test.data.dir:} 

1、修改solrconfig.xml


添加如下:

<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">     
          <lst name="defaults">     
               <str name="config">data-config.xml</str>     
          </lst>     
  </requestHandler>

在同一目录下新建一个data-config.xml

<dataConfig>
    <dataSource name="companyDump" type="JdbcDataSource" driver="com.mysql.jdbc.Driver"
                url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8" user="root"
                password="root"/>
    <document>
        <entity name="company"
        	query="select id,slogan,companyName,categoryId,categoryName,xcategoryName,xcategoryId,regtime,invest,shopNum,url,brandName,product,managementMode,level,logo,createDate from company_basicinfo where isDelete=0 and isActive=1"
        	deltaImportQuery="select id,slogan,companyName,categoryId,categoryName,xcategoryName,xcategoryId,regtime,invest,shopNum,url,brandName,product,managementMode,level,logo,createDate from company_basicinfo where isDelete=0 and isActive=1 and id='${dataimporter.delta.id}'"
          deltaQuery="select id from company_basicinfo where isDelete=0 and isActive=1 and updateDate > '${dataimporter.last_index_time}'">
		  
		  <field column="slogan" name="slogan"/>
		  <field column="companyName" name="companyName"/>		  
		  <field column="categoryId" name="categoryId"/>
		  <field column="categoryName" name="categoryName"/>
		  <field column="xcategoryId" name="xcategoryId"/>
		  <field column="xcategoryName" name="xcategoryName"/>
		  <field column="regtime" name="regtime"/>
		  <field column="invest" name="invest"/>
		  <field column="shopNum" name="shopNum"/>
		  <field column="url" name="url"/>
		  <field column="brandName" name="brandName"/>
		  <field column="product" name="product"/>
		  <field column="managementMode" name="managementMode"/>
		  <field column="level" name="level"/>
		  <field column="logo" name="logo"/>
		  <field column="createDate" name="createDate"/>
		 
	    </entity>
    </document>
</dataConfig>

修改schema.xml

<?xml version="1.0" ?>
<schema name="companySchema" version="1.1">
  <types>  
   <fieldType name="string" class="solr.StrField" sortMissingLast="true"/>
	<fieldType name="tint" class="solr.TrieIntField" precisionStep="8" positionIncrementGap="0"/>
	<fieldType name="tfloat" class="solr.TrieFloatField" precisionStep="8" positionIncrementGap="0"/>
	<fieldType name="tlong" class="solr.TrieLongField" precisionStep="8" positionIncrementGap="0"/>
	<fieldType name="tdouble" class="solr.TrieDoubleField" precisionStep="8" positionIncrementGap="0"/>
	<fieldType name="tdate" class="solr.TrieDateField" precisionStep="8" positionIncrementGap="0"/>	
	<fieldType name="textMaxWord" class="solr.TextField" >  
      <analyzer>  
        <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="max-word"/>  
      </analyzer>  
   </fieldType>  
  </types>

 <fields>   
	<field name="id" type="tint" indexed="true" stored="true" required="true"/>
    <field name="companyName" type="textMaxWord" indexed="true" stored="true"/>
    <field name="slogan" type="string" indexed="true" stored="false"/>
	<field name="categoryId" type="tint" indexed="true" stored="true"/>		
    <field name="categoryName" type="string" indexed="false" stored="true"/>
	<field name="xcategoryId" type="tint" indexed="true" stored="true"/>		
    <field name="xcategoryName" type="string" indexed="false" stored="true"/>
	<field name="regtime" type="string" indexed="false" stored="true"/>
	<field name="invest" type="tint" indexed="true" stored="true"/>	
	<field name="shopNum" type="string" indexed="true" stored="true"/>
	<field name="url" type="string" indexed="false" stored="true"/>
	<field name="brandName" type="textMaxWord" indexed="true" stored="false"/>
	<field name="product" type="textMaxWord" indexed="true" stored="false"/>
	<field name="managementMode" type="string" indexed="false" stored="true"/>
	<field name="level" type="tint" indexed="true" stored="true"/>	
	<field name="logo" type="string" indexed="false" stored="true"/>
    <field name="createDate" type="tdate" indexed="true" stored='true'/>
	<field name="_version_" type="tlong" indexed="true"  stored="true"/>
 </fields>
 <uniqueKey>id</uniqueKey>
 <defaultSearchField>companyName</defaultSearchField>
 <solrQueryParser defaultOperator="OR"/>
</schema>

重启solr使配置生效

打开

导入数据可以看到 跟data-config.xml的配置是对应的



导入完成后查看一下数据 和是否分词





查看分词结果 在schema.xml配置文件中 只指定了companyName、brandName、product进行分词(type指定)所以只看这几个词的分词结果







到此导入mysql数据已经结束

http://www.iteye.com/topic/759148

下一篇记录定时跟新索引文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Python增量导入Oracle数据Solr,您需要遵循以下步骤: 1. 安装Solr和Python的相关库(如pysolr)。 2. 编写Python脚本,连接Oracle数据库并执行增量查询,将查询结果转换为Solr文档格式,并使用pysolr库将文档添加到Solr中。 3. 您可以使用定时任务(如cron)来定期运行Python脚本,以便进行增量导入。 下面是一个示例Python脚本,用于从Oracle数据库中查询并导入数据Solr: ``` import cx_Oracle import pysolr # Oracle数据库连接配置 dsn_tns = cx_Oracle.makedsn('localhost', '1521', 'XE') conn = cx_Oracle.connect(user='username', password='password', dsn=dsn_tns) cursor = conn.cursor() # Solr连接配置 solr = pysolr.Solr('http://localhost:8983/solr/mycore', timeout=10) # 查询增量数据 query = "SELECT * FROM mytable WHERE updated_at > :last_update" last_update = '2021-01-01 00:00:00' # 上次更新时间 cursor.execute(query, last_update=last_update) # 将查询结果转换为Solr文档格式 docs = [] for row in cursor: doc = { 'id': str(row[0]), 'title': row[1], 'content': row[2], 'updated_at': row[3].strftime('%Y-%m-%dT%H:%M:%SZ') } docs.append(doc) # 添加文档到Solr solr.add(docs) # 更新上次更新时间 last_update = datetime.now().strftime('%Y-%m-%d %H:%M:%S') # 关闭Oracle连接和Solr连接 cursor.close() conn.close() solr.commit() solr.close() ``` 此脚本将从Oracle数据库中查询更新时间大于上次更新时间的数据,并将查询结果转换为Solr文档格式。然后,使用pysolr库将文档添加到Solr中,并更新上次更新时间。您可以根据需要调整查询和文档格式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值