Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing chunkSize in writesql #106

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/main/java/joinery/DataFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -2281,9 +2281,9 @@ public static final DataFrame<Object> readSql(final ResultSet rs)
* @param sql the SQL statement
* @throws SQLException if an error occurs executing the statement
*/
public final void writeSql(final Connection c, final String sql)
public final void writeSql(final Connection c, final String sql, final int chunkSize)
throws SQLException {
writeSql(c.prepareStatement(sql));
writeSql(c.prepareStatement(sql), chunkSize);
}

/**
Expand All @@ -2293,9 +2293,9 @@ public final void writeSql(final Connection c, final String sql)
* @param stmt a prepared insert statement
* @throws SQLException if an error occurs executing the statement
*/
public final void writeSql(final PreparedStatement stmt)
public final void writeSql(final PreparedStatement stmt, final int chunkSize)
throws SQLException {
Serialization.writeSql(this, stmt);
Serialization.writeSql(this, stmt, chunkSize);
}

public final String toString(final int limit) {
Expand Down
64 changes: 59 additions & 5 deletions src/main/java/joinery/impl/Serialization.java
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,23 @@ public static DataFrame<Object> readSql(final ResultSet rs)
}
}

public static <V> void writeSql(final DataFrame<V> df, final PreparedStatement stmt)
/**
*
* Executes SQL statement to write local data to SQL database in splits.
* User can specific the number of splits to divide rows of data so the method operates efficiently and minimizes chances of failing
* while writing big query.
* It takes DataFrame object df, SQL statement stmt, and number of splits, chunkSize.
* This method writes directly to connected SQL database, so there is no return type.
*
* @param df DataFrame object of loaded data
* @param stmt SQL statement
* @param chunkSize number of splits for rows of data
* @return none
* Link to the issue: https://github.com/cardillo/joinery/issues/94
*/


public static <V> void writeSql(final DataFrame<V> df, final PreparedStatement stmt, int chunkSize)
throws SQLException {
try {
ParameterMetaData md = stmt.getParameterMetaData();
Expand All @@ -456,13 +472,51 @@ public static <V> void writeSql(final DataFrame<V> df, final PreparedStatement s
columns.add(md.getParameterType(i));
}

for (int r = 0; r < df.length(); r++) {
for (int c = 1; c <= df.size(); c++) {
stmt.setObject(c, df.get(r, c - 1));
if (chunkSize <= 0 | chunkSize >= df.length())
{
chunkSize = 1;
}

int split = df.length() / chunkSize;



if (chunkSize == 1)
{
for (int r = 0; r < df.length(); r++) {
for (int c = 1; c <= df.size(); c++) {
stmt.setObject(c, df.get(r, c - 1));
}
stmt.addBatch();
}
stmt.addBatch();
}

else
{
int index = 0;

for (int z = 0; z < chunkSize-1; z++) {
for (int r = 0; r < split; r++) {
for (int c = 1; c <= df.size(); c++) {
stmt.setObject(c, df.get(index, c - 1));
}
++index;
stmt.addBatch();
}
}

for (;index < df.length();index++)
{
for (int c = 1; c <= df.size(); c++) {
stmt.setObject(c, df.get(index, c - 1));
}
stmt.addBatch();

}
}



stmt.executeBatch();
} finally {
stmt.close();
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/joinery/DataFrameSerializationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,15 +283,16 @@ public void testToStringEmptyHeader()
DataFrame<Object> dfEmptyHeader = DataFrame.readCsv(ClassLoader.getSystemResourceAsStream("serialization_empty_header.csv"));
dfEmptyHeader.transpose().toString();
}

//Link to the issue: https://github.com/cardillo/joinery/issues/94
@Test
public void testToFromSql()
throws Exception {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
try (Connection dbc = DriverManager.getConnection("jdbc:derby:memory:testdb;create=true")) {
dbc.createStatement().executeUpdate("create table test (category varchar(32), name varchar(32), value int)");
PreparedStatement stmt = dbc.prepareStatement("insert into test values (?,?,?)");
df.writeSql(stmt);
int chunkSize = 5;
df.writeSql(stmt, chunkSize);

Map<Object, Object> names = new HashMap<>();
names.put("CATEGORY", "category");
Expand Down