开发者社区> 问答> 正文

JDBC怎么查询和分析?

Restful API外,您还可以使用JDBC + 标准SQL 92进行日志查询与分析。

连接参数

连接参数示例说明
hostcn-shanghai-intranet.log.aliyuncs.com访问点,目前仅支持经典网络内网访问和VPC网络访问
port10005默认使用10005作为端口号
userbq2sjzesjmo86kq访问秘钥 AccesskeyId
password4fdO1fTDDuZP访问秘钥 Accesskey
databasesample-project用户账号下的 项目(Project)
tablesample-logstore项目下的 日志库(Logstore)

例如通过MySQL命令连接示例如下:
  1. mysql -hcn-shanghai-intranet.log.aliyuncs.com -ubq2sjzesjmo86kq -p4fdO1fTDDuZP -P10005

  2. use sample-project; // 使用某个Project


前提条件


访问JDBC接口,必须使用主账号的AK或者子帐号的AK。子帐号必须是Project owner的子帐号,同时子帐号具有Project级别的读权限。

语法说明



注意事项


在where条件中必须包含__date__或__time__来限制查询的时间范围。__date__是timestamp类型__time__是bigint类型。
例如:
  • __date__ > '2017-08-07 00:00:00' and __date__ < '2017-08-08 00:00:00'
  • __time__ > 1502691923 and __time__ < 1502692923

上述两种条件必须出现一个。

过滤语法


关于where下过滤(filter)语法如下:
语义示例说明
字符串搜索key = "value"查询的是分词之后的结果
字符串模糊搜索key = "valu*"查询的是分词之后模糊匹配的结果
数值比较num_field > 1支持的比较运算符包括>、>=、 =、 <和<=
逻辑运算and or not例如a = "x" and b ="y"或a = "x" and not b ="y"
全文搜索__line__ ="abc"如果使用全文索引搜索,需要使用特殊的key(__line__)


计算语法


支持计算操作符参见 分析语法

SQL92语法


过滤 + 计算组合为SQL92语法。
例如对于如下查询:
  1. status>200 |select avg(latency),max(latency) ,count(1) as c GROUP BY  method  ORDER BY c DESC  LIMIT 20

我们可以将查询中过滤部分+ 时间条件组合成为查询的条件,变成标准SQL92语法:
  1. select avg(latency),max(latency) ,count(1) as c from sample-logstore where status>200 and __time__>=1500975424 and __time__ < 1501035044 GROUP BY  method  ORDER BY c DESC  LIMIT 20


通过JDBC协议访问



程序调用


开发者可以在任何一个支持MySQL connector的程序中使用MySQL语法连接日志服务。例如使用JDBC或者Python MySQLdb。
使用样例:
  1. import com.mysql.jdbc.*;

  2. import java.sql.*;
  3. import java.sql.Connection;
  4. import java.sql.ResultSetMetaData;
  5. import java.sql.Statement;
  6. pmport java.sql.Statement;
  7. public static void main(String args[]){
  8. Connection conn = null;      
  9. Statement stmt = null;
  10. try {      
  11. //STEP 2: Register JDBC driver          
  12. Class.forName("com.mysql.jdbc.Driver");        

  13. //STEP 3: Open a connection
  14. System.out.println("Connecting to a selected database...");

  15. conn = DriverManager.getConnection("jdbc:mysql://cn-shanghai-intranet.log.aliyuncs.com:10005/sample-project","accessid","accesskey");
  16. System.out.println("Connected database successfully...");    
  17. //STEP 4: Execute a query
  18. System.out.println("Creating statement...");
  19. stmt = conn.createStatement();            
  20. String sql = "SELECT method,min(latency,10)  as c,max(latency,10) from sample-logstore where  __time__>=1500975424 and __time__ < 1501035044 and latency > 0  and latency < 6142629 and  not (method='Postlogstorelogs' or method='GetLogtailConfig') group by method " ;

  21. String sql-example2 = "select count(1) ,max(latency),avg(latency), histogram(method),histogram(source),histogram(status),histogram(clientip),histogram(__source__) from  test10 where __date__  >'2017-07-20 00:00:00'  and  __date__ <'2017-08-02 00:00:00' and __line__='abc#def' and latency < 100000 and (method = 'getlogstorelogS' or method='Get**' and method <> 'GetCursorOrData' )";

  22. String sql-example3 = "select count(1) from  sample-logstore where     __date__  >       '2017-08-07 00:00:00' and  __date__ <     '2017-08-08 00:00:00' limit 100";

  23. ResultSet rs = stmt.executeQuery(sql);
  24. //STEP 5: Extract data from result set
  25. while(rs.next()){
  26. //Retrieve by column name
  27.   
  28. System.out.println(data.getColumnCount());

[font='YaHei Consolas Hybrid', Consolas, 'Meiryo UI', 'Malgun Gothic', 'Segoe UI', 'Trebuchet MS', Helvetica, monospace, monospace]for [font='YaHei Consolas Hybrid', Consolas, 'Meiryo UI', 'Malgun Gothic', 'Segoe UI', 'Trebuchet MS', Helvetica, monospace, monospace]( [font='YaHei Consolas Hybrid', Consolas, 'Meiryo UI', 'Malgun Gothic', 'Segoe UI', 'Trebuchet MS', Helvetica, monospace, monospace]int[font='YaHei Consolas Hybrid', Consolas, 'Meiryo UI', 'Malgun Gothic', 'Segoe UI', 'Trebuchet MS', Helvetica, monospace, monospace] i [font='YaHei Consolas Hybrid', Consolas, 'Meiryo UI', 'Malgun Gothic', 'Segoe UI', 'Trebuchet MS', Helvetica, monospace, monospace]=[font='YaHei Consolas Hybrid', Consolas, 'Meiryo UI', 'Malgun Gothic', 'Segoe UI', 'Trebuchet MS', Helvetica, monospace, monospace] [font='YaHei Consolas Hybrid', Consolas, 'Meiryo UI', 'Malgun Gothic', 'Segoe UI', 'Trebuchet MS', Helvetica, monospace, monospace]0 [font='YaHei Consolas Hybrid', Consolas, 'Meiryo UI', 'Malgun Gothic', 'Segoe UI', 'Trebuchet MS', Helvetica, monospace, monospace];[font='YaHei Consolas Hybrid', Consolas, 'Meiryo UI', 'Malgun Gothic', 'Segoe UI', 'Trebuchet MS', Helvetica, monospace, monospace]i [font='YaHei Consolas Hybrid', Consolas, 'Meiryo UI', 'Malgun Gothic', 'Segoe UI', 'Trebuchet MS', Helvetica, monospace, monospace]<[font='YaHei Consolas Hybrid', Consolas, 'Meiryo UI', 'Malgun Gothic', 'Segoe UI', 'Trebuchet MS', Helvetica, monospace, monospace] data [font='YaHei Consolas Hybrid', Consolas, 'Meiryo UI', 'Malgun Gothic', 'Segoe UI', 'Trebuchet MS', Helvetica, monospace, monospace].[font='YaHei Consolas Hybrid', Consolas, 'Meiryo UI', 'Malgun Gothic', 'Segoe UI', 'Trebuchet MS', Helvetica, monospace, monospace]getColumnCount [font='YaHei Consolas Hybrid', Consolas, 'Meiryo UI', 'Malgun Gothic', 'Segoe UI', 'Trebuchet MS', Helvetica, monospace, monospace]();++[font='YaHei Consolas Hybrid', Consolas, 'Meiryo UI', 'Malgun Gothic', 'Segoe UI', 'Trebuchet MS', Helvetica, monospace, monospace]i [font='YaHei Consolas Hybrid', Consolas, 'Meiryo UI', 'Malgun Gothic', 'Segoe UI', 'Trebuchet MS', Helvetica, monospace, monospace])[font='YaHei Consolas Hybrid', Consolas, 'Meiryo UI', 'Malgun Gothic', 'Segoe UI', 'Trebuchet MS', Helvetica, monospace, monospace] [font='YaHei Consolas Hybrid', Consolas, 'Meiryo UI', 'Malgun Gothic', 'Segoe UI', 'Trebuchet MS', Helvetica, monospace, monospace]{
  1. String name = data.getColumnName(i+1);
  2. System.out.print(name+":");
  3. System.out.print(rs.getObject(name));
  4. }
  5. System.out.println();
  6. }
  7. rs.close();
  8. } catch (ClassNotFoundException e) {
  9. e.printStackTrace();
  10. } catch (SQLException e) {
  11. e.printStackTrace();
  12. } catch (Exception e) {
  13. e.printStackTrace();
  14. } finally {
  15. if (stmt != null) {
  16. try {
  17. stmt.close();
  18. } catch (SQLException e) {
  19. e.printStackTrace();
  20. }
  21. }
  22. if (conn != null) {
  23. try {
  24. conn.close();
  25. } catch (SQLException e) {
  26. e.printStackTrace();                
  27. }
  28. }          
  29. }      
  30. }    
  31. }


工具类调用


在经典网内网/VPC环境通过MySQL Client进行连接。
[backcolor=transparent]注意:
  1. ①处填写您的Project。
  2. ②处填写您的Logstore。

展开
收起
轩墨 2017-10-23 11:33:51 2048 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
时序数据库TSDB新功能 - 如何用SQL进行时序查询 立即下载
MySQL查询优化实战 立即下载
PolarDB NL2SQL: 帮助您写出准确、优化的SQL 立即下载