Updated on 2023-08-31 GMT+08:00

Producer Sample Commissioning

Prerequisites

  • Ensure that Windows is configured to allow Kafka access through an EIP if you need to debug applications on Windows. For details, see How Do I Access Kafka of the Cluster in Security Mode on Windows Using EIPs?.
  • Ensure that the current user has the read permission on all files in the src/main/resources directory and the dependent library file directory if you need to debug applications on Linux. Ensure that the JDK has been installed and Java environment variables have been set.

Commissioning Applications on Windows

  1. Ensure that the mappings between the host names and service IP addresses of all the hosts in the remote cluster are configured in the local hosts file.
  2. Run Producer.java on IntelliJ IDEA, as shown in Figure 1.

    Figure 1 Running Producer.java.

  3. Check the displayed console window and you can find that Producer is sending messages to the default topic (example-metric1). One piece of log is printed when every 10 messages are sent.

    Figure 2 Producer running window

Commissioning Applications on Linux

  1. In IntelliJ IDEA, configure Artifacts of the project before generating a JAR file.

    1. On the main page of the IDEA, choose File > Project Structures... to access the Project Structure page.
    2. On the Project Structure page, select Artifacts, click +, and choose JAR > Empty.
      Figure 3 Adding Artifacts
    3. Set the name, type, and output path of the JAR package based on site requirements.
      Figure 4 Setting basic information
    4. Select 'kafka-examples' compile output and then choose Put into Output Root. Click Apply.
      Figure 5 Put into Output Root
    5. Click OK.

  2. Generate a JAR file.

    1. Start the IntelliJ IDEA and choose Build > Build Artifacts....
      Figure 6 Build Artifacts
    2. On the displayed page, choose kafka-example:jar > Build to create a JAR file.
      Figure 7 Build
    3. If information similar to the following is displayed in the event log, the JAR file has been generated. You can obtain the JAR package from the path configured in 1.c.
      14:37 Compilation completed successfully in 25 s 991 ms

  3. Copy the JAR package generated after project compilation to the /opt/client /lib directory.

    Figure 8 File location

  4. Copy all files in the src/main/resources directory of the IntelliJ IDEA project to the src/main/resources directory at the same level as the lib folder, that is, /opt/client/src/main/resources.
  5. Go to the /opt/client directory and run the following command to run the sample project:

    java -cp /opt/client/lib/*:/opt/client/src/main/resources com.huawei.bigdata.kafka.example.Producer