Add some connect samples (#3221)

Add connect samples for golang, java , nodejs, php, python.
This commit is contained in:
gengjun-git
2020-03-30 13:54:36 +08:00
committed by GitHub
parent 5f9359d618
commit 2e1a0030bc
19 changed files with 969 additions and 6 deletions

View File

@ -0,0 +1,31 @@
<!--
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.
-->
# 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

View File

@ -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);

2
samples/connect/golang/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
client
go.sum

View File

@ -0,0 +1,35 @@
<!--
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.
-->
# 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.

View File

@ -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")
}

View File

@ -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

View File

@ -0,0 +1,5 @@
client.iml
client.jar
.idea
lib
target

View File

@ -0,0 +1,28 @@
<!--
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.
-->
# 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.

View File

@ -0,0 +1,97 @@
<?xml version="1.0" encoding="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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache</groupId>
<artifactId>doris-client</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<properties>
<file_encoding>UTF-8</file_encoding>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
</dependencies>
<build>
<finalName>client</finalName>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>${file_encoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>${project.basedir}</outputDirectory>
<archive>
<manifest>
<mainClass>client.Client</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>lib</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -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();
}
}
}

1
samples/connect/nodejs/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules

View File

@ -0,0 +1,28 @@
<!--
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.
-->
# 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.

View File

@ -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");
}
});

81
samples/connect/nodejs/package-lock.json generated Normal file
View File

@ -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="
}
}
}

View File

@ -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"
}
}

View File

@ -0,0 +1,27 @@
<!--
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.
-->
# 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.

View File

@ -0,0 +1,99 @@
<!--
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.
-->
<!DOCTYPE html>
<html>
<body>
<?php
$host = "192.168.100.80";
$user = "root";
$password = "";
$database = "db_test";
$port = 9030;
# connect to doris
$conn = new mysqli($host, $user, $password, "", $port);
if ($conn->connect_errno) {
echo "<p> connect to doris failed. " . $conn->connect_errno . "</p>";
}
echo "<p> connect to doris successfully </p>";
# create database
$sql = "CREATE DATABASE IF NOT EXISTS " . $database;
if ($conn->query($sql) === TRUE) {
echo "<p> create database successfully </p>";
} else {
echo "<p> create database failed. " . $conn->error . "</p>";
}
# set db context
if ($conn->select_db($database) === TRUE) {
echo "<p> set db context successfully </p>";
} else {
echo "<p> set db context failed. " . $conn->error . "</p>";
}
# 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 "<p> create table successfully </p>";
} else {
echo "<p> create table failed. " . $conn->error . "</p>";
}
# insert data
$sql = "INSERT INTO table_test values(1, 2, 3), (4, 5, 6), (1, 2, 4)";
if ($conn->query($sql) === TRUE) {
echo "<p> insert data successfully </p>";
} else {
echo "<p> insert data failed. " . $conn->error . "</p>";
}
# query data
$sql = "SELECT siteid, citycode, pv FROM table_test";
$result = $conn->query($sql);
if ($result) {
echo "<p> query data successfully </p>";
echo "<table><tr><th>siteid</th><th>citycode</th><th>pv</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["siteid"] . "</td><td>" . $row["citycode"] . "</td><td>" . $row["pv"] . "</td></tr>";
}
echo "</table>";
} else {
echo "<p> query data failed. " . $conn->error . "</p>";
}
# drop database
$sql = "DROP DATABASE IF EXISTS " . $database;
if ($conn->query($sql) === TRUE) {
echo "<p> drop database successfully </p>";
} else {
echo "<p> drop database failed. " . $conn->error . "</p>";
}
# close connection
$conn->close();
?>
</body>
</html>

View File

@ -0,0 +1,27 @@
<!--
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.
-->
# 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.

View File

@ -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")