Recently about Database
The Problem
You downloaded and installed the latest 64-bit DMG from MySQL and now you’re getting “could not load driver” errors in DBI (Ruby or Perl) and Rails.
The Cause
In Ruby, the ‘mysql’ and ‘mysql-2’ gems were compiled against older version of the MySql library. Ditto DBD:Mysql module in Perl.
The Solution
Update 6/6/2011: Google lead me to a better solution:
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/
That should work for everything! But in case it doesn’t, this will fix Rails:
sudo install_name_tool -change libmysqlclient.16.dylib \ /usr/local/mysql/lib/libmysqlclient.16.dylib \ /usr/local/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle
And this will fix Ruby’s DBI:
sudo install_name_tool -change libmysqlclient.16.dylib \ /usr/local/mysql/lib/libmysqlclient.16.dylib \ /usr/local/lib/ruby/gems/1.9.1/gems/mysql-2.8.1/lib/mysql_api.bundle
Note: you’ll need to substitute both the appropriate path to the gems, and the appropriate version of the MySQL library.
Fixing Perl’s DBI is a little trickier. You need to download the MySql DBD source and run the install_name_tool on the bundle before you install. Something like:
sudo install_name_tool -change libmysqlclient.16.dylib \ /usr/local/mysql/lib/libmysqlclient.16.dylib \ /path-to-my-sql-dbd-source-folder-with-the-bundle-file/mysql.bundle
If you are interested in learning about the Apache Cassandra distributed database, which is used by companies like Digg, Facebook, and Twitter, then you are in luck. Riptano is giving a one-day training course at our cool new training facility. Here you can find information about the course or you can go direct to the registration page.
Be sure to check back at our NIC-U Upcoming Training Courses page for upcoming courses.
Simple Put and Get Example
Direct HBase API:
public class PutAndGet {
public static void main(String[] args) throws IOException {
HTable hTable = new HTable("test");
byte[] rowId = Bytes.toBytes("abcd");
byte[] famA = Bytes.toBytes("famA");
byte[] col1 = Bytes.toBytes("col1");
Put put = new Put(rowId).
add(famA, col1, Bytes.toBytes("hello world!"));
hTable.put(put);
Get get = new Get(rowId);
Result result = hTable.get(get);
byte[] value = result.getValue(famA, col1);
System.out.println(Bytes.toString(value));
}
}
HBase-dsl API:public class PutAndGetWithDsl {
public static void main(String[] args) throws IOException {
HBase<QueryOps, String> hBase = new HBase<QueryOps<String>, String>(String.class);
hBase.save("test").
row("abcd").
family("famA").
col("col1", "hello world!");
String value = hBase.fetch("test").
row("abcd").
family("famA").
value("col1", String.class)
System.out.println(value);
}
}
Now this is where the dsl becomes more powerful!Scanner Example
Direct HBase API:
public class Scanner {
public static void main(String[] args) throws IOException {
byte[] famA = Bytes.toBytes("famA");
byte[] col1 = Bytes.toBytes("col1");
HTable hTable = new HTable("test");
Scan scan = new Scan(Bytes.toBytes("a"), Bytes.toBytes("z"));
scan.addColumn(famA, col1);
SingleColumnValueFilter singleColumnValueFilterA = new SingleColumnValueFilter(
famA, col1, CompareOp.EQUAL, Bytes.toBytes("hello world!"));
singleColumnValueFilterA.setFilterIfMissing(true);
SingleColumnValueFilter singleColumnValueFilterB = new SingleColumnValueFilter(
famA, col1, CompareOp.EQUAL, Bytes.toBytes("hello hbase!"));
singleColumnValueFilterB.setFilterIfMissing(true);
FilterList filter = new FilterList(Operator.MUST_PASS_ONE, Arrays
.asList((Filter) singleColumnValueFilterA,
singleColumnValueFilterB));
scan.setFilter(filter);
ResultScanner scanner = hTable.getScanner(scan);
for (Result result : scanner) {
System.out.println(Bytes.toString(result.getValue(famA, col1)));
}
}
}
HBase-dsl API:public class ScannerWithDsl {
public static void main(String[] args) throws IOException {
HBase<QueryOps, String> hBase = new HBase<QueryOps<String>, String>(String.class);
hBase.scan("test","a","z").
select().
family("famA").
col("col1").
where().
family("famA").
col("col1").eq("hello world!","hello hbase!").
foreach(new ForEach() {
@Override
public void process(Row row) {
System.out.println(row.value("famA", "col1", String.class));
}
});
}
}
See the unit tests, for more examples.
I've just published the second article of a two-part series in the December 2009 NFJS Magazine on Hibernate Performance Tuning. Here's the abstract:
Tuning performance in Hibernate applications is all about reducing the number of database queries or eliminating them entirely using caching. In the first article in this two part series, you saw how to tune object retrieval using eager fetching techniques to optimize queries and avoid lazy-loads. In this second and final article, I'll show you how inheritance strategy affects performance, how to eliminate queries using the Hibernate second-level cache, and show some simple but effective tools you can use to monitor and profile your applications.
If you are using Hibernate and want to know more about how inheritance affects performance, how to use the second-level cache, and some simple monitoring and profiling techniques, check it out and let me know what you think. Note that NFJS Magazine does require a subscription.
I've just published an article in the November 2009 NFJS Magazine on Hibernate Performance Tuning. Here's the abstract:
Many developers treat Hibernate like a "black box" and assume it will simply "Do the Right Thing" when it comes to all things related to the underlying database. This is a faulty assumption because, while Hibernate is great at the mechanics of database interaction, it cannot and will likely not ever be able to figure out the specific details of your domain model and discern the most efficient and best performing data access strategies. In this first article of a two part series, I'll show you how to achieve better performance in your Hibernate applications by focusing on tuning object retrieval, which forms the basis of your "fetch plan" for finding and storing objects in the database.
If you are using Hibernate and want to know more about how to change how objects are fetched from the database, check it out and let me know what you think. Note that NFJS Magazine does require a subscription.

