From 2e1a0030bc9c0c3b2557b20cf3f33e8caaf0741f Mon Sep 17 00:00:00 2001
From: gengjun-git <54172532+gengjun-git@users.noreply.github.com>
Date: Mon, 30 Mar 2020 13:54:36 +0800
Subject: [PATCH] Add some connect samples (#3221)
Add connect samples for golang, java , nodejs, php, python.
---
samples/connect/cpp/README.md | 31 ++++
samples/connect/cpp/doris_client.cpp | 12 +-
samples/connect/golang/.gitignore | 2 +
samples/connect/golang/README.md | 35 ++++
samples/connect/golang/client.go | 103 +++++++++++
samples/connect/golang/go.mod | 22 +++
samples/connect/java/client/.gitignore | 5 +
samples/connect/java/client/README.md | 28 +++
samples/connect/java/client/pom.xml | 97 ++++++++++
.../client/src/main/java/client/Client.java | 166 ++++++++++++++++++
samples/connect/nodejs/.gitignore | 1 +
samples/connect/nodejs/README.md | 28 +++
samples/connect/nodejs/app.js | 100 +++++++++++
samples/connect/nodejs/package-lock.json | 81 +++++++++
samples/connect/nodejs/package.json | 14 ++
samples/connect/php/README.md | 27 +++
samples/connect/php/client.php | 99 +++++++++++
samples/connect/python/README.md | 27 +++
samples/connect/python/connector.py | 97 ++++++++++
19 files changed, 969 insertions(+), 6 deletions(-)
create mode 100644 samples/connect/cpp/README.md
create mode 100644 samples/connect/golang/.gitignore
create mode 100644 samples/connect/golang/README.md
create mode 100644 samples/connect/golang/client.go
create mode 100644 samples/connect/golang/go.mod
create mode 100644 samples/connect/java/client/.gitignore
create mode 100644 samples/connect/java/client/README.md
create mode 100644 samples/connect/java/client/pom.xml
create mode 100644 samples/connect/java/client/src/main/java/client/Client.java
create mode 100644 samples/connect/nodejs/.gitignore
create mode 100644 samples/connect/nodejs/README.md
create mode 100644 samples/connect/nodejs/app.js
create mode 100644 samples/connect/nodejs/package-lock.json
create mode 100644 samples/connect/nodejs/package.json
create mode 100644 samples/connect/php/README.md
create mode 100644 samples/connect/php/client.php
create mode 100644 samples/connect/python/README.md
create mode 100755 samples/connect/python/connector.py
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 @@
+
+
+
+
+
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 "| siteid | citycode | pv |
|---|---|---|
| " . $row["siteid"] . " | " . $row["citycode"] . " | " . $row["pv"] . " |
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")