调试环境

调试环境

我使用的调试的环境是:

  • idea
  • sqoop 1.99.6

调试使用的代码

  /**
 * Created by zj on 16-6-24.
 */
import org.apache.sqoop.client.SqoopClient;
import org.apache.sqoop.model.*;
import org.apache.sqoop.validation.*;   //for status class
import org.apache.sqoop.submission.counter.*;

import java.util.Collections;
import java.util.*;

public class DT {

    //a helper function . so that user has to iterate the list of validation messages.
    // printMessage(link.getConnectorLinkConfig().getConfigs());
    private static void printMessage(List<MConfig> configs) {
        for (MConfig config : configs) {
            List<MInput<?>> inputlist = config.getInputs();
            if (config.getValidationMessages() != null) {
                // print every validation message
                for (Message message : config.getValidationMessages()) {
                    System.out.println("Config validation message: " + message.getMessage());
                }
            }
            for (MInput minput : inputlist) {
                if (minput.getValidationStatus() == Status.WARNING) {
                    for (Message message : config.getValidationMessages()) {
                        System.out.println("Config Input Validation Warning: " + message.getMessage());
                    }
                } else if (minput.getValidationStatus() == Status.ERROR) {
                    for (Message message : config.getValidationMessages()) {
                        System.out.println("Config Input Validation Error: " + message.getMessage());
                    }
                }
            }
        }
    }

    public static void main(String [] args){
        //Initialization
        String url = "http://localhost:12000/sqoop/";
        SqoopClient client = new SqoopClient(url);


        Collection<MConnector> connectorCollections=client.getConnectors();
        for(MConnector mc: connectorCollections) {
            System.out.println(mc.getUniqueName());
            System.out.println(mc.getClassName());
            System.out.println(mc.getPersistenceId());
        }

 /* I have create the link before, and the link object is persisted in the database of derby
        // create a placeholder for link
        long connectorId = 1;
        MLink link = client.createLink(connectorId);
        link.setName("mysql_link");
        link.setCreationUser("zjlearn");
        MLinkConfig linkConfig = link.getConnectorLinkConfig();

        // fill in the link config values
        linkConfig.getStringInput("linkConfig.connectionString").setValue("jdbc:mysql://localhost/my");
        linkConfig.getStringInput("linkConfig.jdbcDriver").setValue("com.mysql.jdbc.Driver");
        linkConfig.getStringInput("linkConfig.username").setValue("root");
        linkConfig.getStringInput("linkConfig.password").setValue("root");



        // save the link object that was filled
        Status status = client.saveLink(link);
        if(status.canProceed()) {
            System.out.println("Created Link with Link Id : " + link.getPersistenceId());
        } else {
            System.out.println("Something went wrong creating the link");
        }
        //create the link2
        long connectorId2 = 2;
        MLink link2 = client.createLink(connectorId2);
        link2.setName("hdfs_link");
        link2.setCreationUser("zjlearn");
        MLinkConfig linkConfig2 = link.getConnectorLinkConfig();

        // fill in the link config values ,  for hdfs connector


        // save the link object that was filled
        Status status2 = client.saveLink(link);
        if(status2.canProceed()) {
            System.out.println("Created Link with Link Id : " + link2.getPersistenceId());
        } else {
            System.out.println("Something went wrong creating the link");
        }
        */



        //create  the job
        //Creating dummy job object
        long fromLinkId = 1;// link for jdbc connector
        long toLinkId = 2; // link for HDFS connector
        MJob job = client.createJob(fromLinkId, toLinkId);
        job.setName("mysql_hafs_dt");
        job.setCreationUser("zjlearn");
        // set the "FROM" link job config values
        MFromConfig fromJobConfig = job.getFromJobConfig();
        fromJobConfig.getStringInput("fromJobConfig.schemaName").setValue("sqoop");
        fromJobConfig.getStringInput("fromJobConfig.tableName").setValue("sqoop");
        fromJobConfig.getStringInput("fromJobConfig.partitionColumn").setValue("id");
        // set the "TO" link job config values
        MToConfig toJobConfig = job.getToJobConfig();
        toJobConfig.getStringInput("toJobConfig.outputDirectory").setValue("/usr/tmp");
        // set the driver config values
        MDriverConfig driverConfig = job.getDriverConfig();
        driverConfig.getStringInput("throttlingConfig.numExtractors").setValue("3");

        Status status3 = client.saveJob(job);
        if(status3.canProceed()) {
            System.out.println("Created Job with Job Id: "+ job.getPersistenceId());
        } else {
            System.out.println("Something went wrong creating the job");
        }

        //start the job
        long jobId= job.getPersistenceId();
        MSubmission Jobsubmission = client.startJob(jobId);
        System.out.println("Job Submission Status : " + Jobsubmission.getStatus());
        if(Jobsubmission.getStatus().isRunning() && Jobsubmission.getProgress() != -1) {
            System.out.println("Progress : " + String.format("%.2f %%", Jobsubmission.getProgress() * 100));
        }
        System.out.println("Hadoop job id :" + Jobsubmission.getExternalJobId());  //the method has change  getExternalJobId
        System.out.println("Job link : " + Jobsubmission.getExternalLink());
        Counters counters = Jobsubmission.getCounters();
        if(counters != null) {
            System.out.println("Counters:");
            for(CounterGroup group : counters) {
                System.out.print("\t");
                System.out.println(group.getName());
                for(Counter counter : group) {
                    System.out.print("\t\t");
                    System.out.print(counter.getName());
                    System.out.print(": ");
                    System.out.println(counter.getValue());
                }
            }
        }
        //there is no method  getExceptionInfo in the Jobsubmission in the sqoop2 but a getErroe method
        if(Jobsubmission.getError() != null) {
            System.out.println("Exception info : " +Jobsubmission.getError());
        }

        //Check job status for a running job
        MSubmission jobStatusSubmission = client.getJobStatus(jobId);
        if(jobStatusSubmission.getStatus().isRunning() && jobStatusSubmission.getProgress() != -1) {
            System.out.println("Progress : " + String.format("%.2f %%", jobStatusSubmission.getProgress() * 100));
        }

        //Stop a running job. there is an error in the web page
        client.stopJob(jobId);

        //get the execute

    }
}

maven依赖

下面是项目使用到的依赖

<dependency>
    <groupId>org.apache.sqoop</groupId>
    <artifactId>sqoop-client</artifactId>
    <version>1.99.6</version>
</dependency>
<dependency>
    <groupId>org.apache.sqoop</groupId>
    <artifactId>sqoop-server</artifactId>
    <version>1.99.6</version>
</dependency>

代码调试方法

这部分主要参考

Sqoop2是一个CS的架构,客户端包括sqoop-shell和sqoop-client,服务器端包括sqoop-server,sqoop-server就是一个部署在Tomcat下的web应用,由几个servlet组成。

调试sqoop-shell

调试sqoop-shell就是在启动sqoop client main方法时,加上调试参数。

sqoop-shell的启动方式为:sqoop.sh client,找到解决问题的起点,查找sqoop.sh脚本,在其中找到这样一段:

  client)
    # Build class path with full path to each library
    for f in $CLIENT_LIB/*.jar; do
      CLASSPATH="${CLASSPATH}:$f"
    done

    EXEC_JAVA='java'
    if [ -n "${JAVA_HOME}" ] ; then
        EXEC_JAVA="${JAVA_HOME}/bin/java"
    fi
    ${EXEC_JAVA} -classpath ${CLASSPATH} org.apache.sqoop.shell.SqoopShell $2
    ;;

这一部分的代码是运行sqoop.sh client 命令时将会执行的代码。可以看到:

${EXEC_JAVA} -classpath ${CLASSPATH} org.apache.sqoop.shell.SqoopShell $2,

这行shell脚本就是启动sqoop客户端的main方法的地方了,在其中加入:

-Xdebug -Xnoagent-Xrunjdwp:transport=dt_socket,address=8199,server=y,suspend=n

这一行代码成了下面这样:

${EXEC_JAVA} -classpath ${CLASSPATH} -Xdebug-Xnoagent -Xrunjdwp:transport=dt_socket,address=8199,server=y,suspend=norg.apache.sqoop.shell.SqoopShell $2

** 注意,放到一行里,不要换行。Server不是指的IP。**

参数说明:
-Xdebug : 启用调试模式
-Xrunjdwp<sub-options> : 加载JVM的JPDA参考实现库
transport=dt_socket :Socket连接,可选dt_shmem 通过共享内存的方式连接到调试服务器
address=8000 :调试服务器监听的端口
server=y : 是否是服务器端,n为客户端
suspend=n : 启动过程是否加载暂停,y为启动时暂停,方便调试启动过程

参数具体的意义可以查看相关文档,IBM上有个系列的文章,是专门讲JAVA的调试体系的,叫做《深入 Java 调试体系深入 Java 调试体系深入JAVA调试体系》,讲的很细很好,相信对于很多人来说,会填补一块JAVA知识的空白。

这样就设置好了,当运行sqoop.sh client 启动sqoop shell的时候,会看到输出中包含下面的内容:

Listening for transport dt_socket ataddress: 8199

在IDE,比如Eclipse里,选择远程调试,在Eclipse中是Remote Java Application。主要是填程序所在机器的网络地址和端口号,在这个例子中,端口号就是8199。

Eclipse这端开始Debug之后,设置好断点,然后再Sqoop所在的机器启动的sqoopshell中进行操作,运行到断点,就会停住,在Eclipse这端跟调试本地程序一样。

调试sqoop-server

因为sqoop-server就是一个JavaWeb应用,所以设置sqoop-server远程调试,就是设置Tomcat为远程调试。运行Tomcat的catalina.sh命令可以看到,Tomcat已经为我们提供了jpda选项:(sqoop2的相关的catalina.sh文件在SQOOP_HOME/server/bin文件夹下面, 运行其可以得到如下的信息)

Usage: catalina.sh ( commands ... )
commands:
  debug             Start Catalina in a debugger
  debug -security   Debug Catalina with a security manager
  jpda start        Start Catalina under JPDA debugger
  run               Start Catalina in the current window
  run -security     Start in the current window with security manager
  start             Start Catalina in a separate window
  start -security   Start in a separate window with security manager
  stop              Stop Catalina, waiting up to 5 seconds for the process to end
  stop n            Stop Catalina, waiting up to n seconds for the process to end
  stop -force       Stop Catalina, wait up to 5 seconds and then use kill -KILL if still running
  stop n -force     Stop Catalina, wait up to n seconds and then use kill -KILL if still running
  version           What version of tomcat are you running?
Note: Waiting for the process to end and use of the -force option require that $CATALINA_PID is defined

由于这个内置的Tomcat的启动是由sqoop.sh脚本控制的,启动命令如下:

sqoop.sh server start

于是去查看sqoop.sh脚本的代码,找到如下部分:

 server)
    if [ $# = 1 ]; then
      echo "Usage: sqoop.sh server <start/stop>"
      exit
    fi
    actionCmd=$2

    source ${BASEDIR}/bin/sqoop-sys.sh
    setup_catalina_opts

    # There seems to be a bug in catalina.sh whereby catalina.sh doesn't respect
    # CATALINA_OPTS when stopping the tomcat server. Consequently, we have to hack around
    # by specifying the CATALINA_OPTS properties in JAVA_OPTS variable
    if [ "$actionCmd" == "stop" ]; then
      export JAVA_OPTS="$JAVA_OPTS $CATALINA_OPTS"
    fi

    # Remove the first 2 command line arguments (server and action command (start/stop)) so we can pass
    # the rest to catalina.sh script
    shift
    shift

    $CATALINA_BIN/catalina.sh $actionCmd "$@"
    ;;

从sqoop.sh server start命令来看,actionCmd就是start了,也就是说,sqoop.sh server start命令最终执行catalina.sh命令时是传入的start,即catalina.sh start。我们想运行:

catalina.sh jpda start

于是把原来

$CATALINA_BIN/catalina.sh $actionCmd "$@"

这行脚本的中直接加入jpda,最终的脚本为:

$CATALINA_BIN/catalina.sh jpda $actionCmd"$@"

上边我们设置sqoop shell 的jpda的时候,是在JVM启动时加入参数,但是从catalina.sh打印出的帮助信息来看,没有传入参数的地方,想到可能是在配置文件或者脚本中有环境变量:

最后在catalina.sh中找到下面的配置:

if [ "$1" = "jpda" ] ; then
  if [ -z "$JPDA_TRANSPORT" ]; then
    JPDA_TRANSPORT="dt_socket"
  fi
  if [ -z "$JPDA_ADDRESS" ]; then
    JPDA_ADDRESS="8000"
  fi
  if [ -z "$JPDA_SUSPEND" ]; then
    JPDA_SUSPEND="n"
  fi
  if [ -z "$JPDA_OPTS" ]; then
    JPDA_OPTS="-agentlib:jdwp=transport=$JPDA_TRANSPORT,address=$JPDA_ADDRESS,server=y,suspend=$JPDA_SUSPEND"
  fi
  CATALINA_OPTS="$CATALINA_OPTS $JPDA_OPTS"
  shift
fi

这是默认的配置,也可以自己更改。

完成上边的配置后,在Eclipse这端,调试sqoop-server项目,Remote Java Application,填上远程JVM地址,端口号,这个例子中是8000,点击debug就可以了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,445评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,889评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,047评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,760评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,745评论 5 367
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,638评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,011评论 3 398
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,669评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,923评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,655评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,740评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,406评论 4 320
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,995评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,961评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,197评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,023评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,483评论 2 342

推荐阅读更多精彩内容