A BigQuery client for Swift.
Add this to your Package.swift:
.package(url: "https://github.com/oliveroneill/BigQuerySwift.git", .upToNextMajor(from: "0.0.1")),
To get an authentication from your service account:
let provider = BigQueryAuthProvider()
try! provider.getAuthenticationToken { response in
switch response {
case .token(let token):
// Your token to be passed into BigQueryClient
print(token)
case .error(_):
fatalError("Something went wrong.")
}
}
You'll need to copy your credentials file at $GOOGLE_APPLICATION_CREDENTIALS
to credentials.json
in the directory that the binary is run in.
To insert:
let client = BigQueryClient<YourEncodableType>(
authenticationToken: "<ENTER-AUTHENTICATION-TOKEN>",
projectID: "<ENTER-PROJECT-ID>",
datasetID: "<ENTER-DATASET-ID>",
tableName: "<ENTER-TABLE-NAME>"
)
try client.insert(rows: rows) { response in
switch response {
case .error(let error):
fatalError("Error: " + error.localizedDescription)
case .bigQueryResponse(let response):
checkErrors(response.insertErrors)
}
}
To query:
let query = "SELECT * FROM users"
try db.query(query) { (r: QueryCallResponse<YourDecodableType>) in
switch r {
case .error(let e):
fatalError("Something went wrong.")
case .queryResponse(let result):
guard let result = result.rows else {
guard let errors = result.errors else {
fatalError("No errors and no rows.")
}
print("BigQuery errors: \(errors)")
return
}
printQueryResult(result)
}
}
For a working example, see here.
Feel free to help out. Currently only insertAll is supported.