c# oracle 添加记录,如何使用C#将Oracle中的400万条记录更快地插入Elasticsearch表中?...

我有以下用C#编写的代码,但是据此,我需要4-5天的时间才能将数据从Oracle数据库迁移到Elasticsearch。我将以100批为单位插入记录。是否还有其他方法可以使400万条记录的迁移速度更快(如果可能的话,可能不到一天)?

public static void Selection()

{

for(int i = 1; i < 4000000; i += 1000)

{

for(int j = i; j < (i+1000); j += 100)

{

OracleCommand cmd = new OracleCommand(BuildQuery(j),

oracle_connection);

OracleDataReader reader = cmd.ExecuteReader();

List list=CreateRecordList(reader);

insert(list);

}

}

}

private static List CreateRecordList(OracleDataReader reader)

{

List l = new List();

string[] str = new string[7];

try

{

while (reader.Read())

{

for (int i = 0; i < 7; i++)

{

str[i] = reader[i].ToString();

}

Record r = new Record(str[0], str[1], str[2], str[3],

str[4], str[5], str[6]);

l.Add(r);

}

}

catch (Exception er)

{

string msg = er.Message;

}

return l;

}

private static string BuildQuery(int from)

{

int to = from + change - 1;

StringBuilder builder = new StringBuilder();

builder.AppendLine(@"select * from");

builder.AppendLine("(");

builder.AppendLine("select FIELD_1, FIELD_2,

FIELD_3, FIELD_4, FIELD_5, FIELD_6,

FIELD_7, ");

builder.Append(" row_number() over(order by FIELD_1)

rn");

builder.AppendLine(" from tablename");

builder.AppendLine(")");

builder.AppendLine(string.Format("where rn between {0} and {1}",

from, to));

builder.AppendLine("order by rn");

return builder.ToString();

}

public static void insert(List l)

{

try

{

foreach(Record r in l)

client.Index(r, "index", "type");

}

catch (Exception er)

{

string msg = er.Message;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值