remoteHadoopGem 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/remoteHadoopGem.rb +108 -0
  2. metadata +46 -0
@@ -0,0 +1,108 @@
1
+ require "uuidtools"
2
+
3
+ class RemoteHadoopGem
4
+
5
+ #
6
+ # ****** commandToHadoop ******
7
+ #
8
+ # function to run a command in a remote hadoop machine
9
+ # require:
10
+ # 1) username (to access the machine via ssh)
11
+ # 2) host (IP of the hadoop machine)
12
+ # 3) keyPathFile (path of the private key to access the machine)
13
+ # 4) command to execute
14
+
15
+ def self.commandToHadoop(username, host, keyPathFile, command)
16
+ res=`ssh -i "#{keyPathFile}" -o StrictHostKeyChecking=no "#{username}"@"#{host}" "export PATH=/hadoop/bin/:$PATH; cd /hadoop/; #{command}"`
17
+ return "#{res}"
18
+ end
19
+
20
+ #
21
+ # ****** commandToHadoopJobID ******
22
+ #
23
+ # function to run a command in a remote hadoop machine; returns the <job-id>
24
+ # require:
25
+ # 1) username (to access the machine via ssh)
26
+ # 2) host (IP of the hadoop machine)
27
+ # 3) keyPathFile (path of the private key to access the machine)
28
+ # 4) command to execute
29
+
30
+ def self.commandToHadoopJobID(username, host, keyPathFile, command)
31
+ # create a file with a random name to obtained the jobID
32
+ filename="tempJobID_"+UUIDTools::UUID.random_create.to_s
33
+ `ssh -n -f -i "#{keyPathFile}" "#{username}"@"#{host}" 'sh -c "export PATH=/hadoop/bin/:$PATH; cd /hadoop/; nohup #{command} >/dev/null 2>#{filename}.txt & " '`
34
+ res=`sleep 5; ssh -i "#{keyPathFile}" "#{username}"@"#{host}" "cat /hadoop/#{filename}.txt | grep 'Running job' " `
35
+ `ssh -i "#{keyPathFile}" "#{username}"@"#{host}" "rm /hadoop/#{filename}.txt"`
36
+ jobid=res.split[6]
37
+
38
+ return "#{jobid}"
39
+ end
40
+
41
+ #
42
+ # ****** copyFileTo ******
43
+ #
44
+ # function to copy a file in a remote hadoop machine
45
+ # require:
46
+ # 1) username (to access the machine via ssh)
47
+ # 2) host (IP of the hadoop machine)
48
+ # 3) keyPathFile (path of the private key to access the machine)
49
+ # 4) filePath (path of the local file)
50
+ # 5) destFilePath (path on the remote hadoop node)
51
+
52
+ def self.copyFileTo(username, host, keyPathFile, filePath, destFilePath)
53
+ res=`scp -r -i "#{keyPathFile}" -o StrictHostKeyChecking=no #{filePath} "#{username}"@"#{host}":#{destFilePath}`
54
+ end
55
+
56
+ #
57
+ # ****** readFileFrom ******
58
+ #
59
+ # function to read a file from a remote hadoop machine
60
+ # require:
61
+ # 1) username (to access the machine via ssh)
62
+ # 2) host (IP of the hadoop machine)
63
+ # 3) keyPathFile (path of the private key to access the machine)
64
+ # 4) destFilePath (path of the remote file to read)
65
+ #
66
+
67
+ def self.readFileFrom(username, host, keyPathFile, destFilePath)
68
+ res=`ssh -i "#{keyPathFile}" -o StrictHostKeyChecking=no "#{username}"@"#{host}" "cat #{destFilePath}"`
69
+ return "#{res}"
70
+ end
71
+
72
+ #
73
+ # ****** jobsList ******
74
+ #
75
+ # function to list the running jobs in a remote hadoop machine
76
+ # require:
77
+ # 1) username (to access the machine via ssh)
78
+ # 2) host (IP of the hadoop machine)
79
+ # 3) keyPathFile (path of the private key to access the machine)
80
+ #
81
+
82
+ def self.jobsList(username, host, keyPathFile)
83
+ res=`ssh -i "#{keyPathFile}" -o StrictHostKeyChecking=no "#{username}"@"#{host}" "export PATH=/hadoop/bin/:$PATH; cd /hadoop/; hadoop job -list "`
84
+ return "#{res}"
85
+ end
86
+
87
+ #
88
+ # ****** jobStatus ******
89
+ #
90
+ # function to read the status of a running job in a remote hadoop machine
91
+ # require:
92
+ # 1) username (to access the machine via ssh)
93
+ # 2) host (IP of the hadoop machine)
94
+ # 3) keyPathFile (path of the private key to access the machine)
95
+ # 4) job_id (id of the job)
96
+ #
97
+
98
+ def self.jobStatus(username, host, keyPathFile, job_id)
99
+ res=`ssh -i "#{keyPathFile}" -o StrictHostKeyChecking=no "#{username}"@"#{host}" "export PATH=/hadoop/bin/:$PATH; cd /hadoop/; hadoop job -status #{job_id} "`
100
+ return "#{res}"
101
+ end
102
+
103
+
104
+ def self.cleanTempFiles(username, host, keyPathFile)
105
+ `ssh -i "#{keyPathFile}" -o StrictHostKeyChecking=no "#{username}"@"#{host}" "export PATH=/hadoop/bin/:$PATH; cd /hadoop/; rm ./tempJobID_* "`
106
+ end
107
+
108
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: remoteHadoopGem
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Fraternale
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-06 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A simple test gem
15
+ email: f.seby87@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/remoteHadoopGem.rb
21
+ homepage: ''
22
+ licenses:
23
+ - MIT
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.25
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: Hola!
46
+ test_files: []