将表格数据写入到java中的多个CSV文件中
问题描述:
我有一个名为employee的表,其中有10 000条记录。将表格数据写入到java中的多个CSV文件中
我必须使用多线程Java将这些数据写入多个CSV文件,每个文件有2 000条记录。
示例代码如下:
public class PrintbyThreads {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(5);
for (int i = 0; i < 5; i++) {
Runnable worker = new PrintFiles("" + i);
executor.execute(worker);
}
executor.shutdown();
while (!executor.isTerminated()) {
}
System.out.println("Finished all threads");
}
}
和
public class PrintFiles extends Thread implements Runnable {
private String command;
PrintFiles() {
super("my extending thread");
System.out.println("my thread created" + this);
start();
}
public PrintFiles(String command) {
this.command = command;
}
public void run() {
System.out.println(Thread.currentThread().getName()
+ " Start. Command = " + command);
dataBaseExecution();
System.out.println(Thread.currentThread().getName() + " End. Command ="
+ command);
}
public synchronized void dataBaseExecution() {
String tableName = "Employee";
String filename = "D:/db2csv/";
int recordsAtTime = 2000;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(
"URL", "Uname", "Password");
Statement stmt = con.createStatement();
stmt.setFetchSize(recordsAtTime);
ResultSet rs = stmt.executeQuery("select empid,empname,managerid from Employee");
int columnCount = rs.getMetaData().getColumnCount();
FileWriter fw = new FileWriter(filename + "" + tableName + ".csv");
for (int i = 1; i <= columnCount; i++) {
fw.append(rs.getMetaData().getColumnName(i));
fw.append(",");
}
fw.append(System.getProperty("line.separator"));
while (rs.next()) {
for (int i = 1; i <= columnCount; i++) {
if (rs.getObject(i) != null) {
String data = rs.getObject(i).toString();
fw.append(data);
fw.append(",");
} else {
String data = "null";
fw.append(data);
fw.append(",");
}
}
// new line entered after each row
fw.append(System.getProperty("line.separator"));
}
fw.flush();
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public String toString() {
return this.command;
}
}
我的代码是给所有有10000 records.but一个CSV文件,我需要有2000条记录中的每个5个CSV文件。
线程1必须先记录2000处理成employee1.csv
线程2具有其他2000条记录加工成employee2.csv
,,,,,等。
通过使用我的线程代码,我该如何实现这个要求?
答
在你的代码中,你可能需要一个分区器,它会给你一个来自索引并索引线程需要处理的内容。
公共类MyPartitioner {
public Map partition(int gridSize,int range) {
Map partitionMap = new HashMap();
int startingIndex = 0;
int endingIndex = range;
for(int i=0; i< gridSize; i++){
Map threadMap = new HashMap();
threadMap.putInt("startingIndex",startingIndex);
threadMap.putInt("endingIndex", endingIndex);
startingIndex = endingIndex+1;
endingIndex += range;
partitionMap.put("Thread:-"+i, threadMap);
}
return partitionMap;
}
}
执行人框架使用这个从指数和指数的工作线程来处理分配行不是所有的行。 在过滤条件的选择查询使用
where id >= :fromId and id <= :toId
请参阅https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question ...你的问题太广泛了,它看起来像“请为我做我的工作” – GhostCat
对不起。我也有一个程序。但由于某种安全原因,我不在这里编写代码。我不需要完整的代码。如果达到2000条记录,只需创建一个更多的CSV文件即可。 –
没有人说你需要放置*生产*代码。放个[mcve]!否则,您将收到**没有任何**,但赞誉和关闭请求。 – GhostCat