Ruby on rails Geocoder ruby gem随需应变?
我有一个拥有400多万个地址/记录的大型数据库。 当数据库是一个小测试集时,rake命令(如下)工作得很好,但现在对于大型数据库,它只是简单地暂停Ruby on rails Geocoder ruby gem随需应变?,ruby-on-rails,ruby,google-maps,geocoding,Ruby On Rails,Ruby,Google Maps,Geocoding,我有一个拥有400多万个地址/记录的大型数据库。 当数据库是一个小测试集时,rake命令(如下)工作得很好,但现在对于大型数据库,它只是简单地暂停 rake geocode:all CLASS=YourModel 2个问题: 1.有没有简单的方法可以让geocoder在调用记录时(动态地)将其编码为null/nil lat和long。我觉得这很难。 2.还有谁在地理编码、删除大型数据集和使用rake命令方面有问题吗 谢谢 更新: 我基于此答案创建拉取请求,现在您可以在以下位置使用批处理: 我会
rake geocode:all CLASS=YourModel
2个问题:
1.有没有简单的方法可以让geocoder在调用记录时(动态地)将其编码为null/nil lat和long。我觉得这很难。
2.还有谁在地理编码、删除大型数据集和使用rake命令方面有问题吗
谢谢 更新:
我基于此答案创建拉取请求,现在您可以在以下位置使用批处理:
我会将此解决方案用于大型数据库,我从rake任务中获得:
您可以根据自己的需要对其进行优化。
创建rake任务的一些示例:
namespace :geocode_my_data do
desc "Geocode all objects in my databse."
task all: :environment do
klass = User
klass.where(geocoded: false).find_each(limit: 100) do |obj|
obj.geocode; obj.save
end
end
end
$> rake geocode_my_data:all
在下面的代码中使用,并将其作为geocode_my_data.rake放入我的lib/tasks文件夹 要运行: 很好
namespace :geocode_my_data do
desc "Geocode all objects without coordinates."
task :all => :environment do
class_name = ENV['CLASS'] || ENV['class']
sleep_timer = ENV['SLEEP'] || ENV['sleep']
raise "Please specify a CLASS (model)" unless class_name
klass = class_from_string(class_name)
klass.not_geocoded.find_each(batch_size: 100) do |obj|
obj.geocode; obj.save
sleep(sleep_timer.to_f) unless sleep_timer.nil?
end
end
end
##
# Get a class object from the string given in the shell environment.
# Similar to ActiveSupport's +constantize+ method.
#
def class_from_string(class_name)
parts = class_name.split("::")
constant = Object
parts.each do |part|
constant = constant.const_get(part)
end
constant
end
睡眠有什么用?谢谢。我想这个限制可以防止我一直被冻结。
rake geocode:all CLASS=YourModel
namespace :geocode_my_data do
desc "Geocode all objects without coordinates."
task :all => :environment do
class_name = ENV['CLASS'] || ENV['class']
sleep_timer = ENV['SLEEP'] || ENV['sleep']
raise "Please specify a CLASS (model)" unless class_name
klass = class_from_string(class_name)
klass.not_geocoded.find_each(batch_size: 100) do |obj|
obj.geocode; obj.save
sleep(sleep_timer.to_f) unless sleep_timer.nil?
end
end
end
##
# Get a class object from the string given in the shell environment.
# Similar to ActiveSupport's +constantize+ method.
#
def class_from_string(class_name)
parts = class_name.split("::")
constant = Object
parts.each do |part|
constant = constant.const_get(part)
end
constant
end