Skip to content

Instantly share code, notes, and snippets.

@shrijeet
Created July 20, 2012 23:38
Show Gist options
  • Save shrijeet/3153858 to your computer and use it in GitHub Desktop.
Save shrijeet/3153858 to your computer and use it in GitHub Desktop.
Serialize failed puts
import org.hbase.async.HBaseClient;
import org.hbase.async.PutRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.stumbleupon.async.Callback;
public class AsyncPutExample {
private static final Logger LOG = LoggerFactory.getLogger(AsyncPutExample.class);
private static long err_ed;
private static long ok_ed;
public static void main(String args[]) throws Exception {
final HBaseClient client = new HBaseClient("zookeeper-server");
final class PutErrback implements Callback<Object, Exception> {
PutRequest put;
public PutErrback(PutRequest put) {
this.put = put;
}
public Object call(final Exception arg) {
serialize(this.put);
err_ed++;
return null;
}
public String toString() {
return "put error callback";
}
}
final class PutOkBack implements Callback<Object, Object> {
public Object call(final Object arg) {
ok_ed++;
return null;
}
public String toString() {
return "put ok callback";
}
}
PutOkBack callback = new PutOkBack();
for (int i = 0; i < 100000; i++) {
PutRequest put = new PutRequest("t0", "k" + i, "f", "q", "v");
final PutErrback errback = new PutErrback(put);
client.put(put).addCallbacks(callback, errback);
if (i % 100 == 0) {
LOG.info("OK " + ok_ed + ", ERR " + err_ed + ", last put " + put);
Thread.sleep(500);
}
}
client.shutdown().joinUninterruptibly();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment