Created
April 27, 2023 20:08
-
-
Save parj/74ed451b7d0a4bd9f498dff0089cb867 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileWriter; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import org.json.simple.JSONArray; | |
import org.json.simple.JSONObject; | |
public class DatabaseToJson { | |
private static final int BATCH_SIZE = 10000; // Number of rows to fetch at a time | |
private static final int THREAD_COUNT = 4; // Number of threads to use for extraction | |
private static final String DB_URL = "jdbc:mysql://localhost:3306/mydatabase"; | |
private static final String DB_USER = "root"; | |
private static final String DB_PASSWORD = "mypassword"; | |
private static final String OUTPUT_FILE = "data.json"; | |
private static final String SELECT_QUERY = "SELECT * FROM mytable LIMIT ? OFFSET ?"; | |
public static void main(String[] args) throws Exception { | |
long start = System.currentTimeMillis(); | |
JSONArray jsonArray = new JSONArray(); | |
int offset = 0; | |
int totalRows = getTotalRows(); | |
ExecutorService executor = Executors.newFixedThreadPool(THREAD_COUNT); | |
while (offset < totalRows) { | |
executor.execute(new ExtractTask(offset, BATCH_SIZE, jsonArray)); | |
offset += BATCH_SIZE; | |
} | |
executor.shutdown(); | |
while (!executor.isTerminated()) {} | |
try (FileWriter fileWriter = new FileWriter(OUTPUT_FILE)) { | |
fileWriter.write(jsonArray.toJSONString()); | |
} | |
long end = System.currentTimeMillis(); | |
System.out.println("Time taken: " + (end - start) / 1000.0 + " seconds"); | |
} | |
private static int getTotalRows() throws SQLException { | |
try (Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD); | |
PreparedStatement preparedStatement = connection.prepareStatement("SELECT COUNT(*) FROM mytable"); | |
ResultSet resultSet = preparedStatement.executeQuery()) { | |
resultSet.next(); | |
return resultSet.getInt(1); | |
} | |
} | |
private static class ExtractTask implements Runnable { | |
private int offset; | |
private int batchSize; | |
private JSONArray jsonArray; | |
public ExtractTask(int offset, int batchSize, JSONArray jsonArray) { | |
this.offset = offset; | |
this.batchSize = batchSize; | |
this.jsonArray = jsonArray; | |
} | |
@Override | |
public void run() { | |
try (Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD); | |
PreparedStatement preparedStatement = connection.prepareStatement(SELECT_QUERY)) { | |
preparedStatement.setInt(1, batchSize); | |
preparedStatement.setInt(2, offset); | |
try (ResultSet resultSet = preparedStatement.executeQuery()) { | |
while (resultSet.next()) { | |
JSONObject jsonObject = new JSONObject(); | |
jsonObject.put("id", resultSet.getInt("id")); | |
jsonObject.put("name", resultSet.getString("name")); | |
jsonObject.put("age", resultSet.getInt("age")); | |
// Add more fields as needed | |
synchronized (jsonArray) { | |
jsonArray.add(jsonObject); | |
} | |
} | |
} | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment