Skip to content

Instantly share code, notes, and snippets.

@esatemre
Last active August 29, 2015 14:12
Show Gist options
  • Save esatemre/498785c49d26e129b7a1 to your computer and use it in GitHub Desktop.
Save esatemre/498785c49d26e129b7a1 to your computer and use it in GitHub Desktop.
HBase Asynchronous Put
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import org.apache.hadoop.hbase.util.Bytes;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.hbase.async.HBaseClient;
import org.hbase.async.PutRequest;
import com.stumbleupon.async.Deferred;
public static void main(String[] args) {
// TODO Auto-generated method stub
HBaseClient client = new HBaseClient("DOMAIN OR IP ADDRESS FOR ZOOKEPER");
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss.SSS");
byte[] table = Bytes.toBytes("TABLE NAME");
byte[] cf = Bytes.toBytes("COLUMN FAMILY NAME");
byte[] cq = Bytes.toBytes("QUALIFIER NAME"); // for my example, i will put empty to cq. Because i will insert json to each one column family
System.out.println(sdf.format(new Date()));
final ArrayList<Deferred<Object>> ds = new ArrayList<Deferred<Object>>();
for (int i = 0; i < 30; i++) {
try {
JSONObject json = new JSONObject();
json.accumulate("id", i);
json.accumulate("name", "name"+i);
json.accumulate("surname", "surname"+i);
json.accumulate("birthdate", sdf.format(new Date()));
byte[] rowkey = Bytes.toBytes(i%5+"_"+i%4+"_"+i%3+"_"+i); // for avoid hotspotting
PutRequest put = new PutRequest(table,rowkey, cf, cq,Bytes.toBytes(json.toString()));
ds.add(client.put(put));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(sdf.format(new Date()));
try {
Deferred.groupInOrder(ds).join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(sdf.format(new Date()));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment