java 從Oracle數據庫到處數據到Elasticsearch全文檢索庫進行全文查詢


首先編寫代碼前,要先把Elasticsearch環境搭建好(這個很簡單,網上百度一大堆)。然后將elasticsearch jar包導入工程當中。

之后開始編碼工作:

第一步:編寫連接本地Elasticsearch環境的代碼

public static Client client=null;
    public static Client getClient(){
       if(client !=null){
           return client;
       }
       Settings settings = Settings.settingsBuilder().put("cluster.name","my-application").build();
       try{
           client = TransportClient.builder().settings(settings).build()
                   .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(null),9300));//如果是Null,則默認連接本地Ip
       }catch(UnknownHostException e){
           e.printStackTrace();
       }
       return client;
    }

第二步,建立索引庫和創建索引

public void createIndexResponse(String indexname, String type, List<String> jsondata){
        //創建索引庫 需要注意的是.setRefresh(true)這里一定要設置,否則第一次建立索引查找不到數據
        IndexRequestBuilder requestBuilder = getClient().prepareIndex(indexname, type).setRefresh(true);
        for(int i=0; i<jsondata.size(); i++){
            requestBuilder.setSource(jsondata.get(i)).execute().actionGet();
        }     
         
    }
    
    /**
     * 創建索引
     * @param client
     * @param jsondata
     * @return
     */
    public IndexResponse createIndexResponse(String indexname, String type,String jsondata){
        IndexResponse response = getClient().prepareIndex(indexname, type)
            .setSource(jsondata)
            .execute()
            .actionGet();
        return response;
    }
    
    /**
     * 執行搜索
     * @param queryBuilder
     * @param indexname
     * @param type
     * @return
     */
    public List<EsBean>  searcher(QueryBuilder queryBuilder, String indexname, String type){
        List<EsBean> list = new ArrayList<EsBean>();//EsBean是數據庫的字段
        SearchResponse searchResponse = getClient().prepareSearch(indexname).setTypes(type)
        .setQuery(queryBuilder)
        .execute()
        .actionGet();
        SearchHits hits = searchResponse.getHits();
        System.out.println("查詢到記錄數=" + hits.getTotalHits());
        SearchHit[] searchHists = hits.getHits();
        if(searchHists.length>0){
            for(SearchHit hit:searchHists){
                Integer id = (Integer)hit.getSource().get("id");
                String name =  (String) hit.getSource().get("name");
                String function =  (String) hit.getSource().get("funciton");
                list.add(new EsBean(id, name, function));
            }
        }
        return list;
    }

第三步,寫入實體類Esbean

public class Esbean{
private String reg_id;
private String organization;


省略setter和getter方法;
public EsBean(String reg_id,String organization){
super();
this.reg_id=reg_id;
this.organization=organization;
}
}

第四步,從oracle數據庫中查詢出數據導入到Esalticsearch庫中

 1 public class jdbc{
 2       public  static jdbc dataFactory = new jdbc();
 3       private static Connection conn;
 4       static final String username="****";
 5       static final String password="*****";
 6       static final String url="***********";
 7       public static synchronized Connection getInstance(){
 8                 if(conn=null || conn.isClosed()){
 9                       class.forName("oracle.jdbc.driver.OracleDriver");
10                    conn=DriverManager.getConnection(url,username,password);
11               }
12               return conn;
13            }
14 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM