Skip to content

Instantly share code, notes, and snippets.

@matteobertozzi
Created December 16, 2012 21:55
Show Gist options
  • Select an option

  • Save matteobertozzi/4313537 to your computer and use it in GitHub Desktop.

Select an option

Save matteobertozzi/4313537 to your computer and use it in GitHub Desktop.
import java.io.*;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.*;
public class HBaseFill {
private static void createTable(final HBaseAdmin admin, final byte[] tableName,
final byte[]... families) throws IOException {
HTableDescriptor htd = new HTableDescriptor(tableName);
for (byte[] family: families) {
HColumnDescriptor hcd = new HColumnDescriptor(family);
htd.addFamily(hcd);
}
byte[][] splitKeys = new byte[16][];
byte[] hex = Bytes.toBytes("0123456789abcdef");
for (int i = 0; i < 16; ++i) {
splitKeys[i] = new byte[] { hex[i] };
}
admin.createTable(htd, splitKeys);
}
public static void loadData(final HTable table, int rows, byte[]... families)
throws IOException {
byte[] qualifier = Bytes.toBytes("q");
table.setAutoFlush(false);
for (int i = 0; i < rows; ++i) {
byte[] value = Bytes.add(Bytes.toBytes(System.currentTimeMillis()), Bytes.toBytes(rows));
byte[] key = Bytes.toBytes(MD5Hash.getMD5AsHex(value));
Put put = new Put(key);
put.setWriteToWAL(false);
for (byte[] family: families) {
put.add(family, qualifier, value);
}
table.put(put);
if ((i % 5000) == 0) {
System.out.println("rows inserted " + i);
table.flushCommits();
}
}
table.flushCommits();
}
public static void main(String[] args) throws Exception {
byte[] tableName = Bytes.toBytes("test-table");
int nputs = 1000000;
if (args.length >= 1) {
tableName = Bytes.toBytes(args[0]);
}
if (args.length >= 2) {
nputs = Long.parseLong(args[1]);
}
byte[] cf0 = Bytes.toBytes("cf0");
byte[] cf1 = Bytes.toBytes("cf1");
Configuration conf = HBaseConfiguration.create();
HBaseAdmin admin = new HBaseAdmin(conf);
try {
if (!admin.tableExists(tableName)) {
createTable(admin, tableName, cf0, cf1);
}
HTable table = new HTable(conf, tableName);
loadData(table, nputs, cf0, cf1);
admin.flush(tableName);
} finally {
admin.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment