Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. public class UserInsert
  2. {
  3.  
  4. //static String tableName = "users";
  5. static String tableName = "users";
  6. static String familyName = "info";
  7.  
  8. public static void main(String[] args) throws Exception
  9. {
  10. // open connection just once and re-user it
  11. Configuration config = HBaseConfiguration.create();
  12. try (HTablehtable = new HTable(config, tableName)) {
  13. int total = 100;
  14. long t1 = System.currentTimeMillis();
  15. // loop to insert users value, which we arecreating
  16. // later we will do better user simulation
  17. for (inti=0; i< total ; i++)
  18. {
  19. int userid = i;
  20. String email = "user-" + i + "@foo.com";
  21. String phone = "555-1234";
  22.  
  23. byte [] key = Bytes.toBytes(userid);
  24. Put put = new Put (key);
  25.  
  26. put.add(Bytes.toBytes(familyName), Bytes.toBytes("email"), Bytes.toBytes(email)); // <-- email goes here
  27. put.add(Bytes.toBytes(familyName), Bytes.toBytes("phone"), Bytes.toBytes(phone)); // <-- phone goes here
  28. htable.put(put);
  29.  
  30. }
  31. long t2 = System.currentTimeMillis();
  32. System.out.println ("inserted " + total + " users in " + (t2-t1) + " ms");
  33. }
  34. }
  35. }
  36. =====
  37. ackage com.hi.hbase.book.chapter2;
  38.  
  39.  
  40. // required imports
  41.  
  42. This class reads the data from the user table
  43.  
  44. public class UserQuery {
  45.  
  46. static String tableName = " users";
  47.  
  48. static String familyName = "info";
  49.  
  50. public static void main(String[] args) throws Exception {
  51. Configuration config = HBaseConfiguration.create();
  52. HTablehtable = new HTable(config, tableName);
  53. // do as much or as little quering as you want, time it
  54. int total = 100;
  55. Random rand = new Random();
  56. for (int i = 0; i<total; i++) {
  57. int id = rand.nextInt(total * 2);
  58. System.out.println("querying for userId : " + id);
  59. byte[] key = Bytes.toBytes(id);
  60.  
  61. long t1 = System.nanoTime();
  62. Get get = new Get(key);
  63. long t2 = System.nanoTime();
  64. Result result = htable.get(get);
  65. if (result.isEmpty()) {
  66. // first check, this row may not exist
  67. System.out.println(" row user=" + id + " : not found");
  68. } else {
  69. // You always access the data through binary key
  70. byte[] family = Bytes.toBytes(familyName);
  71. byte[] emailCol = Bytes.toBytes("email");
  72. KeyValuekv = result.getColumnLatest(family, emailCol);
  73. if (kv == null) {
  74. // even if row exist, this column may not exist
  75. System.out.println(" column 'email' not found");
  76. } else {
  77. // found
  78. byte[] value = kv.getValue();
  79. String email = new String(value);
  80. System.out.println(" email=" + email);
  81. }
  82. }
  83. System.out.println(" query time : " + (t2 - t1) / 1000000.0 +" ms\n");
  84. }
  85. }
  86. }
  87.  
  88. ====
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement