Search in sources :

Example 1 with SpoutConfig

use of org.apache.storm.kafka.SpoutConfig in project storm by apache.

the class KafkaHdfsTopo method getTopology.

public static StormTopology getTopology(Map config) {
    final int spoutNum = getInt(config, SPOUT_NUM, DEFAULT_SPOUT_NUM);
    final int boltNum = getInt(config, BOLT_NUM, DEFAULT_BOLT_NUM);
    final int hdfsBatch = getInt(config, HDFS_BATCH, DEFAULT_HDFS_BATCH);
    // 1 -  Setup Kafka Spout   --------
    String zkConnString = getStr(config, ZOOKEEPER_URI);
    String topicName = getStr(config, KAFKA_TOPIC);
    BrokerHosts brokerHosts = new ZkHosts(zkConnString);
    SpoutConfig spoutConfig = new SpoutConfig(brokerHosts, topicName, "/" + topicName, UUID.randomUUID().toString());
    spoutConfig.scheme = new StringMultiSchemeWithTopic();
    spoutConfig.ignoreZkOffsets = true;
    KafkaSpout spout = new KafkaSpout(spoutConfig);
    // 2 -  Setup HFS Bolt   --------
    String Hdfs_url = getStr(config, HDFS_URI);
    RecordFormat format = new LineWriter("str");
    SyncPolicy syncPolicy = new CountSyncPolicy(hdfsBatch);
    FileRotationPolicy rotationPolicy = new FileSizeRotationPolicy(1.0f, FileSizeRotationPolicy.Units.GB);
    FileNameFormat fileNameFormat = new DefaultFileNameFormat().withPath(getStr(config, HDFS_PATH));
    // Instantiate the HdfsBolt
    HdfsBolt bolt = new HdfsBolt().withFsUrl(Hdfs_url).withFileNameFormat(fileNameFormat).withRecordFormat(format).withRotationPolicy(rotationPolicy).withSyncPolicy(syncPolicy);
    // 3 - Setup Topology  --------
    TopologyBuilder builder = new TopologyBuilder();
    builder.setSpout(SPOUT_ID, spout, spoutNum);
    builder.setBolt(BOLT_ID, bolt, boltNum).localOrShuffleGrouping(SPOUT_ID);
    return builder.createTopology();
}
Also used : TopologyBuilder(org.apache.storm.topology.TopologyBuilder) RecordFormat(org.apache.storm.hdfs.bolt.format.RecordFormat) SpoutConfig(org.apache.storm.kafka.SpoutConfig) ZkHosts(org.apache.storm.kafka.ZkHosts) CountSyncPolicy(org.apache.storm.hdfs.bolt.sync.CountSyncPolicy) CountSyncPolicy(org.apache.storm.hdfs.bolt.sync.CountSyncPolicy) SyncPolicy(org.apache.storm.hdfs.bolt.sync.SyncPolicy) DefaultFileNameFormat(org.apache.storm.hdfs.bolt.format.DefaultFileNameFormat) FileNameFormat(org.apache.storm.hdfs.bolt.format.FileNameFormat) StringMultiSchemeWithTopic(org.apache.storm.kafka.StringMultiSchemeWithTopic) FileRotationPolicy(org.apache.storm.hdfs.bolt.rotation.FileRotationPolicy) DefaultFileNameFormat(org.apache.storm.hdfs.bolt.format.DefaultFileNameFormat) BrokerHosts(org.apache.storm.kafka.BrokerHosts) HdfsBolt(org.apache.storm.hdfs.bolt.HdfsBolt) KafkaSpout(org.apache.storm.kafka.KafkaSpout) FileSizeRotationPolicy(org.apache.storm.hdfs.bolt.rotation.FileSizeRotationPolicy)

Example 2 with SpoutConfig

use of org.apache.storm.kafka.SpoutConfig in project storm by apache.

the class KafkaSpoutNullBoltTopo method getTopology.

public static StormTopology getTopology(Map config) {
    final int spoutNum = getInt(config, SPOUT_NUM, DEFAULT_SPOUT_NUM);
    final int boltNum = getInt(config, BOLT_NUM, DEFAULT_BOLT_NUM);
    // 1 -  Setup Kafka Spout   --------
    String zkConnString = getStr(config, ZOOKEEPER_URI);
    String topicName = getStr(config, KAFKA_TOPIC);
    BrokerHosts brokerHosts = new ZkHosts(zkConnString);
    SpoutConfig spoutConfig = new SpoutConfig(brokerHosts, topicName, "/" + topicName, UUID.randomUUID().toString());
    spoutConfig.scheme = new StringMultiSchemeWithTopic();
    spoutConfig.ignoreZkOffsets = true;
    KafkaSpout spout = new KafkaSpout(spoutConfig);
    // 2 -   DevNull Bolt   --------
    DevNullBolt bolt = new DevNullBolt();
    // 3 - Setup Topology  --------
    TopologyBuilder builder = new TopologyBuilder();
    builder.setSpout(SPOUT_ID, spout, spoutNum);
    builder.setBolt(BOLT_ID, bolt, boltNum).localOrShuffleGrouping(SPOUT_ID);
    return builder.createTopology();
}
Also used : TopologyBuilder(org.apache.storm.topology.TopologyBuilder) SpoutConfig(org.apache.storm.kafka.SpoutConfig) DevNullBolt(org.apache.storm.perf.bolt.DevNullBolt) ZkHosts(org.apache.storm.kafka.ZkHosts) StringMultiSchemeWithTopic(org.apache.storm.kafka.StringMultiSchemeWithTopic) KafkaSpout(org.apache.storm.kafka.KafkaSpout) BrokerHosts(org.apache.storm.kafka.BrokerHosts)

