ログの照会および分析には、機能別操作一覧 に加えて、Java Database Connectivity (JDBC) および標準の SQL-92 を使用できます。
接続パラメーター
パラメーター | 例 | 説明 |
---|---|---|
host | regionid.example.com | エンドポイントサービスエンドポイント 現在 Log Service へのアクセスは、クラシックネットワークまたは Virtual Private Cloud (VPC) からイントラネットを介して行うことができます。 |
port | 10005 | ポート番号 デフォルト値:10005 |
user | bq2s**** | AccessKey ID |
password | 4fdO**** | AccessKey Secret |
削除 | sample-project | ご使用のアカウント内の プロジェクト |
table | sample-logstore | プロジェクト内の Logstore |
mysql -hcn-shanghai-intranet.log.aliyuncs.com -ubq2**** -p4fd**** -P10005
use sample-project; //Use a project.
前提条件
- Alibaba Cloud アカウントまたは RAM ユーザーのAccessKey を取得して、JDBC にアクセスしていること。 RAM ユーザーはプロジェクト所有者に属し、プロジェクトからデータを読み取る権限があります。
- MySQL は、JDBC を介して接続されたクエリのページネーションをサポートしません。
構文
注意事項
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条件のフィルター構文です。
意味論 | 例 | 説明 |
---|---|---|
文字検索 | key = "value" | 単語セグメンテーション後にデータを照会します。 |
あいまい一致検索 | key = "valu*" | 単語セグメンテーション後にあいまい一致検索でデータを照会します。 |
数値比較 | num_field > 1 | > 、>= 、= 、< 、<= などの比較演算子がサポートされています。 |
論理演算 | and or not | たとえば、 a = "x" and b = "y" または a = "x" and not b = "y" 。 |
全文検索 | __line__ ="abc" | 特別なキー (__line__ ) が必要です。 |
計算構文
サポートされている計算演算子の詳細については、「リアルタイム分析」の構文の説明をご参照ください。
SQL-92 構文
SQL92 構文は、フィルタ構文と計算構文の組み合わせです。
以下のクエリが例として使用されています。
status>200 |select avg(latency),max(latency) ,count(1) as c GROUP BY method ORDER BY c DESC LIMIT 20
標準の SQL-92 構文に従って、クエリ内のフィルターと時間条件は、次のように新しいクエリ条件に結合できます。
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 構文を使用して、MySQL コネクタをサポートする任意のアプリケーションで Log Service に接続できます。 たとえば、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 the JDBC driver.
Class.forName("com.mysql.jdbc.Driver");
// Step 3: Establish a connection.
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection("jdbc:mysql://cn-shanghai-intranet.log.aliyuncs.com:10005/sample-project","accessid","accesskey");
System.out.println("Connected database successfully...") ;
// Step 4: Start a query.
System.out.println( "Creating statement ...");
stmt = conn.createStatement();
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 " ;
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' )";
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";
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();
}
}
}
}
}
クラシックネットワークまたは VPC 上で、MySQLクライアントを介して Log Service に接続できます。
説明
- 1 にプロジェクト名を入力します。
- 2 に Logstore 名を入力します。