Chilkat Online Tools

BatchGetQueryExecution Swift3 Example

Athena

func chilkatTest() {
    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    let rest = CkoRest()!
    var success: Bool

    let authAws = CkoAuthAws()!
    authAws.accessKey = "AWS_ACCESS_KEY"
    authAws.secretKey = "AWS_SECRET_KEY"

    // Don't forget to change the region to your particular region. (Also make the same change in the call to Connect below.)
    authAws.region = "us-west-2"
    authAws.serviceName = "athena"
    // SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
    rest.setAuthAws(authAws)

    // URL: https://athena.us-west-2.amazonaws.com/
    // Use the same region as specified above.
    success = rest.connect("athena.us-west-2.amazonaws.com", port: 443, tls: true, autoReconnect: true)
    if success != true {
        print("ConnectFailReason: \(rest.connectFailReason.intValue)")
        print("\(rest.lastErrorText!)")
        return
    }

    // The following code creates the JSON request body.
    // The JSON created by this code is shown below.

    // Use this online tool to generate code from sample JSON:
    // Generate Code to Create JSON

    let json = CkoJsonObject()!
    json.update("QueryExecutionIds[0]", value: "string")

    // The JSON request body created by the above code:

    // {
    //   "QueryExecutionIds": [
    //     "string"
    //   ]
    // }

    rest.addHeader("Content-Type", value: "application/x-amz-json-1.1")
    rest.addHeader("X-Amz-Target", value: "AmazonAthena.BatchGetQueryExecution")

    let sbRequestBody = CkoStringBuilder()!
    json.emitSb(sbRequestBody)
    let sbResponseBody = CkoStringBuilder()!
    success = rest.fullRequestSb("POST", uriPath: "/", requestBody: sbRequestBody, responseBody: sbResponseBody)
    if success != true {
        print("\(rest.lastErrorText!)")
        return
    }

    var respStatusCode: Int = rest.responseStatusCode.intValue
    print("response status code = \(respStatusCode)")
    if respStatusCode != 200 {
        print("Response Header:")
        print("\(rest.responseHeader!)")
        print("Response Body:")
        print("\(sbResponseBody.getAsString()!)")
        return
    }

    let jResp = CkoJsonObject()!
    jResp.loadSb(sbResponseBody)

    // The following code parses the JSON response.
    // A sample JSON response is shown below the sample code.

    // Use this online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    var EffectiveEngineVersion: String?
    var SelectedEngineVersion: String?
    var Query: String?
    var Catalog: String?
    var Database: String?
    var QueryExecutionId: String?
    var S3AclOption: String?
    var EncryptionOption: String?
    var KmsKey: String?
    var ExpectedBucketOwner: String?
    var OutputLocation: String?
    var StatementType: String?
    var DataManifestLocation: String?
    var DataScannedInBytes: Int
    var EngineExecutionTimeInMillis: Int
    var QueryPlanningTimeInMillis: Int
    var QueryQueueTimeInMillis: Int
    var ServiceProcessingTimeInMillis: Int
    var TotalExecutionTimeInMillis: Int
    var ErrorCategory: Int
    var ErrorMessage: String?
    var ErrorType: Int
    var Retryable: Int
    var CompletionDateTime: Int
    var State: String?
    var StateChangeReason: String?
    var SubmissionDateTime: Int
    var WorkGroup: String?
    var ErrorCode: String?

    var i: Int = 0
    var count_i: Int = jResp.size(ofArray: "QueryExecutions").intValue
    while i < count_i {
        jResp.i = i
        EffectiveEngineVersion = jResp.string(of: "QueryExecutions[i].EngineVersion.EffectiveEngineVersion")
        SelectedEngineVersion = jResp.string(of: "QueryExecutions[i].EngineVersion.SelectedEngineVersion")
        Query = jResp.string(of: "QueryExecutions[i].Query")
        Catalog = jResp.string(of: "QueryExecutions[i].QueryExecutionContext.Catalog")
        Database = jResp.string(of: "QueryExecutions[i].QueryExecutionContext.Database")
        QueryExecutionId = jResp.string(of: "QueryExecutions[i].QueryExecutionId")
        S3AclOption = jResp.string(of: "QueryExecutions[i].ResultConfiguration.AclConfiguration.S3AclOption")
        EncryptionOption = jResp.string(of: "QueryExecutions[i].ResultConfiguration.EncryptionConfiguration.EncryptionOption")
        KmsKey = jResp.string(of: "QueryExecutions[i].ResultConfiguration.EncryptionConfiguration.KmsKey")
        ExpectedBucketOwner = jResp.string(of: "QueryExecutions[i].ResultConfiguration.ExpectedBucketOwner")
        OutputLocation = jResp.string(of: "QueryExecutions[i].ResultConfiguration.OutputLocation")
        StatementType = jResp.string(of: "QueryExecutions[i].StatementType")
        DataManifestLocation = jResp.string(of: "QueryExecutions[i].Statistics.DataManifestLocation")
        DataScannedInBytes = jResp.int(of: "QueryExecutions[i].Statistics.DataScannedInBytes").intValue
        EngineExecutionTimeInMillis = jResp.int(of: "QueryExecutions[i].Statistics.EngineExecutionTimeInMillis").intValue
        QueryPlanningTimeInMillis = jResp.int(of: "QueryExecutions[i].Statistics.QueryPlanningTimeInMillis").intValue
        QueryQueueTimeInMillis = jResp.int(of: "QueryExecutions[i].Statistics.QueryQueueTimeInMillis").intValue
        ServiceProcessingTimeInMillis = jResp.int(of: "QueryExecutions[i].Statistics.ServiceProcessingTimeInMillis").intValue
        TotalExecutionTimeInMillis = jResp.int(of: "QueryExecutions[i].Statistics.TotalExecutionTimeInMillis").intValue
        ErrorCategory = jResp.int(of: "QueryExecutions[i].Status.AthenaError.ErrorCategory").intValue
        ErrorMessage = jResp.string(of: "QueryExecutions[i].Status.AthenaError.ErrorMessage")
        ErrorType = jResp.int(of: "QueryExecutions[i].Status.AthenaError.ErrorType").intValue
        Retryable = jResp.int(of: "QueryExecutions[i].Status.AthenaError.Retryable").intValue
        CompletionDateTime = jResp.int(of: "QueryExecutions[i].Status.CompletionDateTime").intValue
        State = jResp.string(of: "QueryExecutions[i].Status.State")
        StateChangeReason = jResp.string(of: "QueryExecutions[i].Status.StateChangeReason")
        SubmissionDateTime = jResp.int(of: "QueryExecutions[i].Status.SubmissionDateTime").intValue
        WorkGroup = jResp.string(of: "QueryExecutions[i].WorkGroup")
        i = i + 1
    }

    i = 0
    count_i = jResp.size(ofArray: "UnprocessedQueryExecutionIds").intValue
    while i < count_i {
        jResp.i = i
        ErrorCode = jResp.string(of: "UnprocessedQueryExecutionIds[i].ErrorCode")
        ErrorMessage = jResp.string(of: "UnprocessedQueryExecutionIds[i].ErrorMessage")
        QueryExecutionId = jResp.string(of: "UnprocessedQueryExecutionIds[i].QueryExecutionId")
        i = i + 1
    }

    // A sample JSON response body parsed by the above code:

    // {
    //   "QueryExecutions": [
    //     {
    //       "EngineVersion": {
    //         "EffectiveEngineVersion": "string",
    //         "SelectedEngineVersion": "string"
    //       },
    //       "Query": "string",
    //       "QueryExecutionContext": {
    //         "Catalog": "string",
    //         "Database": "string"
    //       },
    //       "QueryExecutionId": "string",
    //       "ResultConfiguration": {
    //         "AclConfiguration": {
    //           "S3AclOption": "string"
    //         },
    //         "EncryptionConfiguration": {
    //           "EncryptionOption": "string",
    //           "KmsKey": "string"
    //         },
    //         "ExpectedBucketOwner": "string",
    //         "OutputLocation": "string"
    //       },
    //       "StatementType": "string",
    //       "Statistics": {
    //         "DataManifestLocation": "string",
    //         "DataScannedInBytes": number,
    //         "EngineExecutionTimeInMillis": number,
    //         "QueryPlanningTimeInMillis": number,
    //         "QueryQueueTimeInMillis": number,
    //         "ServiceProcessingTimeInMillis": number,
    //         "TotalExecutionTimeInMillis": number
    //       },
    //       "Status": {
    //         "AthenaError": {
    //           "ErrorCategory": number,
    //           "ErrorMessage": "string",
    //           "ErrorType": number,
    //           "Retryable": boolean
    //         },
    //         "CompletionDateTime": number,
    //         "State": "string",
    //         "StateChangeReason": "string",
    //         "SubmissionDateTime": number
    //       },
    //       "WorkGroup": "string"
    //     }
    //   ],
    //   "UnprocessedQueryExecutionIds": [
    //     {
    //       "ErrorCode": "string",
    //       "ErrorMessage": "string",
    //       "QueryExecutionId": "string"
    //     }
    //   ]
    // }

}