使用jdbc访问日志服务

简介

一直以来,日志服务提供了 以restfull API方式写入、查询日志数据,管理自己的项目及日志库。现在日志服务新增提供了mysql 接口,用户可以使用jdbc连接到日志服务,通过标准的sql语法进行查询和计算。

使用方法

数据模型映射

日志服务数据模型 SQL数据模型
project database
logstore table
accesskeyId user
accessKey password

支持的region

目前仅支持经典网络内网访问和VPC网络访问。各个地域的地址参考文档。地址为内网域名和VPC域名,端口为10005。

帐号和权限

访问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

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

支持的filter语法

  1. 字符串搜索:key = "value" ,查询的是分词之后的结果。
  2. 字符串模糊搜索: key = "valu*" , 查询的是分词之后模糊匹配的结果。
  3. 数值比较: num_field > 1, 支持的比较运算符包括> >= = < <=。
  4. 逻辑运算: and or not。例如 a = "x" and b ="y" 或 a = "x" and not b ="y"
  5. 如果使用全文索引搜索,需要使用特殊的key __line__ ,例如 __line__ ="abc"

支持的计算

支持的计算语法参见文档

在程序中使用

开发者可以在任何一个支持mysql connector的程序中使用mysql语法连接日志服务。例如使用jdbc或者python MySQLdb。

使用样例

import com.mysql.jdbc.*;

import java.sql.*;
import java.sql.Connection;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

public class testjdbc {
    public static void main(String args[]){

        Connection conn = null;
        Statement stmt = null;
        try {
            //STEP 2: Register JDBC driver
            Class.forName("com.mysql.jdbc.Driver");

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

            conn = DriverManager.getConnection("jdbc:mysql://cn-shanghai-intranet.sls.aliyuncs.com:10005/shproject1","accessid","accesskey");
            System.out.println("Connected database successfully...");


            //STEP 4: Execute a query
            System.out.println("Creating statement...");
            stmt = conn.createStatement();

            String sql = "SELECT method,min(latency,10)  as c,max(latency,10) from sls_operation_log " +
                    "where  time__>=1500975424 and __time < 1501035044  and time < 1501035044 " +
                    "    and latency > 0  and latency < 6142629 and  not  (  method='Postlogstorelogs' or method='GetLogtailConfig') group by method " ;

            sql = "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' )";


            sql = "select count(1) from  sls_operation_log where     date  >       '2017-08-07 00:00:00'   " +
                    " and  date <     '2017-08-08 00:00:00' limit 100";

            ResultSet rs = stmt.executeQuery(sql);

            //STEP 5: Extract data from result set
            while(rs.next()){
                //Retrieve by column name

                ResultSetMetaData data = rs.getMetaData();
                System.out.println(data.getColumnCount());
                for(int i = 0;i < data.getColumnCount();++i) {
                    String name = data.getColumnName(i+1);
                    System.out.print(name+":");
                    System.out.print(rs.getObject(name));
                }

                System.out.println();
            }
            rs.close();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

使用mysql client连接

image

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值