Skip to content

Instantly share code, notes, and snippets.

@nagataka
Created April 21, 2015 15:02
Show Gist options
  • Select an option

  • Save nagataka/3885c3a8c7f27dc95ecc to your computer and use it in GitHub Desktop.

Select an option

Save nagataka/3885c3a8c7f27dc95ecc to your computer and use it in GitHub Desktop.
HBase Java API test
package hbase;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
public class HbaseConnector {
public static void main(String[] args) throws IOException{
Configuration config = HBaseConfiguration.create();
HBaseAdmin admin = new HBaseAdmin(config);
HTableDescriptor htd = new HTableDescriptor("users");
htd.addFamily(new HColumnDescriptor("csv"));
admin.createTable(htd);
String[][] users = {
{ "user1", "address1", "user1@mydomain.com"},
{ "user2", "address2", "user2@mydomain.com"},
{ "user3", "address3", "user3@mydomain.com"},
{ "user4", "address4", "user4@mydomain.com"},
{ "user5", "address5", "user5@mydomain.com"},
{ "user6", "address6", "user6@mydomain.com"} };
HTable table = new HTable(config, "users");
for (int i = 0; i< users.length; i++) {
Put user = new Put(Bytes.toBytes(users[i][0]));
user.add(Bytes.toBytes("name"), Bytes.toBytes("first"), Bytes.toBytes(users[i][0]));
user.add(Bytes.toBytes("address"), Bytes.toBytes("address"), Bytes.toBytes(users[i][1]));
user.add(Bytes.toBytes("email"), Bytes.toBytes("email"), Bytes.toBytes(users[i][2]));
table.put(user);
}
table.flushCommits();
table.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment