Guest User

Untitled

a guest
Apr 22nd, 2015
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.04 KB | None | 0 0
  1. package org.apache.zeppelin.presto;
  2.  
  3. import java.sql.*;
  4. import java.util.List;
  5. import java.util.Properties;
  6.  
  7. import org.apache.zeppelin.interpreter.*;
  8. import org.apache.commons.lang.StringUtils;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11.  
  12. import org.apache.zeppelin.interpreter.InterpreterResult.Code;
  13. import org.apache.zeppelin.scheduler.Scheduler;
  14. import org.apache.zeppelin.scheduler.SchedulerFactory;
  15.  
  16. /**
  17.  * Presto interpreter for Zeppelin.
  18.  */
  19. public class PrestoInterpreter extends Interpreter {
  20.   Logger logger = LoggerFactory.getLogger(PrestoInterpreter.class);
  21.   int commandTimeOut = 600000;
  22.  
  23.   static final String PRESTOSERVER_URL = "presto.url";
  24.   static final String PRESTOSERVER_USER = "presto.user";
  25.   static final String PRESTOSERVER_PASSWORD = "presto.password";
  26.  
  27.   static {
  28.     Interpreter.register(
  29.       "presto",
  30.       "presto",
  31.       PrestoInterpreter.class.getName(),
  32.       new InterpreterPropertyBuilder()
  33.         .add(PRESTOSERVER_URL, "jdbc:presto://localhost:8089", "The URL for Presto")
  34.         .add(PRESTOSERVER_USER, "Presto", "The Presto user")
  35.         .add(PRESTOSERVER_PASSWORD, "", "The password for the Presto user").build());
  36.   }
  37.  
  38.   public PrestoInterpreter(Properties property) {
  39.     super(property);
  40.   }
  41.  
  42.   Connection jdbcConnection;
  43.   Exception exceptionOnConnect;
  44.  
  45.   //Test only method
  46.   public Connection getJdbcConnection()
  47.       throws SQLException {
  48.     String url = getProperty(PRESTOSERVER_URL);
  49.     String user = getProperty(PRESTOSERVER_USER);
  50.     String password = getProperty(PRESTOSERVER_PASSWORD);
  51.  
  52.     return DriverManager.getConnection(url, user, password);
  53.   }
  54.  
  55.   @Override
  56.   public void open() {
  57.     logger.info("Jdbc open connection called!");
  58.     try {
  59.       String driverName = "org.apache.Presto.jdbc.PrestoDriver";
  60.       Class.forName(driverName);
  61.     } catch (ClassNotFoundException e) {
  62.       logger.error("Can not open connection", e);
  63.       exceptionOnConnect = e;
  64.       return;
  65.     }
  66.     try {
  67.       jdbcConnection = getJdbcConnection();
  68.       exceptionOnConnect = null;
  69.       logger.info("Successfully created Jdbc connection");
  70.     }
  71.     catch (SQLException e) {
  72.       logger.error("Cannot open connection", e);
  73.       exceptionOnConnect = e;
  74.     }
  75.   }
  76.  
  77.   @Override
  78.   public void close() {
  79.     try {
  80.       if (jdbcConnection != null) {
  81.         jdbcConnection.close();
  82.       }
  83.     }
  84.     catch (SQLException e) {
  85.       logger.error("Cannot close connection", e);
  86.     }
  87.     finally {
  88.       jdbcConnection = null;
  89.       exceptionOnConnect = null;
  90.     }
  91.   }
  92.  
  93.   Statement currentStatement;
  94.   private InterpreterResult executeSql(String sql) {
  95.     try {
  96.       if (exceptionOnConnect != null) {
  97.         return new InterpreterResult(Code.ERROR, exceptionOnConnect.getMessage());
  98.       }
  99.       currentStatement = jdbcConnection.createStatement();
  100.       StringBuilder msg = null;
  101.       if (StringUtils.containsIgnoreCase(sql, "EXPLAIN ")) {
  102.         //return the explain as text, make this visual explain later
  103.         msg = new StringBuilder();
  104.       }
  105.       else {
  106.         msg = new StringBuilder("%table ");
  107.       }
  108.       ResultSet res = currentStatement.executeQuery(sql);
  109.       try {
  110.         ResultSetMetaData md = res.getMetaData();
  111.         for (int i = 1; i < md.getColumnCount() + 1; i++) {
  112.           if (i == 1) {
  113.             msg.append(md.getColumnName(i));
  114.           } else {
  115.             msg.append("\t" + md.getColumnName(i));
  116.           }
  117.         }
  118.         msg.append("\n");
  119.         while (res.next()) {
  120.           for (int i = 1; i < md.getColumnCount() + 1; i++) {
  121.             msg.append(res.getString(i) + "\t");
  122.           }
  123.           msg.append("\n");
  124.         }
  125.       }
  126.       finally {
  127.         try {
  128.           res.close();
  129.           currentStatement.close();
  130.         }
  131.         finally {
  132.           currentStatement = null;
  133.         }
  134.       }
  135.  
  136.       InterpreterResult rett = new InterpreterResult(Code.SUCCESS, msg.toString());
  137.       return rett;
  138.     }
  139.     catch (SQLException ex) {
  140.       logger.error("Can not run " + sql, ex);
  141.       return new InterpreterResult(Code.ERROR, ex.getMessage());
  142.     }
  143.   }
  144.  
  145.   @Override
  146.   public InterpreterResult interpret(String cmd, InterpreterContext contextInterpreter) {
  147.     logger.info("Run SQL command '" + cmd + "'");
  148.     return executeSql(cmd);
  149.   }
  150.  
  151.   @Override
  152.   public void cancel(InterpreterContext context) {
  153.     if (currentStatement != null) {
  154.       try {
  155.         currentStatement.cancel();
  156.       }
  157.       catch (SQLException ex) {
  158.       }
  159.       finally {
  160.         currentStatement = null;
  161.       }
  162.     }
  163.   }
  164.  
  165.   @Override
  166.   public FormType getFormType() {
  167.     return FormType.SIMPLE;
  168.   }
  169.  
  170.   @Override
  171.   public int getProgress(InterpreterContext context) {
  172.     return 0;
  173.   }
  174.  
  175.   @Override
  176.   public Scheduler getScheduler() {
  177.     return SchedulerFactory.singleton().createOrGetFIFOScheduler(
  178.         PrestoInterpreter.class.getName() + this.hashCode());
  179.   }
  180.  
  181.   @Override
  182.   public List<String> completion(String buf, int cursor) {
  183.     return null;
  184.   }
  185.  
  186. }
Advertisement
Add Comment
Please, Sign In to add comment