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.

Did you find the above information helpful?

Unhelpful
Mostly Unhelpful
A little helpful
Helpful
Very helpful

What might be the problems?

Insufficient
Outdated
Unclear or awkward
Redundant or clumsy
Lack of context for the complex system or functionality

More suggestions

0/200

Please give us your feedback.

Submitted

Thank you for your feedback.

问题反馈