gem 'bulk_insert' #批量插入
bundle install
["1.180.3.187", 161, 2601] ["1.180.3.178", 161, 2601, 44222, 44333]
def test3(param_str) log_dir = File.expand_path(File.join(Rails.root.to_s, 'log/nmapresult01_def_v2.log')) file = File.open(log_dir) file.each_line { |line| obj_line = JSON.parse(line) if obj_line.count > 0 puts "obj_line:#{obj_line}" # 批量插入 time = Time.now ip = obj_line.first obj_line.delete_at(0) #删除数据第一个元素 Port.bulk_insert(:ip, :port, :created_at, :updated_at) do |ip_port| ip_port.set_size = 1000 obj_line.each do |pt| ip_port.add [ip, pt, time, time] end end end } file.close param_str end
obj_line:["1.180.3.187", 161, 2601] (0.2ms) BEGIN SQL (0.5ms) INSERT INTO `ports` (`ip`,`port`,`created_at`,`updated_at`) VALUES ('1.180.3.187',161,'2021-07-11 17:43:21','2021-07-11 17:43:21'),('1.180.3.187',2601,'2021-07-11 17:43:21','2021-07-11 17:43:21') (13.1ms) COMMIT obj_line:["1.180.3.178", 161, 2601, 44222, 44333] (0.2ms) BEGIN SQL (0.4ms) INSERT INTO `ports` (`ip`,`port`,`created_at`,`updated_at`) VALUES ('1.180.3.178',161,'2021-07-11 17:43:21','2021-07-11 17:43:21'),('1.180.3.178',2601,'2021-07-11 17:43:21','2021-07-11 17:43:21'),('1.180.3.178',44222,'2021-07-11 17:43:21','2021-07-11 17:43:21'),('1.180.3.178',44333,'2021-07-11 17:43:21','2021-07-11 17:43:21') (12.5ms) COMMIT
class CreatePorts < ActiveRecord::Migration[5.0] def change create_table :ports do |t| t.string :ip t.integer :port, default: 0 t.timestamps end end end
CREATE TABLE `ports` ( `id` int(11) NOT NULL AUTO_INCREMENT, `ip` varchar(255) DEFAULT NULL, `port` int(11) DEFAULT '0', `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
参考示例文章:
https://www.cnblogs.com/lmg-jie/p/9199529.html
文档
https://github.com/jamis/bulk_insert