Psychedelic Panorama of Foo

Á¦ À̸§ÀÌ Inigo Montoya ÀÔ´Ï´Ù. ´ç½ÅÀÌ ³» ¾Æ¹öÁö¸¦ Á׿´¾î. Á×À» ÁغñÇØ.

¼ö¿äÀÏ, 9¿ù 02, 2009

 

Console Progress Bar in Groovy

¾È¿µÇϼ¼¿ä! Recently, I've been doing a lot of work with ETL (Extract, Transform, Load) scripting. I've found that groovy is an ideal language for ETL. That's not what this blog is about though. Typically, when loading data into a database using java, ruby, groovy, etc..., you don't know how far along the load is. You only know when it finishes. Lately, I've needed to load 30k+ records at a time. This can take a pretty long while to do. Sometimes, I really want to know how far along it is. In SQL, this would be an impossible feat, but with groovy, it's quite easy.

Below is some example code for a progress bar. The trick is in \r which moves the print cursor to the front of the line without a newline:

def template = "\r|%s| %s" def progressLength = 70 def progressRatio = progressLength/100 for (i in 0..max) { def percent = (int) ((i/max) * 100) def progress = (int) ((i / max) * (100 * progressRatio)) def progressBuffer = new StringBuilder() for (x in 0..progress) { progressBuffer.append('=') } for (x in progress..progressLength) { progressBuffer.append(' ') } print String.format(template, progressBuffer, "${percent}%") } println ''

If I want to use this in my ETL, here's an example:

def insertStatement = targetDb.getConnection().prepareStatement(insertBuffer.toString()) int recordCount = 0 def template = "\r|%s| %s (%d/%d) records" def progressLength = 50 // This is the length of the progress bar in characters. Adjust to the size of your console as appropriate. def progressRatio = progressLength/100 println "Copying records " sourceDb.eachRow(sourceQuery.toString(), { sourceRow -> for (i in 0..columnNames.size()-1) { insertStatement.setObject(i+1, sourceRow[i], getColumnTypeByName(sourceMd, tableName, columnNames[i])) } insertStatement.execute() recordCount++ def percent = (int) ((recordCount/totalRecords) * 100) def progress = (int) ((recordCount/totalRecords) * (100 * progressRatio)) def progressBuffer = new StringBuilder() for (x in 0..progress) { progressBuffer.append('=') } for (x in progress..progressLength) { progressBuffer.append(' ') } print String.format(template, progressBuffer, "${percent}%", recordCount, totalRecords) }) println "...done. $recordCount records migrated" insertStatement.close()

There you have it. It copies records from one database table to another and gives the progress of that copy. Here is the example output.

Migrating table CA_A21_SUB_ACCT_T with 182 records Truncating table CA_A21_SUB_ACCT_T Copying records |=================================================== | 100% (182/182) records...done. 182 records migrated Migrating table CA_ACCOUNT_EXT_T with 20826 records Truncating table CA_ACCOUNT_EXT_T Copying records |= | 1% (292/20826) records

·¹À̺í: ,





This page is powered by Blogger. Isn't yours?

¿¡ °¡ÀÔ °Ô½Ã¹° [Atom]