Skip to content

Commit efdc7d4

Browse files
shepazonrlhagerm
andauthored
Add getJobRuns and deleteTable examples for Swift (#7511)
* Add getJobRuns and deleteTable examples for Swift This PR also fixes some minor errors in the code and includes other minor adjustments. --------- Co-authored-by: Rachel Hagerman <[email protected]>
1 parent 30cf4c2 commit efdc7d4

File tree

3 files changed

+136
-23
lines changed

3 files changed

+136
-23
lines changed

.doc_gen/metadata/glue_metadata.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,15 @@ glue_GetJobRuns:
862862
- cpp.example_code.glue.client_configuration
863863
- cpp.example_code.glue.glue_client
864864
- cpp.example_code.glue.GetJobRuns
865+
Swift:
866+
versions:
867+
- sdk_version: 1
868+
github: swift/example_code/glue
869+
excerpts:
870+
- description:
871+
snippet_tags:
872+
- swift.glue.import
873+
- swift.glue.GetJobRuns
865874
services:
866875
glue: {GetJobRuns}
867876
glue_GetJobRun:
@@ -1083,6 +1092,15 @@ glue_DeleteTable:
10831092
- description:
10841093
snippet_tags:
10851094
- rust.glue.delete_table
1095+
Swift:
1096+
versions:
1097+
- sdk_version: 1
1098+
github: swift/example_code/glue
1099+
excerpts:
1100+
- description:
1101+
snippet_tags:
1102+
- swift.glue.import
1103+
- swift.glue.DeleteTable
10861104
services:
10871105
glue: {DeleteTable}
10881106
glue_DeleteDatabase:

swift/example_code/glue/README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,19 @@ Code examples that show you how to perform the essential operations within a ser
4141
Code excerpts that show you how to call individual service functions.
4242

4343
- [CreateCrawler](scenario/Sources/entry.swift#L134)
44-
- [CreateJob](scenario/Sources/entry.swift#L275)
44+
- [CreateJob](scenario/Sources/entry.swift#L282)
4545
- [DeleteCrawler](scenario/Sources/entry.swift#L178)
46-
- [DeleteDatabase](scenario/Sources/entry.swift#L463)
47-
- [DeleteJob](scenario/Sources/entry.swift#L349)
46+
- [DeleteDatabase](scenario/Sources/entry.swift#L469)
47+
- [DeleteJob](scenario/Sources/entry.swift#L356)
48+
- [DeleteTable](scenario/Sources/entry.swift#L501)
4849
- [GetCrawler](scenario/Sources/entry.swift#L220)
49-
- [GetDatabase](scenario/Sources/entry.swift#L399)
50-
- [GetJobRun](scenario/Sources/entry.swift#L557)
51-
- [GetTables](scenario/Sources/entry.swift#L422)
52-
- [ListJobs](scenario/Sources/entry.swift#L312)
50+
- [GetDatabase](scenario/Sources/entry.swift#L406)
51+
- [GetJobRun](scenario/Sources/entry.swift#L622)
52+
- [GetJobRuns](scenario/Sources/entry.swift#L585)
53+
- [GetTables](scenario/Sources/entry.swift#L429)
54+
- [ListJobs](scenario/Sources/entry.swift#L319)
5355
- [StartCrawler](scenario/Sources/entry.swift#L198)
54-
- [StartJobRun](scenario/Sources/entry.swift#L518)
56+
- [StartJobRun](scenario/Sources/entry.swift#L546)
5557

5658

5759
<!--custom.examples.start-->

swift/example_code/glue/scenario/Sources/entry.swift

Lines changed: 108 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,14 @@ struct ExampleCommand: ParsableCommand {
267267
} else if state == .stopping {
268268
return false
269269
}
270-
Thread.sleep(forTimeInterval: 4)
270+
271+
// Wait four seconds before trying again.
272+
273+
do {
274+
try await Task.sleep(for: .seconds(4))
275+
} catch {
276+
print("*** Error pausing the task.")
277+
}
271278
}
272279
}
273280
// snippet-end:[swift.glue.getCrawlerState]
@@ -459,7 +466,6 @@ struct ExampleCommand: ParsableCommand {
459466
}
460467
// snippet-end:[swift.glue.GetTables]
461468

462-
// snippet-start:[swift.glue.BatchDeleteTable]
463469
// snippet-start:[swift.glue.DeleteDatabase]
464470
/// Delete the specified database.
465471
///
@@ -486,34 +492,56 @@ struct ExampleCommand: ParsableCommand {
486492
tableNames.append(name)
487493
}
488494

489-
// Delete the tables.
490-
491-
do {
492-
_ = try await glueClient.batchDeleteTable(
493-
input: BatchDeleteTableInput(
494-
databaseName: databaseName,
495-
tablesToDelete: tableNames
495+
// Delete the tables. If there's only one table, use
496+
// `deleteTable()`, otherwise, use `batchDeleteTable()`. You can
497+
// use `batchDeleteTable()` for a single table, but this
498+
// demonstrates the use of `deleteTable()`.
499+
500+
if tableNames.count == 1 {
501+
// snippet-start:[swift.glue.DeleteTable]
502+
do {
503+
print(" Deleting table...")
504+
_ = try await glueClient.deleteTable(
505+
input: DeleteTableInput(
506+
databaseName: databaseName,
507+
name: tableNames[0]
508+
)
496509
)
497-
)
498-
} catch {
499-
print("*** Unable to delete the tables.")
510+
} catch {
511+
print("*** Unable to delete the table.")
512+
}
513+
// snippet-end:[swift.glue.DeleteTable]
514+
} else {
515+
// snippet-start:[swift.glue.BatchDeleteTable]
516+
do {
517+
print(" Deleting tables...")
518+
_ = try await glueClient.batchDeleteTable(
519+
input: BatchDeleteTableInput(
520+
databaseName: databaseName,
521+
tablesToDelete: tableNames
522+
)
523+
)
524+
} catch {
525+
print("*** Unable to delete the tables.")
526+
}
527+
// snippet-end:[swift.glue.BatchDeleteTable]
500528
}
501-
return true
502529
}
503530

504531
// Delete the database itself.
505532

506533
do {
534+
print(" Deleting the database itself...")
507535
_ = try await glueClient.deleteDatabase(
508536
input: DeleteDatabaseInput(name: databaseName)
509537
)
510538
} catch {
511539
print("*** Unable to delete the database.")
540+
return false
512541
}
513542
return true
514543
}
515544
// snippet-end:[swift.glue.DeleteDatabase]
516-
// snippet-end:[swift.glue.BatchDeleteTable]
517545

518546
// snippet-start:[swift.glue.StartJobRun]
519547
/// Start an AWS Glue job run.
@@ -554,6 +582,43 @@ struct ExampleCommand: ParsableCommand {
554582
}
555583
// snippet-end:[swift.glue.StartJobRun]
556584

585+
// snippet-start:[swift.glue.GetJobRuns]
586+
/// Return a list of the job runs for the specified job.
587+
///
588+
/// - Parameters:
589+
/// - glueClient: The AWS Glue client to use.
590+
/// - jobName: The name of the job for which to return its job runs.
591+
/// - maxResults: The maximum number of job runs to return (default:
592+
/// 1000).
593+
///
594+
/// - Returns: An array of `GlueClientTypes.JobRun` objects describing
595+
/// each job run.
596+
func getJobRuns(glueClient: GlueClient, name jobName: String, maxResults: Int? = nil) async -> [GlueClientTypes.JobRun] {
597+
do {
598+
let output = try await glueClient.getJobRuns(
599+
input: GetJobRunsInput(
600+
jobName: jobName,
601+
maxResults: maxResults
602+
)
603+
)
604+
605+
guard let jobRuns = output.jobRuns else {
606+
print("*** No job runs found.")
607+
return []
608+
}
609+
610+
return jobRuns
611+
} catch is EntityNotFoundException {
612+
print("*** The specified job name, \(jobName), doesn't exist.")
613+
return []
614+
} catch {
615+
print("*** Unexpected error getting job runs:")
616+
dump(error)
617+
return []
618+
}
619+
}
620+
// snippet-end:[swift.glue.GetJobRuns]
621+
557622
// snippet-start:[swift.glue.GetJobRun]
558623
/// Get information about a specific AWS Glue job run.
559624
///
@@ -660,6 +725,13 @@ struct ExampleCommand: ParsableCommand {
660725

661726
print("Getting the crawler's database...")
662727
let database = await getDatabase(glueClient: glueClient, name: databaseName)
728+
729+
guard let database else {
730+
print("*** Unable to get the database.")
731+
return
732+
}
733+
print("Database URI: \(database.locationUri ?? "<unknown>")")
734+
663735
let tableList = await getTablesInDatabase(glueClient: glueClient, databaseName: databaseName)
664736

665737
print("Found \(tableList.count) table(s):")
@@ -765,10 +837,31 @@ struct ExampleCommand: ParsableCommand {
765837
print("*** Warning: Job run timed out.")
766838
jobRunFinished = true
767839
default:
768-
Thread.sleep(forTimeInterval: 0.25)
840+
do {
841+
try await Task.sleep(for: .milliseconds(250))
842+
} catch {
843+
print("*** Error pausing the task.")
844+
}
769845
}
770846
} while jobRunFinished != true
771847

848+
//=====================================================================
849+
// 7.5. List the job runs for this job, showing each job run's ID and
850+
// its execution time.
851+
//=====================================================================
852+
853+
print("Getting all job runs for the job \(jobName):")
854+
let jobRuns = await getJobRuns(glueClient: glueClient, name: jobName)
855+
856+
if jobRuns.count == 0 {
857+
print(" <no job runs found>")
858+
} else {
859+
print("Found \(jobRuns.count) job runs... listing execution times:")
860+
for jobRun in jobRuns {
861+
print(" \(jobRun.id ?? "<unnamed>"): \(jobRun.executionTime) seconds")
862+
}
863+
}
864+
772865
//=====================================================================
773866
// 8. List the jobs for the user's account.
774867
//=====================================================================

0 commit comments

Comments
 (0)