Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class UserInsert
- {
- //static String tableName = "users";
- static String tableName = "users";
- static String familyName = "info";
- public static void main(String[] args) throws Exception
- {
- // open connection just once and re-user it
- Configuration config = HBaseConfiguration.create();
- try (HTablehtable = new HTable(config, tableName)) {
- int total = 100;
- long t1 = System.currentTimeMillis();
- // loop to insert users value, which we arecreating
- // later we will do better user simulation
- for (inti=0; i< total ; i++)
- {
- int userid = i;
- String email = "user-" + i + "@foo.com";
- String phone = "555-1234";
- byte [] key = Bytes.toBytes(userid);
- Put put = new Put (key);
- put.add(Bytes.toBytes(familyName), Bytes.toBytes("email"), Bytes.toBytes(email)); // <-- email goes here
- put.add(Bytes.toBytes(familyName), Bytes.toBytes("phone"), Bytes.toBytes(phone)); // <-- phone goes here
- htable.put(put);
- }
- long t2 = System.currentTimeMillis();
- System.out.println ("inserted " + total + " users in " + (t2-t1) + " ms");
- }
- }
- }
- =====
- ackage com.hi.hbase.book.chapter2;
- // required imports
- This class reads the data from the user table
- public class UserQuery {
- static String tableName = " users";
- static String familyName = "info";
- public static void main(String[] args) throws Exception {
- Configuration config = HBaseConfiguration.create();
- HTablehtable = new HTable(config, tableName);
- // do as much or as little quering as you want, time it
- int total = 100;
- Random rand = new Random();
- for (int i = 0; i<total; i++) {
- int id = rand.nextInt(total * 2);
- System.out.println("querying for userId : " + id);
- byte[] key = Bytes.toBytes(id);
- long t1 = System.nanoTime();
- Get get = new Get(key);
- long t2 = System.nanoTime();
- Result result = htable.get(get);
- if (result.isEmpty()) {
- // first check, this row may not exist
- System.out.println(" row user=" + id + " : not found");
- } else {
- // You always access the data through binary key
- byte[] family = Bytes.toBytes(familyName);
- byte[] emailCol = Bytes.toBytes("email");
- KeyValuekv = result.getColumnLatest(family, emailCol);
- if (kv == null) {
- // even if row exist, this column may not exist
- System.out.println(" column 'email' not found");
- } else {
- // found
- byte[] value = kv.getValue();
- String email = new String(value);
- System.out.println(" email=" + email);
- }
- }
- System.out.println(" query time : " + (t2 - t1) / 1000000.0 +" ms\n");
- }
- }
- }
- ====
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement