Review Board 1.7.22


SQOOP-642 Provide resource reading for both interactive and batch mode processing

Review Request #8046 - Created Nov. 14, 2012 and submitted

Jarek Cecho
SQOOP-642
Reviewers
Sqoop
sqoop-sqoop2
I've hacked main SqoopShell class to check if file $HOME/.sqoop2rc exists. If so it's interpreted before any other action.
I've tested it on real cluster.
client/src/main/java/org/apache/sqoop/client/shell/SqoopShell.java
Revision 5ac8dd2715573113583679a4ab379f488763fa80 New Change
[20] 17 lines
[+20]
18
package org.apache.sqoop.client.shell;
18
package org.apache.sqoop.client.shell;
19

    
   
19

   
20
import java.io.BufferedReader;
20
import java.io.BufferedReader;
21
import java.io.File;
21
import java.io.File;
22
import java.io.FileReader;
22
import java.io.FileReader;

    
   
23
import java.io.IOException;
23
import java.util.HashSet;
24
import java.util.HashSet;
24
import java.util.Iterator;
25
import java.util.Iterator;
25

    
   
26

   
26
import org.apache.sqoop.client.utils.ThrowableDisplayer;
27
import org.apache.sqoop.client.utils.ThrowableDisplayer;
27
import org.codehaus.groovy.runtime.MethodClosure;
28
import org.codehaus.groovy.runtime.MethodClosure;
28
import org.codehaus.groovy.tools.shell.Command;
29
import org.codehaus.groovy.tools.shell.Command;
29
import org.codehaus.groovy.tools.shell.CommandRegistry;
30
import org.codehaus.groovy.tools.shell.CommandRegistry;
30
import org.codehaus.groovy.tools.shell.Groovysh;
31
import org.codehaus.groovy.tools.shell.Groovysh;
31
import org.codehaus.groovy.tools.shell.IO.Verbosity;
32
import org.codehaus.groovy.tools.shell.IO.Verbosity;
32

    
   
33

   
33
public final class SqoopShell
34
/**
34
{
35
 * Main entry point to Sqoop client.
35
  private static final String banner =
36
 *

    
   
37
 * Sqoop shell is implemented on top of Groovy shell.

    
   
38
 */

    
   
39
public final class SqoopShell {

    
   
40

   

    
   
41
  /**

    
   
42
   * Location of resource file that can contain few initial commands that will

    
   
43
   * be loaded during each client execution.

    
   
44
   */

    
   
45
  private static final String RC_FILE = ".sqoop2rc";

    
   
46

   

    
   
47
  /**

    
   
48
   * Banner message that is displayed in interactive mode after client start.

    
   
49
   */

    
   
50
  private static final String BANNER =
36
      "@|green Sqoop Shell:|@ Type '@|bold help|@' or '@|bold \\h|@' for help.";
51
      "@|green Sqoop Shell:|@ Type '@|bold help|@' or '@|bold \\h|@' for help.";
37

    
   
52

   

    
   
53
  /**

    
   
54
   * Hash of commands that we want to have in history in all cases.

    
   
55
   */
38
  public final static HashSet<String> commandsToKeep;
56
  public final static HashSet<String> commandsToKeep;

    
   
57

   
39
  static {
58
  static {
40
    commandsToKeep = new HashSet<String>();
59
    commandsToKeep = new HashSet<String>();
41
    commandsToKeep.add("exit");
60
    commandsToKeep.add("exit");
42
    commandsToKeep.add("history");
61
    commandsToKeep.add("history");
43
  }
62
  }
44

    
   
63

   

    
   
64
  /**

    
   
65
   * Main entry point to the client execution.

    
   
66
   *

    
   
67
   * @param args Command line arguments

    
   
68
   * @throws Exception

    
   
69
   */
45
  public static void main (String[] args) throws Exception
70
  public static void main (String[] args) throws Exception
46
  {
71
  {
47
    System.setProperty("groovysh.prompt", "sqoop");
72
    System.setProperty("groovysh.prompt", "sqoop");
48
    Groovysh shell = new Groovysh();
73
    Groovysh shell = new Groovysh();
49

    
   
74

   
[+20] [20] 21 lines
[+20] public static void main (String[] args) throws Exception
71
    shell.register(new DeleteCommand(shell));
96
    shell.register(new DeleteCommand(shell));
72
    shell.register(new UpdateCommand(shell));
97
    shell.register(new UpdateCommand(shell));
73
    shell.register(new CloneCommand(shell));
98
    shell.register(new CloneCommand(shell));
74
    shell.register(new SubmissionCommand(shell));
99
    shell.register(new SubmissionCommand(shell));
75

    
   
100

   

    
   
101
    // Let's see if user do have resource file with initial commands that he

    
   
102
    // would like to apply.

    
   
103
    String homeDir = System.getProperty("user.home");

    
   
104
    File rcFile = new File(homeDir, RC_FILE);

    
   
105

   

    
   
106
    if(rcFile.exists()) {

    
   
107
      shell.getIo().out.println("Loading resource file " + RC_FILE);

    
   
108
      interpretFileContent(rcFile, shell);

    
   
109
      shell.getIo().out.println("Resource file loaded.");

    
   
110
    }

    
   
111

   
76
    if (args.length == 0) {
112
    if (args.length == 0) {
77
      // Interactive mode:
113
      // Interactive mode:
78
      shell.getIo().setVerbosity(Verbosity.QUIET);
114
      shell.getIo().setVerbosity(Verbosity.QUIET);
79
      shell.getIo().out.println(banner);
115
      shell.getIo().out.println(BANNER);
80
      shell.getIo().out.println();
116
      shell.getIo().out.println();
81
      shell.run(args);
117
      shell.run(args);
82

    
   
118

   
83
    } else {
119
    } else {
84
      // Batch mode (with a script file):
120
      // Batch mode (with a script file):
85
      File script = new File(args[0]);
121
      File script = new File(args[0]);
86
      if (!script.isAbsolute()) {
122
      if (!script.isAbsolute()) {
87
        String userDir = System.getProperty("user.dir");
123
        String userDir = System.getProperty("user.dir");
88
        script = new File(userDir, args[0]);
124
        script = new File(userDir, args[0]);
89
      }
125
      }
90

    
   
126

   

    
   
127
      interpretFileContent(script, shell);

    
   
128
    }

    
   
129
  }

    
   
130

   

    
   
131
  /**

    
   
132
   * Interpret file content in given shell.

    
   
133
   *

    
   
134
   * @param script Script file that should be interpreted

    
   
135
   * @param shell Shell where the script should be interpreted

    
   
136
   * @throws IOException

    
   
137
   */

    
   
138
  private static void interpretFileContent(File script, Groovysh shell) throws IOException {
91
      BufferedReader in = new BufferedReader(new FileReader(script));
139
    BufferedReader in = new BufferedReader(new FileReader(script));
92
      String line;
140
    String line;

    
   
141

   

    
   
142
    // Iterate over all lines and executed them one by one
93
      while ((line = in.readLine()) != null) {
143
    while ((line = in.readLine()) != null) {

    
   
144

   

    
   
145
      // Skip comments and empty lines as we don't need to interpret those

    
   
146
      if(line.isEmpty() || line.startsWith("#")) {

    
   
147
        continue;

    
   
148
      }

    
   
149

   

    
   
150
      // Render shell and command to get user perception that it was run as usual
94
        shell.getIo().out.print(shell.renderPrompt());
151
      shell.getIo().out.print(shell.renderPrompt());
95
        shell.getIo().out.println(line);
152
      shell.getIo().out.println(line);

    
   
153

   

    
   
154
      // Manually trigger command line parsing
96
        Object result = shell.execute(line);
155
      Object result = shell.execute(line);
97
        if (result != null) {
156
      if (result != null) {
98
          shell.getIo().out.println(result);
157
        shell.getIo().out.println(result);
99
        }
158
      }
100
      }
159
    }
101
    }
160
  }
102
  }

   
103

    
   
161

   
104
  private SqoopShell() {
162
  private SqoopShell() {
105
    // Instantiation of this class is prohibited
163
    // Instantiation of this class is prohibited
106
  }
164
  }
107
}
165
}
  1. client/src/main/java/org/apache/sqoop/client/shell/SqoopShell.java: Loading...