码迷,mamicode.com
首页 > 其他好文 > 详细

rails应用中各数据平台的对接

时间:2018-02-01 17:18:46      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:bre   body   gpo   nbsp   dex   color   each   ruby   collect   

1.mongo

#Gemfile添加如下两个gem包
gem mongoid, ~> 5.1.0 gem mongo, ~> 2.4’
@client = Mongo::Client.new([host1,host2],:database=>database_name,:user=>"user_name",:password=>"password")
@db = @client[:database_name].database
@collection_names = @db.collection_names
@collection_names.each do |collection_name|
i=0
@db[collection_name.to_sym].find.each do |x|
  puts x
  i = i + 1
  break if i == 10
end
end

 

2.redis

$redis = Redis.new(:host => "host", :port => 6379, :db => 2, :password=>"password")
$redis.keys

 

3.elasticsearch 

gem elasticsearch
gem elasticsearch-rails
gem elasticsearch-model
$elastic = Elasticsearch::Client.new hosts: [{ host: ‘host1‘,port: ‘9200‘,user: ‘elastic‘,password: ‘password‘},{host: ‘host1‘,port: ‘9200‘,user: ‘elastic‘,password: ‘password‘}], randomize_hosts: true, log: false,send_get_body_as: "post" 
Elasticsearch::Model.client
= $elastic
$elastic.search index:"index_name"

 

4.kafka

gem ruby-kafka, ~> 0.5.0
kafka = Kafka.new(seed_brokers:["host:9092"])
consumer = kafka.consumer(:group_id=>"100")
consumer.subscribe("topic_name")
i=0
consumer.each_message do |meg|
  puts i=i+1
  puts meg.value
  break if i==10
end

 

rails应用中各数据平台的对接

标签:bre   body   gpo   nbsp   dex   color   each   ruby   collect   

原文地址:https://www.cnblogs.com/xiaoff/p/8398730.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!