PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms 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
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  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. package org.apache.hadoop.hive.jdbc;
  19. import org.apache.hadoop.hive.metastore.api.MetaException;
  20. import org.apache.hadoop.hive.service.HiveClient;
  21. import org.apache.hadoop.hive.service.HiveInterface;
  22. import org.apache.hadoop.hive.service.HiveServer;
  23. import org.apache.thrift.TException;
  24. import org.apache.thrift.protocol.TBinaryProtocol;
  25. import org.apache.thrift.protocol.TProtocol;
  26. import org.apache.thrift.transport.TSocket;
  27. import org.apache.thrift.transport.TTransport;
  28. import org.apache.thrift.transport.TTransportException;
  29. import java.sql.Array;
  30. import java.sql.Blob;
  31. import java.sql.CallableStatement;
  32. import java.sql.Clob;
  33. import java.sql.Connection;
  34. import java.sql.DatabaseMetaData;
  35. import java.sql.NClob;
  36. import java.sql.PreparedStatement;
  37. import java.sql.SQLClientInfoException;
  38. import java.sql.SQLException;
  39. import java.sql.SQLWarning;
  40. import java.sql.SQLXML;
  41. import java.sql.Savepoint;
  42. import java.sql.Statement;
  43. import java.sql.Struct;
  44. import java.util.Map;
  45. import java.util.Properties;
  46. /**
  47. * HiveConnection.
  48. *
  49. */
  50. public class HiveConnection implements java.sql.Connection {
  51. private TTransport transport;
  52. private HiveInterface client;
  53. private boolean isClosed = true;
  54. private SQLWarning warningChain = null;
  55. private static final String URI_PREFIX = "jdbc:hive://";
  56. /**
  57. * TODO: - parse uri (use java.net.URI?).
  58. */
  59. public HiveConnection(String uri, Properties info) throws SQLException {
  60. if (!uri.startsWith(URI_PREFIX)) {
  61. throw new SQLException("Invalid URL: " + uri, "08S01");
  62. }
  63. // remove prefix
  64. uri = uri.substring(URI_PREFIX.length());
  65. // If uri is not specified, use local mode.
  66. if (uri.isEmpty()) {
  67. try {
  68. client = new HiveServer.HiveServerHandler();
  69. } catch (MetaException e) {
  70. throw new SQLException("Error accessing Hive metastore: "
  71. + e.getMessage(), "08S01");
  72. }
  73. } else {
  74. // parse uri
  75. // form: hostname:port/databasename
  76. String[] parts = uri.split("/");
  77. String[] hostport = parts[0].split(":");
  78. int port = 10000;
  79. String host = hostport[0];
  80. try {
  81. port = Integer.parseInt(hostport[1]);
  82. } catch (Exception e) {
  83. }
  84. transport = new TSocket(host, port);
  85. TProtocol protocol = new TBinaryProtocol(transport);
  86. client = new HiveClient(protocol);
  87. try {
  88. transport.open();
  89. } catch (TTransportException e) {
  90. throw new SQLException("Could not establish connecton to "
  91. + uri + ": " + e.getMessage(), "08S01");
  92. }
  93. }
  94. isClosed = false;
  95. configureConnection();
  96. }
  97. private void configureConnection() throws SQLException {
  98. Statement stmt = createStatement();
  99. stmt.execute(
  100. "set hive.fetch.output.serde = org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe");
  101. stmt.close();
  102. }
  103. /*
  104. * (non-Javadoc)
  105. *
  106. * @see java.sql.Connection#clearWarnings()
  107. */
  108. public void clearWarnings() throws SQLException {
  109. warningChain = null;
  110. }
  111. /*
  112. * (non-Javadoc)
  113. *
  114. * @see java.sql.Connection#close()
  115. */
  116. public void close() throws SQLException {
  117. if (!isClosed) {
  118. try {
  119. client.clean();
  120. } catch (TException e) {
  121. throw new SQLException("Error while cleaning up the server resources", e);
  122. } finally {
  123. isClosed = true;
  124. if (transport != null) {
  125. transport.close();
  126. }
  127. }
  128. }
  129. }
  130. /*
  131. * (non-Javadoc)
  132. *
  133. * @see java.sql.Connection#commit()
  134. */
  135. public void commit() throws SQLException {
  136. // TODO Auto-generated method stub
  137. throw new SQLException("Method not supported");
  138. }
  139. /*
  140. * (non-Javadoc)
  141. *
  142. * @see java.sql.Connection#createArrayOf(java.lang.String,
  143. * java.lang.Object[])
  144. */
  145. public Array createArrayOf(String arg0, Object[] arg1) throws SQLException {
  146. // TODO Auto-generated method stub
  147. throw new SQLException("Method not supported");
  148. }
  149. /*
  150. * (non-Javadoc)
  151. *
  152. * @see java.sql.Connection#createBlob()
  153. */
  154. public Blob createBlob() throws SQLException {
  155. // TODO Auto-generated method stub
  156. throw new SQLException("Method not supported");
  157. }
  158. /*
  159. * (non-Javadoc)
  160. *
  161. * @see java.sql.Connection#createClob()
  162. */
  163. public Clob createClob() throws SQLException {
  164. // TODO Auto-generated method stub
  165. throw new SQLException("Method not supported");
  166. }
  167. /*
  168. * (non-Javadoc)
  169. *
  170. * @see java.sql.Connection#createNClob()
  171. */
  172. public NClob createNClob() throws SQLException {
  173. // TODO Auto-generated method stub
  174. throw new SQLException("Method not supported");
  175. }
  176. /*
  177. * (non-Javadoc)
  178. *
  179. * @see java.sql.Connection#createSQLXML()
  180. */
  181. public SQLXML createSQLXML() throws SQLException {
  182. // TODO Auto-generated method stub
  183. throw new SQLException("Method not supported");
  184. }
  185. /**
  186. * Creates a Statement object for sending SQL statements to the database.
  187. *
  188. * @throws SQLException
  189. * if a database access error occurs.
  190. * @see java.sql.Connection#createStatement()
  191. */
  192. public Statement createStatement() throws SQLException {
  193. if (isClosed) {
  194. throw new SQLException("Can't create Statement, connection is closed");
  195. }
  196. return new HiveStatement(client);
  197. }
  198. /*
  199. * (non-Javadoc)
  200. *
  201. * @see java.sql.Connection#createStatement(int, int)
  202. */
  203. public Statement createStatement(int resultSetType, int resultSetConcurrency)
  204. throws SQLException {
  205. // TODO Auto-generated method stub
  206. throw new SQLException("Method not supported");
  207. }
  208. /*
  209. * (non-Javadoc)
  210. *
  211. * @see java.sql.Connection#createStatement(int, int, int)
  212. */
  213. public Statement createStatement(int resultSetType, int resultSetConcurrency,
  214. int resultSetHoldability) throws SQLException {
  215. // TODO Auto-generated method stub
  216. throw new SQLException("Method not supported");
  217. }
  218. /*
  219. * (non-Javadoc)
  220. *
  221. * @see java.sql.Connection#createStruct(java.lang.String, java.lang.Object[])
  222. */
  223. public Struct createStruct(String typeName, Object[] attributes)
  224. throws SQLException {
  225. // TODO Auto-generated method stub
  226. throw new SQLException("Method not supported");
  227. }
  228. /*
  229. * (non-Javadoc)
  230. *
  231. * @see java.sql.Connection#getAutoCommit()
  232. */
  233. public boolean getAutoCommit() throws SQLException {
  234. return true;
  235. }
  236. /*
  237. * (non-Javadoc)
  238. *
  239. * @see java.sql.Connection#getCatalog()
  240. */
  241. public String getCatalog() throws SQLException {
  242. return "";
  243. }
  244. /*
  245. * (non-Javadoc)
  246. *
  247. * @see java.sql.Connection#getClientInfo()
  248. */
  249. public Properties getClientInfo() throws SQLException {
  250. // TODO Auto-generated method stub
  251. throw new SQLException("Method not supported");
  252. }
  253. /*
  254. * (non-Javadoc)
  255. *
  256. * @see java.sql.Connection#getClientInfo(java.lang.String)
  257. */
  258. public String getClientInfo(String name) throws SQLException {
  259. // TODO Auto-generated method stub
  260. throw new SQLException("Method not supported");
  261. }
  262. /*
  263. * (non-Javadoc)
  264. *
  265. * @see java.sql.Connection#getHoldability()
  266. */
  267. public int getHoldability() throws SQLException {
  268. // TODO Auto-generated method stub
  269. throw new SQLException("Method not supported");
  270. }
  271. /*
  272. * (non-Javadoc)
  273. *
  274. * @see java.sql.Connection#getMetaData()
  275. */
  276. public DatabaseMetaData getMetaData() throws SQLException {
  277. return new HiveDatabaseMetaData(client);
  278. }
  279. /*
  280. * (non-Javadoc)
  281. *
  282. * @see java.sql.Connection#getTransactionIsolation()
  283. */
  284. public int getTransactionIsolation() throws SQLException {
  285. return Connection.TRANSACTION_NONE;
  286. }
  287. /*
  288. * (non-Javadoc)
  289. *
  290. * @see java.sql.Connection#getTypeMap()
  291. */
  292. public Map<String, Class<?>> getTypeMap() throws SQLException {
  293. // TODO Auto-generated method stub
  294. throw new SQLException("Method not supported");
  295. }
  296. /*
  297. * (non-Javadoc)
  298. *
  299. * @see java.sql.Connection#getWarnings()
  300. */
  301. public SQLWarning getWarnings() throws SQLException {
  302. return warningChain;
  303. }
  304. /*
  305. * (non-Javadoc)
  306. *
  307. * @see java.sql.Connection#isClosed()
  308. */
  309. public boolean isClosed() throws SQLException {
  310. return isClosed;
  311. }
  312. /*
  313. * (non-Javadoc)
  314. *
  315. * @see java.sql.Connection#isReadOnly()
  316. */
  317. public boolean isReadOnly() throws SQLException {
  318. return false;
  319. }
  320. /*
  321. * (non-Javadoc)
  322. *
  323. * @see java.sql.Connection#isValid(int)
  324. */
  325. public boolean isValid(int timeout) throws SQLException {
  326. // TODO Auto-generated method stub
  327. throw new SQLException("Method not supported");
  328. }
  329. /*
  330. * (non-Javadoc)
  331. *
  332. * @see java.sql.Connection#nativeSQL(java.lang.String)
  333. */
  334. public String nativeSQL(String sql) throws SQLException {
  335. // TODO Auto-generated method stub
  336. throw new SQLException("Method not supported");
  337. }
  338. /*
  339. * (non-Javadoc)
  340. *
  341. * @see java.sql.Connection#prepareCall(java.lang.String)
  342. */
  343. public CallableStatement prepareCall(String sql) throws SQLException {
  344. // TODO Auto-generated method stub
  345. throw new SQLException("Method not supported");
  346. }
  347. /*
  348. * (non-Javadoc)
  349. *
  350. * @see java.sql.Connection#prepareCall(java.lang.String, int, int)
  351. */
  352. public CallableStatement prepareCall(String sql, int resultSetType,
  353. int resultSetConcurrency) throws SQLException {
  354. // TODO Auto-generated method stub
  355. throw new SQLException("Method not supported");
  356. }
  357. /*
  358. * (non-Javadoc)
  359. *
  360. * @see java.sql.Connection#prepareCall(java.lang.String, int, int, int)
  361. */
  362. public CallableStatement prepareCall(String sql, int resultSetType,
  363. int resultSetConcurrency, int resultSetHoldability) throws SQLException {
  364. // TODO Auto-generated method stub
  365. throw new SQLException("Method not supported");
  366. }
  367. /*
  368. * (non-Javadoc)
  369. *
  370. * @see java.sql.Connection#prepareStatement(java.lang.String)
  371. */
  372. public PreparedStatement prepareStatement(String sql) throws SQLException {
  373. return new HivePreparedStatement(client, sql);
  374. }
  375. /*
  376. * (non-Javadoc)
  377. *
  378. * @see java.sql.Connection#prepareStatement(java.lang.String, int)
  379. */
  380. public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
  381. throws SQLException {
  382. return new HivePreparedStatement(client, sql);
  383. }
  384. /*
  385. * (non-Javadoc)
  386. *
  387. * @see java.sql.Connection#prepareStatement(java.lang.String, int[])
  388. */
  389. public PreparedStatement prepareStatement(String sql, int[] columnIndexes)
  390. throws SQLException {
  391. // TODO Auto-generated method stub
  392. throw new SQLException("Method not supported");
  393. }
  394. /*
  395. * (non-Javadoc)
  396. *
  397. * @see java.sql.Connection#prepareStatement(java.lang.String,
  398. * java.lang.String[])
  399. */
  400. public PreparedStatement prepareStatement(String sql, String[] columnNames)
  401. throws SQLException {
  402. // TODO Auto-generated method stub
  403. throw new SQLException("Method not supported");
  404. }
  405. /*
  406. * (non-Javadoc)
  407. *
  408. * @see java.sql.Connection#prepareStatement(java.lang.String, int, int)
  409. */
  410. public PreparedStatement prepareStatement(String sql, int resultSetType,
  411. int resultSetConcurrency) throws SQLException {
  412. return new HivePreparedStatement(client, sql);
  413. }
  414. /*
  415. * (non-Javadoc)
  416. *
  417. * @see java.sql.Connection#prepareStatement(java.lang.String, int, int, int)
  418. */
  419. public PreparedStatement prepareStatement(String sql, int resultSetType,
  420. int resultSetConcurrency, int resultSetHoldability) throws SQLException {
  421. // TODO Auto-generated method stub
  422. throw new SQLException("Method not supported");
  423. }
  424. /*
  425. * (non-Javadoc)
  426. *
  427. * @see java.sql.Connection#releaseSavepoint(java.sql.Savepoint)
  428. */
  429. public void releaseSavepoint(Savepoint savepoint) throws SQLException {
  430. // TODO Auto-generated method stub
  431. throw new SQLException("Method not supported");
  432. }
  433. /*
  434. * (non-Javadoc)
  435. *
  436. * @see java.sql.Connection#rollback()
  437. */
  438. public void rollback() throws SQLException {
  439. // TODO Auto-generated method stub
  440. throw new SQLException("Method not supported");
  441. }
  442. /*
  443. * (non-Javadoc)
  444. *
  445. * @see java.sql.Connection#rollback(java.sql.Savepoint)
  446. */
  447. public void rollback(Savepoint savepoint) throws SQLException {
  448. // TODO Auto-generated method stub
  449. throw new SQLException("Method not supported");
  450. }
  451. /*
  452. * (non-Javadoc)
  453. *
  454. * @see java.sql.Connection#setAutoCommit(boolean)
  455. */
  456. public void setAutoCommit(boolean autoCommit) throws SQLException {
  457. // TODO Auto-generated method stub
  458. throw new SQLException("Method not supported");
  459. }
  460. /*
  461. * (non-Javadoc)
  462. *
  463. * @see java.sql.Connection#setCatalog(java.lang.String)
  464. */
  465. public void setCatalog(String catalog) throws SQLException {
  466. // TODO Auto-generated method stub
  467. throw new SQLException("Method not supported");
  468. }
  469. /*
  470. * (non-Javadoc)
  471. *
  472. * @see java.sql.Connection#setClientInfo(java.util.Properties)
  473. */
  474. public void setClientInfo(Properties properties)
  475. throws SQLClientInfoException {
  476. // TODO Auto-generated method stub
  477. throw new SQLClientInfoException("Method not supported", null);
  478. }
  479. /*
  480. * (non-Javadoc)
  481. *
  482. * @see java.sql.Connection#setClientInfo(java.lang.String, java.lang.String)
  483. */
  484. public void setClientInfo(String name, String value)
  485. throws SQLClientInfoException {
  486. // TODO Auto-generated method stub
  487. throw new SQLClientInfoException("Method not supported", null);
  488. }
  489. /*
  490. * (non-Javadoc)
  491. *
  492. * @see java.sql.Connection#setHoldability(int)
  493. */
  494. public void setHoldability(int holdability) throws SQLException {
  495. // TODO Auto-generated method stub
  496. throw new SQLException("Method not supported");
  497. }
  498. /*
  499. * (non-Javadoc)
  500. *
  501. * @see java.sql.Connection#setReadOnly(boolean)
  502. */
  503. public void setReadOnly(boolean readOnly) throws SQLException {
  504. // TODO Auto-generated method stub
  505. throw new SQLException("Method not supported");
  506. }
  507. /*
  508. * (non-Javadoc)
  509. *
  510. * @see java.sql.Connection#setSavepoint()
  511. */
  512. public Savepoint setSavepoint() throws SQLException {
  513. // TODO Auto-generated method stub
  514. throw new SQLException("Method not supported");
  515. }
  516. /*
  517. * (non-Javadoc)
  518. *
  519. * @see java.sql.Connection#setSavepoint(java.lang.String)
  520. */
  521. public Savepoint setSavepoint(String name) throws SQLException {
  522. // TODO Auto-generated method stub
  523. throw new SQLException("Method not supported");
  524. }
  525. /*
  526. * (non-Javadoc)
  527. *
  528. * @see java.sql.Connection#setTransactionIsolation(int)
  529. */
  530. public void setTransactionIsolation(int level) throws SQLException {
  531. // TODO Auto-generated method stub
  532. throw new SQLException("Method not supported");
  533. }
  534. /*
  535. * (non-Javadoc)
  536. *
  537. * @see java.sql.Connection#setTypeMap(java.util.Map)
  538. */
  539. public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
  540. // TODO Auto-generated method stub
  541. throw new SQLException("Method not supported");
  542. }
  543. /*
  544. * (non-Javadoc)
  545. *
  546. * @see java.sql.Wrapper#isWrapperFor(java.lang.Class)
  547. */
  548. public boolean isWrapperFor(Class<?> iface) throws SQLException {
  549. // TODO Auto-generated method stub
  550. throw new SQLException("Method not supported");
  551. }
  552. /*
  553. * (non-Javadoc)
  554. *
  555. * @see java.sql.Wrapper#unwrap(java.lang.Class)
  556. */
  557. public <T> T unwrap(Class<T> iface) throws SQLException {
  558. // TODO Auto-generated method stub
  559. throw new SQLException("Method not supported");
  560. }
  561. }