设为首页 收藏本站
查看: 649|回复: 0

[经验分享] Solr 学习(3) —-Solr 数据导入 <一>DIH简单使用

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-7-16 11:58:21 | 显示全部楼层 |阅读模式
  转载自 http://martin3000.iteye.com/blog/1328833

  使用DataImportHandler进行简单数据导入还是比较有效的,特别是DIH中针对简单的数据库表,可以把完全导入和增量导入合并成一个语句,非常方便。我的使用方式如下所示
  1。配置schema
  
  



Xml代码 http://martin3000.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=%3CrequestHandler%20name%3D%22%2Fdataimport%22%20class%3D%22org.apache.solr.handler.dataimport.DataImportHandler%22%3E%0A%20%20%20%20%3Clst%20name%3D%22defaults%22%3E%0A%20%20%20%20%20%20%3Cstr%20name%3D%22config%22%3E%2Fhome%2Ftomcat%2Fbin%2Fsolr%2Fconf%2Fdata-config.xml%3C%2Fstr%3E%0A%20%20%20%20%3C%2Flst%3E%0A%20%20%3C%2FrequestHandler%3E DSC0000.png


  •    
  •       /home/tomcat/bin/solr/conf/data-config.xml
  •    
  •   
  
  2.添加data-config文件
  data-config.xml
  
  



Xml代码 http://martin3000.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=%3CdataConfig%3E%0A%20%20%3CdataSource%20type%3D%22JdbcDataSource%22%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20driver%3D%22com.mysql.jdbc.Driver%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20url%3D%22jdbc%3Amysql%3A%2F%2F127.0.0.1%2Fdb%22%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20user%3D%22root%22%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20password%3D%22pass%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20batchSize%3D%22-1%22%2F%3E%0A%20%20%3Cdocument%3E%0A%09%3Centity%20name%3D%22id%22%20pk%3D%22id%22%20%20%0A%09%09%20%20%20%20query%3D%22select%20id%2Cusername%2Ctext%2Ccat%20%20from%20hot%20where%20


  •   
  •   
  •    
  •          
  •          
  •          
  •          
  •    
  •   

  
  3.让DIH周期性的运行
   修改dataimport.properties文件,这个是自动生成的,同在solr/conf下,添加参数
  interval 间隔时间 单位 分钟
  syncEnabled=1 打开周期运行
  params 其实就是具体调用的url,周期运行就是周期性的访问一个url
  
  



Java代码 http://martin3000.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=%23Wed%20Dec%2028%2009%3A29%3A42%20UTC%202011%0Aport%3D8983%0Ainterval%3D5%0Alast_index_time%3D2011-12-28%2009%5C%3A29%5C%3A26%0AsyncEnabled%3D1%0Awebapp%3Dsolr%0Aid.last_index_time%3D2011-12-28%2009%5C%3A29%5C%3A26%0Aserver%3D127.0.0.1%0Aparams%3D%2Fselect%3Fqt%5C%3D%2Fdataimport%26command%5C%3Dfull-import%26clean%5C%3Dfalse%26commit%5C%3Dtrue%26optimize%5C%3Dfalse

  • #Wed Dec 28 09:29:42 UTC 2011
  • port=8983
  • interval=5
  • last_index_time=2011-12-28 09\:29\:26
  • syncEnabled=1
  • webapp=solr
  • id.last_index_time=2011-12-28 09\:29\:26
  • server=127.0.0.1
  • params=/select?qt\=/dataimport&command\=full-import&clean\=false&commit\=true&optimize\=false
  
  到此还并不能周期运行,在solr的wiki中有一段实现这个功能的代码,但并没有加入到solr的发行包中,于是我们需要重新编译这段代码,打包放到webapp/solr/WEB-INF/lib中才行
  



Xml代码 http://martin3000.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=%3Cweb-app%3E%0A%20%20%20%3Clistener%3E%0A%20%20%20%20%20%20%20%3Clistener-class%3Eorg.apache.solr.handler.dataimport.scheduler.ApplicationListener%3C%2Flistener-class%3E%0A%20%20%3C%2Flistener%3E%0A%20%20...%0A%3C%2Fweb-app%3E


  •    
  •        org.apache.solr.handler.dataimport.scheduler.ApplicationListener
  •   
  •   ...

  
  
  以下是solr wiki上周期运行的代码,我已打好包,放在附件里。
  
  



Java代码 http://martin3000.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=package%20org.apache.solr.handler.dataimport.scheduler%3B%0A%0Aimport%20java.io.FileInputStream%3B%0Aimport%20java.io.FileNotFoundException%3B%0Aimport%20java.io.IOException%3B%0Aimport%20java.util.Properties%3B%0A%0Aimport%20org.apache.solr.core.SolrResourceLoader%3B%0Aimport%20org.slf4j.Logger%3B%0Aimport%20org.slf4j.LoggerFactory%3B%0A%0Apublic%20class%20SolrDataImportProperties%20%7B%0A%20%20%20%20%20%20%20%20private%20Properties%20properties%3B%0A%0A%20%20%20%20%20%20%20%20public%20static%20final%20String%20SYNC_ENABLED%20%20%20%20%20%20%20%20%20%3D%20%22syncEnabled%22%3B%0A%20%20%20%20%20%20%20%20public%20static%20final%20String%20SYNC_CORES%20%20%20%20%20%20%20%20%20%20%20%3D%20%22syncCores%22%3B%0A%20%20%20%20%20%20%20%20public%20static%20final%20String%20SERVER%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3D%20%22server%22%3B%0A%20%20%20%20%20%20%20%20public%20static%20final%20String%20PORT%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3D%20%22port%22%3B%0A%20%20%20%20%20%20%20%20public%20static%20final%20String%20WEBAPP%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3D%20%22webapp%22%3B%0A%20%20%20%20%20%20%20%20public%20static%20final%20String%20PARAMS%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3D%20%22params%22%3B%0A%20%20%20%20%20%20%20%20public%20static%20final%20String%20INTERVAL%20%20%20%20%20%20%20%20%20%20%20%20%20%3D%20%22interval%22%3B%0A%0A%20%20%20%20%20%20%20%20private%20static%20final%20Logger%20logger%20%3D%20LoggerFactory.getLogger(SolrDataImportProperties.class)%3B%0A%0A%20%20%20%20%20%20%20%20public%20SolrDataImportProperties()%7B%0A%2F%2F%20%20%20%20%20%20%20%20%20%20%20%20%20%20loadProperties(true)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20public%20void%20loadProperties(boolean%20force)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20try%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20SolrResourceLoader%20loader%20%3D%20new%20SolrResourceLoader(null)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20logger.info(%22Instance%20dir%20%3D%20%22%20%2B%20loader.getInstanceDir())%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20String%20configDir%20%3D%20loader.getConfigDir()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20configDir%20%3D%20SolrResourceLoader.normalizeDir(configDir)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if(force%20%7C%7C%20properties%20%3D%3D%20null)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20properties%20%3D%20new%20Properties()%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20String%20dataImportPropertiesPath%20%3D%20configDir%20%2B%20%22%5C%5Cdataimport.properties%22%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20FileInputStream%20fis%20%3D%20new%20FileInputStream(dataImportPropertiesPath)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20properties.load(fis)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7Dcatch(FileNotFoundException%20fnfe)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20logger.error(%22Error%20locating%20DataImportScheduler%20dataimport.properties%20file%22%2C%20fnfe)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7Dcatch(IOException%20ioe)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20logger.error(%22Error%20reading%20DataImportScheduler%20dataimport.properties%20file%22%2C%20ioe)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7Dcatch(Exception%20e)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20logger.error(%22Error%20loading%20DataImportScheduler%20properties%22%2C%20e)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20public%20String%20getProperty(String%20key)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20properties.getProperty(key)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%7D

  • package org.apache.solr.handler.dataimport.scheduler;

  • import java.io.FileInputStream;
  • import java.io.FileNotFoundException;
  • import java.io.IOException;
  • import java.util.Properties;

  • import org.apache.solr.core.SolrResourceLoader;
  • import org.slf4j.Logger;
  • import org.slf4j.LoggerFactory;

  • public class SolrDataImportProperties {
  •         private Properties properties;

  •         public static final String SYNC_ENABLED         = "syncEnabled";
  •         public static final String SYNC_CORES           = "syncCores";
  •         public static final String SERVER               = "server";
  •         public static final String PORT                 = "port";
  •         public static final String WEBAPP               = "webapp";
  •         public static final String PARAMS               = "params";
  •         public static final String INTERVAL             = "interval";

  •         private static final Logger logger = LoggerFactory.getLogger(SolrDataImportProperties.class);

  •         public SolrDataImportProperties(){
  • //              loadProperties(true);
  •         }

  •         public void loadProperties(boolean force){
  •                 try{
  •                         SolrResourceLoader loader = new SolrResourceLoader(null);
  •                         logger.info("Instance dir = " + loader.getInstanceDir());

  •                         String configDir = loader.getConfigDir();
  •                         configDir = SolrResourceLoader.normalizeDir(configDir);
  •                         if(force || properties == null){
  •                                 properties = new Properties();

  •                                 String dataImportPropertiesPath = configDir + "\\dataimport.properties";

  •                                 FileInputStream fis = new FileInputStream(dataImportPropertiesPath);
  •                                 properties.load(fis);
  •                         }
  •                 }catch(FileNotFoundException fnfe){
  •                         logger.error("Error locating DataImportScheduler dataimport.properties file", fnfe);
  •                 }catch(IOException ioe){
  •                         logger.error("Error reading DataImportScheduler dataimport.properties file", ioe);
  •                 }catch(Exception e){
  •                         logger.error("Error loading DataImportScheduler properties", e);
  •                 }
  •         }

  •         public String getProperty(String key){
  •                 return properties.getProperty(key);
  •         }
  • }
  
  
  



Java代码 http://martin3000.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=package%20org.apache.solr.handler.dataimport.scheduler%3B%0A%0Aimport%20java.util.Calendar%3B%0Aimport%20java.util.Date%3B%0Aimport%20java.util.Timer%3B%0A%0Aimport%20javax.servlet.ServletContext%3B%0Aimport%20javax.servlet.ServletContextEvent%3B%0Aimport%20javax.servlet.ServletContextListener%3B%0A%0Aimport%20org.slf4j.Logger%3B%0Aimport%20org.slf4j.LoggerFactory%3B%0A%0Apublic%20class%20ApplicationListener%20implements%20ServletContextListener%20%7B%0A%0A%20%20%20%20%20%20%20%20private%20static%20final%20Logger%20logger%20%3D%20LoggerFactory.getLogger(ApplicationListener.class)%3B%0A%0A%20%20%20%20%20%20%20%20%40Override%0A%20%20%20%20%20%20%20%20public%20void%20contextDestroyed(ServletContextEvent%20servletContextEvent)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ServletContext%20servletContext%20%3D%20servletContextEvent.getServletContext()%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20get%20our%20timer%20from%20the%20context%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Timer%20timer%20%3D%20(Timer)servletContext.getAttribute(%22timer%22)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20cancel%20all%20active%20tasks%20in%20the%20timers%20queue%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(timer%20!%3D%20null)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20timer.cancel()%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20remove%20the%20timer%20from%20the%20context%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20servletContext.removeAttribute(%22timer%22)%3B%0A%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20%40Override%0A%20%20%20%20%20%20%20%20public%20void%20contextInitialized(ServletContextEvent%20servletContextEvent)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ServletContext%20servletContext%20%3D%20servletContextEvent.getServletContext()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20try%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20create%20the%20timer%20and%20timer%20task%20objects%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Timer%20timer%20%3D%20new%20Timer()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20HTTPPostScheduler%20task%20%3D%20new%20HTTPPostScheduler(servletContext.getServletContextName()%2C%20timer)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20get%20our%20interval%20from%20HTTPPostScheduler%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20int%20interval%20%3D%20task.getIntervalInt()%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20get%20a%20calendar%20to%20set%20the%20start%20time%20(first%20run)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Calendar%20calendar%20%3D%20Calendar.getInstance()%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20set%20the%20first%20run%20to%20now%20%2B%20interval%20(to%20avoid%20fireing%20while%20the%20app%2Fserver%20is%20starting)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20calendar.add(Calendar.MINUTE%2C%20interval)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Date%20startTime%20%3D%20calendar.getTime()%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20schedule%20the%20task%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20timer.scheduleAtFixedRate(task%2C%20startTime%2C%201000%20*%2060%20*%20interval)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20save%20the%20timer%20in%20context%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20servletContext.setAttribute(%22timer%22%2C%20timer)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%20catch%20(Exception%20e)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if(e.getMessage().endsWith(%22disabled%22))%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20logger.info(%22Schedule%20disabled%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7Delse%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20logger.error(%22Problem%20initializing%20the%20scheduled%20task%3A%20%22%2C%20e)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%0A%7D

  • package org.apache.solr.handler.dataimport.scheduler;

  • import java.util.Calendar;
  • import java.util.Date;
  • import java.util.Timer;

  • import javax.servlet.ServletContext;
  • import javax.servlet.ServletContextEvent;
  • import javax.servlet.ServletContextListener;

  • import org.slf4j.Logger;
  • import org.slf4j.LoggerFactory;

  • public class ApplicationListener implements ServletContextListener {

  •         private static final Logger logger = LoggerFactory.getLogger(ApplicationListener.class);

  •         @Override
  •         public void contextDestroyed(ServletContextEvent servletContextEvent) {
  •                 ServletContext servletContext = servletContextEvent.getServletContext();

  •                 // get our timer from the context
  •                 Timer timer = (Timer)servletContext.getAttribute("timer");

  •                 // cancel all active tasks in the timers queue
  •                 if (timer != null)
  •                         timer.cancel();

  •                 // remove the timer from the context
  •                 servletContext.removeAttribute("timer");

  •         }

  •         @Override
  •         public void contextInitialized(ServletContextEvent servletContextEvent) {
  •                 ServletContext servletContext = servletContextEvent.getServletContext();
  •                 try{
  •                         // create the timer and timer task objects
  •                         Timer timer = new Timer();
  •                         HTTPPostScheduler task = new HTTPPostScheduler(servletContext.getServletContextName(), timer);

  •                         // get our interval from HTTPPostScheduler
  •                         int interval = task.getIntervalInt();

  •                         // get a calendar to set the start time (first run)
  •                         Calendar calendar = Calendar.getInstance();

  •                         // set the first run to now + interval (to avoid fireing while the app/server is starting)
  •                         calendar.add(Calendar.MINUTE, interval);
  •                         Date startTime = calendar.getTime();

  •                         // schedule the task
  •                         timer.scheduleAtFixedRate(task, startTime, 1000 * 60 * interval);

  •                         // save the timer in context
  •                         servletContext.setAttribute("timer", timer);

  •                 } catch (Exception e) {
  •                         if(e.getMessage().endsWith("disabled")){
  •                                 logger.info("Schedule disabled");
  •                         }else{
  •                                 logger.error("Problem initializing the scheduled task: ", e);
  •                         }
  •                 }
  •         }

  • }
  



Java代码 http://martin3000.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=package%20org.apache.solr.handler.dataimport.scheduler%3B%0A%0Aimport%20java.io.IOException%3B%0Aimport%20java.net.HttpURLConnection%3B%0Aimport%20java.net.MalformedURLException%3B%0Aimport%20java.net.URL%3B%0Aimport%20java.text.DateFormat%3B%0Aimport%20java.text.SimpleDateFormat%3B%0Aimport%20java.util.Date%3B%0Aimport%20java.util.Timer%3B%0Aimport%20java.util.TimerTask%3B%0A%0Aimport%20org.slf4j.Logger%3B%0Aimport%20org.slf4j.LoggerFactory%3B%0A%0A%0Apublic%20class%20HTTPPostScheduler%20extends%20TimerTask%20%7B%0A%20%20%20%20%20%20%20%20private%20String%20syncEnabled%3B%0A%20%20%20%20%20%20%20%20private%20String%5B%5D%20syncCores%3B%0A%20%20%20%20%20%20%20%20private%20String%20server%3B%0A%20%20%20%20%20%20%20%20private%20String%20port%3B%0A%20%20%20%20%20%20%20%20private%20String%20webapp%3B%0A%20%20%20%20%20%20%20%20private%20String%20params%3B%0A%20%20%20%20%20%20%20%20private%20String%20interval%3B%0A%20%20%20%20%20%20%20%20private%20String%20cores%3B%0A%20%20%20%20%20%20%20%20private%20SolrDataImportProperties%20p%3B%0A%20%20%20%20%20%20%20%20private%20boolean%20singleCore%3B%0A%0A%20%20%20%20%20%20%20%20private%20static%20final%20Logger%20logger%20%3D%20LoggerFactory.getLogger(HTTPPostScheduler.class)%3B%0A%0A%20%20%20%20%20%20%20%20public%20HTTPPostScheduler(String%20webAppName%2C%20Timer%20t)%20throws%20Exception%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2Fload%20properties%20from%20global%20dataimport.properties%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20p%20%3D%20new%20SolrDataImportProperties()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20reloadParams()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20fixParams(webAppName)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if(!syncEnabled.equals(%221%22))%20throw%20new%20Exception(%22Schedule%20disabled%22)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if(syncCores%20%3D%3D%20null%20%7C%7C%20(syncCores.length%20%3D%3D%201%20%26%26%20syncCores%5B0%5D.isEmpty()))%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20singleCore%20%3D%20true%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20logger.info(%22%3Cindex%20update%20process%3E%20Single%20core%20identified%20in%20dataimport.properties%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7Delse%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20singleCore%20%3D%20false%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20logger.info(%22%3Cindex%20update%20process%3E%20Multiple%20cores%20identified%20in%20dataimport.properties.%20Sync%20active%20for%3A%20%22%20%2B%20cores)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20private%20void%20reloadParams()%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20p.loadProperties(true)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20syncEnabled%20%3D%20p.getProperty(SolrDataImportProperties.SYNC_ENABLED)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cores%20%20%20%20%20%20%20%20%20%20%20%3D%20p.getProperty(SolrDataImportProperties.SYNC_CORES)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20server%20%20%20%20%20%20%20%20%20%20%3D%20p.getProperty(SolrDataImportProperties.SERVER)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20port%20%20%20%20%20%20%20%20%20%20%20%20%3D%20p.getProperty(SolrDataImportProperties.PORT)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20webapp%20%20%20%20%20%20%20%20%20%20%3D%20p.getProperty(SolrDataImportProperties.WEBAPP)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20params%20%20%20%20%20%20%20%20%20%20%3D%20p.getProperty(SolrDataImportProperties.PARAMS)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20interval%20%20%20%20%20%20%20%20%3D%20p.getProperty(SolrDataImportProperties.INTERVAL)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20syncCores%20%20%20%20%20%20%20%3D%20cores%20!%3D%20null%20%3F%20cores.split(%22%2C%22)%20%3A%20null%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20private%20void%20fixParams(String%20webAppName)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if(server%20%3D%3D%20null%20%7C%7C%20server.isEmpty())%20%20server%20%3D%20%22localhost%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if(port%20%3D%3D%20null%20%7C%7C%20port.isEmpty())%20%20%20%20%20%20%20%20%20%20%20%20%20%20port%20%3D%20%228080%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if(webapp%20%3D%3D%20null%20%7C%7C%20webapp.isEmpty())%20%20webapp%20%3D%20webAppName%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if(interval%20%3D%3D%20null%20%7C%7C%20interval.isEmpty()%20%7C%7C%20getIntervalInt()%20%3C%3D%200)%20interval%20%3D%20%2230%22%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20public%20void%20run()%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20try%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20check%20mandatory%20params%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if(server.isEmpty()%20%7C%7C%20webapp.isEmpty()%20%7C%7C%20params%20%3D%3D%20null%20%7C%7C%20params.isEmpty())%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20logger.warn(%22%3Cindex%20update%20process%3E%20Insuficient%20info%20provided%20for%20data%20import%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20logger.info(%22%3Cindex%20update%20process%3E%20Reloading%20global%20dataimport.properties%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20reloadParams()%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20single-core%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7Delse%20if(singleCore)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20prepUrlSendHttpPost()%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20multi-core%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7Delse%20if(syncCores.length%20%3D%3D%200%20%7C%7C%20(syncCores.length%20%3D%3D%201%20%26%26%20syncCores%5B0%5D.isEmpty()))%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20logger.warn(%22%3Cindex%20update%20process%3E%20No%20cores%20scheduled%20for%20data%20import%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20logger.info(%22%3Cindex%20update%20process%3E%20Reloading%20global%20dataimport.properties%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20reloadParams()%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7Delse%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for(String%20core%20%3A%20syncCores)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20prepUrlSendHttpPost(core)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7Dcatch(Exception%20e)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20logger.error(%22Failed%20to%20prepare%20for%20sendHttpPost%22%2C%20e)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20reloadParams()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%0A%0A%20%20%20%20%20%20%20%20private%20void%20prepUrlSendHttpPost()%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20String%20coreUrl%20%3D%20%22http%3A%2F%2F%22%20%2B%20server%20%2B%20%22%3A%22%20%2B%20port%20%2B%20%22%2F%22%20%2B%20webapp%20%2B%20params%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sendHttpPost(coreUrl%2C%20null)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20private%20void%20prepUrlSendHttpPost(String%20coreName)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20String%20coreUrl%20%3D%20%22http%3A%2F%2F%22%20%2B%20server%20%2B%20%22%3A%22%20%2B%20port%20%2B%20%22%2F%22%20%2B%20webapp%20%2B%20%22%2F%22%20%2B%20coreName%20%2B%20params%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sendHttpPost(coreUrl%2C%20coreName)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%0A%20%20%20%20%20%20%20%20private%20void%20sendHttpPost(String%20completeUrl%2C%20String%20coreName)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20DateFormat%20df%20%3D%20new%20SimpleDateFormat(%22dd.MM.yyyy%20HH%3Amm%3Ass%20SSS%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Date%20startTime%20%3D%20new%20Date()%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20prepare%20the%20core%20var%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20String%20core%20%3D%20coreName%20%3D%3D%20null%20%3F%20%22%22%20%3A%20%22%5B%22%20%2B%20coreName%20%2B%20%22%5D%20%22%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20logger.info(core%20%2B%20%22%3Cindex%20update%20process%3E%20Process%20started%20at%20..............%20%22%20%2B%20df.format(startTime))%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20try%7B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20URL%20url%20%3D%20new%20URL(completeUrl)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20HttpURLConnection%20conn%20%3D%20(HttpURLConnection)url.openConnection()%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20conn.setRequestMethod(%22POST%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20conn.setRequestProperty(%22type%22%2C%20%22%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20conn.setDoOutput(true)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Send%20HTTP%20POST%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20conn.connect()%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20logger.info(core%20%2B%20%22%3Cindex%20update%20process%3E%20Request%20method%5Ct%5Ct%5Ct%22%20%2B%20conn.getRequestMethod())%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20logger.info(core%20%2B%20%22%3Cindex%20update%20process%3E%20Succesfully%20connected%20to%20server%5Ct%22%20%2B%20server)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20logger.info(core%20%2B%20%22%3Cindex%20update%20process%3E%20Using%20port%5Ct%5Ct%5Ct%22%20%2B%20port)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20logger.info(core%20%2B%20%22%3Cindex%20update%20process%3E%20Application%20name%5Ct%5Ct%5Ct%22%20%2B%20webapp)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20logger.info(core%20%2B%20%22%3Cindex%20update%20process%3E%20URL%20params%5Ct%5Ct%5Ct%22%20%2B%20params)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20logger.info(core%20%2B%20%22%3Cindex%20update%20process%3E%20Full%20URL%5Ct%5Ct%5Ct%5Ct%22%20%2B%20conn.getURL())%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20logger.info(core%20%2B%20%22%3Cindex%20update%20process%3E%20Response%20message%5Ct%5Ct%5Ct%22%20%2B%20conn.getResponseMessage())%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20logger.info(core%20%2B%20%22%3Cindex%20update%20process%3E%20Response%20code%5Ct%5Ct%5Ct%22%20%2B%20conn.getResponseCode())%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2Flisten%20for%20change%20in%20properties%20file%20if%20an%20error%20occurs%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if(conn.getResponseCode()%20!%3D%20200)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20reloadParams()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20conn.disconnect()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20logger.info(core%20%2B%20%22%3Cindex%20update%20process%3E%20Disconnected%20from%20server%5Ct%5Ct%22%20%2B%20server)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Date%20endTime%20%3D%20new%20Date()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20logger.info(core%20%2B%20%22%3Cindex%20update%20process%3E%20Process%20ended%20at%20................%20%22%20%2B%20df.format(endTime))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7Dcatch(MalformedURLException%20mue)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20logger.error(%22Failed%20to%20assemble%20URL%20for%20HTTP%20POST%22%2C%20mue)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7Dcatch(IOException%20ioe)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20logger.error(%22Failed%20to%20connect%20to%20the%20specified%20URL%20while%20trying%20to%20send%20HTTP%20POST%22%2C%20ioe)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7Dcatch(Exception%20e)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20logger.error(%22Failed%20to%20send%20HTTP%20POST%22%2C%20e)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20public%20int%20getIntervalInt()%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20try%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20Integer.parseInt(interval)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7Dcatch(NumberFormatException%20e)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20logger.warn(%22Unable%20to%20convert%20

  • package org.apache.solr.handler.dataimport.scheduler;

  • import java.io.IOException;
  • import java.net.HttpURLConnection;
  • import java.net.MalformedURLException;
  • import java.net.URL;
  • import java.text.DateFormat;
  • import java.text.SimpleDateFormat;
  • import java.util.Date;
  • import java.util.Timer;
  • import java.util.TimerTask;

  • import org.slf4j.Logger;
  • import org.slf4j.LoggerFactory;


  • public class HTTPPostScheduler extends TimerTask {
  •         private String syncEnabled;
  •         private String[] syncCores;
  •         private String server;
  •         private String port;
  •         private String webapp;
  •         private String params;
  •         private String interval;
  •         private String cores;
  •         private SolrDataImportProperties p;
  •         private boolean singleCore;

  •         private static final Logger logger = LoggerFactory.getLogger(HTTPPostScheduler.class);

  •         public HTTPPostScheduler(String webAppName, Timer t) throws Exception{
  •                 //load properties from global dataimport.properties
  •                 p = new SolrDataImportProperties();
  •                 reloadParams();
  •                 fixParams(webAppName);

  •                 if(!syncEnabled.equals("1")) throw new Exception("Schedule disabled");

  •                 if(syncCores == null || (syncCores.length == 1 && syncCores[0].isEmpty())){
  •                         singleCore = true;
  •                         logger.info(" Single core identified in dataimport.properties");
  •                 }else{
  •                         singleCore = false;
  •                         logger.info(" Multiple cores identified in dataimport.properties. Sync active for: " + cores);
  •                 }
  •         }

  •         private void reloadParams(){
  •                 p.loadProperties(true);
  •                 syncEnabled = p.getProperty(SolrDataImportProperties.SYNC_ENABLED);
  •                 cores           = p.getProperty(SolrDataImportProperties.SYNC_CORES);
  •                 server          = p.getProperty(SolrDataImportProperties.SERVER);
  •                 port            = p.getProperty(SolrDataImportProperties.PORT);
  •                 webapp          = p.getProperty(SolrDataImportProperties.WEBAPP);
  •                 params          = p.getProperty(SolrDataImportProperties.PARAMS);
  •                 interval        = p.getProperty(SolrDataImportProperties.INTERVAL);
  •                 syncCores       = cores != null ? cores.split(",") : null;
  •         }

  •         private void fixParams(String webAppName){
  •                 if(server == null || server.isEmpty())  server = "localhost";
  •                 if(port == null || port.isEmpty())              port = "8080";
  •                 if(webapp == null || webapp.isEmpty())  webapp = webAppName;
  •                 if(interval == null || interval.isEmpty() || getIntervalInt()

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-87282-1-1.html 上篇帖子: Apache Solr初体验四 下篇帖子: solr 3.5 配置及应用(二)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表