简体   繁体   English

如何在 ruby 文件中连接到 cosmos db

[英]How to connect to cosmos db in ruby file

I want to connect to Azure cosmos DB in ruby file, Please can anyone suggest how can I connect with the DB, is there any gem available, to which I can pass connection string of that DB.我想在 ruby 文件中连接到 Azure cosmos DB,请任何人建议我如何与 DB 连接,是否有可用的 gem,我可以传递该 DB 的连接字符串。

Thanks谢谢

The following info worked for me, I found it here Connect to CosmosDB using RUBY以下信息对我有用,我在这里找到了Connect to CosmosDB using RUBY

If you have not already, first specify connection properties in an ODBC DSN (data source name).如果您还没有,首先在 ODBC DSN(数据源名称)中指定连接属性。 This is the last step of the driver installation.这是驱动程序安装的最后一步。 You can use the Microsoft ODBC Data Source Administrator to create and configure ODBC DSNs.您可以使用 Microsoft ODBC 数据源管理器来创建和配置 ODBC DSN。

You need the following info from CosmosDB您需要来自 CosmosDB 的以下信息

AccountEndpoint: The Cosmos DB account URL from the Keys blade of the Cosmos DB account AccountKey: In the Azure portal, navigate to the Cosmos DB service and select your Azure Cosmos DB account. AccountEndpoint: The Cosmos DB account URL from the Keys blade of the Cosmos DB account AccountKey: In the Azure portal, navigate to the Cosmos DB service and select your Azure Cosmos DB account. From the resource menu, go to the Keys page.从资源菜单 go 到 Keys 页面。 Find the PRIMARY KEY value and set AccountKey to this value.找到 PRIMARY KEY 值并将 AccountKey 设置为该值。

you will need to install the following gems您将需要安装以下 gem

gem install dbi
gem install dbd-odbc
gem install ruby-odbc

The write something like this写这样的东西

#connect to the DSN
require 'DBI'
cnxn = DBI.connect('DBI:ODBC:CData CosmosDB Source','','')

#execute a SELECT query and store the result set
resultSet = cnxn.execute("SELECT City, CompanyName FROM Customers")

#display the names of the columns
resultSet.column_names.each do |name|
  print name, "\t"
end
puts

#display the results
while row = resultSet.fetch do
  (0..resultSet.column_names.size - 1).each do |n|
    print row[n], "\t"
  end
  puts
end
resultSet.finish

#close the connection
cnxn.disconnect if cnxn

Since there is no prerequisite from your side in the question, I'm assuming you want to know the complete procedure to work on Azure CosmosDB using Ruby.由于您在问题中没有先决条件,我假设您想知道使用 Ruby 在 Azure CosmosDB 上工作的完整过程。

You need to create an Azure storage account and Azure CosmosDB account.您需要创建一个 Azure 存储帐户和 Azure CosmosDB 帐户。 The easiest way to create an Azure storage account is by using the Azure portal.创建 Azure 存储帐户的最简单方法是使用 Azure 门户。 To learn more, see Create a storage account .若要了解详细信息,请参阅创建存储帐户

To create an Azure Cosmos DB Table API account, see Create a database account .要创建 Azure Cosmos DB 表 API 帐户,请参阅创建数据库帐户

To use Azure Storage or Azure Cosmos DB, you must download and use the Ruby Azure package that includes a set of convenience libraries that communicate with the Table REST services. To use Azure Storage or Azure Cosmos DB, you must download and use the Ruby Azure package that includes a set of convenience libraries that communicate with the Table REST services.

Use RubyGems to obtain the package使用 RubyGems 获得 package

  1. Use a command-line interface such as PowerShell (Windows), Terminal (Mac), or Bash (Unix).使用命令行界面,例如 PowerShell (Windows)、终端 (Mac) 或 Bash (Unix)。
  2. Type gem install azure-storage-table in the command window to install the gem and dependencies.在命令 window 中键入 gem install azure-storage-table 以安装 gem 和依赖项。

Import the package Use your favorite text editor, add the following to the top of the Ruby file where you intend to use Storage: require "azure/storage/table"导入 package 使用您喜欢的文本编辑器,将以下内容添加到您打算使用存储的 Ruby 文件的顶部:需要“azure/storage/table”

Add your connection string You can either connect to the Azure storage account or the Azure Cosmos DB Table API account.添加连接字符串 您可以连接到 Azure 存储帐户或 Azure Cosmos DB 表 API 帐户。 Get the connection string based on the type of account you are using.根据您使用的帐户类型获取连接字符串。

Add an Azure Storage connection The Azure Storage module reads the environment variables AZURE_STORAGE_ACCOUNT and AZURE_STORAGE_ACCESS_KEY for information required to connect to your Azure Storage account.添加 Azure 存储连接 Azure 存储模块读取环境变量 AZURE_STORAGE_ACCOUNT 和 AZURE_STORAGE_ACCESS_KEY 以获取连接到您的 Z3A580F1420368F Storage 所需的信息的信息。 If these environment variables are not set, you must specify the account information before using Azure::Storage::Table::TableService with the following code:如果未设置这些环境变量,则必须在使用 Azure::Storage::Table::TableService 之前指定账户信息,代码如下:

Azure.config.storage_account_name = "your Azure Storage account" Azure.config.storage_access_key = "your Azure Storage access key" Azure.config.storage_account_name = "your Azure Storage account" Azure.config.storage_access_key = "your Azure Storage access key"

Add an Azure Cosmos DB connection To connect to Azure Cosmos DB, copy your primary connection string from the Azure portal, and create a Client object using your copied connection string. Add an Azure Cosmos DB connection To connect to Azure Cosmos DB, copy your primary connection string from the Azure portal, and create a Client object using your copied connection string. You can pass the Client object when you create a TableService object:您可以在创建 TableService object 时传递 Client object:

common_client = Azure::Storage::Common::Client.create(storage_account_name:'myaccount', storage_access_key:'mykey', storage_table_host:'mycosmosdb_endpoint') common_client = Azure::Storage::Common::Client.create(storage_account_name:'myaccount', storage_access_key:'mykey', storage_table_host:'mycosmosdb_endpoint')

table_client = Azure::Storage::Table::TableService.new(client: common_client) table_client = Azure::Storage::Table::TableService.new(client: common_client)

Please refer to link if required.如果需要,请参考链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM