多線程抓取資料測試

2024-05-04 05:58
文章标签 抓取 資料 測試 多線程

本文主要是介绍多線程抓取資料測試,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

select * into test_source_data from sys.all_parameters

select * into test_dest_data from test_source_data where 1=2;

三個cs文件

DataCopy.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace MutlThreadTest
{
    public class DataCopy
    {
        string strDBconn = "Persist Security Info=False;Initial Catalog=GSONG;Data Source=.;User ID=gs;password=xxxxxx;Max Pool Size=10000;timeout=999999999";
        

        #region getSource
        public SqlDataReader getSource()
        {
            SqlDataReader sqldr;
            string strSql = "select * from test_source_data ";
            SqlConnection sqlconn = new SqlConnection(strDBconn);
            if (sqlconn.State !=ConnectionState.Open)
            {
                sqlconn.Open();
            }
            SqlCommand sqlcmd = new SqlCommand(strSql, sqlconn);
            sqldr = sqlcmd.ExecuteReader();


            return sqldr;

        }
        #endregion


        #region getTblFrame
        public DataTable getTblFrame(string strTblName)
        {
            string strQuery = "select top 1 * from " + strTblName;
            DataTable dt = new DataTable();
            DataSet ds = new DataSet();
            using (SqlConnection sqlconn = new SqlConnection(strDBconn))
            {
                if (sqlconn.State != ConnectionState.Open)
                {
                    sqlconn.Open();
                }
                using (SqlDataAdapter sda = new SqlDataAdapter(strQuery, sqlconn))
                {
                    sda.Fill(ds);
                    if (ds != null)
                    {
                        dt = ds.Tables[0];
                    }
                }
            }
            return dt;
        }
        #endregion


        #region bulkCopy
        public void bulkCopy(object obj)
        {
            DataTable dtSource = (obj as mutlThreadPara).vSourceDt;
            string strDestTblName = (obj as mutlThreadPara).strDestTblName;
            using (SqlConnection sqlconn=new SqlConnection(strDBconn))
            {
                if (sqlconn.State !=ConnectionState.Open)
                {
                    sqlconn.Open();
                }
                using (SqlBulkCopy sqlbc=new SqlBulkCopy(sqlconn))
                {
                    sqlbc.DestinationTableName = strDestTblName;
                    sqlbc.WriteToServer(dtSource);
                    dtSource.Clear();
                }

            }
        }
        #endregion

    }
}
 

mutlThreadPara.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace MutlThreadTest
{
    public class mutlThreadPara
    {
        public DataTable vSourceDt;
        public string strDestTblName;
        
    }
}
 

 

Program.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace MutlThreadTest
{
    class Program
    {
        static void Main(string[] args)
        {
            DataCopy dc = new DataCopy();
            SqlDataReader sqldr = dc.getSource();
            string strDestTblName = "test_dest_data";
            DataTable dtDestFrame = dc.getTblFrame(strDestTblName);
            DataTable dtSource = dtDestFrame.Clone();
            if (sqldr.HasRows)
            {
                int iBatchFlag = 0;
                while (sqldr.Read())
                {
                    DataRow dr = dtSource.NewRow();
                    for (int i=0;i<sqldr.FieldCount;i++)
                    {
                        string strColName = sqldr.GetName(i).ToString();
                        dr[strColName] = sqldr[strColName].ToString();
                    }
                    dtSource.Rows.Add(dr);
                    iBatchFlag += 1;
                    if (iBatchFlag>=1000)
                    {
                        mutlThreadPara mtp = new mutlThreadPara { vSourceDt = dtSource ,strDestTblName = strDestTblName };
                        Thread td = new Thread(new ParameterizedThreadStart(dc.bulkCopy));
                        td.Start(mtp);
                        td.IsBackground = false;
                    }


                }
                if (sqldr.IsClosed == false)
                {
                    mutlThreadPara mtp = new mutlThreadPara { vSourceDt = dtSource, strDestTblName = strDestTblName };
                    Thread td = new Thread(new ParameterizedThreadStart(dc.bulkCopy));
                    td.Start(mtp);
                    td.IsBackground = false;
                }
                sqldr.Close();
                
            }
            


            
        }
        
    }

}
 

 

 

 

 

 

 

 

 

 

 

