| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 
 | package com.winterchen.hadoopdemo.service.impl;
 import cn.hutool.core.date.DateUtil;
 import com.winterchen.hadoopdemo.constants.OozieConstants;
 import com.winterchen.hadoopdemo.enums.FrequencyTypeEnum;
 import com.winterchen.hadoopdemo.enums.TaskTypeEnum;
 import com.winterchen.hadoopdemo.model.CoordinatorRequest;
 import com.winterchen.hadoopdemo.model.OozieConfig;
 import com.winterchen.hadoopdemo.model.WorkflowRequest;
 import com.winterchen.hadoopdemo.service.OozieService;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
 import org.apache.oozie.client.*;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.util.StringUtils;
 
 import java.io.IOException;
 import java.io.OutputStreamWriter;
 import java.io.Writer;
 import java.util.Properties;
 
 
 
 
 
 
 
 @Slf4j
 @Service
 public class OozieServiceImpl implements OozieService {
 
 @Autowired
 private FileSystem fileSystem;
 
 private final OozieConfig oozieConfig;
 
 @Autowired
 public OozieServiceImpl(OozieConfig oozieConfig) {
 this.oozieConfig = oozieConfig;
 }
 
 @Override
 public String submitWorkflow(WorkflowRequest workflowRequest) {
 try {
 OozieClient oozieClient = new OozieClient(oozieConfig.getUrl());
 oozieClient.setDebugMode(1);
 Path appPath = new Path(fileSystem.getHomeDirectory(), workflowRequest.getWfPath().concat(workflowRequest.getWfName()).concat(".xml"));
 
 
 
 String sqlPath = workflowRequest.getWfPath().concat("sql/".concat(workflowRequest.getWfName()).concat("-sql.q"));
 createSqlFileAndUpload(workflowRequest.getSql(), sqlPath);
 
 
 String shellFileName = workflowRequest.getWfName() + "-shell.sh";
 String shellFilePath = workflowRequest.getWfPath().concat(workflowRequest.getWfName()).concat("/shell/");
 String shellPath = createShellFileAndUpload(shellFileName, shellFilePath);
 
 
 createWfFileAndUpload(workflowRequest.getWfName(), workflowRequest.getWfPath(), sqlPath, workflowRequest.getCallbackId());
 
 
 Properties prop = oozieClient.createConfiguration();
 prop.setProperty(OozieClient.APP_PATH, appPath.toString());
 prop.setProperty(oozieClient.LIBPATH, oozieConfig.getOozieLibPath());
 prop.setProperty(oozieClient.USE_SYSTEM_LIBPATH, String.valueOf(oozieConfig.isOozieSystemLibPath()));
 
 
 prop.setProperty(OozieConstants.NAME_NODE, oozieConfig.getNameNode());
 prop.setProperty(OozieConstants.JOB_TRACKER,oozieConfig.getJobTracker());
 Path outputPath = new Path(fileSystem.getHomeDirectory(), workflowRequest.getWfPath().concat("output/"));
 prop.setProperty(OozieConstants.JOB_OUTPUT, outputPath.toString());
 prop.setProperty(OozieConstants.JDBC_URL, oozieConfig.getJdbcUrl());
 prop.setProperty(OozieConstants.PASSWORD, StringUtils.isEmpty(oozieConfig.getPassword()) ? "" : oozieConfig.getPassword());
 prop.setProperty(OozieConstants.SQL_INPUT,workflowRequest.getWfPath().concat("sql/"));
 prop.setProperty(OozieConstants.USER_NAME,"admin");
 prop.setProperty(OozieConstants.TASK_TYPE, TaskTypeEnum.WORKFLOW.name());
 prop.setProperty(OozieConstants.SHELL_FILE_NAME,shellFileName);
 prop.setProperty(OozieConstants.SHELL_FILE_PATH, shellPath);
 prop.setProperty(OozieConstants.CALLBACK_ID, workflowRequest.getCallbackId());
 prop.setProperty(OozieConstants.QUEUE_NAME, oozieConfig.getQueueName());
 
 String jobId = oozieClient.submit(prop);
 oozieClient.start(jobId);
 
 log.debug("workflow job submitted, jobId = {}", jobId);
 
 return jobId;
 } catch (OozieClientException e) {
 log.error("workflow任务提交失败" ,e);
 }
 
 return null;
 }
 
 @Override
 public String submitCoordinator(CoordinatorRequest coordinatorRequest) {
 
 try {
 OozieClient oozieClient = new OozieClient(oozieConfig.getUrl());
 oozieClient.setDebugMode(1);
 Path rootPath = new Path(fileSystem.getHomeDirectory(), coordinatorRequest.getCoordPath());
 Path appPath = new Path(fileSystem.getHomeDirectory(), coordinatorRequest.getCoordPath()
 .concat(coordinatorRequest.getCoordName()).concat(".xml"));
 Path wf = new Path(fileSystem.getHomeDirectory(), coordinatorRequest.getWfPath());
 
 
 createCoordFileAndUpload(coordinatorRequest.getCoordName(),coordinatorRequest.getCoordPath(),
 wf.toString().concat("/").concat(coordinatorRequest.getWfName()).concat(".xml"),coordinatorRequest.getFrequencyType(), coordinatorRequest.getCallbackId());
 
 
 String shellFileName = coordinatorRequest.getWfName() + "-shell.sh";
 String shellFilePath = coordinatorRequest.getWfPath().concat(coordinatorRequest.getWfName()).concat("/shell/");
 String shellPath = createShellFileAndUpload(shellFileName, shellFilePath);
 
 
 Properties prop = oozieClient.createConfiguration();
 prop.setProperty(OozieClient.COORDINATOR_APP_PATH, appPath.toString());
 prop.setProperty(oozieClient.LIBPATH, oozieConfig.getOozieLibPath());
 prop.setProperty(oozieClient.USE_SYSTEM_LIBPATH, String.valueOf(oozieConfig.isOozieSystemLibPath()));
 prop.setProperty(OozieConstants.JOB_TRACKER,oozieConfig.getJobTracker());
 prop.setProperty(OozieConstants.USER_NAME,"admin");
 prop.setProperty(OozieConstants.WORKFLOW_ROOT, rootPath.toString());
 String start = DateUtil.format(DateUtil.parse(coordinatorRequest.getStartTime(), "yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd'T'HH:mm'Z'");
 prop.setProperty(OozieConstants.START, start);
 String end = DateUtil.format(DateUtil.parse(coordinatorRequest.getEndTime(), "yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd'T'HH:mm'Z'");
 prop.setProperty(OozieConstants.END, end);
 Path outputPath = new Path(fileSystem.getHomeDirectory(), coordinatorRequest.getWfPath().concat("output/"));
 prop.setProperty(OozieConstants.JOB_OUTPUT, outputPath.toString());
 prop.setProperty(OozieConstants.JDBC_URL, oozieConfig.getJdbcUrl());
 prop.setProperty(OozieConstants.PASSWORD, StringUtils.isEmpty(oozieConfig.getPassword()) ? "" : oozieConfig.getPassword());
 prop.setProperty(OozieConstants.SQL_INPUT,coordinatorRequest.getWfPath().concat("sql/"));
 prop.setProperty(OozieConstants.TASK_TYPE, TaskTypeEnum.COORDINATOR.name());
 prop.setProperty(OozieConstants.SHELL_FILE_NAME,shellFileName);
 prop.setProperty(OozieConstants.SHELL_FILE_PATH, shellPath);
 prop.setProperty(OozieConstants.CALLBACK_ID, coordinatorRequest.getCallbackId());
 prop.setProperty(OozieConstants.QUEUE_NAME, oozieConfig.getQueueName());
 
 
 prop.setProperty(OozieConstants.NAME_NODE, oozieConfig.getNameNode());
 
 String jobId = oozieClient.submit(prop);
 
 log.debug("workflow job submitted, jobId = {}", jobId);
 
 return jobId;
 } catch (OozieClientException e) {
 log.error("workflow任务提交失败" ,e);
 }
 
 return null;
 }
 
 @Override
 public String createSqlFileAndUpload(String sql, String sqlPath) {
 Writer writer = null;
 try {
 Path sqlP = new Path(fileSystem.getHomeDirectory(),sqlPath);
 writer = new OutputStreamWriter(fileSystem.create(sqlP));
 
 writer.write(sql);
 return sqlP.toString();
 } catch (IOException e) {
 log.error("创建sql文件失败", e);
 } finally {
 if (null != writer) {
 try {
 writer.close();
 } catch (IOException e) {
 log.error("关闭流失败", e);
 }
 }
 }
 return null;
 }
 
 @Override
 public String createWfFileAndUpload(String wfName, String wfPath, String sqlFileName, String callbackId) {
 Writer writer = null;
 try {
 Path wf = new Path(fileSystem.getHomeDirectory(),wfPath.concat(wfName).concat(".xml"));
 writer = new OutputStreamWriter(fileSystem.create(wf));
 String wfApp =
 "<workflow-app xmlns='uri:oozie:workflow:0.4' name='" + wfName + "'>\n" +
 "    <start to='my-hive2-action'/>\n" +
 "    <action name='my-hive2-action'>\n" +
 "       <hive2 xmlns='uri:oozie:hive2-action:0.1'>\n" +
 "           <name-node>${nameNode}</name-node>\n" +
 "           <prepare>\n" +
 "               <delete path='${jobOutput}'/>\n" +
 "           </prepare>\n" +
 "           <configuration>\n" +
 "                <property>\n" +
 "                    <name>mapred.compress.map.output</name>\n" +
 "                    <value>true</value>\n" +
 "                </property>\n" +
 "           </configuration>\n" +
 "           <jdbc-url>${jdbcUrl}</jdbc-url>\n" +
 
 "           <script>" + sqlFileName + "</script>\n" +
 "           <param>InputDir=${sqlInput}</param>\n" +
 "           <param>OutputDir=${jobOutput}</param>\n" +
 "       </hive2>\n" +
 "    <ok to='success-action'/>\n" +
 "    <error to='error-action'/>\n" +
 "    </action>\n" +
 "    <!-- 成功回调 -->\n" +
 "    <action name='success-action'>\n" +
 "        <shell xmlns=\"uri:oozie:shell-action:0.2\">\n" +
 "            <job-tracker>${jobTracker}</job-tracker>\n" +
 "            <name-node>${nameNode}</name-node>\n" +
 "            <configuration>\n" +
 "                <property>\n" +
 "                  <name>mapred.job.queue.name</name>\n" +
 "                  <value>${queueName}</value>\n" +
 "                </property>\n" +
 "            </configuration>\n" +
 "            <exec>${shellFileName}</exec>\n" +
 "            <argument>${taskType}</argument>\n" +
 "            <argument>OK</argument>\n" +
 "            <argument>${callbackId}</argument>\n" +
 "            <file>${shellFilePath}#${shellFilePath}</file> <!--Copy the executable to compute node's current working directory -->\n" +
 "        </shell>\n" +
 "        <ok to='end' />\n" +
 "        <error to='fail' />\n" +
 "    </action>\n" +
 "     \n" +
 "    <!-- 失败回调 -->\n" +
 "    <action name='error-action'>\n" +
 "        <shell xmlns=\"uri:oozie:shell-action:0.2\">\n" +
 "            <job-tracker>${jobTracker}</job-tracker>\n" +
 "            <name-node>${nameNode}</name-node>\n" +
 "            <configuration>\n" +
 "                <property>\n" +
 "                  <name>mapred.job.queue.name</name>\n" +
 "                  <value>${queueName}</value>\n" +
 "                </property>\n" +
 "            </configuration>\n" +
 "            <exec>${shellFileName}</exec>\n" +
 "            <argument>${taskType}</argument>\n" +
 "            <argument>FAIL</argument>\n" +
 "            <argument>${callbackId}</argument>\n" +
 "            <file>${shellFilePath}#${shellFilePath}</file> <!--Copy the executable to compute node's current working directory -->\n" +
 "        </shell>\n" +
 "        <ok to='end' />\n" +
 "        <error to='fail' />\n" +
 "    </action>\n" +
 "    <kill name='fail'>\n" +
 "        <message>执行脚本失败</message>\n" +
 "    </kill>\n" +
 "    <end name='end'/>\n"   +
 "</workflow-app>";
 writer.write(wfApp);
 return wf.toString();
 } catch (IOException e) {
 log.error("创建workflow文件失败", e);
 } finally {
 if (null != writer) {
 try {
 writer.close();
 } catch (IOException e) {
 log.error("关闭流失败", e);
 }
 }
 }
 return null;
 }
 
 @Override
 public String createCoordFileAndUpload(String coordName, String coordPath, String wfPath, FrequencyTypeEnum frequencyType, String callbackId) {
 Writer writer = null;
 try {
 Path coord = new Path(fileSystem.getHomeDirectory(),coordPath.concat(coordName).concat(".xml"));
 writer = new OutputStreamWriter(fileSystem.create(coord));
 String frequency = FrequencyTypeEnum.getExpressionByName(frequencyType.name(), 1);
 String wfApp =
 "<coordinator-app name='" + coordName + "' frequency='" + frequency + "' start='${start}' end='${end}' timezone='Asia/Shanghai'\n" +
 "                 xmlns='uri:oozie:coordinator:0.4'>\n" +
 "        <action>\n" +
 "        <workflow>\n" +
 "            <app-path>" + wfPath + "</app-path>\n" +
 "        </workflow>\n" +
 "    </action>\n" +
 "</coordinator-app>";
 writer.write(wfApp);
 return coordName.toString();
 } catch (IOException e) {
 log.error("创建coordinator文件失败", e);
 } finally {
 if (null != writer) {
 try {
 writer.close();
 } catch (IOException e) {
 log.error("关闭流失败", e);
 }
 }
 }
 return null;
 }
 
 @Override
 public String createShellFileAndUpload(String shellFileName, String shellFilePath) {
 Writer writer = null;
 try {
 Path shellPath = new Path(fileSystem.getHomeDirectory(),shellFilePath.concat(shellFileName));
 writer = new OutputStreamWriter(fileSystem.create(shellPath));
 String shell =
 "#!/bin/bash\n" +
 "echo 'curl " + oozieConfig.getCallbackUrl() + "';\n" +
 "curl -X GET " + oozieConfig.getCallbackUrl();
 writer.write(shell);
 return shellPath.toString();
 } catch (IOException e) {
 log.error("创建shell文件失败", e);
 } finally {
 if (null != writer) {
 try {
 writer.close();
 } catch (IOException e) {
 log.error("关闭流失败", e);
 }
 }
 }
 return null;
 }
 
 @Override
 public void executeCallback(String executeType, String taskType, String callbackId) {
 
 log.info("回调处理,executeType={}, taskType={}, callbackId={}", executeType, taskType, callbackId);
 }
 
 @Override
 public void killCoordinatorJob(String jobId) {
 OozieClient oozieClient = new OozieClient(oozieConfig.getUrl());
 oozieClient.setDebugMode(1);
 try {
 oozieClient.kill(jobId);
 } catch (OozieClientException e) {
 log.error("停止定时任务失败", e);
 }
 }
 }
 
 |