README.rst 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. Phoenix database adapter for Python
  2. ===================================
  3. ``phoenixdb`` is a Python library for accessing
  4. `Apache Phoenix <http://phoenix.apache.org/>`_
  5. using the
  6. `remote query server <http://phoenix.apache.org/server.html>`_.
  7. This library implements the
  8. standard `DB API 2.0 <https://www.python.org/dev/peps/pep-0249/>`_ interface and a
  9. subset of `SQLAlchemy <https://www.sqlalchemy.org/>`_, either of which should be familiar
  10. to most Python programmers.
  11. Installation
  12. ------------
  13. The source code is part of the phoenix-queryserver source distribution.
  14. You can download it from <https://phoenix.apache.org/>, or get the latest development version
  15. from <https://github.com/apache/phoenix-queryserver>
  16. Extract the archive and then install it manually::
  17. cd /path/to/phoenix-queryserver-x.y.z/python/phoenixdb
  18. python setup.py install
  19. Usage
  20. -----
  21. The library implements the standard DB API 2.0 interface, so it can be
  22. used the same way you would use any other SQL database from Python, for example::
  23. import phoenixdb
  24. import phoenixdb.cursor
  25. database_url = 'http://localhost:8765/'
  26. conn = phoenixdb.connect(database_url, autocommit=True)
  27. cursor = conn.cursor()
  28. cursor.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, username VARCHAR)")
  29. cursor.execute("UPSERT INTO users VALUES (?, ?)", (1, 'admin'))
  30. cursor.execute("SELECT * FROM users")
  31. print(cursor.fetchall())
  32. cursor = conn.cursor(cursor_factory=phoenixdb.cursor.DictCursor)
  33. cursor.execute("SELECT * FROM users WHERE id=1")
  34. print(cursor.fetchone()['USERNAME'])
  35. Setting up a development environment
  36. ------------------------------------
  37. If you want to quickly try out the included examples, you can set up a
  38. local `virtualenv <https://virtualenv.pypa.io/en/latest/>`_ with all the
  39. necessary requirements::
  40. virtualenv e
  41. source e/bin/activate
  42. pip install -r requirements.txt
  43. python setup.py develop
  44. You can start a Phoenix QueryServer instance on http://localhost:8765 for testing by running
  45. the following command in the pohoenix-queryserver-parent directory::
  46. mvn clean verify -am -pl phoenix-queryserver-it -Dtest=foo \
  47. -Dit.test=QueryServerBasicsIT\#startLocalPQS \
  48. -Ddo.not.randomize.pqs.port=true -Dstart.unsecure.pqs=true
  49. You can start a secure (https+kerberos) Phoenix QueryServer instance on https://localhost:8765
  50. for testing by running the following command in the phoenix-queryserver-parent directory::
  51. mvn clean verify -am -pl phoenix-queryserver-it -Dtest=foo \
  52. -Dit.test=SecureQueryServerPhoenixDBIT\#startLocalPQS \
  53. -Ddo.not.randomize.pqs.port=true -Dstart.secure.pqs=true
  54. this will also create a shell script in phoenix-queryserver-it/target/krb_setup.sh, that you can use to set
  55. up the environment for the tests.
  56. If you want to use the library without installing the phoenixdb library, you can use
  57. the `PYTHONPATH` environment variable to point to the library directly::
  58. cd phoenix-queryserver-parent/python-phoenixdb
  59. python setup.py build
  60. cd ~/my_project
  61. PYTHONPATH=$PHOENIX_HOME/build/lib python my_app.py
  62. Don't forget to run flake8 on your changes.
  63. Running the test suite
  64. ----------------------
  65. The library comes with a test suite for testing Python DB API 2.0 compliance and
  66. various Phoenix-specific features. In order to run the test suite, you need a
  67. working Phoenix database and set the ``PHOENIXDB_TEST_DB_URL`` environment variable::
  68. export PHOENIXDB_TEST_DB_URL='http://localhost:8765/'
  69. nosetests
  70. If you use a secure PQS server, you can set the connection parameters via the following environment
  71. variables:
  72. - PHOENIXDB_TEST_DB_TRUSTSTORE
  73. - PHOENIXDB_TEST_DB_AUTHENTICATION
  74. - PHOENIXDB_TEST_DB_AVATICA_USER
  75. - PHOENIXDB_TEST_DB_AVATICA_PASSWORD
  76. Similarly, tox can be used to run the test suite against multiple Python versions::
  77. pyenv install 3.5.5
  78. pyenv install 3.6.4
  79. pyenv install 2.7.14
  80. pyenv global 2.7.14 3.5.5 3.6.4
  81. PHOENIXDB_TEST_DB_URL='http://localhost:8765' tox
  82. You can use tox and docker to run the tests on supported python versions up to 3.8 without
  83. installing the environments locally::
  84. docker build -t toxtest .
  85. docker run --rm -v `pwd`:/src toxtest
  86. You can also run the test suite from maven as part of the Java build by setting the
  87. run.full.python.testsuite property. You DO NOT need to set the PHOENIXDB_* enviroment variables,
  88. maven will set them up for you. The output of the test run will be saved in
  89. phoenix-queryserver/phoenix-queryserver-it/target/python-stdout.log and python-stderr.log::
  90. mvn clean verify -Drun.full.python.testsuite=true
  91. Known issues
  92. ------------
  93. - TIME and DATE columns in Phoenix are stored as full timestamps with a millisecond accuracy,
  94. but the remote protocol only exposes the time (hour/minute/second) or date (year/month/day)
  95. parts of the columns. (`CALCITE-797 <https://issues.apache.org/jira/browse/CALCITE-797>`_, `CALCITE-798 <https://issues.apache.org/jira/browse/CALCITE-798>`_)
  96. - TIMESTAMP columns in Phoenix are stored with a nanosecond accuracy, but the remote protocol truncates them to milliseconds. (`CALCITE-796 <https://issues.apache.org/jira/browse/CALCITE-796>`_)
  97. SQLAlchemy feature support
  98. --------------------------
  99. SQLAlchemy has a wide breadth of API, ranging from basic SQL commands to object-relational mapping support.
  100. Today, python-phoenixdb only supports the following subset of the complete SQLAlchemy API:
  101. - `Textual SQL <https://docs.sqlalchemy.org/en/13/core/tutorial.html#using-textual-sql>`_
  102. All other API should be considered not implemented.