diff --git a/samples/connect/cpp/README.md b/samples/connect/cpp/README.md new file mode 100644 index 0000000000..98144e0190 --- /dev/null +++ b/samples/connect/cpp/README.md @@ -0,0 +1,31 @@ + + + +# How to use: + 1. g++ doris_client.cpp -o doris_client `mysql_config --cflags --libs` + 2. ./doris_client + +# What can this demo do: + This is a cpp demo for doris client, you can test basic function such as + connection, CRUD of your doris. You should install mysql prior to running + this demo. + +# Supported mysql version: 5.6, 5.7, ..., 8.0 + diff --git a/samples/connect/cpp/doris_client.cpp b/samples/connect/cpp/doris_client.cpp index 4cb4c8e026..e3306da13c 100644 --- a/samples/connect/cpp/doris_client.cpp +++ b/samples/connect/cpp/doris_client.cpp @@ -98,11 +98,11 @@ int main() { // Doris connection host string host = "127.0.0.1"; // Doris connection port - int port = 8080; + int port = 9030; // Doris connection username - string user = "username"; + string user = "root"; // Doris connection password - string password = "password"; + string password = ""; // Local mysql sock address string sock_add = "/var/lib/mysql/mysql.sock"; // database to create @@ -131,17 +131,17 @@ int main() { // create doris table string sql_create_table = "CREATE TABLE cpp_doris_table(siteid INT,citycode SMALLINT,pv BIGINT SUM) "\ "AGGREGATE KEY(siteid, citycode) DISTRIBUTED BY HASH(siteid) BUCKETS 10 "\ - "PROPERTIES(\"replication_num\" = \"1\");"; + "PROPERTIES(\"replication_num\" = \"1\")"; std::cout << sql_create_table << std::endl; client_new.exec(sql_create_table); // insert into doris table - string sql_insert = "insert into cpp_doris_table values(1, 2, 3), (4,5,6), (1,2,4);"; + string sql_insert = "insert into cpp_doris_table values(1, 2, 3), (4,5,6), (1,2,4)"; std::cout << sql_insert << std::endl; client_new.exec(sql_insert); // select from doris table - string sql_select = "select * from cpp_doris_table;"; + string sql_select = "select * from cpp_doris_table"; std::cout << sql_select << std::endl; client_new.exec(sql_select); diff --git a/samples/connect/golang/.gitignore b/samples/connect/golang/.gitignore new file mode 100644 index 0000000000..0c78cdd1a0 --- /dev/null +++ b/samples/connect/golang/.gitignore @@ -0,0 +1,2 @@ +client +go.sum diff --git a/samples/connect/golang/README.md b/samples/connect/golang/README.md new file mode 100644 index 0000000000..e9bb01ceb1 --- /dev/null +++ b/samples/connect/golang/README.md @@ -0,0 +1,35 @@ + + + +# How to use: +=====for Go 1.11 or higher + 1. go build + 2. ./client + +=====before Go 1.11 + 1. copy client.go to your GOPATH/src + 2. go get -u github.com/go-sql-driver/mysql + 3. go build + 4. ./client + +# What can this demo do: + This is a golang demo for doris client, you can test basic function such as + connection, CRUD of your doris. + diff --git a/samples/connect/golang/client.go b/samples/connect/golang/client.go new file mode 100644 index 0000000000..b6613dcbc8 --- /dev/null +++ b/samples/connect/golang/client.go @@ -0,0 +1,103 @@ +/** +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +**/ + +package main + +import ( + "database/sql" + "fmt" + _ "github.com/go-sql-driver/mysql" +) + +func main() { + host := "127.0.0.1" + port := 9030 + user := "root" + password := "" + dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/", user, password, host, port) + + //connect to doris + driver, err := sql.Open("mysql", dsn) + if err != nil { + fmt.Printf("open mysql driver failed, error[%v]\n", err) + return + } + if err := driver.Ping(); err != nil { + fmt.Printf("ping doris failed, error[%v]\n", err) + return + } + fmt.Printf("connect to doris successfully\n") + + //create database + if _, err := driver.Exec("CREATE DATABASE IF NOT EXISTS db_test"); err != nil { + fmt.Printf("create database failed, error[%v]\n", err) + return + } + fmt.Printf("create database successfully\n") + + //set db context + if _, err := driver.Exec("USE db_test"); err != nil { + fmt.Printf("set db context failed, error[%v]\n", err) + return + } + fmt.Printf("set db context successfully\n") + + //create table + SQL := "CREATE TABLE IF NOT EXISTS table_test(siteid INT, citycode SMALLINT, pv BIGINT SUM) " + + "AGGREGATE KEY(siteid, citycode) " + + "DISTRIBUTED BY HASH(siteid) BUCKETS 10 " + + "PROPERTIES(\"replication_num\" = \"1\")" + if _, err := driver.Exec(SQL); err != nil { + fmt.Printf("create table failed, error[%v]\n", err) + return + } + fmt.Printf("create table successfully\n") + + //insert data + SQL = "INSERT INTO table_test values(1, 2, 3), (4, 5, 6), (1, 2, 4)" + if _, err := driver.Exec(SQL); err != nil { + fmt.Printf("insert data failed, error[%v]\n", err) + return + } + fmt.Printf("insert data successfully\n") + + //query data + rows, err := driver.Query("SELECT * FROM table_test") + if err != nil { + fmt.Printf("query data from doris failed, error[%v]\n", err) + return + } + for rows.Next() { + var siteId, cityCode int + var pv int64 + if err := rows.Scan(&siteId, &cityCode, &pv); err != nil { + fmt.Printf("scan columns failed, error[%v]\n", err) + return + } + fmt.Printf("%d\t%d\t%d\n", siteId, cityCode, pv) + } + fmt.Printf("query data successfully\n") + + //drop database + if _, err := driver.Exec("DROP DATABASE IF EXISTS db_test"); err != nil { + fmt.Printf("drop database failed, error[%v]\n", err) + return + } + fmt.Printf("drop database successfully\n") +} diff --git a/samples/connect/golang/go.mod b/samples/connect/golang/go.mod new file mode 100644 index 0000000000..9576273ea7 --- /dev/null +++ b/samples/connect/golang/go.mod @@ -0,0 +1,22 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +module client + +go 1.12 + +require github.com/go-sql-driver/mysql v1.5.0 diff --git a/samples/connect/java/client/.gitignore b/samples/connect/java/client/.gitignore new file mode 100644 index 0000000000..fe0263cc03 --- /dev/null +++ b/samples/connect/java/client/.gitignore @@ -0,0 +1,5 @@ +client.iml +client.jar +.idea +lib +target diff --git a/samples/connect/java/client/README.md b/samples/connect/java/client/README.md new file mode 100644 index 0000000000..0ac3c59304 --- /dev/null +++ b/samples/connect/java/client/README.md @@ -0,0 +1,28 @@ + + +# How to use: + 1. mvn package + 2. java -jar client.jar + +# What can this demo do: + This is a java demo for doris client, you can test basic function such as + connection, CRUD of your doris. You should install maven prior to run + this demo. + diff --git a/samples/connect/java/client/pom.xml b/samples/connect/java/client/pom.xml new file mode 100644 index 0000000000..58a94c55e4 --- /dev/null +++ b/samples/connect/java/client/pom.xml @@ -0,0 +1,97 @@ + + + + + + 4.0.0 + + org.apache + doris-client + 0.0.1 + jar + + + UTF-8 + + + + + mysql + mysql-connector-java + 5.1.26 + + + + + client + ${project.basedir}/src/main/java + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + ${file_encoding} + + + + org.apache.maven.plugins + maven-jar-plugin + + + package + + jar + + + + + ${project.basedir} + + + client.Client + true + lib + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-dependencies + package + + copy-dependencies + + + ${project.basedir}/lib + + + + + + + diff --git a/samples/connect/java/client/src/main/java/client/Client.java b/samples/connect/java/client/src/main/java/client/Client.java new file mode 100644 index 0000000000..b8342ea3f5 --- /dev/null +++ b/samples/connect/java/client/src/main/java/client/Client.java @@ -0,0 +1,166 @@ +/** +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +**/ + +package client; + +import java.sql.*; + +public class Client { + + public static void main(String[] args) { + String host = "127.0.0.1"; + //query_port in fe.conf + String port = "9030"; + String user = "root"; + //password is empty by default + String password = ""; + + //connect to doris + Connection conn = null; + try { + conn = getConn(host, port, user, password, ""); + } catch (Exception e) { + System.out.println("connect to doris failed"); + e.printStackTrace(); + return; + } + System.out.println("connect to doris successfully"); + + //create statement + Statement stmt = null; + try { + stmt = conn.createStatement(); + } catch (SQLException e) { + System.out.println("create statement failed"); + e.printStackTrace(); + closeConn(conn); + return; + } + System.out.println("create statement successfully"); + + //create database + try { + stmt.execute("CREATE DATABASE IF NOT EXISTS db_test"); + } catch (SQLException e) { + System.out.println("create database failed"); + e.printStackTrace(); + closeStmt(stmt); + closeConn(conn); + return; + } + System.out.println("create database successfully"); + + //set db context + try { + stmt.execute("USE db_test"); + } catch (SQLException e) { + System.out.println("set db context failed"); + e.printStackTrace(); + closeStmt(stmt); + closeConn(conn); + return; + } + System.out.println("set db context successfully"); + + //create table + try { + stmt.execute("CREATE TABLE IF NOT EXISTS table_test(siteid INT, citycode SMALLINT, pv BIGINT SUM) " + + "AGGREGATE KEY(siteid, citycode) " + + "DISTRIBUTED BY HASH(siteid) BUCKETS 10 " + + "PROPERTIES(\"replication_num\" = \"1\")"); + } catch (Exception e) { + System.out.println("create table failed"); + e.printStackTrace(); + closeStmt(stmt); + closeConn(conn); + return; + } + System.out.println("create table successfully"); + + //insert data + try { + stmt.execute("INSERT INTO table_test values(1, 2, 3), (4, 5, 6), (1, 2, 4)"); + } catch (Exception e) { + System.out.println("insert data failed"); + e.printStackTrace(); + closeStmt(stmt); + closeConn(conn); + return; + } + System.out.println("insert data successfully"); + + //query data + try { + ResultSet result = stmt.executeQuery("SELECT * FROM table_test"); + System.out.println("data queried is :"); + while (result.next()) { + int siteid = result.getInt("siteid"); + int citycode = result.getInt("citycode"); + int pv = result.getInt("pv"); + System.out.println("\t" + siteid + "\t" + citycode + "\t" + pv); + } + } catch (Exception e) { + System.out.println("query data failed"); + e.printStackTrace(); + closeStmt(stmt); + closeConn(conn); + return; + } + + //drop database + try { + stmt.execute("DROP DATABASE IF EXISTS db_test"); + } catch (Exception e) { + System.out.println("drop database failed"); + e.printStackTrace(); + closeStmt(stmt); + closeConn(conn); + return; + } + System.out.println("drop database successfully"); + closeStmt(stmt); + closeConn(conn); + } + + public static Connection getConn(String host, String port, String user, String password, String database) throws Exception { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://" + host + ":" + port + "/" + database + "?user=" + user + "&password=" + password; + return DriverManager.getConnection(url); + } + + public static void closeConn(Connection conn) { + try { + conn.close(); + System.out.println("conn closed"); + } catch (Exception e) { + System.out.println("close conn failed"); + e.printStackTrace(); + } + } + + public static void closeStmt(Statement stmt) { + try { + stmt.close(); + System.out.println("stmt closed"); + } catch (Exception e) { + System.out.println("close stmt failed"); + e.printStackTrace(); + } + } +} diff --git a/samples/connect/nodejs/.gitignore b/samples/connect/nodejs/.gitignore new file mode 100644 index 0000000000..3c3629e647 --- /dev/null +++ b/samples/connect/nodejs/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/samples/connect/nodejs/README.md b/samples/connect/nodejs/README.md new file mode 100644 index 0000000000..b7669f5104 --- /dev/null +++ b/samples/connect/nodejs/README.md @@ -0,0 +1,28 @@ + + + +# How to use: + 1. npm install + 2. node app.js + +# What can this demo do: + This is a nodejs demo for doris client, you can test basic function such as + connection, CRUD of your doris. + diff --git a/samples/connect/nodejs/app.js b/samples/connect/nodejs/app.js new file mode 100644 index 0000000000..2050cf09cd --- /dev/null +++ b/samples/connect/nodejs/app.js @@ -0,0 +1,100 @@ +/** +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +**/ + +var mysql = require("mysql"); + +//connect to doris +var conn = mysql.createConnection({ + host: "127.0.0.1", + user: "root", + password: "", + //端口为fe.conf中的query_port + port: 9030, +}); +conn.connect(function(err){ + if (err) { + console.log("connect to doris failed: " + err); + } else { + console.log("connect to doris successfully"); + } +}); + +//create database +conn.query("CREATE DATABASE IF NOT EXISTS db_test", function(err){ + if (err) { + console.log("create database failed: " + err); + } else { + console.log("create database successfully"); + } +}); + +//set db context +conn.query("USE db_test", function(err){ + if (err) { + console.log("set db context failed: " + err); + } else { + console.log("set db context successfully"); + } +}); + +//create table +var sql = "CREATE TABLE IF NOT EXISTS table_test(siteid INT, citycode SMALLINT, pv BIGINT SUM) " + + "AGGREGATE KEY(siteid, citycode) " + + "DISTRIBUTED BY HASH(siteid) BUCKETS 10 " + + "PROPERTIES(\"replication_num\" = \"1\")"; +conn.query(sql, function(err){ + if (err) { + console.log("create table failed: " + err); + } else { + console.log("create table successfully"); + } +}); + +//insert data +sql = "INSERT INTO table_test values(1, 2, 3), (4, 5, 6), (1, 2, 4)" +conn.query(sql, function(err){ + if (err) { + console.log("insert data failed: " + err); + } else { + console.log("insert data successfully"); + } +}); + +//query data +conn.query("SELECT * FROM table_test", function(err, result){ + if (err) { + console.log("query data failed: " + err); + return; + } + + console.log("query data successfully"); + console.log("siteid\tcitycode\tpv"); + result.forEach(function(row){ + console.log(row.siteid + "\t" + row.citycode + "\t" + row.pv); + }); +}); + +//drop database +conn.query("DROP DATABASE IF EXISTS db_test", function(err){ + if (err) { + console.log("drop database failed: ", err); + } else { + console.log("drop database successfully"); + } +}); diff --git a/samples/connect/nodejs/package-lock.json b/samples/connect/nodejs/package-lock.json new file mode 100644 index 0000000000..98c25ae58d --- /dev/null +++ b/samples/connect/nodejs/package-lock.json @@ -0,0 +1,81 @@ +{ + "name": "client", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "bignumber.js": { + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.0.0.tgz", + "integrity": "sha512-t/OYhhJ2SD+YGBQcjY8GzzDHEk9f3nerxjtfa6tlMXfe7frs/WozhvCNoGvpM0P3bNf3Gq5ZRMlGr5f3r4/N8A==" + }, + "core-util-is": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", + "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" + }, + "inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" + }, + "isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" + }, + "mysql": { + "version": "2.18.1", + "resolved": "https://registry.npmjs.org/mysql/-/mysql-2.18.1.tgz", + "integrity": "sha512-Bca+gk2YWmqp2Uf6k5NFEurwY/0td0cpebAucFpY/3jhrwrVGuxU2uQFCHjU19SJfje0yQvi+rVWdq78hR5lig==", + "requires": { + "bignumber.js": "9.0.0", + "readable-stream": "2.3.7", + "safe-buffer": "5.1.2", + "sqlstring": "2.3.1" + } + }, + "process-nextick-args": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", + "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==" + }, + "readable-stream": { + "version": "2.3.7", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.7.tgz", + "integrity": "sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw==", + "requires": { + "core-util-is": "1.0.2", + "inherits": "2.0.4", + "isarray": "1.0.0", + "process-nextick-args": "2.0.1", + "safe-buffer": "5.1.2", + "string_decoder": "1.1.1", + "util-deprecate": "1.0.2" + } + }, + "safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, + "sqlstring": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.1.tgz", + "integrity": "sha1-R1OT/56RR5rqYtyvDKPRSYOn+0A=" + }, + "string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "requires": { + "safe-buffer": "5.1.2" + } + }, + "util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" + } + } +} diff --git a/samples/connect/nodejs/package.json b/samples/connect/nodejs/package.json new file mode 100644 index 0000000000..451d80ce24 --- /dev/null +++ b/samples/connect/nodejs/package.json @@ -0,0 +1,14 @@ +{ + "name": "client", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "mysql": "^2.18.1" + } +} diff --git a/samples/connect/php/README.md b/samples/connect/php/README.md new file mode 100644 index 0000000000..7bc19cc616 --- /dev/null +++ b/samples/connect/php/README.md @@ -0,0 +1,27 @@ + + +# How to use: + 1. you should add this file to a web server which support php + 2. php should enable mysqli extension + +# What can this demo do: + This is a php demo for doris client, you can test basic function such as + connection, CRUD of your doris. + diff --git a/samples/connect/php/client.php b/samples/connect/php/client.php new file mode 100644 index 0000000000..9a151b04ae --- /dev/null +++ b/samples/connect/php/client.php @@ -0,0 +1,99 @@ + + + + + +connect_errno) { + echo "

connect to doris failed. " . $conn->connect_errno . "

"; +} +echo "

connect to doris successfully

"; + +# create database +$sql = "CREATE DATABASE IF NOT EXISTS " . $database; +if ($conn->query($sql) === TRUE) { + echo "

create database successfully

"; +} else { + echo "

create database failed. " . $conn->error . "

"; +} + +# set db context +if ($conn->select_db($database) === TRUE) { + echo "

set db context successfully

"; +} else { + echo "

set db context failed. " . $conn->error . "

"; +} + +# create table +$sql = "CREATE TABLE IF NOT EXISTS table_test(siteid INT, citycode SMALLINT, pv BIGINT SUM) " . + "AGGREGATE KEY(siteid, citycode) " . + "DISTRIBUTED BY HASH(siteid) BUCKETS 10 " . + "PROPERTIES(\"replication_num\" = \"1\")"; +if ($conn->query($sql) === TRUE) { + echo "

create table successfully

"; +} else { + echo "

create table failed. " . $conn->error . "

"; +} + +# insert data +$sql = "INSERT INTO table_test values(1, 2, 3), (4, 5, 6), (1, 2, 4)"; +if ($conn->query($sql) === TRUE) { + echo "

insert data successfully

"; +} else { + echo "

insert data failed. " . $conn->error . "

"; +} + +# query data +$sql = "SELECT siteid, citycode, pv FROM table_test"; +$result = $conn->query($sql); +if ($result) { + echo "

query data successfully

"; + echo ""; + while($row = $result->fetch_assoc()) { + echo ""; + } + echo "
siteidcitycodepv
" . $row["siteid"] . "" . $row["citycode"] . "" . $row["pv"] . "
"; +} else { + echo "

query data failed. " . $conn->error . "

"; +} + +# drop database +$sql = "DROP DATABASE IF EXISTS " . $database; +if ($conn->query($sql) === TRUE) { + echo "

drop database successfully

"; +} else { + echo "

drop database failed. " . $conn->error . "

"; +} + +# close connection +$conn->close(); + +?> + + diff --git a/samples/connect/python/README.md b/samples/connect/python/README.md new file mode 100644 index 0000000000..737dde0870 --- /dev/null +++ b/samples/connect/python/README.md @@ -0,0 +1,27 @@ + + +# How to use: + 1. pip install mysql-connector-python + 2. ./connector.py + +# What can this demo do: + This is a python demo for doris client, you can test basic function such as + connection, CRUD of your doris. + diff --git a/samples/connect/python/connector.py b/samples/connect/python/connector.py new file mode 100755 index 0000000000..2ef5e0ab7f --- /dev/null +++ b/samples/connect/python/connector.py @@ -0,0 +1,97 @@ +#!/usr/bin/python +# -*- coding: UTF-8 -*- +""" +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +""" + +import mysql.connector + +config = { + "user": "root", + "password": "", + "host": "192.168.100.80", + "port": 9030, + "charset": "utf8" +} + +# connect to doris +try: + cnx = mysql.connector.connect(**config) +except mysql.connector.Error as err: + print("connect to doris failed. {}".format(err)) + exit(1) +print("connect to doris successfully") + +cursor = cnx.cursor() + +# create database +try: + cursor.execute("CREATE DATABASE IF NOT EXISTS db_test") +except mysql.connector.Error as err: + print("create database failed. {}".format(err)) + exit(1) +print("create database successfully") + +# set db context +try: + cursor.execute("USE db_test") +except mysql.connector.Error as err: + print("set db context failed. {}".format(err)) + exit(1) +print("set db context successfully") + +# create table +sql = ("CREATE TABLE IF NOT EXISTS table_test(siteid INT, citycode SMALLINT, pv BIGINT SUM) " + "AGGREGATE KEY(siteid, citycode) " + "DISTRIBUTED BY HASH(siteid) BUCKETS 10 " + "PROPERTIES(\"replication_num\" = \"1\")") +try: + cursor.execute(sql) +except mysql.connector.Error as err: + print("create table failed. {}".format(err)) + exit(1) +print("create table successfully") + +# insert data +sql = "INSERT INTO table_test values(1, 2, 3), (4, 5, 6), (1, 2, 4)" +try: + cursor.execute(sql) +except mysql.connector.Error as err: + print("insert data failed. {}".format(err)) + exit(1) +print("insert data successfully") + +# query data +sql = "SELECT siteid, citycode, pv FROM table_test" +try: + cursor.execute(sql) +except mysql.connector.Error as err: + print("query data failed. {}".format(err)) + exit(1) +print("query data successfully") +print("siteid\tcitycode\tpv") +for (siteid, citycode, pv) in cursor: + print("{}\t{}\t{}").format(siteid, citycode, pv) + +# drop database +try: + cursor.execute("DROP DATABASE IF EXISTS db_test") +except mysql.connector.Error as err: + print("drop database failed. {}".format(err)) + exit(1) +print("drop database successfully")