PageRenderTime 66ms CodeModel.GetById 14ms app.highlight 45ms RepoModel.GetById 2ms app.codeStats 0ms

/tags/release-0.2.0-rc0/hive/external/jdbc/src/java/org/apache/hadoop/hive/jdbc/HiveConnection.java

#
Java | 669 lines | 248 code | 108 blank | 313 comment | 7 complexity | bcd95481781bd33775e802554fd65fcb MD5 | raw file
  1/**
  2 * Licensed to the Apache Software Foundation (ASF) under one
  3 * or more contributor license agreements.  See the NOTICE file
  4 * distributed with this work for additional information
  5 * regarding copyright ownership.  The ASF licenses this file
  6 * to you under the Apache License, Version 2.0 (the
  7 * "License"); you may not use this file except in compliance
  8 * with the License.  You may obtain a copy of the License at
  9 *
 10 *     http://www.apache.org/licenses/LICENSE-2.0
 11 *
 12 * Unless required by applicable law or agreed to in writing, software
 13 * distributed under the License is distributed on an "AS IS" BASIS,
 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15 * See the License for the specific language governing permissions and
 16 * limitations under the License.
 17 */
 18
 19package org.apache.hadoop.hive.jdbc;
 20
 21import org.apache.hadoop.hive.metastore.api.MetaException;
 22import org.apache.hadoop.hive.service.HiveClient;
 23import org.apache.hadoop.hive.service.HiveInterface;
 24import org.apache.hadoop.hive.service.HiveServer;
 25import org.apache.thrift.TException;
 26import org.apache.thrift.protocol.TBinaryProtocol;
 27import org.apache.thrift.protocol.TProtocol;
 28import org.apache.thrift.transport.TSocket;
 29import org.apache.thrift.transport.TTransport;
 30import org.apache.thrift.transport.TTransportException;
 31
 32import java.sql.Array;
 33import java.sql.Blob;
 34import java.sql.CallableStatement;
 35import java.sql.Clob;
 36import java.sql.Connection;
 37import java.sql.DatabaseMetaData;
 38import java.sql.NClob;
 39import java.sql.PreparedStatement;
 40import java.sql.SQLClientInfoException;
 41import java.sql.SQLException;
 42import java.sql.SQLWarning;
 43import java.sql.SQLXML;
 44import java.sql.Savepoint;
 45import java.sql.Statement;
 46import java.sql.Struct;
 47import java.util.Map;
 48import java.util.Properties;
 49
 50/**
 51 * HiveConnection.
 52 *
 53 */
 54public class HiveConnection implements java.sql.Connection {
 55  private TTransport transport;
 56  private HiveInterface client;
 57  private boolean isClosed = true;
 58  private SQLWarning warningChain = null;
 59
 60  private static final String URI_PREFIX = "jdbc:hive://";
 61
 62  /**
 63   * TODO: - parse uri (use java.net.URI?).
 64   */
 65  public HiveConnection(String uri, Properties info) throws SQLException {
 66    if (!uri.startsWith(URI_PREFIX)) {
 67      throw new SQLException("Invalid URL: " + uri, "08S01");
 68    }
 69
 70    // remove prefix
 71    uri = uri.substring(URI_PREFIX.length());
 72
 73    // If uri is not specified, use local mode.
 74    if (uri.isEmpty()) {
 75      try {
 76        client = new HiveServer.HiveServerHandler();
 77      } catch (MetaException e) {
 78        throw new SQLException("Error accessing Hive metastore: "
 79            + e.getMessage(), "08S01");
 80      }
 81    } else {
 82      // parse uri
 83      // form: hostname:port/databasename
 84      String[] parts = uri.split("/");
 85      String[] hostport = parts[0].split(":");
 86      int port = 10000;
 87      String host = hostport[0];
 88      try {
 89        port = Integer.parseInt(hostport[1]);
 90      } catch (Exception e) {
 91      }
 92      transport = new TSocket(host, port);
 93      TProtocol protocol = new TBinaryProtocol(transport);
 94      client = new HiveClient(protocol);
 95      try {
 96        transport.open();
 97      } catch (TTransportException e) {
 98        throw new SQLException("Could not establish connecton to "
 99            + uri + ": " + e.getMessage(), "08S01");
100      }
101    }
102    isClosed = false;
103    configureConnection();
104  }
105
106  private void configureConnection() throws SQLException {
107    Statement stmt = createStatement();
108    stmt.execute(
109        "set hive.fetch.output.serde = org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe");
110    stmt.close();
111  }
112
113  /*
114   * (non-Javadoc)
115   * 
116   * @see java.sql.Connection#clearWarnings()
117   */
118
119  public void clearWarnings() throws SQLException {
120    warningChain = null;
121  }
122
123  /*
124   * (non-Javadoc)
125   * 
126   * @see java.sql.Connection#close()
127   */
128
129  public void close() throws SQLException {
130    if (!isClosed) {
131      try {
132        client.clean();
133      } catch (TException e) {
134        throw new SQLException("Error while cleaning up the server resources", e);
135      } finally {
136        isClosed = true;
137        if (transport != null) {
138          transport.close();
139        }
140      }
141    }
142  }
143
144  /*
145   * (non-Javadoc)
146   * 
147   * @see java.sql.Connection#commit()
148   */
149
150  public void commit() throws SQLException {
151    // TODO Auto-generated method stub
152    throw new SQLException("Method not supported");
153  }
154
155  /*
156   * (non-Javadoc)
157   * 
158   * @see java.sql.Connection#createArrayOf(java.lang.String,
159   * java.lang.Object[])
160   */
161
162  public Array createArrayOf(String arg0, Object[] arg1) throws SQLException {
163    // TODO Auto-generated method stub
164    throw new SQLException("Method not supported");
165  }
166
167  /*
168   * (non-Javadoc)
169   * 
170   * @see java.sql.Connection#createBlob()
171   */
172
173  public Blob createBlob() throws SQLException {
174    // TODO Auto-generated method stub
175    throw new SQLException("Method not supported");
176  }
177
178  /*
179   * (non-Javadoc)
180   * 
181   * @see java.sql.Connection#createClob()
182   */
183
184  public Clob createClob() throws SQLException {
185    // TODO Auto-generated method stub
186    throw new SQLException("Method not supported");
187  }
188
189  /*
190   * (non-Javadoc)
191   * 
192   * @see java.sql.Connection#createNClob()
193   */
194
195  public NClob createNClob() throws SQLException {
196    // TODO Auto-generated method stub
197    throw new SQLException("Method not supported");
198  }
199
200  /*
201   * (non-Javadoc)
202   * 
203   * @see java.sql.Connection#createSQLXML()
204   */
205
206  public SQLXML createSQLXML() throws SQLException {
207    // TODO Auto-generated method stub
208    throw new SQLException("Method not supported");
209  }
210
211  /**
212   * Creates a Statement object for sending SQL statements to the database.
213   * 
214   * @throws SQLException
215   *           if a database access error occurs.
216   * @see java.sql.Connection#createStatement()
217   */
218
219  public Statement createStatement() throws SQLException {
220    if (isClosed) {
221      throw new SQLException("Can't create Statement, connection is closed");
222    }
223    return new HiveStatement(client);
224  }
225
226  /*
227   * (non-Javadoc)
228   * 
229   * @see java.sql.Connection#createStatement(int, int)
230   */
231
232  public Statement createStatement(int resultSetType, int resultSetConcurrency)
233      throws SQLException {
234    // TODO Auto-generated method stub
235    throw new SQLException("Method not supported");
236  }
237
238  /*
239   * (non-Javadoc)
240   * 
241   * @see java.sql.Connection#createStatement(int, int, int)
242   */
243
244  public Statement createStatement(int resultSetType, int resultSetConcurrency,
245      int resultSetHoldability) throws SQLException {
246    // TODO Auto-generated method stub
247    throw new SQLException("Method not supported");
248  }
249
250  /*
251   * (non-Javadoc)
252   * 
253   * @see java.sql.Connection#createStruct(java.lang.String, java.lang.Object[])
254   */
255
256  public Struct createStruct(String typeName, Object[] attributes)
257      throws SQLException {
258    // TODO Auto-generated method stub
259    throw new SQLException("Method not supported");
260  }
261
262  /*
263   * (non-Javadoc)
264   * 
265   * @see java.sql.Connection#getAutoCommit()
266   */
267
268  public boolean getAutoCommit() throws SQLException {
269    return true;
270  }
271
272  /*
273   * (non-Javadoc)
274   * 
275   * @see java.sql.Connection#getCatalog()
276   */
277
278  public String getCatalog() throws SQLException {
279    return "";
280  }
281
282  /*
283   * (non-Javadoc)
284   * 
285   * @see java.sql.Connection#getClientInfo()
286   */
287
288  public Properties getClientInfo() throws SQLException {
289    // TODO Auto-generated method stub
290    throw new SQLException("Method not supported");
291  }
292
293  /*
294   * (non-Javadoc)
295   * 
296   * @see java.sql.Connection#getClientInfo(java.lang.String)
297   */
298
299  public String getClientInfo(String name) throws SQLException {
300    // TODO Auto-generated method stub
301    throw new SQLException("Method not supported");
302  }
303
304  /*
305   * (non-Javadoc)
306   * 
307   * @see java.sql.Connection#getHoldability()
308   */
309
310  public int getHoldability() throws SQLException {
311    // TODO Auto-generated method stub
312    throw new SQLException("Method not supported");
313  }
314
315  /*
316   * (non-Javadoc)
317   * 
318   * @see java.sql.Connection#getMetaData()
319   */
320
321  public DatabaseMetaData getMetaData() throws SQLException {
322    return new HiveDatabaseMetaData(client);
323  }
324
325  /*
326   * (non-Javadoc)
327   * 
328   * @see java.sql.Connection#getTransactionIsolation()
329   */
330
331  public int getTransactionIsolation() throws SQLException {
332    return Connection.TRANSACTION_NONE;
333  }
334
335  /*
336   * (non-Javadoc)
337   * 
338   * @see java.sql.Connection#getTypeMap()
339   */
340
341  public Map<String, Class<?>> getTypeMap() throws SQLException {
342    // TODO Auto-generated method stub
343    throw new SQLException("Method not supported");
344  }
345
346  /*
347   * (non-Javadoc)
348   * 
349   * @see java.sql.Connection#getWarnings()
350   */
351
352  public SQLWarning getWarnings() throws SQLException {
353    return warningChain;
354  }
355
356  /*
357   * (non-Javadoc)
358   * 
359   * @see java.sql.Connection#isClosed()
360   */
361
362  public boolean isClosed() throws SQLException {
363    return isClosed;
364  }
365
366  /*
367   * (non-Javadoc)
368   * 
369   * @see java.sql.Connection#isReadOnly()
370   */
371
372  public boolean isReadOnly() throws SQLException {
373    return false;
374  }
375
376  /*
377   * (non-Javadoc)
378   * 
379   * @see java.sql.Connection#isValid(int)
380   */
381
382  public boolean isValid(int timeout) throws SQLException {
383    // TODO Auto-generated method stub
384    throw new SQLException("Method not supported");
385  }
386
387  /*
388   * (non-Javadoc)
389   * 
390   * @see java.sql.Connection#nativeSQL(java.lang.String)
391   */
392
393  public String nativeSQL(String sql) throws SQLException {
394    // TODO Auto-generated method stub
395    throw new SQLException("Method not supported");
396  }
397
398  /*
399   * (non-Javadoc)
400   * 
401   * @see java.sql.Connection#prepareCall(java.lang.String)
402   */
403
404  public CallableStatement prepareCall(String sql) throws SQLException {
405    // TODO Auto-generated method stub
406    throw new SQLException("Method not supported");
407  }
408
409  /*
410   * (non-Javadoc)
411   * 
412   * @see java.sql.Connection#prepareCall(java.lang.String, int, int)
413   */
414
415  public CallableStatement prepareCall(String sql, int resultSetType,
416      int resultSetConcurrency) throws SQLException {
417    // TODO Auto-generated method stub
418    throw new SQLException("Method not supported");
419  }
420
421  /*
422   * (non-Javadoc)
423   * 
424   * @see java.sql.Connection#prepareCall(java.lang.String, int, int, int)
425   */
426
427  public CallableStatement prepareCall(String sql, int resultSetType,
428      int resultSetConcurrency, int resultSetHoldability) throws SQLException {
429    // TODO Auto-generated method stub
430    throw new SQLException("Method not supported");
431  }
432
433  /*
434   * (non-Javadoc)
435   * 
436   * @see java.sql.Connection#prepareStatement(java.lang.String)
437   */
438
439  public PreparedStatement prepareStatement(String sql) throws SQLException {
440    return new HivePreparedStatement(client, sql);
441  }
442
443  /*
444   * (non-Javadoc)
445   * 
446   * @see java.sql.Connection#prepareStatement(java.lang.String, int)
447   */
448
449  public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
450      throws SQLException {
451    return new HivePreparedStatement(client, sql);
452  }
453
454  /*
455   * (non-Javadoc)
456   * 
457   * @see java.sql.Connection#prepareStatement(java.lang.String, int[])
458   */
459
460  public PreparedStatement prepareStatement(String sql, int[] columnIndexes)
461      throws SQLException {
462    // TODO Auto-generated method stub
463    throw new SQLException("Method not supported");
464  }
465
466  /*
467   * (non-Javadoc)
468   * 
469   * @see java.sql.Connection#prepareStatement(java.lang.String,
470   * java.lang.String[])
471   */
472
473  public PreparedStatement prepareStatement(String sql, String[] columnNames)
474      throws SQLException {
475    // TODO Auto-generated method stub
476    throw new SQLException("Method not supported");
477  }
478
479  /*
480   * (non-Javadoc)
481   * 
482   * @see java.sql.Connection#prepareStatement(java.lang.String, int, int)
483   */
484
485  public PreparedStatement prepareStatement(String sql, int resultSetType,
486      int resultSetConcurrency) throws SQLException {
487    return new HivePreparedStatement(client, sql);
488  }
489
490  /*
491   * (non-Javadoc)
492   * 
493   * @see java.sql.Connection#prepareStatement(java.lang.String, int, int, int)
494   */
495
496  public PreparedStatement prepareStatement(String sql, int resultSetType,
497      int resultSetConcurrency, int resultSetHoldability) throws SQLException {
498    // TODO Auto-generated method stub
499    throw new SQLException("Method not supported");
500  }
501
502  /*
503   * (non-Javadoc)
504   * 
505   * @see java.sql.Connection#releaseSavepoint(java.sql.Savepoint)
506   */
507
508  public void releaseSavepoint(Savepoint savepoint) throws SQLException {
509    // TODO Auto-generated method stub
510    throw new SQLException("Method not supported");
511  }
512
513  /*
514   * (non-Javadoc)
515   * 
516   * @see java.sql.Connection#rollback()
517   */
518
519  public void rollback() throws SQLException {
520    // TODO Auto-generated method stub
521    throw new SQLException("Method not supported");
522  }
523
524  /*
525   * (non-Javadoc)
526   * 
527   * @see java.sql.Connection#rollback(java.sql.Savepoint)
528   */
529
530  public void rollback(Savepoint savepoint) throws SQLException {
531    // TODO Auto-generated method stub
532    throw new SQLException("Method not supported");
533  }
534
535  /*
536   * (non-Javadoc)
537   * 
538   * @see java.sql.Connection#setAutoCommit(boolean)
539   */
540
541  public void setAutoCommit(boolean autoCommit) throws SQLException {
542    // TODO Auto-generated method stub
543    throw new SQLException("Method not supported");
544  }
545
546  /*
547   * (non-Javadoc)
548   * 
549   * @see java.sql.Connection#setCatalog(java.lang.String)
550   */
551
552  public void setCatalog(String catalog) throws SQLException {
553    // TODO Auto-generated method stub
554    throw new SQLException("Method not supported");
555  }
556
557  /*
558   * (non-Javadoc)
559   * 
560   * @see java.sql.Connection#setClientInfo(java.util.Properties)
561   */
562
563  public void setClientInfo(Properties properties)
564      throws SQLClientInfoException {
565    // TODO Auto-generated method stub
566    throw new SQLClientInfoException("Method not supported", null);
567  }
568
569  /*
570   * (non-Javadoc)
571   * 
572   * @see java.sql.Connection#setClientInfo(java.lang.String, java.lang.String)
573   */
574
575  public void setClientInfo(String name, String value)
576      throws SQLClientInfoException {
577    // TODO Auto-generated method stub
578    throw new SQLClientInfoException("Method not supported", null);
579  }
580
581  /*
582   * (non-Javadoc)
583   * 
584   * @see java.sql.Connection#setHoldability(int)
585   */
586
587  public void setHoldability(int holdability) throws SQLException {
588    // TODO Auto-generated method stub
589    throw new SQLException("Method not supported");
590  }
591
592  /*
593   * (non-Javadoc)
594   * 
595   * @see java.sql.Connection#setReadOnly(boolean)
596   */
597
598  public void setReadOnly(boolean readOnly) throws SQLException {
599    // TODO Auto-generated method stub
600    throw new SQLException("Method not supported");
601  }
602
603  /*
604   * (non-Javadoc)
605   * 
606   * @see java.sql.Connection#setSavepoint()
607   */
608
609  public Savepoint setSavepoint() throws SQLException {
610    // TODO Auto-generated method stub
611    throw new SQLException("Method not supported");
612  }
613
614  /*
615   * (non-Javadoc)
616   * 
617   * @see java.sql.Connection#setSavepoint(java.lang.String)
618   */
619
620  public Savepoint setSavepoint(String name) throws SQLException {
621    // TODO Auto-generated method stub
622    throw new SQLException("Method not supported");
623  }
624
625  /*
626   * (non-Javadoc)
627   * 
628   * @see java.sql.Connection#setTransactionIsolation(int)
629   */
630
631  public void setTransactionIsolation(int level) throws SQLException {
632    // TODO Auto-generated method stub
633    throw new SQLException("Method not supported");
634  }
635
636  /*
637   * (non-Javadoc)
638   * 
639   * @see java.sql.Connection#setTypeMap(java.util.Map)
640   */
641
642  public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
643    // TODO Auto-generated method stub
644    throw new SQLException("Method not supported");
645  }
646
647  /*
648   * (non-Javadoc)
649   * 
650   * @see java.sql.Wrapper#isWrapperFor(java.lang.Class)
651   */
652
653  public boolean isWrapperFor(Class<?> iface) throws SQLException {
654    // TODO Auto-generated method stub
655    throw new SQLException("Method not supported");
656  }
657
658  /*
659   * (non-Javadoc)
660   * 
661   * @see java.sql.Wrapper#unwrap(java.lang.Class)
662   */
663
664  public <T> T unwrap(Class<T> iface) throws SQLException {
665    // TODO Auto-generated method stub
666    throw new SQLException("Method not supported");
667  }
668
669}