Click here to Skip to main content
Click here to Skip to main content

Infobright/MySQL Statistics Utility

By , 6 Jun 2012
 

Introduction

This Java CLI uses the JDBC driver to connect to an Infobright database and display its statistics (size, compressed size, compression ratio, etc.). The code can easily be modified for use with a MySQL database. The goal of this project is to provide simple, easy to read code, that shows users how to work with Java, the JDBC driver, Infobright, and MySQL.

Required software  

  • Java JRE (1.5 or greater) 
  • Java IDE (Eclipse recommended) 
  • Infobright or MySQL database  
  • JDBC Driver for MySQL  

Running the executable 

Included with the source code is a runnable JAR file. It can be run from the terminal as follows: 

java -jar ice_tools.jar [OPTIONS]  

The options are listed below.  

  • -u username  
  • -P port (NOTE: This is a capital 'P') 
  • -h hostname 
  • -p (NOTE: This flag does not take any arguments. If used, the program will ask for a password.)

 i.e. java -jar ice_tools.jar -u admin -P 3306 -h 192.168.1.10 -p 

 NOTE: If an option is not specified, the default will be used.  

  • Username: 'root' 
  • Password: blank
  • Port: '5029' 
  • Host: 'localhost' 

Editing the source code

Using the IDE of your choosing, create a new project and import the provided files. In order to get the  code to run successfully, you must download the JDBC Driver for MySQL and add a reference to the included JAR file. To do this in Eclipse, configure the build path and add the external JAR as shown below.  

 

Now, let's take a look at some of the code. The UserInterface class is the main class for the command line interface. The code  is not very interesting as it asks for user input, iterates through a data structure, and prints out formatted text.

The MySqlConnection class is also pretty straight forward. It holds all of the connection information and the functions used to connect and disconnect from the database. 

When the program is first run, a statistics data structure is built. I would like to take a moment to describe this structure. It has three levels: overall statistics, database statistics, and tables statistics (column statistics could be added as a fourth level). The overall statistics are held in the Statistic class. It has compressed size, raw size, compression, and a hash map that maps database names to DatabaseStatistic objects. The DatabaseStatistic class holds statistics for a particular database: compressed size, raw size, compression, and a hash map that maps table names to TableStatistic objects. The TableStatistic class holds statistics for a particular table: compressed size, raw size, and compression. Note that these classes are very similar and could probably be reduced to one class. I only keep them separate to better visualize the data. 

The Statistic class contains the most interesting code. The functions inside this class are used to build the data structure. The function listed below is used to gather table statistics for a specific database. It returns a hash map of the results. Note that runQuery is a helper function.

private Map<String, TableStatistic> getTables(String databaseName) throws SQLException {
	ResultSet results = runQuery("USE " + databaseName);
	String query = "SHOW TABLE STATUS WHERE ENGINE='BRIGHTHOUSE'";
	Map<String, TableStatistic> tables = new HashMap<String, TableStatistic>();
	results = runQuery(query);
	while (results.next()) {
		double compressedSize = Double.parseDouble(results.getString("Data_length")) / 1048576.0;
		double compression = Double.parseDouble(results.getString("Comment").split(": ")[1].split(",")[0]);
		double rawSize = compressedSize * compression;
		tables.put(results.getString("Name"), new TableStatistic(rawSize, compressedSize, compression));
	}
	results.close();
	return tables;

} 

NOTE: The above query is for an Infobright database. If you would like to get statistics for an InnoDB or MyISAM database, remove "where Engine='BRIGHTHOUSE'" from the query. Also, keep in mind that the compression ratio will not be stored in the the "Comment" field. This is specific to Infobright.

Useful Queries 

When tackling this project, I ran into several issues. Here are some MySQL queries that I found helpful. 

This lists the size (in MB) of each individual database:  

SELECT table_schema, sum( data_length + index_length ) / 1024 / 1024 'Data Base Size in MB',TABLE_COMMENT FROM information_schema.TABLES GROUP BY table_schema; 

This shows the version of the currently installed DBMS:  

show variables like 'version_comment'; 

History

The most recent version adds the ability to view total raw size, total compressed size, and total compression for the entire database. 

Conclusion 

This code is meant to be used as a starting point for building a database statistics application. You are encouraged to modify the code to fulfill your specific needs. 

License

This article, along with any associated source code and files, is licensed under The MIT License

About the Author

Ryan Krage
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questiontotal raw size of the entire databasememberFrancis Nicholas Jr4 Jun '12 - 5:37 
Thank you for your response Ryan. Is there a way I can get the total raw size of the entire database? If so, I can I go about it? if not, is there a query that I can run some query to determine that size? I am asking because InfoBright goes by 1 TB of raw database size per license and I want to determine if we are over 1 TB already.

I look forward to hearing from you.
AnswerRe: total raw size of the entire databasememberRyan Krage4 Jun '12 - 5:53 
Unfortunately, there is no simple query that finds total raw size. This does seem like an important statistic to have, so in the next few days I will update the Java utility to include total raw size.
GeneralRe: total raw size of the entire database [modified]memberFrancis Nicholas Jr4 Jun '12 - 7:18 
Yes. I think it is a very important statistic. Especially when it comes to licensing limitations with InfoBright. One needs to know the overall database raw disk size.

modified 5 Jun '12 - 7:06.

GeneralRe: total raw size of the entire databasememberRyan Krage6 Jun '12 - 9:30 
I just updated the article. Download the zip file and test out the new features.
GeneralRe: total raw size of the entire databasememberFrancis Nicholas Jr11 Jun '12 - 3:31 
I just this. I tested the code and it worked beautifully. Thanks again for everything.
 
Please, again, let me know how I can contribute to this project moving forward.
GeneralRe: total raw size of the entire databasememberRyan Krage11 Jun '12 - 10:57 
Do you have any feature requests? I am releasing a similar C# WPF tool soon. Would you be interested in a Java GUI?
GeneralRe: total raw size of the entire databasememberFrancis Nicholas Jr12 Jun '12 - 6:33 
I am definitely interested in a Java GUI
GeneralRe: total raw size of the entire databasememberFrancis Nicholas Jr11 Jun '12 - 3:17 
Ryan - Any plan on updating the code? Please let me know how I can be of help.
Generalstatistics to a mySQL databasememberFrancis Nicholas Jr27 Jun '12 - 8:42 
Ryan: Please what part of the code can I edit to also apply the statistics to a mySQL database instead of InfoBright?
GeneralRe: statistics to a mySQL databasememberRyan Krage28 Jun '12 - 7:54 
First, you will need to modify the getVersion function in the Statistics class to account for a MySQL database. Now, most MySQL storage engines do not support compression so you will probably have to get rid of the compression and raw size statistics when building up the data structure. This is done in the Statistics class as well. Lastly, you will need to edit the printing functions in the UserInterface class to reflect the changes made to the statistics.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 6 Jun 2012
Article Copyright 2012 by Ryan Krage
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid