zoukankan      html  css  js  c++  java
  • 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

    [Haima的博客] http://www.cnblogs.com/haima/
  • 相关阅读:
    缺少一个=出现的问题
    快速排序+归并排序
    ACwing简单题(14)
    浅谈#ifndef
    fstream 使用详解
    _stat函数的使用
    关于文件结构体的使用
    new的使用
    ACwing13题目
    ACwing13题
  • 原文地址:https://www.cnblogs.com/haima/p/15000353.html
Copyright © 2011-2022 走看看