Pulsar Function examples

String realize additional functions (Pulsar 2.4.2 version) in a stand-alone environment

1 Start single Pulsar

     $ bin/pulsar-daemon start standalone

2 Create function

1) Prepare the environment

    Project references compile 'org.apache.pulsar: pulsar-functions-api: 2.4.2'

2) Create JAVA function (this function is used for the data source is the topic schema string, tiopic schema is output string)

     image.png

     Export jar package, put pulsar directory server, in the present example / data / jar / lower

3) use the command-line tool loading functions to Pulsar,                     

   bin/pulsar-admin functions create \

   --classname test.AppStrFunction \

   --jar /data/jar/pf.jar \

   --inputs persistent://public/default/tlstest \

   --output persistent://public/default/teststr \

   --tenant public \

   --namespace default \

   --name appStrFunction

   Parameter Description:

                     

parameter
Explanation
functions Notification pulsar broker, the operation function
create After you create a function, by default create a successful startup
classname Function class name, we need to add the package name
jar Running path specified jar package
inputs Source specified function data where support multiple topics as input
output If the function has output (in some cases, function no output), specify the function outputs topic, only one output
tenant Specifies the tenant name of the function to run
namespace Specify the namespace of the function to run
name Specify the name of the function to run
The following is a function of other related operations

Stop function

bin/pulsar-admin functions stop \

--tenant public \

--namespace default \

--name appStrFunction

Start function

bin/pulsar-admin functions start \

--tenant public \

--namespace default \

--name appStrFunction

Delete function

bin/pulsar-admin functions delete \

--tenant public \

--namespace default \

--name appStrFunction

Log function installation directory / logs in pulsar / functions under

3 test functions

   According to function successfully loaded front to start

1) Send a message to tlstest theme   

import java.util.concurrent.TimeUnit;
import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.Schema;
public class SendMsgTest{
  public static void main(String[] args){
      String url="pulsar://192.168.1.48:6650";
  try{
     PulsarClient client =PulsarClient.builder()
           .serviceUrl(url)
           .connectionTimeout(10,TimeUnit.SECONDS)
           .build();
     Producer<String> producer=client.newProducer(Schema.STRING)
           .topic("tlstest")
           .sendTimeout(10,TimeUnit.SECONDS)
           .producerName("senduser")
           .create();
           producer.send("this is a book");
           System.out.print("send ok");
           client.close();
      }catch(Exception e){
        e.printStackTrace();
      }
  }
}

2) reads the topic news teststr

   

import org.apache.pulsar.client.api.Consumer;
import org.apache.pulsar.client.api.Message;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.Schema;
import org.apache.pulsar.client.api.SubscriptionInitialPosition;
import org.apache.pulsar.client.api.SubscriptionType;
import org.apache.pulsar.client.impl.schema.JSONSchema;
import schema.OrderModel;
import com.alibaba.fastjson.JSON;
public class RecFunTest {
public static void main(String[] args) {
String url = "http://192.168.1.48:8080";
try{
  PulsarClient client =PulsarClient.builder()
    .serviceUrl(url)
    .build();
 Consumer<String> consumer=client.newConsumer(Schema.STRING)
    .topic("teststr")
    .subscriptionInitialPosition(SubscriptionInitialPosition.Earliest)
    .subscriptionType(SubscriptionType.Exclusive)//订阅模式  Exclusive(独占,默认模式) Failover(灾备)Shared(共享)
    .subscriptionName("wbq")//订阅者名称
    .subscribe();
 while (true) {
   Message<String> mondmsg = consumer.receive();
   String msg=mondmsg.getValue();
                System.out.println("receive message=:"+msg);
             }
  }catch(Exception e){
     e.printStackTrace();
  }
 }
}


Guess you like

Origin blog.51cto.com/14602923/2463298