2013-10-29 40 views
0

我是做delta數據導入的。 我使用delete_item表獲取數據,我應該從solr索引中刪除數據。如何在完成delta-import之後查詢Solr

我該怎麼辦查詢

TRUNCATE TABLE delete_item 

執行增量導入後。

它可以用solr做,或者我應該用cron作業來做。

回答

2

沒有開箱即用,爲此配置XML解決方案。從Solr的角度來看,這是有道理的。 Solr希望自行管理,而不是管理其他數據源。但你可以做幾件事情。

就我個人而言,我會推薦(2),因爲這不包括編寫需要部署到您的solr實例的自定義代碼。因此,該解決方案可轉移到solr雲。

1.自定義事件監聽

如在此答覆中提到https://stackoverflow.com/a/9100844/2160152Solr - How can I receive notifications of failed imports from my DataImportHandler?你可以寫a custom EventListener。該監聽器可能會連接到您的數據庫並執行截斷。

import java.sql.Connection; 
import java.sql.SQLException; 

import org.apache.solr.handler.dataimport.Context; 
import org.apache.solr.handler.dataimport.EventListener; 

public class ImportEndListener implements EventListener { 

    @Override 
    public void onEvent(Context aContext) { 
     Connection connection = getConnection(); 
     try { 
      connection.createStatement() 
       .executeUpdate("TRUNCATE TABLE delete_item"); 
     } catch (SQLException e) { 
      // TODO think of something better 
      e.printStackTrace(); 
     } finally { 
      try { 
       connection.close(); 
      } catch (SQLException e) { 
       // TODO think of something better 
       e.printStackTrace(); 
      } 
     } 
    } 

    private Connection getConnection() { 
     // TODO get a connection to your database, somehow 
     return null; 
    } 

} 

該監聽器需要編譯並捆綁在一個jar文件中。然後you need to make your jar and all its' dependencies available to Solr as described in the wiki(該文章是關於插件,但適用於任何自定義代碼)。

2. Redisign「deleted_item」表

就像在博客中表示「Data Import Handler – removing data from index」你可以通過一個時間戳列deleted_at延長你的表。然後,您需要擴展onDelete觸發器以將當前時間插入到該列中。

如果你有,你可以重新制定deletedPkQuery屬性在實體如下

deletedPkQuery="SELECT id FROM deleted_item WHERE deleted_at > '${dataimporter.last_index_time}'" 

這樣就沒有必要截斷表,除非你要保存的磁盤空間。

+0

現在我正在使用第二個變體。 – yAnTar

+0

在給出答案之前,我找到了這個解決方案,而且我需要一點點。 – yAnTar