人工智能学习

ruby rails 批量插入数据,bulk_insert-----Gem包使用

本文主要是介绍ruby rails 批量插入数据,bulk_insert-----Gem包使用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Gemfile文件里添加

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

这篇关于ruby rails 批量插入数据,bulk_insert-----Gem包使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!