Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to execute a query in google big query #142

Open
karasarvani opened this issue Nov 4, 2019 · 1 comment
Open

Unable to execute a query in google big query #142

karasarvani opened this issue Nov 4, 2019 · 1 comment

Comments

@karasarvani
Copy link

I tried executing a query in BigQuery using this library gogol-bigquery. Please find the following code snippet, QueryRequest and QueryResponse generated. Please let me know why I am getting a Left instead of Right and is there anything missing in Request

JobsQuery :

JobsQuery' {_jqPayload = QueryRequest' {_qrLocation = Just "asia-south1", _qrUseQueryCache = True, _qrPreserveNulls =
Nothing, _qrKind = "bigquery#queryRequest", _qrQueryParameters = Nothing, _qrQuery = Just "select order_id from
projectId:dataset.tablename limit 100", _qrParameterMode = Nothing, _qrTimeoutMs = Nothing,
_qrUseLegacySQL = True, _qrDryRun = Nothing, _qrMaxResults = Nothing, _qrDefaultDataSet = Nothing}, _jqProjectId =
"projectId"}

Response:

Left ("Error in $: not enough input","")

Code:

txnreport :: ActionM ()
txnreport = do
body <- jsonData :: ActionM HT.Body
-- query <- liftIO $ constructQuery body
let respFn = GT._cliResponse $ executeQuery "a"
eitherVal <- liftIO $ runResourceT $ respFn mempty
html $ T.pack $ show eitherVal

setParams :: QueryRequest -> QueryRequest
setParams qr = qr & (qrLocation .~ (Just "asia-south1")) . (qrUseLegacySQL .~ True) . (qrQuery .~ (Just "select order_id from projectId:dataset.table limit 100"))

executeQuery :: T.Text -> GClient (Rs JobsQuery)
executeQuery query = do
let
qr = queryRequest
modified = setParams qr
in
requestClient $ jobsQuery modified "projectId"

@mayank-17
Copy link

Here is how you use it

bqQueryRequest :: T.Text -> IO Value
bqQueryRequest query =  do
    bqProjectId <- getBqProjectId
    let
        queryRequest = GBT.newQueryRequest
            { connectionProperties = Nothing
            , createSession = Nothing
            , defaultDataset = Nothing
            , dryRun = Nothing
            , kind = "bigquery#queryRequest"
            , labels = Nothing
            , location = Just "asia-south1"
            , maxResults = Nothing
            , maximumBytesBilled = Nothing
            , parameterMode = Nothing
            , preserveNulls = Nothing
            , query = Just query
            , queryParameters = Nothing
            , requestId = Nothing
            , timeoutMs = Just 276447232
            , useLegacySql = False
            , useQueryCache = False
            }
        bigQueryJobsQuery = GBQ.BigQueryJobsQuery queryRequest (T.pack bqProjectId)

    lgr <- Google.newLogger Google.Error stdout
    _ <- print queryRequest
    manager <- newTlsManagerWith tlsManagerSettings
    googleEnv <-
            Google.newEnv
              <&>
                (Google.envLogger .~ lgr)
                . (Google.envScopes .~ (Proxy :: Proxy '[Bigquery'FullControl]))
                . (Google.envManager .~ manager)
    bqRes <- liftIO $ runResourceT $ Google.sendEither googleEnv mempty bigQueryJobsQuery

    case bqRes of
        Left _  -> print(("BQ Response Error " :: [Char]) <> show bqRes)
        Right _ -> print("Got BQ Response successful " :: [Char])
    return ( case bqRes of
                Right b -> toJSON b
                Left _  -> Null )
    where
        getBqProjectId :: IO String
        getBqProjectId = fromMaybe (error "BQ_PROJECT_ID not present in ENV") <$> (lookupEnv "BQ_PROJECT_ID")

Calling the function

bqQueryRequest "SELECT 1 AS a, 2 AS b FROM __table_name__"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants