Connecting to snowflake in Jupyter Notebook.

huangapple go评论67阅读模式
英文:

Connecting to snowflake in Jupyter Notebook

问题

以下是您要翻译的代码部分:

import snowflake.connector
conn = snowflake.connector.connect(account='account',
                                    user='user',
                                    password='password',
                                    database='db')

错误信息如下:

OperationalError                          Traceback (most recent call last)
File ~/Library/Python/3.8/lib/python/site-packages/snowflake/connector/connection.py:1125, in SnowflakeConnection._authenticate(self, auth_instance)
   1124 try:
- 1125     auth.authenticate(
   1126         auth_instance=auth_instance,
   1127         account=self.account,
   1128         user=self.user,
   1129         database=self.database,
   1130         schema=self.schema,
   1131         warehouse=self.warehouse,
   1132         role=self.role,
   1133         passcode=self._passcode,
   1134         passcode_in_password=self._passcode_in_password,
   1135         mfa_callback=this._mfa_callback,
   1136         password_callback=this._password_callback,
   1137         session_parameters=this._session_parameters,
   1138     )
   1139 except OperationalError as e:

File ~/Library/Python/3.8/lib/python/site-packages/snowflake/connector/auth/_auth.py:250, in Auth.authenticate(this, auth_instance, account, user, database, schema, warehouse, role, passcode, passcode_in_password, mfa_callback, password_callback, session_parameters, timeout)
    249 try:
-- 250     ret = this._rest._post_request(
    251     url,
    252     headers,
    253     json.dumps(body),
    254     timeout=auth_timeout,
    255     socket_timeout=auth_timeout,
    256 )

File ~/Library/Python/3.8/lib/python/site-packages/snowflake/connector/network.py:716, in SnowflakeRestful._post_request(this, url, headers, body, token, timeout, _no_results, no_retry, socket_timeout, _include_retry_params)
    714 pprint(ret)
-- 716 ret = this.fetch(
    717     "post",
    718     full_url,
    719     headers,
    720     data=body,
    721     timeout=timeout,
    722     token=token,
    723     no_retry=no_retry,
    724     socket_timeout=socket_timeout,
    725     _include_retry_params=_include_retry_params,
    726 )
    727 logger.debug(
    728     "ret[code] = {code}, after post request".format(
    729         code=(ret.get("code", "N/A"))
    730     )
    731 )

File ~/Library/Python/3.8/lib/python/site-packages/snowflake/connector/network.py:814, in SnowflakeRestful.fetch(this, method, full_url, headers, data, timeout, **kwargs)
    813 while True:
-- 814 ret = this._request_exec_wrapper(
    815     session, method, full_url, headers, data, retry_ctx, **kwargs
    816 )
    817 if ret is not None:

File ~/Library/Python/3.8/lib/python/site-packages/snowflake/connector/network.py:937, in SnowflakeRestful._request_exec_wrapper(this, session, method, full_url, headers, data, retry_ctx, no_retry, token, **kwargs)
    936 if not no_retry:
-- 937 raise e
    938 logger.debug("Ignored error", exc_info=True)

File ~/Library/Python/3.8/lib/python/site-packages/snowflake/connector/network.py:868, in SnowflakeRestful._request_exec_wrapper(this, session, method, full_url, headers, data, retry_ctx, no_retry, token, **kwargs)
    867 return return_object
-- 868 this._handle_unknown_error(method, full_url, headers, data, conn)
    869 TelemetryService.get_instance().log_http_request_error(
    870 "HttpRequestUnknownError",
    871 full_url,
   (...)
    876 retry_count=retry_ctx.cnt,
    877 )

File ~/Library/Python/3.8/lib/python/site-packages/snowflake/connector/network.py:992, in SnowflakeRestful._handle_unknown_error(this, method, full_url, headers, data, conn)
    987 logger.error(
    988 f"Failed to get the response. Hanging? "
    989 f"method: {method}, url: {full_url}, headers:{headers}, "
    990 f"data: {data}"
    991 )
-- 992 Error.errorhandler_wrapper(
    993 conn,
    994 None,
    995 OperationalError,
    996 {
    997     "msg": f"Failed to get the response. Hanging? method: {method}, url: {full_url}",
    998     "errno": ER_FAILED_TO_REQUEST,
    999 },
   1000 )

OperationalError: 250003: Failed to get the response. Hanging? method: post, url: https://xw81982.us-east4.gcp.snowflakecomputing.com.snowflakecomputing.com:443/session/v1/login-request?request_id=cedc0862-8df4-4cb1-a617-5216d2a16254&databaseName=Product&request_guid=435b4fa9-7df9-494c-b0b1-5238c26c032e

The above exception was the direct cause of the following exception:

