DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Data
  4. Connect to Cloudant Data in Ruby

Connect to Cloudant Data in Ruby

Connect to Cloudant data in Ruby with ruby-dbi, dbd-odbc, and ruby-odbc.

Jerod Johnson user avatar by
Jerod Johnson
·
May. 27, 19 · Tutorial
Like (3)
Save
Tweet
Share
6.68K Views

Join the DZone community and get the full member experience.

Join For Free

The CData ODBC Driver for Cloudant makes it easy to integrate connectivity to live Cloudant data in Ruby. This article shows how to create a simple Ruby app that connects to Cloudant data, executes a query, and displays the results.

Create an ODBC Connection to Cloudant Data

If you have not already, first specify connection properties in an ODBC DSN (data source name). This is the last step of the driver installation. You can use the Microsoft ODBC Data Source Administrator to create and configure ODBC DSNs.

Set the following connection properties to connect to Cloudant:

  • User: Set this to your username.
  • Password: Set this to your password.

Installing Ruby and Necessary Gems

If you do not have Ruby installed, refer to the Ruby installation page. With Ruby installed, you will need to install the ruby-dbi, dbd-odbc, and ruby-odbc gems:

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

Create a Ruby App With Connectivity to Cloudant Data

Create a new Ruby file (for example: CloudantSelect.rb) and open it in a text editor. Copy the following code into your file:

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

#execute a SELECT query and store the result set
resultSet = cnxn.execute("SELECT MovieRuntime, MovieRating FROM Movies")

#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

With the file completed, you are ready to display your Cloudant data with Ruby. To do so, simply run your file from the command line:

ruby CloudantSelect.rb

Writing SQL-92 queries to Cloudant allows you to quickly and easily incorporate Cloudant data into your own Ruby applications. Download a free trial today!

Data (computing)

Published at DZone with permission of Jerod Johnson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Java REST API Frameworks
  • 10 Most Popular Frameworks for Building RESTful APIs
  • Isolating Noisy Neighbors in Distributed Systems: The Power of Shuffle-Sharding
  • Data Stream Using Apache Kafka and Camel Application

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: