Connect to Kafka.

@param zkHost ZK host
@param zkPath ZK port
@throws UnknownHostException exception
555853
@Given("^I connect to kafka at '(.+)' using path '(.+)'$")
    public void connectKafka(String zkHost, String zkPath) throws UnknownHostException {
        String zkPort = zkHost.split(":")[1];
        zkHost = zkHost.split(":")[0];
        commonspec.getKafkaUtils().setZkHost(zkHost, zkPort, zkPath);
        commonspec.getKafkaUtils().connect();
    }
940931
public KafkaQueue<ID, DATA> init() throws Exception {
        if (kafkaClient == null) {
            setKafkaClient(buildKafkaClient(), true);
        }

        super.init();

        if (kafkaClient == null) {
            throw new IllegalStateException("Kafka client is null.");
        }

        return this;
    }
1182000
def _setup_kafka(self):
        
        # close older connections
        if self.consumer is not None:
            self.logger.debug("Closing existing kafka consumer")
            self.consumer.close()
            self.consumer = None
        if self.producer is not None:
            self.logger.debug("Closing existing kafka producer")
            self.producer.flush()
            self.producer.close(timeout=10)
            self.producer = None

        # create new connections
        self._consumer_thread = None
        self.logger.debug("Creating kafka connections")
        self.consumer = self._create_consumer()
        if not self.closed:
            self.logger.debug("Kafka Conumer created")
        self.producer = self._create_producer()
        if not self.closed:
            self.logger.debug("Kafka Producer created")

        if not self.closed:
            self.kafka_connected = True
            self.logger.info("Connected successfully to Kafka")
            self._spawn_kafka_consumer_thread()
924457
private void connect(ZooKeeperClient injectedClient) throws InterruptedException {
        if (!initialized.get()) {
            if (injectedClient == null) {
                List<InetSocketAddress> hosts = new ArrayList<InetSocketAddress>();
                for (String host : config.hosts.split(",")) {
                    String[] parts = host.split(":");
                    try {
                        hosts.add(new InetSocketAddress(parts[0], Integer.parseInt(parts[1])));
                    } catch (Exception e) {
                        LOG.error("Invalid ZK host '"+host+"', need to skip, problem: "+e.getMessage());
                    }
                }
                // TODO: What about empty hosts at this point?
                LOG.info("Connecting to hosts: {}", hosts.toString());
                injectedClient = new ZooKeeperClient(Amount.of((int) config.zkTimeout, Time.MILLISECONDS),
                        hosts);
            }
            zk = injectedClient;
            claimer.start();
            LOG.info("Registering connection watcher.");
            zk.register(connectionWatcher);
        }
        // and then see that we can actually get the client without problems
        try {
            zk.get();
        } catch (ZooKeeperConnectionException e) {
            throw ZKException.from(e);
        }
    }
643767
private static void addFolderToZip(String path, File folder, ZipOutputStream zos) throws IOException {
        String currentPath = StringUtils.isNotEmpty(path)? path + "/" + folder.getName(): folder.getName();

        for (File file : folder.listFiles()) {
            if (file.isDirectory()) {
                addFolderToZip(currentPath, file, zos);
            } else {
                addFileToZip(currentPath, file, zos);
            }
        }
    }
668858
private static void addFileToZip(String path, File file, ZipOutputStream zos) throws IOException {
        String currentPath = StringUtils.isNotEmpty(path)? path + "/" + file.getName(): file.getName();

        zos.putNextEntry(new ZipEntry(currentPath));

        InputStream is = new BufferedInputStream(new FileInputStream(file));
        try {
            IOUtils.copy(is, zos);
        } finally {
            IOUtils.closeQuietly(is);
        }

        zos.closeEntry();
    }
1136149
def setup_zk
      @zk = ZK.new(@zkservers) if @zkservers
      @zk.register(redis_nodes_path) { |event| handle_zk_event(event) }
      if @safe_mode
        @zk.on_expired_session { purge_clients }
      end
      @zk.on_connected { @zk.stat(redis_nodes_path, :watch => true) }
      @zk.stat(redis_nodes_path, :watch => true)
      update_znode_timestamp
    end
813294
public final void mKAFKA() throws RecognitionException {
		try {
			int _type = KAFKA;
			int _channel = DEFAULT_TOKEN_CHANNEL;
			// druidG.g:597:17: ( ( 'KAFKA' | 'kafka' ) )
			// druidG.g:597:18: ( 'KAFKA' | 'kafka' )
			{
			// druidG.g:597:18: ( 'KAFKA' | 'kafka' )
			int alt13=2;
			int LA13_0 = input.LA(1);
			if ( (LA13_0=='K') ) {
				alt13=1;
			}
			else if ( (LA13_0=='k') ) {
				alt13=2;
			}

			else {
				NoViableAltException nvae =
					new NoViableAltException("", 13, 0, input);
				throw nvae;
			}

			switch (alt13) {
				case 1 :
					// druidG.g:597:19: 'KAFKA'
					{
					match("KAFKA"); 

					}
					break;
				case 2 :
					// druidG.g:597:27: 'kafka'
					{
					match("kafka"); 

					}
					break;

			}

			}

			state.type = _type;
			state.channel = _channel;
		}
		finally {
			// do for sure before leaving
		}
	}
613477
public void commit(KafkaTopicPartitionOffset... tpoList) {
        Map<String, Map<TopicPartition, OffsetAndMetadata>> topicOffsets = new HashMap<>();
        for (KafkaTopicPartitionOffset tpo : tpoList) {
            Map<TopicPartition, OffsetAndMetadata> offsets = topicOffsets.get(tpo.topic);
            if (offsets == null) {
                offsets = new HashMap<>();
                topicOffsets.put(tpo.topic, offsets);
            }
            TopicPartition tp = new TopicPartition(tpo.topic, tpo.partition);
            OffsetAndMetadata oam = new OffsetAndMetadata(tpo.offset);
            offsets.put(tp, oam);
        }
        for (Entry<String, Map<TopicPartition, OffsetAndMetadata>> entry : topicOffsets
                .entrySet()) {
            String topic = entry.getKey();
            KafkaConsumer<String, byte[]> consumer = _getConsumer(topic);
            synchronized (consumer) {
                Set<String> subscription = consumer.subscription();
                if (subscription == null || !subscription.contains(topic)) {
                    // this consumer has not subscribed to the topic
                    LOGGER.warn("Not subscribed to topic [" + topic + "] yet!");
                } else {
                    try {
                        consumer.commitSync(entry.getValue());
                    } catch (WakeupException e) {
                    } catch (Exception e) {
                        throw new KafkaException(e);
                    }
                }
            }
        }
    }
302953
public void setHost(String host)
        throws java.net.UnknownHostException
    {
        _addr=null;
        if (host!=null)
        {
            if (host.indexOf('/')>0)
                host=host.substring(0,host.indexOf('/'));
            _addrIsHost=!Character.isDigit((host.charAt(0)));
            _addr=InetAddress.getByName(host);
        }
    }
263623
public static void main(String[] args) throws Exception {

		// ---- print some usage help ----

		System.out.println("Usage with built-in data generator: StateMachineExample [--error-rate <probability-of-invalid-transition>] [--sleep <sleep-per-record-in-ms>]");
		System.out.println("Usage with Kafka: StateMachineExample --kafka-topic <topic> [--brokers <brokers>]");
		System.out.println("Options for both the above setups: ");
		System.out.println("\t[--backend <file|rocks>]");
		System.out.println("\t[--checkpoint-dir <filepath>]");
		System.out.println("\t[--async-checkpoints <true|false>]");
		System.out.println("\t[--incremental-checkpoints <true|false>]");
		System.out.println("\t[--output <filepath> OR null for stdout]");
		System.out.println();

		// ---- determine whether to use the built-in source, or read from Kafka ----

		final SourceFunction<Event> source;
		final ParameterTool params = ParameterTool.fromArgs(args);

		if (params.has("kafka-topic")) {
			// set up the Kafka reader
			String kafkaTopic = params.get("kafka-topic");
			String brokers = params.get("brokers", "localhost:9092");

			System.out.printf("Reading from kafka topic %s @ %s\n", kafkaTopic, brokers);
			System.out.println();

			Properties kafkaProps = new Properties();
			kafkaProps.setProperty("bootstrap.servers", brokers);

			FlinkKafkaConsumer010<Event> kafka = new FlinkKafkaConsumer010<>(kafkaTopic, new EventDeSerializer(), kafkaProps);
			kafka.setStartFromLatest();
			kafka.setCommitOffsetsOnCheckpoints(false);
			source = kafka;
		}
		else {
			double errorRate = params.getDouble("error-rate", 0.0);
			int sleep = params.getInt("sleep", 1);

			System.out.printf("Using standalone source with error rate %f and sleep delay %s millis\n", errorRate, sleep);
			System.out.println();

			source = new EventsGeneratorSource(errorRate, sleep);
		}

		// ---- main program ----

		// create the environment to create streams and configure execution
		final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
		env.enableCheckpointing(2000L);

		final String stateBackend = params.get("backend", "memory");
		if ("file".equals(stateBackend)) {
			final String checkpointDir = params.get("checkpoint-dir");
			boolean asyncCheckpoints = params.getBoolean("async-checkpoints", false);
			env.setStateBackend(new FsStateBackend(checkpointDir, asyncCheckpoints));
		} else if ("rocks".equals(stateBackend)) {
			final String checkpointDir = params.get("checkpoint-dir");
			boolean incrementalCheckpoints = params.getBoolean("incremental-checkpoints", false);
			env.setStateBackend(new RocksDBStateBackend(checkpointDir, incrementalCheckpoints));
		}

		final String outputFile = params.get("output");

		// make parameters available in the web interface
		env.getConfig().setGlobalJobParameters(params);

		DataStream<Event> events = env.addSource(source);

		DataStream<Alert> alerts = events
				// partition on the address to make sure equal addresses
				// end up in the same state machine flatMap function
				.keyBy(Event::sourceAddress)

				// the function that evaluates the state machine over the sequence of events
				.flatMap(new StateMachineMapper());

		// output the alerts to std-out
		if (outputFile == null) {
			alerts.print();
		} else {
			alerts
				.writeAsText(outputFile, FileSystem.WriteMode.OVERWRITE)
				.setParallelism(1);
		}

		// trigger program execution
		env.execute("State machine job");
	}
