Skip to content

Commit 1f07c78

Browse files
committed
Add getJobRuns and deleteTable examples for Swift
This PR also fixes some minor errors in the code and includes other minor adjustments.
1 parent ea52583 commit 1f07c78

File tree

3 files changed

+135
-23
lines changed

3 files changed

+135
-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#L621)
52+
- [GetJobRuns](scenario/Sources/entry.swift#L584)
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#L545)
5557

5658

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

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

Lines changed: 107 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,24 +492,46 @@ 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
)
@@ -513,7 +541,6 @@ struct ExampleCommand: ParsableCommand {
513541
return true
514542
}
515543
// snippet-end:[swift.glue.DeleteDatabase]
516-
// snippet-end:[swift.glue.BatchDeleteTable]
517544

518545
// snippet-start:[swift.glue.StartJobRun]
519546
/// Start an AWS Glue job run.
@@ -554,6 +581,43 @@ struct ExampleCommand: ParsableCommand {
554581
}
555582
// snippet-end:[swift.glue.StartJobRun]
556583

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

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

665736
print("Found \(tableList.count) table(s):")
@@ -765,10 +836,31 @@ struct ExampleCommand: ParsableCommand {
765836
print("*** Warning: Job run timed out.")
766837
jobRunFinished = true
767838
default:
768-
Thread.sleep(forTimeInterval: 0.25)
839+
do {
840+
try await Task.sleep(for: .milliseconds(250))
841+
} catch {
842+
print("*** Error pausing the task.")
843+
}
769844
}
770845
} while jobRunFinished != true
771846

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

0 commit comments

Comments
 (0)