使用操作

交互方式

Hubble的交互方式有命令行方式和jdbc方式两种。

命令行方式

hubblebin目录下,脚本程序hubble-sql.sh启动cli。

有用的参数有:

--catalog <catalog>             进入hubble命令行的默认catalog
--database <database>           进入hubble命令行的默认database
-e <execute>                    使用-e后边跟SQL进行查询,用引号将需要执行的SQL包起来
-f <file>                       使用-f后边跟文件进行查询
--server <server>               hubble提供服务的地址
--user <user>                   hubble的用户名
--password <password>           hubble的密码
--hubvar<name>                  外部输入参数名称
--output-format <output-format> 执行查询输出的格式,包括ALIGNED, VERTICAL, JSON, CSV, TSV,CSV_HEADER, CSV_UNQUOTED,CSV_HEADER_UNQUOTED等格式,默认CSV格式输出

例如

1.在命令行下执行查询:

bin/hubble-sql.sh --server=hubble01:30008 --user=hubble
hubble-cli> show catalogs;
+---------+
| Catalog |
+---------+
| hubble  |
| system  |
+---------+
(4 rows in set)

2.47 sec

2.使用-e参数查询:

bin/hubble-sql.sh --server=hubble01:30008 --user=hubble -e "show catalogs"
"hubble"
"system"

3.使用-f参数查询:

新建文本showcata.sql

内容show catalogs

bin/hubble-sql.sh --server=hubble01:30008 --user=hubble -f showcata.sql
"hubble"
"system"

4.使用-hubvar参数查询:

使用语法:

bin/hubble-sql.sh --server= ip+端口 --user=用户 --catalog=catalog数据源 --password 
--execute"slect * from tablename where column='\${hubvar:参数名称}' " 
--hubvar "参数名称=xx"

数据样例

 f1 | f2 | f3 
----+----+----
 l  | 18 | 男 
 c  | 20 | 男 
 w  | 22 | 女 

样例: 查询表t12的内容f1字段判断条件通过hubvar传递

[hubble@hubble01 bin]$./hubble-sql.sh --server=https://hubble01.hubbledb.cn:7077 --user=hubble  --catalog=hubble --password  
--execute "select * from hubble.test.t12 where f1 ='\${hubvar:a}'" --hubvar "a=l";

--返回查询结果
"l","18","男 " 

注意: 使用命令行传参时需要加转义符号\ 并且 ''单引号不是${hubvar:name}中参数具体看字段类型

样例: 使用多个传递参数

[hubble@hubble01 bin]$ ./hubble-sql.sh --server=https://hubble01.hubbledb.cn:7077 --user=hubble  --
catalog=hubble --password  --execute "select * from hubble.test.t12 where \${hubvar:a} ='\${hubvar:b}'
" --hubvar "a=f1" --hubvar "b=l" ;                                                                                                    
--返回查询结果                                                                   
"l","18","男 " 

-f命令执行sql文件并使用hubvar传递参数

使用语法:

#client参数配置
bin/hubble-sql.sh --server= ip+端口 --user=用户 --catalog=catalog数据源 --password 
-f 调用sql文件名称
--hubvar "参数设置名称=xx"
#sql文件中配置
select * from tablename where cloumn ='${hubvar:xxx}' 

样例: 使用-f 命令查询a.sql 并使用hubvar传递文件中参数

创建a.sql文件

select * from hubble.test.t12 where f1 ='${hubvar:a}' and f2=${hubvar:b};

注意: sql文件调用时不需要加转义符\

执行命令调用a.sql

[hubble@hubble01 bin]$ ./hubble-sql.sh --server=https://hubble01.hubbledb.cn:7077 --user=hubble  --catalog=hubble --password  -f a.sql --hubvar "a=c"  --hubvar "b=20";

--返回查询结果                                                                   
"c","20","男 " 

5.用户密码认证

[hubble@hubble01 hubble-5.2.1]$ export HUBBLE_PASSWORD=hubble; bin/hubble-sql.sh --server=https://hubble01.hubbledb.cn:33484    --user=hubble --password 
Password: 
hubble> show schemas in hubble;
       Schema       
--------------------
 default            
 information_schema 
(2 rows)

Query 20230329_060232_00000_rnitj, FINISHED, 5 nodes
Splits: 87 total, 87 done (100.00%)
4.01 [2 rows, 35B] [0 rows/s, 9B/s]

hubble> create schema hubble.zl;

注意:

用户密码认证 需要通过https 方式访问

export HUBBLE_PASSWORD 设置密码

server 指定master 域名

JDBC方式-无认证

使用Hubble JDBC驱动提供JDBC方式访问Hubble。

包括两种:写JDBC程序,导入JDBC驱动来访问。使用dbeaver等通用数据库端工具的方式进行访问。

代码方式

java方式

url可以是下列中的任一种:

jdbc:hubble://host:port
jdbc:hubble://host:port/<catalog>
jdbc:hubble://host:port/<catalog>/<database>

catalogshema为可选项。可以限定jdbc程序默认连接的catalogdatabase

在项目中导入jdbc驱动包,并创建下面的类:

package com.beagledata.hubble.hubblejdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcTest {

	private static final String JDBC_URL = "jdbc:hubble://master:30008/hubble/tpcds";
	private static final String username = "hadoop";

	public static void main(String[] args) {
		try {
			Class.forName("com.beagledata.hubble.jdbc.HubbleDriver");
			
			try(Connection conn = DriverManager.getConnection(JDBC_URL, username, null);
				Statement stmt = conn.createStatement()){
				ResultSet res = stmt.executeQuery("select * from tpcds.lineitem  limit 20");
				int col = res.getMetaData().getColumnCount();
		        while (res.next()) {
		            for (int i = 1; i <= col; i++) {
		                System.out.print(res.getString(i) + "\t");
		                if ((i == 2) && (res.getString(i).length() < 8)) {
		                    System.out.print("\t");
		                }
		             }
		            System.out.println("");
		        }
			}
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}
	}
}

运行,即可查询出tpcds库下的lineitem表中的数据。

使用hubvar调用参数

package com.zendesk.maxwell.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import com.beagledata.hubble.jdbc.HubbleDriver;



public class HubbleDemo {
        public static void main(String[] args) {
                String sql="select * from hubble.default.t7 where nmae='${hubvar:nmae}'";
                try {
                        testconnection1(sql);
                } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }
        
        public static void testconnection1(String sql) throws ClassNotFoundException, SQLException {
        Class.forName("com.beagledata.hubble.jdbc.HubbleDriver");
                
                Properties pro = new Properties();        
                String url= "jdbc:hubble://192.168.100.123:7077";
                HubbleDriver hubbleDriver =new HubbleDriver();
                pro.put("user", "hubble");
                pro.put("hubvar:nmae", "a3");
                Connection conn=hubbleDriver.connect(url, pro);
                
                Statement stmt = conn.createStatement();
                

                ResultSet re=stmt.executeQuery(sql);
                while(re.next()) {
                        System.out.println(re.getString(1));
                }
                
                stmt.close();
                conn.close();
        }

}

python方式

导入的Hubble包需要找相关工作人员获取

# coding:utf8
import hubble

conn = hubble.dbapi.connect(
    host='192.168.100.134',#主机IP
    port=20088,#数据库端口为启动端口
    user='hubble',#用户
    catalog='hubble',#指定数据源
    schema='test'# 库名
)


cur = conn.cursor()
# -----------------------  sql 查询 -------------------------------
sl_sql="""select * from hubble.test.t_test2 limit 1 """
cur.execute(sl_sql)
#获取每一行,可选其他参数:fetchone获取一行
rows = cur.fetchall()

# 显示每列的详细信息
des = cur.description
print("表的描述:", des)
# 获取表头
print("表头:", ",".join([item[0] for item in des]))
print(rows)
conn.close()
cur.close()

插入数据

import hubble
from  hubble import transaction
with hubble.dbapi.connect(
    host='192.168.100.134',#主机ip
    port=20088,#数据库端口
    user='hubble',#用户名
    catalog='hubble',#数据源   
    schema='test',#库名
    isolation_level=transaction.IsolationLevel.REPEATABLE_READ,#开启事物
) as conn:
    cur = conn.cursor()
#------------------------------------insertsql-----------------------------------
    ins_sql="""INSERT INTO hubble.test.t_test2 VALUES (1, '2', '3')"""
    cur.execute(ins_sql)
    cur.fetchall()
#释放资源
    caonn.close()
    cur.close()

通用客户端方式

1.使用DBeaver进行连接。

下载地址:DBeaver

打开DBeaver后,在导航栏找到数据库-驱动管理器

driver
driver

选择新建

jdbc
jdbc

填写驱动名称,类型选择Generic,类名填写com.beagledata.hubble.jdbc.HubbleDriver

添加文件添加Hubble的JDBC驱动程序。

点击OK

接下来进行配置连接,文件-新建

new
new

选择数据库连接,并点击Next按钮。

newhubble
newhubble

选择上面配的驱动,点击Next按钮。

config
config

填写上正确的信息后,点击finish

连接后就可以进行使用了。

JDBC方式-有认证

代码方式

java代码

package com.beagledata.hubble.jdbc;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcTest {

    private static final String JDBC_URL = "jdbc:hubble://master:30008/hubble/tpcds?SSL=true&SSLVerification=NONE";
    private static final String username = "hubble";
    private static final String password = "hubble";
    public static void main(String[] args) {
        try {
            Class.forName("com.beagledata.hubble.jdbc.HubbleDriver");

            try(Connection conn = DriverManager.getConnection(JDBC_URL, username, password);
                Statement stmt = conn.createStatement()){
                ResultSet res = stmt.executeQuery("select * from tpcds.lineitem  limit 20");
                int col = res.getMetaData().getColumnCount();
                while (res.next()) {
                    for (int i = 1; i <= col; i++) {
                        System.out.print(res.getString(i) + "\t");
                        if ((i == 2) && (res.getString(i).length() < 8)) {
                            System.out.print("\t");
                        }
                    }
                    System.out.println("");
                }
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
}

python代码

from hubble.dbapi import connect
from hubble.auth import BasicAuthentication
import urllib3

# 用户名和密码
username = "hubble"
password = "hubble"

# 禁用 urllib3 警告
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

# 连接到 hubble
conn = connect(
    host="192.168.100.123",
    port="7077", 
    user=username,
    auth=BasicAuthentication(username, password),
    http_scheme="https",
    verify=None
)
cur = conn.cursor()
#查询sql
sl_sql="""SELECT * FROM hubble.zh_wh_odsfff.t12"""
# 执行查询
result = cur.execute(sl_sql)

# 打印查询结果
for row in result:
    print(row)

注意:

必须添加 SSL=true&SSLVerification=NONE

通用客户端

在jdbc参数后添加证书认证

jdbc:hubble://192.168.100.123:33484/hubble/zl?SSL=true&SSLVerification=NONE 

注意:
必须添加 SSL=true&SSLVerification=NONE