10-12-2022 08:31 AM
I am working on Databricks version 10.4 premium cluster and while importing sql from databricks module I am getting below error.
cannot import name 'sql' from 'databricks' (/databricks/python/lib/python3.8/site-packages/databricks/__init__.py).Trying to execute below sample code
from databricks import sql
import os
with sql.connect(server_hostname = os.getenv("DATABRICKS_SERVER_HOSTNAME"),
http_path = os.getenv("DATABRICKS_HTTP_PATH"),
access_token = os.getenv("DATABRICKS_TOKEN")) as connection:
with connection.cursor() as cursor:
cursor.execute("SELECT * FROM default.x LIMIT 2")
result = cursor.fetchall()
for row in result:
print(row)
ANy suggestion here
10-12-2022 09:05 AM
@Ritu Kumari ignore my last response lol Looks like you need to install this lib:
https://docs.databricks.com/dev-tools/python-sql-connector.html#package
databricks-sql-connector
Usage:
pip install databricks-sql-connector
See also databricks-sql-connector in the Python Package Index (PyPI).
10-12-2022 09:50 AM
Just use natively spark.sql this way code is much faster and simpler you use the power of RDD. All you need is
spark.sql("SELECT * FROM default.x LIMIT 2")
11-19-2022 10:28 PM
Hi @Ritu Kumari
Hope all is well!
Just wanted to check in if you were able to resolve your issue and would you be happy to share the solution or mark an answer as best? Else please let us know if you need more help.
We'd love to hear from you.
Thanks!
11-20-2022 04:08 AM
Just currios, why did you use spark.sql for your case I think it easy than import sql from databricks.
11-20-2022 11:05 PM
I basically used pyodbc
Spark sql is also way.
04-04-2023 03:59 AM
There are 2 instances I got this error -
%pip install databricks.sql
2. The hostname, os.getenv("DATABRICKS_SERVER_HOSTNAME") returns, should NOT contain "https://".
Please upvote the answer if it solves your issue.
07-25-2023 04:31 PM
I resolve the same error installing library from cluster interface (UI)
Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections.
Click here to register and join today!
Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.