753821
private function bootZmqConnection()
    {
        $this->info(sprintf('Starting ZMQ listener on: %s:%s', config('ratchet.zmq.host'), config('ratchet.zmq.port')));

        $context = new ZMQContext($this->getEventLoop());
        $socket = $context->getSocket(config('ratchet.zmq.method', \ZMQ::SOCKET_PULL));
        $socket->bind(sprintf('tcp://%s:%d', config('ratchet.zmq.host', '127.0.0.1'), config('ratchet.zmq.port', 5555)));

        $socket->on('messages', function ($messages) {
            $this->ratchetServer->onEntry($messages);
        });

        $socket->on('message', function ($message) {
            $this->ratchetServer->onEntry($message);
        });
    }
247642
def reinit(self):
        
        log.debug("Reinitializing socket connection for %s:%d" % (self.host, self.port))

        if self._sock:
            self.close()

        try:
            self._sock = socket.create_connection((self.host, self.port), self.timeout)
        except socket.error:
            log.exception('Unable to connect to kafka broker at %s:%d' % (self.host, self.port))
            self._raise_connection_error()
454605
def zk_client(host, scheme, credential):
    

    if not re.match(r".*:\d+$", host):
        host = "%s:%d" % (host, DEFAULT_ZK_PORT)

    client = KazooClient(hosts=host)
    client.start()

    if scheme != "":
        client.add_auth(scheme, credential)

    return client
642307
protected function connect()
	{
		$this->context = new ZMQContext($this->ioThreadTotal, $this->persistent);
		$this->socket  = $this->context->getSocket($this->socketType, $this->persistenceKey);
	    $this->socket->connect($this->connection);
	}
1237855
protected boolean doConnectAndCreateZkPath() {
        if (RpcServerConf.ENABLE_ZK_REGISTRY) {
            String path = ZkPathUtil.buildPath(NaviCommonConstant.ZOOKEEPER_BASE_PATH,
                    RpcServerConf.ZK_REGISTRY_NAMESPACE);
            try {
                LOG.info("Connecting to zookeeper server - " + RpcServerConf.ZK_SERVER_LIST);
                zkClient = new SimpleZooKeeperClient(RpcServerConf.ZK_SERVER_LIST,
                        RpcServerConf.ZK_DIGEST_AUTH, new ServerServiceWatcher());
                LOG.info("Connect to zookeeper server successfully!");
                LOG.info("Trying create service registry path - " + path);
                zkClient.createNodeForRecursive(path, "".getBytes());
                LOG.info("Create service registry path successfully!");
            } catch (NodeExistsException e) {
                LOG.warn("Zookeeper path is already exists - " + path);
            } catch (Exception e) {
                LOG.error("Zookeeper client initialization failed, " + e.getMessage(), e);
                return false;
            }
        }
        return true;
    }
766390
@Override
    public void start() throws AtlasException {
        if (isHAEnabled()) {
            LOG.info("Not starting embedded instances when HA is enabled.");
            return;
        }
        if (isEmbedded()) {
            try {
                startZk();
                startKafka();
            } catch (Exception e) {
                throw new AtlasException("Failed to start embedded kafka", e);
            }
        }
    }