| 41 | }); |
| 42 | |
| 43 | async function quickstart() { |
| 44 | // Create the cluster config |
| 45 | const cluster = { |
| 46 | projectId: projectId, |
| 47 | region: region, |
| 48 | cluster: { |
| 49 | clusterName: clusterName, |
| 50 | config: { |
| 51 | masterConfig: { |
| 52 | numInstances: 1, |
| 53 | machineTypeUri: 'n1-standard-2', |
| 54 | }, |
| 55 | workerConfig: { |
| 56 | numInstances: 2, |
| 57 | machineTypeUri: 'n1-standard-2', |
| 58 | }, |
| 59 | }, |
| 60 | }, |
| 61 | }; |
| 62 | |
| 63 | // Create the cluster |
| 64 | const [operation] = await clusterClient.createCluster(cluster); |
| 65 | const [response] = await operation.promise(); |
| 66 | |
| 67 | // Output a success message |
| 68 | console.log(`Cluster created successfully: ${response.clusterName}`); |
| 69 | |
| 70 | const job = { |
| 71 | projectId: projectId, |
| 72 | region: region, |
| 73 | job: { |
| 74 | placement: { |
| 75 | clusterName: clusterName, |
| 76 | }, |
| 77 | pysparkJob: { |
| 78 | mainPythonFileUri: jobFilePath, |
| 79 | }, |
| 80 | }, |
| 81 | }; |
| 82 | |
| 83 | const [jobOperation] = await jobClient.submitJobAsOperation(job); |
| 84 | const [jobResponse] = await jobOperation.promise(); |
| 85 | |
| 86 | const matches = |
| 87 | jobResponse.driverOutputResourceUri.match('gs://(.*?)/(.*)'); |
| 88 | |
| 89 | const storage = new Storage(); |
| 90 | |
| 91 | const output = await storage |
| 92 | .bucket(matches[1]) |
| 93 | .file(`${matches[2]}.000000000`) |
| 94 | .download(); |
| 95 | |
| 96 | // Output a success message. |
| 97 | console.log(`Job finished successfully: ${output}`); |
| 98 | |
| 99 | // Delete the cluster once the job has terminated. |
| 100 | const deleteClusterReq = { |