原理是使用ORACLE的CTL文件,然后用系統的命令直接調用導入。
測試過導入幾百個文件,220分鍾導入3.7億條,每秒大概2.8萬條。
1.CTL文件模板
LOAD DATA
INFILE '<!--input file name-->'
APPEND INTO TABLE STAT_PVTEMP
FIELDS TERMINATED BY '|'
(Email,ClientIP,Tstamp,URL)
INFILE '<!--input file name-->'
APPEND INTO TABLE STAT_PVTEMP
FIELDS TERMINATED BY '|'
(Email,ClientIP,Tstamp,URL)
2.用服務程序調用目標文件夾下的文件,然后按照CTL文件模板生成文件。
取相應的配置信息:
static string downloadFilePath = System.Web.Configuration.WebConfigurationManager.AppSettings["DownloadFilePath"].ToString();
static string ctlTemplateFile = System.Web.Configuration.WebConfigurationManager.AppSettings["CtlTemplateFile"].ToString();
static string batTemplateFile = System.Web.Configuration.WebConfigurationManager.AppSettings["BatTemplateFile"].ToString();
static string exeTemplatePath = System.Web.Configuration.WebConfigurationManager.AppSettings["ExeTemplatePath"].ToString();
生成導入文件的執行命令:
static string ctlTemplateFile = System.Web.Configuration.WebConfigurationManager.AppSettings["CtlTemplateFile"].ToString();
static string batTemplateFile = System.Web.Configuration.WebConfigurationManager.AppSettings["BatTemplateFile"].ToString();
static string exeTemplatePath = System.Web.Configuration.WebConfigurationManager.AppSettings["ExeTemplatePath"].ToString();
string batCmd = "sqlldr userid=用戶ID/密碼@服務 control="+ctlFilePath+" log="+logFilePath+" bad="+badFilePath+" errors=1000";
執行的命令的函數:
public static string ExeCommand(string commandText)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
string stroutput = null;
try
{
p.Start();
p.StandardInput.WriteLine(commandText);
p.StandardInput.WriteLine("exit");
stroutput = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
}
catch (Exception ex)
{
stroutput = ex.Message;
}
return stroutput;
}
以上方法僅供學習研究,有好的方法可以討論。
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
string stroutput = null;
try
{
p.Start();
p.StandardInput.WriteLine(commandText);
p.StandardInput.WriteLine("exit");
stroutput = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
}
catch (Exception ex)
{
stroutput = ex.Message;
}
return stroutput;
}