RT,想在单独运行一个 ruby 文件 用 elasticsearch-persistence 执行数据增删改查 代码如下
# encoding: utf-8
require 'active_record'
require 'will_paginate'
require 'mechanize'
require 'elasticsearch'
# require 'elasticsearch/rails'
# require 'elasticsearch/model'
require 'elasticsearch/persistence'
require 'elasticsearch/persistence/model'
ActiveRecord::Base.establish_connection(
。。。。。。。
)
class User < ActiveRecord::Base #newbaichebao_items
end
class EsUser
include Elasticsearch::Persistence::Model
# include Elasticsearch::Persistence::Repository
# def initialize(options={})
# index options[:index] || 'rails'
# client Elasticsearch::Client.new url: options[:url], log: options[:log]
#
# end
attribute :user_id, String
attribute :phone, String
attribute :name, String
attribute :registration_time, String
attribute :registered_location , String
attribute :channel, String
attribute :fraction, Integer
attribute :number_of_terms , Integer
attribute :last_login_time , String
end
def main
repository = Elasticsearch::Persistence::Repository.new do
# Configure the Elasticsearch client
client Elasticsearch::Client.new url: '10.797.26.99:9200', log: true
index :rails
# type :my_note
end
# str = Mechanize.new
puts a = User.count / 1000 + 1
# puts EsUser.find()
# for i in 1..a
# puts ">>>>>>>>>>>>>>>>>#{i}"
# users = User.paginate(:page => i, :per_page => 1000)
User.find_each do |user|
puts user.name
fen = LicenseLogging.where(:user_id=>user.id.to_s,:name=>"credit_indicator").blank? ? 0 : LicenseLogging.where(:user_id=>user.id.to_s,:name=>"credit_indicator").last.remarks
es = EsUser.new
es.user_id = "#{ids}"
es.phone = user.name
es.registration_time = user.created_at.to_s
es.registered_location = user.reg_location.to_s
es.channel = user.port_type
es.fraction = fen.to_i
es.number_of_terms = num
es.last_login_time = user.updated_at.to_s
p es
es.save
end
# end
end
main
但是始终改不了默认的本地连接。。。
/home/yangmi/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/net/http.rb:879:in `initialize': Network is unreachable - connect(2) for "localhost" port 9200 (Faraday::ConnectionFailed)
请教大家了