query.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env python
  2. # Licensed to Cloudera, Inc. under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. Cloudera, Inc. 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. from py4j.java_gateway import JavaGateway
  18. gateway = JavaGateway()
  19. jdbc_driver = 'com.mysql.jdb.Driver'
  20. db_url = 'jdbc:mysql://localhost/hue'
  21. username = 'root'
  22. password = 'root'
  23. conn = gateway.jvm.java.sql.DriverManager.getConnection(db_url, username, password)
  24. try:
  25. stmt = conn.createStatement()
  26. try:
  27. rs = stmt.executeQuery('select username,email from auth_user')
  28. try:
  29. md = rs.getMetaData()
  30. for i in xrange(md.getColumnCount()):
  31. print md.getColumnTypeName(i + 1)
  32. while rs.next():
  33. username = rs.getString("username")
  34. email = rs.getString("email")
  35. print username, email
  36. finally:
  37. rs.close()
  38. finally:
  39. stmt.close()
  40. finally:
  41. conn.close()