You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无直接选项时,如何为Azure Databricks数据创建Cognitive Search索引?

从Azure Databricks创建Azure Cognitive Search索引的可行方案

方法1:通过Azure Blob Storage中转

  • 在Databricks中将表格数据导出到Azure Blob Storage(推荐用Parquet格式,读写效率更高),示例代码:
df.write.format("parquet") \
  .option("header", "true") \
  .mode("overwrite") \
  .save("wasbs://<容器名称>@<存储账户名称>.blob.core.windows.net/<存储路径>")
  • 登录Azure门户进入你的Azure Cognitive Search服务,创建索引时选择Azure Blob Storage作为数据源,配置存储账户连接信息并指定Blob路径,后续按向导完成索引创建与数据导入。

方法2:在Databricks中直接调用Azure Cognitive Search SDK推送数据

  • 在Databricks集群中安装Python SDK:%pip install azure-search-documents
  • 编写代码定义索引结构,再将Databricks DataFrame数据批量上传到搜索索引,示例代码片段:
from azure.core.credentials import AzureKeyCredential
from azure.search.documents import SearchClient
from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents.indexes.models import SearchIndex, SimpleField, SearchableField

# 配置搜索服务参数
service_endpoint = "<你的搜索服务端点>"
admin_key = "<你的搜索服务管理员密钥>"
index_name = "<目标索引名称>"

# 创建索引并定义字段结构
index_client = SearchIndexClient(endpoint=service_endpoint, credential=AzureKeyCredential(admin_key))
fields = [
    SimpleField(name="id", type="Edm.String", key=True),
    SearchableField(name="column1", type="Edm.String"),
    # 根据你的表格字段补充其他字段定义
]
index = SearchIndex(name=index_name, fields=fields)
index_client.create_or_update_index(index)

# 将DataFrame转为字典列表,批量上传到索引
search_client = SearchClient(endpoint=service_endpoint, index_name=index_name, credential=AzureKeyCredential(admin_key))
docs = df.toDict("records")
search_client.upload_documents(documents=docs)

方法3:同步数据到Azure SQL/Cosmos DB后导入

  • 使用Databricks的JDBC连接器,将表格数据同步到Azure SQL Database(或Cosmos DB),示例SQL DB写入代码:
df.write.format("jdbc") \
  .option("url", "jdbc:sqlserver://<SQL服务器名称>.database.windows.net:1433;database=<数据库名称>") \
  .option("dbtable", "<目标表名称>") \
  .option("user", "<用户名>") \
  .option("password", "<密码>") \
  .mode("overwrite") \
  .save()
  • 在Azure Cognitive Search创建索引时,选择对应的Azure SQL Database或Azure Cosmos DB作为数据源,配置连接后完成数据导入与索引创建。

内容的提问来源于stack exchange,提问作者Sezz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 20:56:09