サイズが5mbから1gb+の範囲のスペース区切りのログファイルを読み取り、ファイルに含まれる情報に基づいてレポートを印刷するときに後で使用できるように、この情報をMySQLデータベースに保存するアプリケーションで忙しいです。私が試した/見つけた方法はうまくいきますが、非常に遅いです。
私は何か間違ったことをしていますか?または、非常に大きなテキストファイルを処理するためのより良い方法はありますか?
次のようにtextfieldparserを使用してみました。
Using parser As New TextFieldParser("C:\logfiles\testfile.txt")
parser.TextFieldType = FieldType.Delimited
parser.CommentTokens = New String() {"#"}
parser.Delimiters = New String() {" "}
parser.HasFieldsEnclosedInQuotes = False
parser.TrimWhiteSpace = True
While Not parser.EndOfData
Dim input As String() = parser.ReadFields()
If input.Length = 10 Then
'add this to a datatable
End If
End While
End Using
これは機能しますが、大きなファイルの場合は非常に遅くなります。
次に、事前にディレクトリに書き込んだschema.iniファイルと組み合わせて、次の関数に従ってテキストファイルへのOleDB接続を使用してみました。
Function GetSquidData(ByVal logfile_path As String) As System.Data.DataTable
Dim myData As New DataSet
Dim strFilePath As String = ""
If logfile_path.EndsWith("\") Then
strFilePath = logfile_path
Else
strFilePath = logfile_path & "\"
End If
Dim mySelectQry As String = "SELECT * FROM testfile.txt WHERE Client_IP <> """""
Dim myConnection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFilePath & ";Extended Properties=""text;HDR=NO;""")
Dim dsCmd As New System.Data.OleDb.OleDbDataAdapter(mySelectQry, myConnection)
dsCmd.Fill(myData, "logdata")
If Not myConnection.State = ConnectionState.Closed Then
myConnection.Close()
End If
Return myData.Tables("logdata")
End Function
schema.iniファイル:
[testfile.txt]
Format=Delimited( )
ColNameHeader=False
Col1=Timestamp text
Col2=Elapsed text
Col3=Client_IP text
Col4=Action_Code text
Col5=Size double
Col6=Method text
Col7=URI text
Col8=Ident text
Col9=Hierarchy_From text
Col10=Content text
誰かがこれらのファイルをより速く読む方法について何か考えがありますか?
-編集-上記のコードのタイプミスを修正しました