Because OushuDB is compatible with PostgreSQL, OushuDB can be accessed using the PostgreSQL driver and programming interface.
-
https://jdbc.postgresql.org/
-
https://www.postgresql.org/docs/8.2/static/libpq.html
-
https://odbc.postgresql.org/
JDBC example
A simple example of JDBC accessing OushuDB is given below.
//OushuDBJDBC.javaimportjava.sql.Connection;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;importjava.sql.DriverManager;importjava.sql.ResultSetMetaData;publicclassOushuDBJDBC {publicstaticConnectiongetConn() {Connectionconn=null;try {Class.forName( "org.postgresql.Driver");Stringurl="jdbc:postgresql://localhost:5432/postgres";try {conn=DriverManager.getConnection(url, "ChangLei", "");}catch (SQLExceptione) {e.printStackTrace();}}catch (ClassNotFoundExceptione) {e.printStackTrace();}returnconn;}publicstaticvoid main(String[] args) {Connectionconn=getConn();Stringsql="select * from gp_segment_configuration";try {Statementstmt=conn.createStatement();ResultSetrs=stmt.executeQuery(sql);ResultSetMetaDatarsmd=rs.getMetaData();intnumCol=rsmd.getColumnCount ();for (inti=0; i<numCol; i++) {System.out.print(rsmd.getColumnName(i+1) +' ');}System.out.println();while(rs.next()) {for (inti=0; i<numCol; i++) {System.out.print(rs.getString(i+1) +' ');}System.out.println();}}catch (SQLExceptione) {e.printStackTrace();}}}
Compiling and running the above code requires the following steps:
-
Install the JDK, and confirm that the java and javac commands work
-
Copy the above code to OushuDBJDBC.java, you need to change the OushuDB username and password in the following line.
# To access OushuDB, if you don't already have a user, you need to create a user, assign permissions to it, and modify pg_hba.conf so that the user can connect to the database. See the following links: # - Create user: http://www.oushu.com/docs/hawq/reference/sql/create-user/# - Assign permissions: http://www.oushu.com/docs/hawq /reference/sql/grant/# - Modify pg_hba.conf: https://www.postgresql.org/docs/8.2/static/auth-pg-hba-conf.html# Note that the master node needs to be reset after modifying pg_hba.conf To load pg_hba.conf, you can use the command: hawq stop --reload conn = DriverManager . getConnection ( url , "ChangLei" , "" );
Download the JDBC driver:
wgethttps://jdbc.postgresql.org/download/postgresql-9.4.1212.jar
compile
javac-cp./postgresql-9.4.1212.jarOushuDBJDBC.java
Run OushuDBJDBC
changlei: dev ChangLei $ java OushuDBJDBC
registration_order role status port hostname address description
0 m u 5432 changlei changlei null
1 p u 40000 localhost 127.0.0.1