OperationalError                          Traceback (most recent call last)
Cell In[6], line 2
      1 import snowflake.connector
-- 2 conn = snowflake.connector.connect(account='xw81982.us-east4.gcp.snowflakecomputing.com',
      3                                         user='03pes03',
      4                                         password='Copper333,,',
      5                                         database='Product')

File ~/Library/Python/3.8/lib/python/site-packages/snowflake/connector/__init__.py:51, in Connect(**kwargs)
     50 def Connect(**kwargs) -> SnowflakeConnection:
-- 51 return SnowflakeConnection(**kwargs)

File ~/Library/Python/3.8/lib/python/site-packages/snowflake/connector/connection.py:304, in SnowflakeConnection.__init__(self, **kwargs)
    302 this.converter = None
    303 this.__set_error_attributes()
-- 304 this.connect(**kwargs)
    305 this._telemetry = TelemetryClient(this._rest)
    307 # get the imported modules from sys.modules

File ~/Library/Python/3.8/lib/python/site-packages/snowflake/connector/connection.py:571, in SnowflakeConnection.connect(this, **kwargs)
    569         connection_diag.generate_report()
    570 else:
-- 571 this.__open_connection()

File ~/Library/Python/3.8/lib/python/site-packages/snowflake/connector/connection.py:839, in SnowflakeConnection.__open_connection(this)
    835 else:
    836     # okta URL, e.g., https://<account>.

<details>
<summary>英文:</summary>

