Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. package com.medium.hive.hook;
  2.  
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import org.apache.hadoop.hive.ql.QueryPlan;
  5. import org.apache.hadoop.hive.ql.hooks.ExecuteWithHookContext;
  6. import org.apache.hadoop.hive.ql.hooks.HookContext;
  7. import org.apache.hadoop.hive.ql.hooks.HookContext.HookType;
  8. import org.apache.hadoop.hive.ql.hooks.ReadEntity;
  9. import org.apache.hadoop.hive.ql.hooks.WriteEntity;
  10. import org.apache.hadoop.hive.ql.plan.HiveOperation;
  11.  
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14.  
  15. import org.apache.hadoop.hive.metastore.api.Database;
  16. import org.apache.hadoop.hive.ql.hooks.Entity;
  17.  
  18. import java.util.HashSet;
  19. import java.util.Set;
  20.  
  21. public class CustomHook implements ExecuteWithHookContext {
  22.  
  23. private static final Logger LOGGER = LoggerFactory.getLogger(CustomHook.class);
  24. private static final HashSet<String> OPERATION_NAMES = new HashSet<>();
  25.  
  26. static {
  27. OPERATION_NAMES.add(HiveOperation.CREATETABLE.getOperationName());
  28. OPERATION_NAMES.add(HiveOperation.ALTERDATABASE.getOperationName());
  29. OPERATION_NAMES.add(HiveOperation.ALTERDATABASE_OWNER.getOperationName());
  30. OPERATION_NAMES.add(HiveOperation.ALTERTABLE_ADDCOLS.getOperationName());
  31. OPERATION_NAMES.add(HiveOperation.ALTERTABLE_LOCATION.getOperationName());
  32. OPERATION_NAMES.add(HiveOperation.ALTERTABLE_PROPERTIES.getOperationName());
  33. OPERATION_NAMES.add(HiveOperation.ALTERTABLE_RENAME.getOperationName());
  34. OPERATION_NAMES.add(HiveOperation.ALTERTABLE_RENAMECOL.getOperationName());
  35. OPERATION_NAMES.add(HiveOperation.ALTERTABLE_REPLACECOLS.getOperationName());
  36. OPERATION_NAMES.add(HiveOperation.CREATEDATABASE.getOperationName());
  37. OPERATION_NAMES.add(HiveOperation.DROPDATABASE.getOperationName());
  38. OPERATION_NAMES.add(HiveOperation.DROPTABLE.getOperationName());
  39. }
  40.  
  41. @Override
  42. public void run(HookContext hookContext) throws Exception {
  43. assert (hookContext.getHookType() == HookType.POST_EXEC_HOOK);
  44.  
  45. QueryPlan plan = hookContext.getQueryPlan();
  46.  
  47. String operationName = plan.getOperationName();
  48. logWithHeader("Query executed: " + plan.getQueryString());
  49. logWithHeader("Operation: " + operationName);
  50. if (OPERATION_NAMES.contains(operationName)
  51. && !plan.isExplain()) {
  52. logWithHeader("Monitored Operation");
  53. Set<ReadEntity> inputs = hookContext.getInputs();
  54. Set<WriteEntity> outputs = hookContext.getOutputs();
  55.  
  56. for (Entity entity : inputs) {
  57. logWithHeader("Hook metadata input value: " + toJson(entity));
  58. }
  59.  
  60. for (Entity entity : outputs) {
  61. logWithHeader("Hook metadata output value: " + toJson(entity));
  62. }
  63.  
  64. } else {
  65. logWithHeader("Non-monitored Operation, ignoring hook");
  66. }
  67. }
  68.  
  69. private static String toJson(Entity entity) throws Exception {
  70. ObjectMapper mapper = new ObjectMapper();
  71. switch (entity.getType()) {
  72. case DATABASE:
  73. Database db = entity.getDatabase();
  74. return mapper.writeValueAsString(db);
  75. case TABLE:
  76. return mapper.writeValueAsString(entity.getTable().getTTable());
  77. }
  78. return null;
  79. }
  80.  
  81. private void logWithHeader(Object obj){
  82. LOGGER.info("[CustomHook][Thread: "+Thread.currentThread().getName()+"] | " + obj);
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement