Updated on 2023-11-30 GMT+08:00

Lua SDK

This chapter describes how to access a DMS Kafka queue by using a Linux Lua client. To obtain related configuration information, see Preparing the Environment.

DMS is compatible with native Kafka APIs. For details on how to call native Kafka APIs, see the Kafka Documentation.

Preparing the Client Environment

  1. Download the SDK package.

    Decompress the kafka-dms-lua.tar.gz file from the SDK package.

  2. Install the LuaJIT tool.

    Run the following command to check whether the LuaJIT tool has been installed:

    luajit -v

    Install the LuaJIT tool if it has not been installed.
    • Ubuntu OS:
      • Run the apt-cache search luajit command to check whether LuaJIT exists in the software repository. If not, configure the correct software repository address.
      • Run the apt-get install luajit command to install LuaJIT.
    • SUSE OS:
      • Run the zypper search luajit command to check whether LuaJIT exists in the software repository. If not, configure the correct software repository address.
      • Run the zypper install luajit command to install LuaJIT.
    • CentOS or Red Hat OS:
      • yum search luajit
      • yum install luajit

  3. Decompress the kafka-dms-lua.tar.gz package.

    The following assumes that the package is decompressed in the {DMSPATH} directory.

  4. Run the following command to copy the Kafka dependency packages to the lib directory:

    cp {DMSPATH}/kafka-dms-lua/librdkafka/lib/* /usr/lib/ -R

  5. Modify the parameters in the example_dms.lua file.

    cd {DMSPATH}/kafka-dmslua/luadms

    vi example_dms.lua

    Modify the following parameters:

    local BROKERS_ADDRESS = { "broker" }
    local TOPIC_NAME = "your kafka topic"
    
    config["sasl.project.id"]="your projectId"
    config["sasl.access.key"]="your ak"
    config["sasl.security.key"]="your sk"
    Table 1 Parameter description

    Parameter

    Description

    How to Obtain

    BROKERS_ADDRESS

    Kafka endpoint

    For details, see Preparing the Environment.

    TOPIC_NAME

    Kafka topic ID

    sasl.project.id

    Project ID

    sasl.access.key

    Tenant AK

    security.key

    Tenant SK

Running Example

Run the following command to produce messages:

luajit example_dms.lua

After this command is run, 10 messages are automatically sent to the Kafka queue.

Code Example

Producer parameters

for i = 0,10 do
    producer:produce(topic, KAFKA_PARTITION_UA, "this is test message"..tostring(i))
end

while producer:outq_len() ~= 0 do
    producer:poll(10)
end