I have a very base script that works to connect to snowflake python connect but once I drop it in a jupyter notebook , I get the error below and really have no idea why?


    import snowflake.connector
    conn = snowflake.connector.connect(account=&#39;account&#39;,
                                            user=&#39;user&#39;,
                                            password=&#39;password&#39;,
                                            database=&#39;db&#39;) 


ERROR


    OperationalError                          Traceback (most recent call last)
    File ~/Library/Python/3.8/lib/python/site-packages/snowflake/connector/connection.py:1125, in SnowflakeConnection._authenticate(self, auth_instance)
       1124 try:
    -&gt; 1125     auth.authenticate(
       1126         auth_instance=auth_instance,
       1127         account=self.account,
       1128         user=self.user,
       1129         database=self.database,
       1130         schema=self.schema,
       1131         warehouse=self.warehouse,
       1132         role=self.role,
       1133         passcode=self._passcode,
       1134         passcode_in_password=self._passcode_in_password,
       1135         mfa_callback=self._mfa_callback,
       1136         password_callback=self._password_callback,
       1137         session_parameters=self._session_parameters,
       1138     )
       1139 except OperationalError as e:
    
    File ~/Library/Python/3.8/lib/python/site-packages/snowflake/connector/auth/_auth.py:250, in Auth.authenticate(self, auth_instance, account, user, database, schema, warehouse, role, passcode, passcode_in_password, mfa_callback, password_callback, session_parameters, timeout)
        249 try:
    --&gt; 250     ret = self._rest._post_request(
        251         url,
        252         headers,
        253         json.dumps(body),
        254         timeout=auth_timeout,
        255         socket_timeout=auth_timeout,
        256     )
        257 except ForbiddenError as err:
        258     # HTTP 403
    
    File ~/Library/Python/3.8/lib/python/site-packages/snowflake/connector/network.py:716, in SnowflakeRestful._post_request(self, url, headers, body, token, timeout, _no_results, no_retry, socket_timeout, _include_retry_params)
        714     pprint(ret)
    --&gt; 716 ret = self.fetch(
        717     &quot;post&quot;,
        718     full_url,
        719     headers,
        720     data=body,
        721     timeout=timeout,
        722     token=token,
        723     no_retry=no_retry,
        724     socket_timeout=socket_timeout,
        725     _include_retry_params=_include_retry_params,
        726 )
        727 logger.debug(
        728     &quot;ret[code] = {code}, after post request&quot;.format(
        729         code=(ret.get(&quot;code&quot;, &quot;N/A&quot;))
        730     )
        731 )
    
    File ~/Library/Python/3.8/lib/python/site-packages/snowflake/connector/network.py:814, in SnowflakeRestful.fetch(self, method, full_url, headers, data, timeout, **kwargs)
        813 while True:
    --&gt; 814     ret = self._request_exec_wrapper(
        815         session, method, full_url, headers, data, retry_ctx, **kwargs
        816     )
        817     if ret is not None:
    
    File ~/Library/Python/3.8/lib/python/site-packages/snowflake/connector/network.py:937, in SnowflakeRestful._request_exec_wrapper(self, session, method, full_url, headers, data, retry_ctx, no_retry, token, **kwargs)
        936 if not no_retry:
    --&gt; 937     raise e
        938 logger.debug(&quot;Ignored error&quot;, exc_info=True)
    
    File ~/Library/Python/3.8/lib/python/site-packages/snowflake/connector/network.py:868, in SnowflakeRestful._request_exec_wrapper(self, session, method, full_url, headers, data, retry_ctx, no_retry, token, **kwargs)
        867     return return_object
    --&gt; 868 self._handle_unknown_error(method, full_url, headers, data, conn)
        869 TelemetryService.get_instance().log_http_request_error(
        870     &quot;HttpRequestUnknownError&quot;,
        871     full_url,
       (...)
        876     retry_count=retry_ctx.cnt,
        877 )
    
    File ~/Library/Python/3.8/lib/python/site-packages/snowflake/connector/network.py:992, in SnowflakeRestful._handle_unknown_error(self, method, full_url, headers, data, conn)
        987 logger.error(
        988     f&quot;Failed to get the response. Hanging? &quot;
        989     f&quot;method: {method}, url: {full_url}, headers:{headers}, &quot;
        990     f&quot;data: {data}&quot;
        991 )
    --&gt; 992 Error.errorhandler_wrapper(
        993     conn,
        994     None,
        995     OperationalError,
        996     {
        997         &quot;msg&quot;: f&quot;Failed to get the response. Hanging? method: {method}, url: {full_url}&quot;,
        998         &quot;errno&quot;: ER_FAILED_TO_REQUEST,
        999     },
       1000 )
    
    File ~/Library/Python/3.8/lib/python/site-packages/snowflake/connector/errors.py:290, in Error.errorhandler_wrapper(connection, cursor, error_class, error_value)
        274 &quot;&quot;&quot;Error handler wrapper that calls the errorhandler method.
        275 
        276 Args:
       (...)
        287     exception to the first handler in that order.
        288 &quot;&quot;&quot;
    --&gt; 290 handed_over = Error.hand_to_other_handler(
        291     connection,
        292     cursor,
        293     error_class,
        294     error_value,
        295 )
        296 if not handed_over:
    
    File ~/Library/Python/3.8/lib/python/site-packages/snowflake/connector/errors.py:348, in Error.hand_to_other_handler(connection, cursor, error_class, error_value)
        347 elif connection is not None:
    --&gt; 348     connection.errorhandler(connection, cursor, error_class, error_value)
        349     return True
    
    File ~/Library/Python/3.8/lib/python/site-packages/snowflake/connector/errors.py:221, in Error.default_errorhandler(connection, cursor, error_class, error_value)
        220 done_format_msg = error_value.get(&quot;done_format_msg&quot;)
    --&gt; 221 raise error_class(
        222     msg=error_value.get(&quot;msg&quot;),
        223     errno=None if errno is None else int(errno),
        224     sqlstate=error_value.get(&quot;sqlstate&quot;),
        225     sfqid=error_value.get(&quot;sfqid&quot;),
        226     query=error_value.get(&quot;query&quot;),
        227     done_format_msg=(
        228         None if done_format_msg is None else bool(done_format_msg)
        229     ),
        230     connection=connection,
        231     cursor=cursor,
        232 )
    
    OperationalError: 250003: Failed to get the response. Hanging? method: post, url: https://xw81982.us-east4.gcp.snowflakecomputing.com.snowflakecomputing.com:443/session/v1/login-request?request_id=cedc0862-8df4-4cb1-a617-5216d2a16254&amp;databaseName=Product&amp;request_guid=435b4fa9-7df9-494c-b0b1-5238c26c032e
    
    The above exception was the direct cause of the following exception:
    
    OperationalError                          Traceback (most recent call last)
    Cell In[6], line 2
          1 import snowflake.connector
    ----&gt; 2 conn = snowflake.connector.connect(account=&#39;xw81982.us-east4.gcp.snowflakecomputing.com&#39;,
          3                                         user=&#39;03pes03&#39;,
          4                                         password=&#39;Copper333,,&#39;,
          5                                         database=&#39;Product&#39;)   
    
    File ~/Library/Python/3.8/lib/python/site-packages/snowflake/connector/__init__.py:51, in Connect(**kwargs)
         50 def Connect(**kwargs) -&gt; SnowflakeConnection:
    ---&gt; 51     return SnowflakeConnection(**kwargs)
    
    File ~/Library/Python/3.8/lib/python/site-packages/snowflake/connector/connection.py:304, in SnowflakeConnection.__init__(self, **kwargs)
        302 self.converter = None
        303 self.__set_error_attributes()
    --&gt; 304 self.connect(**kwargs)
        305 self._telemetry = TelemetryClient(self._rest)
        307 # get the imported modules from sys.modules
    
    File ~/Library/Python/3.8/lib/python/site-packages/snowflake/connector/connection.py:571, in SnowflakeConnection.connect(self, **kwargs)
        569         connection_diag.generate_report()
        570 else:
    --&gt; 571     self.__open_connection()
    
    File ~/Library/Python/3.8/lib/python/site-packages/snowflake/connector/connection.py:839, in SnowflakeConnection.__open_connection(self)
        835 else:
        836     # okta URL, e.g., https://&lt;account&gt;.okta.com/
        837     self.auth_class = AuthByOkta(application=self.application)
    --&gt; 839 self.authenticate_with_retry(self.auth_class)
        841 self._password = None  # ensure password won&#39;t persist
        842 self.auth_class.reset_secrets()
    
    File ~/Library/Python/3.8/lib/python/site-packages/snowflake/connector/connection.py:1099, in SnowflakeConnection.authenticate_with_retry(self, auth_instance)
       1096 def authenticate_with_retry(self, auth_instance) -&gt; None:
       1097     # make some changes if needed before real __authenticate
       1098     try:
    -&gt; 1099         self._authenticate(auth_instance)
       1100     except ReauthenticationRequest as ex:
       1101         # cached id_token expiration error, we have cleaned id_token and try to authenticate again
       1102         logger.debug(&quot;ID token expired. Reauthenticating...: %s&quot;, ex)
    
    File ~/Library/Python/3.8/lib/python/site-packages/snowflake/connector/connection.py:1170, in SnowflakeConnection._authenticate(self, auth_instance)
       1168 except OperationalError as auth_op:
       1169     if auth_op.errno == ER_FAILED_TO_CONNECT_TO_DB:
    -&gt; 1170         raise auth_op from e
       1171     logger.debug(&quot;Continuing authenticator specific timeout handling&quot;)
       1172     continue
    
    File ~/Library/Python/3.8/lib/python/site-packages/snowflake/connector/connection.py:1147, in SnowflakeConnection._authenticate(self, auth_instance)
       1145 while True:
       1146     try:
    -&gt; 1147         auth_instance.handle_timeout(
       1148             authenticator=self._authenticator,
       1149             service_name=self.service_name,
       1150             account=self.account,
       1151             user=self.user,
       1152             password=self._password,
       1153         )
       1154         auth.authenticate(
       1155             auth_instance=auth_instance,
       1156             account=self.account,
       (...)
       1166             session_parameters=self._session_parameters,
       1167         )
       1168     except OperationalError as auth_op:
    
    File ~/Library/Python/3.8/lib/python/site-packages/snowflake/connector/auth/by_plugin.py:214, in AuthByPlugin.handle_timeout(***failed resolving arguments***)
        212 if not self._retry_ctx.should_retry():
        213     self._retry_ctx.reset()
    --&gt; 214     raise OperationalError(
        215         msg=f&quot;Could not connect to Snowflake backend after {self._retry_ctx.get_current_retry_count()} attempt(s).&quot;
        216         &quot;Aborting&quot;,
        217         errno=ER_FAILED_TO_CONNECT_TO_DB,
        218     )
        219 else:
        220     logger.debug(
        221         f&quot;Hit connection timeout, attempt number {self._retry_ctx.get_current_retry_count()}.&quot;
        222         &quot; Will retry in a bit...&quot;
        223     )
    
    OperationalError: 250001: Could not connect to Snowflake backend after 0 attempt(s).Aborting

</details>


# 答案1
**得分**: 0

**无法在0次尝试后连接到Snowflake后端**

假设:

**提供的帐户信息不正确。** - 它包含完整的URL

---

如果URL是:

    https://xxxxx.region.cloud_provider.snowflakecomputing.com

那么帐户不应包括 `.snowflakecomputing.com`

    import snowflake.connector
    conn = snowflake.connector.connect(account='xxxxx.region.cloud_provider',
                                       user='user',
                                       password='password',
                                       database='db')

在连接设置期间还建议显式列出 `role/warehouse`,否则将使用用户的默认设置。

<details>
<summary>英文:</summary>

&gt; *Could not connect to Snowflake backend after 0 attempt(s)*

Hypothesis: 

**Provided account is incorrect.** - It contains full url

---

If the url is:

    https://xxxxx.region.cloud_provider.snowflakecomputing.com

then account should not include `.snowflakecomputing.com`

    import snowflake.connector
    conn = snowflake.connector.connect(account=&#39;xxxxx.region.cloud_provider&#39;,
                                       user=&#39;user&#39;,
                                       password=&#39;password&#39;,
                                       database=&#39;db&#39;) 

It is also recommended to explicitly list `role/warehouse` during the connection setup, otherwise user&#39;s default will be used.

</details>



huangapple
  • 本文由 发表于 2023年4月7日 05:22:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75953893.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定