zoukankan      html  css  js  c++  java
  • java 执行 shell脚本通过mysql load data导入数据

    1:load_data_test.sh

    #!/bin/sh
    
    file=$1
    
    load_data_log=/mnt/load_data_test/load.log
    
    load_sql="LOAD DATA LOCAL INFILE '/mnt/load_data_test/${file}' 
    IGNORE INTO TABLE t_test
    CHARACTER SET utf8 
    FIELDS TERMINATED BY ','
        OPTIONALLY ENCLOSED BY ''
    LINES TERMINATED BY '
    '"
    
    mysql 2>>${load_data_log} -uroot -pxxx --local-infile=1  << EOF
    
    use app_info;
    
    $load_sql;
    
    exit
    
    EOF
    
    echo "$?"
    
    exit

    备注:$?如果为1表示脚本执行出错,为0表示脚本执行成功。

    2:java代码

        public static void main(String[] args) {
    
            String sh = System.getProperty("shell");
            
            try{
                Runtime rt = Runtime.getRuntime();
                Process proc = rt.exec(sh,null,null);
                
                InputStream stderr =  proc.getInputStream();
                InputStreamReader isr = new InputStreamReader(stderr,"UTF-8");
                BufferedReader br = new BufferedReader(isr);
                
                String line;
                while ((line = br.readLine()) != null) {
                    System.out.println("执行结果:"+line);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
    
            
        }

    3:测试

    # java -Dshell='/mnt/load_data_test/load_data_test.sh 00.txt' -jar t03.jar
    执行结果:0
  • 相关阅读:
    Admin注册和路由分发详解
    自定义Xadmin
    苑昊老师的博客
    pip 国内源 配置
    Django模型层(2)
    Django 中间件
    Django form表单
    整理的最全 python常见面试题(基本必考)
    AJAX
    Cookie、Session和自定义分页
  • 原文地址:https://www.cnblogs.com/yshyee/p/11826416.html
Copyright © 2011-2022 走看看