Example 3 with SpoutConfig

use of org.apache.storm.kafka.SpoutConfig in project pancm_project by xuwujing.

the class MykafkaSpout method main.

/**
 * The entry point of application.
 *
 * @param args the input arguments
 */
/*
	 * 通过zookeeper进行获取kafka的数据
	 */
public static void main(String[] args) {
    String topic = "pcm_test1";
    ZkHosts zkHosts = new ZkHosts("192.169.0.23:2181");
    SpoutConfig spoutConfig = new SpoutConfig(zkHosts, topic, "", "MyTrack");
    List<String> zkServers = new ArrayList<String>();
    zkServers.add("192.169.0.23");
    spoutConfig.zkServers = zkServers;
    spoutConfig.zkPort = 2181;
    spoutConfig.socketTimeoutMs = 60 * 1000;
    spoutConfig.scheme = new SchemeAsMultiScheme(new StringScheme());
    TopologyBuilder builder = new TopologyBuilder();
    // 设置1个Executeor(线程),默认一个
    builder.setSpout("spout", new KafkaSpout(spoutConfig), 1);
    // 设置storm 设置1个Executeor(线程) 没有设置Task,默认一个
    builder.setBolt("bolt1", new MyKafkaBolt(), 1).shuffleGrouping("spout");
    Config conf = new Config();
    conf.setDebug(false);
    if (args.length > 0) {
        System.out.println("远程模式");
        try {
            StormSubmitter.submitTopology(args[0], conf, builder.createTopology());
        } catch (AlreadyAliveException e) {
            e.printStackTrace();
        } catch (InvalidTopologyException e) {
            e.printStackTrace();
        } catch (org.apache.storm.generated.AuthorizationException e) {
            e.printStackTrace();
        }
    } else {
        System.out.println("本地模式");
        LocalCluster localCluster = new LocalCluster();
        localCluster.submitTopology("mytopology", conf, builder.createTopology());
    }
}
Also used : LocalCluster(org.apache.storm.LocalCluster) TopologyBuilder(org.apache.storm.topology.TopologyBuilder) SpoutConfig(org.apache.storm.kafka.SpoutConfig) Config(org.apache.storm.Config) SpoutConfig(org.apache.storm.kafka.SpoutConfig) InvalidTopologyException(org.apache.storm.generated.InvalidTopologyException) ZkHosts(org.apache.storm.kafka.ZkHosts) ArrayList(java.util.ArrayList) AlreadyAliveException(org.apache.storm.generated.AlreadyAliveException) SchemeAsMultiScheme(org.apache.storm.spout.SchemeAsMultiScheme) KafkaSpout(org.apache.storm.kafka.KafkaSpout) StringScheme(org.apache.storm.kafka.StringScheme)

Aggregations

KafkaSpout (org.apache.storm.kafka.KafkaSpout)3 SpoutConfig (org.apache.storm.kafka.SpoutConfig)3 ZkHosts (org.apache.storm.kafka.ZkHosts)3 TopologyBuilder (org.apache.storm.topology.TopologyBuilder)3 BrokerHosts (org.apache.storm.kafka.BrokerHosts)2 StringMultiSchemeWithTopic (org.apache.storm.kafka.StringMultiSchemeWithTopic)2 ArrayList (java.util.ArrayList)1 Config (org.apache.storm.Config)1 LocalCluster (org.apache.storm.LocalCluster)1 AlreadyAliveException (org.apache.storm.generated.AlreadyAliveException)1 InvalidTopologyException (org.apache.storm.generated.InvalidTopologyException)1 HdfsBolt (org.apache.storm.hdfs.bolt.HdfsBolt)1 DefaultFileNameFormat (org.apache.storm.hdfs.bolt.format.DefaultFileNameFormat)1 FileNameFormat (org.apache.storm.hdfs.bolt.format.FileNameFormat)1 RecordFormat (org.apache.storm.hdfs.bolt.format.RecordFormat)1 FileRotationPolicy (org.apache.storm.hdfs.bolt.rotation.FileRotationPolicy)1 FileSizeRotationPolicy (org.apache.storm.hdfs.bolt.rotation.FileSizeRotationPolicy)1 CountSyncPolicy (org.apache.storm.hdfs.bolt.sync.CountSyncPolicy)1 SyncPolicy (org.apache.storm.hdfs.bolt.sync.SyncPolicy)1 StringScheme (org.apache.storm.kafka.StringScheme)1