这篇关于多線程抓取資料測試的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/958459

相关文章

Python Selenium动态渲染页面和抓取的使用指南

《PythonSelenium动态渲染页面和抓取的使用指南》在Web数据采集领域,动态渲染页面已成为现代网站的主流形式,本文将从技术原理,环境配置,核心功能系统讲解Selenium在Python动态... 目录一、Selenium技术架构解析二、环境搭建与基础配置1. 组件安装2. 驱动配置3. 基础操作模

一文教你Python如何快速精准抓取网页数据

《一文教你Python如何快速精准抓取网页数据》这篇文章主要为大家详细介绍了如何利用Python实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例

PHP抓取网站图片脚本

方法一: <?phpheader("Content-type:image/jpeg"); class download_image{function read_url($str) { $file=fopen($str,"r");$result = ''; while(!feof($file)) { $result.=fgets($file,9999); } fclose($file); re

用Java抓取CSDN主页上的图片

一,步骤一:获取网页源码 1,定义要爬取的页面的URL对象 //定义即将访问的链接String url="http://www.csdn.net";//获取CSDN的URL对象URL realURL = new URL(url); 2,获得这个链接的一个连接对象 URLConnection connection = realURL.openConnection();

【go语言爬虫】go语言高性能抓取手机号码归属地、所属运营商

一、需求分析 根据手机号码获取手机号码的归属地和所属运营商类型 类似:四川 18683339513 乐山 614000 0833 中国联通 二、运行效果 三、实现源代码 package main//网址:https://github.com/M2shad0w/phone-go//安装包:go get github.com/M2shad0w/phone-goimport ("fmt

【python爬虫】网贷天眼平台表格数据抓取

一、需求分析 抓取url: http://www.p2peye.com/shuju/ptsj/ 抓取字段: 昨日数据 排序 平台名称 成交额 综合利率 投资人 借款周期 借款人 满标速度 累计贷款余额 资金净流入 二、python爬虫源代码 # -*- coding:utf-8*-import sysreload(sys)sys.setdefaultencoding('utf-

【R语言爬虫】网贷天眼数据平台表格数据抓取2

一、需求分析 抓取url: http://www.p2peye.com/shuju/ptsj/ 昨日数据: 字段:排序 平台名称 成交额 综合利率 投资人 借款周期 借款人 满标速度 累计贷款余额 资金净流入 二、rvest爬虫实现源代码 rm(list=ls())gc()options(scipen = 200)library('rvest')timestart<-Sys

【python 百度指数抓取】python 模拟登陆百度指数,图像识别百度指数

一、算法思想 目的奔着去抓取百度指数的搜索指数,搜索指数的爬虫不像是其他爬虫,难度系数很高,分析之后发现是图片,坑爹的狠,想了下,由于之前做过身份证号码识别,验证码识别之类,豁然开朗,不就是图像识别麽,图像识别我不怕你,于是就有了思路,果然有异曲同工之妙,最后成功被我攻破了,大致思路如下: 1、首先得模拟登陆百度账号(用selenium+PhantomJS模拟登陆百度,获取cookie) 2

python scrapy爬虫框架 抓取BOSS直聘平台 数据可视化统计分析

使用python scrapy实现BOSS直聘数据抓取分析 前言   随着金秋九月的悄然而至,我们迎来了业界俗称的“金九银十”跳槽黄金季,周围的朋友圈中弥漫着探索新机遇的热烈氛围。然而,作为深耕技术领域的程序员群体,我们往往沉浸在代码的浩瀚宇宙中,享受着解决技术难题的乐趣,却也不经意间与职场外部的风云变幻保持了一定的距离,对行业动态或许仅有一鳞半爪的了解,甚至偶有盲区。   但正是这份对技术

Scrapy ——自动多网页爬取(抓取某人博客所有文章)(四)

首先创建project:   [python]  view plain  copy     转存失败重新上传取消<