【问题标题】:CSV File download from Databricks Filestore in Python not working从 Python 中的 Databricks Filestore 下载 CSV 文件不起作用
【发布时间】:2021-05-24 20:24:52
【问题描述】:

我正在使用下面的 Python 代码从 Databricks Filestore 下载 csv 文件。通常,保存在 Filestore 中的文件可以通过浏览器下载。

当我在浏览器中直接输入文件的 url 时,文件下载正常。但是当我尝试通过下面的代码执行相同操作时,下载文件的内容不是 csv,而是一些 html 代码 - 见下文。

这是我的 Python 代码:

def download_from_dbfs_filestore(file):
    url ="https://databricks-hot-url/files/{0}".format(file)
    req = requests.get(url)
    req_content = req.content
    my_file = open(file,'wb')
    my_file.write(req_content)
    my_file.close()

这里是html。它似乎是在引用登录页面,但不确定从这里做什么:

<!doctype html><html><head><meta charset="utf-8"/>
<meta http-equiv="Content-Language" content="en"/>
<title>Databricks - Sign In</title><meta name="viewport" content="width=960"/>
<link rel="icon" type="image/png" href="/favicon.ico"/>
<meta http-equiv="content-type" content="text/html; charset=UTF8"/><link rel="icon" href="favicon.ico">
</head><body class="light-mode"><uses-legacy-bootstrap><div id="login-page">
</div></uses-legacy-bootstrap><script src="login/login.xxxxx.js"></script>
</body>
</html>

【问题讨论】:

  • 您的意思是代码在 Databricks 笔记本之外执行?您使用的是社区版还是“普通”Databricks?

标签: databricks azure-databricks


【解决方案1】:

使用base64模块b64decode解决了问题:

import base64 
DOMAIN = <your databricks 'host' url>
TOKEN = <your databricks 'token'>
jsonbody = {"path": <your dbfs Filestore path>}
response = requests.get('https://%s/api/2.0/dbfs/read/' % (DOMAIN), headers={'Authorization': 'Bearer %s' % TOKEN},json=jsonbody )
if response.status_code == 200:
    csv=base64.b64decode(response.json()["data"]).decode('utf-8')
    print(csv)

【讨论】:

    猜你喜欢
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    • 2017-10-19
    • 2020-05-25
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多