All Documents
Current Document

Content is empty

If you don't find the content you expect, please try another search term

Documentation

Use the API to upload data

Last updated:2020-12-28 14:55:49

Overview

KLog provides the API for uploading data. For more information, see Putlogs. This topic describes how to serialize raw log data to a protocol buffer stream so that the API can write the data to KLog.

Step 1: Define the protocol format

Create a klog.proto file in the following format:

message Log
{
    message Content
    {
        required string key   = 1; // The key of the field.
        required string value = 2; // The value of the field.
    }
    required int64 time = 1; // The UNIX timestamp
    repeated Content contents = 2; // The combination of key-value pairs.
}
message LogGroup
{
    repeated Log    logs        = 1; // The log array consisting of multiple logs.
    optional string reserved    = 2; // Reserved.
    optional string filename    = 3; // The name of the log file.
    optional string source      = 4; // The source of logs, represented by a machine IP address usually.
}
message LogGroupList // Used to return logs, which can be consumed by calling PullLogs.
{
    repeated LogGroup logGroupList = 1; // The LogGroup list.
}

Step 2: Compile the klog.proto file

After you define the protocol format, use the protocol buffer compiler to compile the klog.proto file to classes in the specific language. These classes provide simple methods for you to access each field. This allows you to serialize or deserialize structured data in the same way as you access class methods.

In this example, the proto complier is used to compile the klog.proto file to a Java file in the same directory. Compilation command:

 protoc.exe --java_out=./ klog.proto  
  • Note: --java_out=./ indicates that a Java file is generated in the current directory. ./klog.proto indicates the klog.proto file in the current directory.

After the command is executed, the klog.java file is generated.

Step 3: Call the API

Copy the klog.java and klog.proto files to your project directory for compilation.

On this page
Pure ModeNormal Mode

Pure Mode

Click to preview the document content in full screen
Feedback