Java calls Linux to execute Python crawlers and stores data in elastic search - (java background code)

Keywords: Java Session Linux ElasticSearch

This blog is mainly java code. If you need scripts and java connection elastic search tool class code, please move to the previous blog.

1. Creating Link Execution Linux Script Tool Class

package com.yjlc.platform.utils.Elasticsearch;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.StreamGobbler;

import java.io.*;
/**
 * --------------------------------------------------------------
 * CopyRights(c)2018,YJLC
 * All Rights Reserved
 * <p>
 * FileName: SingletonUtil.java
 * Description:
 * Author: cyb
 * CreateDate: 2018-11-15
 * --------------------------------------------------------------
 */
public class SingletonUtil {
    //Non parametric structure
    private SingletonUtil(){}
    private volatile static SingletonUtil instance;
    //Character encoding defaults to utf-8
    public static String  DEFAULTCHART="UTF-8";
    public static Connection conn;
    private String ip;
    private String userName;
    private String userPwd;
    public static Boolean flag=false;
//Parametric structure
    public SingletonUtil(String ip, String userName, String userPwd) {
        this.ip = ip;
        this.userName = userName;
        this.userPwd = userPwd;
    }

    public SingletonUtil getInstance(String ip, String userName, String userPwd){
        if(instance==null){
            synchronized(SingletonUtil.class){
                //Prevent multi-threading from multiple creation
                if(instance==null){
                    instance=new SingletonUtil(ip,userName, userPwd);
                }
            }
        }
        flag= instance.login();//Call the login method
        return instance;
    }
    //Sign in
    public Boolean login(){
        boolean flg=false;
        try {
            System.out.println("Access connection");
            conn = new Connection(ip);
            try {
                conn.connect();//Connect
            } catch (IOException e) {
                e.printStackTrace();
            }
            flg=conn.authenticateWithPassword(userName, userPwd);//Authentication
            if (flg){
                System.out.println("The certification is successful!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return flg;
    }

    /**
     *@description:Return in plain text format
     *@author:cyb
     *@date: 2018-11-15 16:56
    *@param: in
    *@param: charset
     *@return: java.lang.String
     */
    public static String processStdout(InputStream in, String charset){
        InputStream    stdout = new StreamGobbler(in);
        StringBuffer buffer = new StringBuffer();;
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(stdout,charset));
            String line=null;
            while((line=br.readLine()) != null){
                buffer.append(line+"\n");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer.toString();
    }
}

II. Control Layer

/**
     *@description:Open crawler
     *@author:cyb
     *@date: 2018-11-14 15:59
     *@param: id
     *@param: execute
     *@return: java.util.Map<java.lang.String,java.lang.Object>
     */
    @RequestMapping("openTask")
    @ResponseBody
    public Map<String,Object> openTask(String id,Boolean execute){
        Map<String,Object> map = new HashMap<>();
        //according to id Query Task Details
        BsKnowledgeInfoDTO  knowledgeInfoDTO=  knolegeService.getDataInfoById(id);
        if(execute==true){
            execute=false;
        }else {
            execute=true;
        }
        knowledgeInfoDTO.setExecute(execute);//Modify the status of the task (open, close)
        int k = knolegeService.updateDataInfo(knowledgeInfoDTO);
//        StringBuilder url = new StringBuilder(knowledgeInfoDTO.getPath()) ;//Reptilian Target Path
        StringBuilder url= new StringBuilder("https://mil.news.sina.com.cn/");
        StringBuilder reptileMethod= new StringBuilder("http://192.168.200.8:8000/news");//Reptile method http://192.168.200.8:8000/news
        StringBuilder themeid= new StringBuilder("hottopic");//Storage index name
       //http://192.168.200.8:8000/news?themeid=hottopic&url=https://mil.news.sina.com.cn/history/2018-11-15/doc-ihmutuec0443667.shtml
        StringBuilder path =reptileMethod.append("?").append("themid=").append(themeid).append("&").append("url=").append(url);
        String ip="192.168.200.8";//Linux Route
        String userName ="root";
        String userPwd ="yjlc20148";
        int w = knolegeService.reptile(path.toString(),ip,userName,userPwd);
        if(w==200){
            map.put("code",200);
            map.put("message","Reptilian success!");
        }else if(w==206){
            map.put("code",206);
            map.put("message","Connection failed!");
        }
        return map;
    }

3. Service layer (service interface layer is omitted here)

/**
 *@description: Reptile
 *@author:cyb
 *@date: 2018-11-15 20:52
*@param: path Crawler method path + ES storage index + crawler target url set
*@param: ip Connect ip address
*@param: userName : User name
*@param: userPwd: User password
 *@return: int
 */
@Override
public int reptile(String path,String ip,String userName,String userPwd) {
    SingletonUtil singletonUtil = new SingletonUtil("192.168.200.8", "root","yjlc20148");
    singletonUtil.getInstance(ip, userName,userPwd);
    Boolean b =SingletonUtil.flag;//See if the connection is successful
    if(b==true){
        System.out.println("=====First step=====");
        Session session= null;//Open a session
        try {
            session = singletonUtil.conn.openSession();
            session.execCommand("sh /opt/zc/linux_sina.sh");//Executive order
        } catch (IOException e) {
            e.printStackTrace();
        }
        //TODO:Multiple commands
        String result=singletonUtil.processStdout(session.getStdout(),singletonUtil.DEFAULTCHART);
        //If the standard output is empty, the script is executed incorrectly.
        if(StringUtils.isBlank(result)){
            System.out.println("Script error");
           result=singletonUtil.processStdout(session.getStderr(),singletonUtil.DEFAULTCHART);
        }
        System.out.println("The first step script runs successfully"+result);
        ConnectNetworkUtil connectNetworkUtil = new ConnectNetworkUtil();
        connectNetworkUtil.ConnectNetwork(path);
        System.out.println("Acquisition success!");
        session.close();//Close session
        singletonUtil.conn.close();//The crawler closes the connection
        return 200;//Successful crawler
    }else {
        return 206;//connection failed
    }

}

 

The above code has omitted the service interface layer and the java connection elastic search tool class (as mentioned in the previous blog). The above code is only for reference. If there are unreasonable or irregular parts in the code, please point out that the technology lies in communication!

Posted by asgsoft on Tue, 22 Jan 2019 14:12:13 -0800