一、首先在win系统下C:\Windows\System32\drivers\etc文件夹中hosts文件加入例如以下内容:

10.61.6.167 slaves1

10.61.6.168 slaves2

10.61.6.169 slaves3

二、启动kafka集群类

package com.conn.server.start;





import java.io.IOException;





import ch.ethz.ssh2.Connection;

import ch.ethz.ssh2.Session;









public class StartKafka {  

 

    public static void main(String[] args) {

    StartKafka startzk=new StartKafka();

    //启动kafka自带zookeeper集群

    startzk.startZk1();

    startzk.startZk2();

    startzk.startZk3();

    //启动kafka

    startzk.startKafka1();

    startzk.startKafka2();

    startzk.startKafka3();

    }

          

    public static void startZk1(){

        String hostname = "10.61.6.166";  

        String username = "root";  

        String password = "pass@word2";  

        //指明连接主机的IP地址  

        Connection conn = new Connection (hostname);  

        Session ssh = null;  

        try {  

            //连接到主机  

            conn.connect();  

            //使用username和password校验  

            boolean isconn = conn.authenticateWithPassword(username, password);  

            if(!isconn){  

                System.out.println("username称或者是password不对");  

            }else{  

                System.out.println("已经连接OK");  

                ssh = conn.openSession();  

                //使用多个命令用分号隔开  



                ssh.execCommand("cd /home/lee/kafka/kafka;bin/zookeeper-server-start.sh config/zookeeper.properties&");  

              

                //仅仅同意使用一行命令,即ssh对象仅仅能使用一次execCommand这种方法,多次使用则会出现异常  

            }  

            //连接的Session和Connection对象都须要关闭  

            ssh.close();  

            conn.close();  

              

        } catch (IOException e) {  

            e.printStackTrace();  

        }  

          

    }  

  

    public static void startZk2(){

        String hostname = "10.61.6.168";  

        String username = "root";  

        String password = "pass@word2";  

        //指明连接主机的IP地址  

        Connection conn = new Connection (hostname);  

        Session ssh = null;  

        try {  

            //连接到主机  

            conn.connect();  

            //使用username和password校验  

            boolean isconn = conn.authenticateWithPassword(username, password);  

            if(!isconn){  

                System.out.println("username称或者是password不对");  

            }else{  

                System.out.println("已经连接OK");  

                ssh = conn.openSession();  

               ssh.execCommand("cd /home/lee/kafka/kafka;bin/zookeeper-server-start.sh config/zookeeper.properties&");  

              

            }  

            ssh.close();  

            conn.close();  

              

        } catch (IOException e) {  

            e.printStackTrace();  

        }  

    }  

    

    public static void startZk3(){

        String hostname = "10.61.6.169";  

        String username = "root";  

        String password = "pass@word2";  

        //指明连接主机的IP地址  

        Connection conn = new Connection (hostname);  

        Session ssh = null;  

        try {  

            //连接到主机  

            conn.connect();  

            //使用username和password校验  

            boolean isconn = conn.authenticateWithPassword(username, password);  

            if(!isconn){  

                System.out.println("username称或者是password不对");  

            }else{  

                System.out.println("已经连接OK");  

                ssh = conn.openSession();  

               ssh.execCommand("cd /home/lee/kafka/kafka;bin/zookeeper-server-start.sh config/zookeeper.properties&");  

               }  

            ssh.close();  

            conn.close();  

              

        } catch (IOException e) {  

            e.printStackTrace();  

        }  

    }  

    

    public static void startKafka1(){

        String hostname = "10.61.6.167";  

        String username = "root";  

        String password = "pass@word2";  

        //指明连接主机的IP地址  

        Connection conn = new Connection (hostname);  

        Session ssh = null;  

        try {  

            //连接到主机  

            conn.connect();  

            //使用username和password校验  

            boolean isconn = conn.authenticateWithPassword(username, password);  

            if(!isconn){  

                System.out.println("username称或者是password不对");  

            }else{  

                System.out.println("已经连接OK");  

                ssh = conn.openSession();  

                ssh.execCommand("cd /home/lee/kafka/kafka;bin/kafka-server-start.sh config/server.properties&");  

            }  

            ssh.close();  

            conn.close();  

              

        } catch (IOException e) {  

            e.printStackTrace();  

        }  

    }  

    

    public static void startKafka2(){

        String hostname = "10.61.6.168";  

        String username = "root";  

        String password = "pass@word2";  

        //指明连接主机的IP地址  

        Connection conn = new Connection (hostname);  

        Session ssh = null;  

        try {  

            //连接到主机  

            conn.connect();  

            //使用username和password校验  

            boolean isconn = conn.authenticateWithPassword(username, password);  

            if(!isconn){  

                System.out.println("username称或者是password不对");  

            }else{  

                System.out.println("已经连接OK");  

                ssh = conn.openSession();  

                ssh.execCommand("cd /home/lee/kafka/kafka;bin/kafka-server-start.sh config/server.properties&");  

            }  

            ssh.close();  

            conn.close();  

              

        } catch (IOException e) {  

            e.printStackTrace();  

        }  

    }  

    

    public static void startKafka3(){

        String hostname = "10.61.6.169";  

        String username = "root";  

        String password = "pass@word2";  

        //指明连接主机的IP地址  

        Connection conn = new Connection (hostname);  

        Session ssh = null;  

        try {  

            //连接到主机  

            conn.connect();  

            //使用username和password校验  

            boolean isconn = conn.authenticateWithPassword(username, password);  

            if(!isconn){  

                System.out.println("username称或者是password不对");  

            }else{  

                System.out.println("已经连接OK");  

                ssh = conn.openSession();  

                ssh.execCommand("cd /home/lee/kafka/kafka;bin/kafka-server-start.sh config/server.properties&");  

            }  

            ssh.close();  

            conn.close();  

              

        } catch (IOException e) {  

            e.printStackTrace();  

        }  

    }  

    

}

三、生产者类

package com.performanceTest;



import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.util.Properties;



import kafka.javaapi.producer.Producer;

import kafka.producer.KeyedMessage;

import kafka.producer.ProducerConfig;





public class ProducerSample {

public static void main(String[] args) throws FileNotFoundException {  

        //ProducerSample ps = new ProducerSample();  

  

        Properties props = new Properties();  

        props.put("zookeeper.connect", "slaves1:2182,slaves2:2182,slaves3:2182");    //这里也能够改为集群各节点的ip地址

        props.put("serializer.class", "kafka.serializer.StringEncoder");  

        props.put("metadata.broker.list","slaves1:9092,slaves2:9092,slaves3:9092");

        props.put("request.required.acks", "1");

        props.put("batch.num.messages","200");

        ProducerConfig config = new ProducerConfig(props);  

        Producer<String, String> producer = new Producer<String, String>(config);

File file=new File("E:/test","110-140_1.txt");

        BufferedReader readtxt=new BufferedReader(new FileReader(file));

        String line=null;

        byte[] item=null;

try {

while((line=readtxt.readLine())!=null){

line = line.replaceAll("\\t", ",");

item=line.getBytes();

String str = new String(item);

producer.send(new KeyedMessage<String, String>("mykafka",str));

}

} catch (IOException e) {

e.printStackTrace();

}

     

    }  

}

四、消费者类

package com.performanceTest;





import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Properties;





import kafka.consumer.ConsumerConfig;

import kafka.consumer.ConsumerIterator;

import kafka.consumer.KafkaStream;

import kafka.javaapi.consumer.ConsumerConnector;





public class ConsumerTest extends Thread {

private final ConsumerConnector consumer;  

    private final String topic;  

    private  final List<String> messages = new ArrayList<String>();

      

    public static void main(String[] args) {  

        ConsumerTest consumerThread = new ConsumerTest("mykafka");  

        consumerThread.start();  

    }  

      

    public ConsumerTest(String topic) {  

    System.out.println(topic);

        consumer = kafka.consumer.Consumer  

                .createJavaConsumerConnector(createConsumerConfig());  

        this.topic = topic;  

    }  

      

    private static ConsumerConfig createConsumerConfig() {  

        Properties props = new Properties();  

       props.put("zookeeper.connect", "slaves1:2182,slaves2:2182,slaves3:2182"); 

        props.put("group.id", "0");  

        props.put("zookeeper.session.timeout.ms", "400000");  

        props.put("zookeeper.sync.time.ms", "200");  

        props.put("auto.commit.interval.ms", "1000");  

      

        return new ConsumerConfig(props);  

      

    }

win系统下启动linux上的kafka集群及使用的更多相关文章

  1. Linux上安装Hadoop集群(CentOS7+hadoop-2.8.0)--------hadoop环境的搭建

    Linux上安装Hadoop集群(CentOS7+hadoop-2.8.0)------https://blog.csdn.net/pucao_cug/article/details/71698903 ...

  2. win系统下的eclipse连接和使用linux上的hadoop集群

    准备工作 先在win系统的hosts文件中加入下面内容 10.61.6.164master     //hadoop集群的master节点 一.首先在eclipse上安装hadoop插件 下载hado ...

  3. 在linux上搭建nacos集群(步骤详细,linux小白也搞得定)

    (1)nacos官网:https://github.com/alibaba/nacos/releases/tag/1.2.1下载nacos安装包到window本地(后缀为tar.zip) (2)在li ...

  4. Linux上搭建Hadoop集群

    本文将为初学者的搭建简单的伪分布式集群,将搭建一台虚拟机,用于学习Hadoop 工具:vm虚拟机,centOS7,jdk-8,Hadoop2.7,xftp,xshell 用户:在虚拟机中创建一个had ...

  5. 在Linux上安装Zookeeper集群

    xl_echo编辑整理,欢迎转载,转载请声明文章来源.欢迎添加echo微信(微信号:t2421499075)交流学习. 百战不败,依不自称常胜,百败不颓,依能奋力前行.——这才是真正的堪称强大!! - ...

  6. linux上配置spark集群

    环境: linux spark1.6.0 hadoop2.2.0 一.安装scala(每台机器)   1.下载scala-2.11.0.tgz   放在目录: /opt下,tar -zxvf scal ...

  7. Linux上安装Hadoop集群(CentOS7+hadoop-2.8.0)

    1下载hadoop 2安装3个虚拟机并实现ssh免密码登录 2.1安装3个机器 2.2检查机器名称 2.3修改/etc/hosts文件 2.4 给3个机器生成秘钥文件 2.5 在hserver1上创建 ...

  8. Linux上安装Hadoop集群(CentOS7+hadoop-2.8.3)

    https://blog.csdn.net/pucao_cug/article/details/71698903 1下载hadoop 2安装3个虚拟机并实现ssh免密码登录 2.1安装3个机器 2.2 ...

  9. Kafka集群安装部署、Kafka生产者、Kafka消费者

    Storm上游数据源之Kakfa 目标: 理解Storm消费的数据来源.理解JMS规范.理解Kafka核心组件.掌握Kakfa生产者API.掌握Kafka消费者API.对流式计算的生态环境有深入的了解 ...

随机推荐

  1. mysql查询表中最小可用id值

    今天在看实验室的项目时,碰到的一个问题,.先把sql语句扔出来 // 这条语句在id没有1时,不能得到正确的查询结果. select min(id+1) from oslist c where not ...

  2. Python机器学习2.2

    使用Python实现感知器学习算法 在<Python机器学习>中的2.2节中,创建了罗森布拉特感知器的类,通过fit方法初始化权重self.w_,再fit方法循环迭代样本,更新权重,使用p ...

  3. HDU 1166 排兵布阵(线段树单点更新)

    题意: 给定n个兵营的士兵初始值, 然后有最多40000个操作: 操作一共有两种, 一个是查询给定[a,b]区间兵营的士兵总和. 另一个是增加/减少指定兵营的士兵数目. 输出每次查询的值. 分析: 线 ...

  4. Ubuntu 配置Apache虚拟目录

    http://blog.csdn.net/spring21st/article/details/6589300 Ubuntu 配置Apache虚拟目录 http://blog.csdn.net/spr ...

  5. CentOS7 设置代理

    大多数公司的网络都使用局域网加代理上网,也就是说上外网必须使用公司指定的代理服务器,这有几个好处: 1. 首先代理可以一定程度提高浏览速度,因为可以将更多的网页缓存在代理服务器上,需要的时候直接拿就很 ...

  6. FZU2206函数求解

    Problem 2206 函数求解 Accept: 154    Submit: 456 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Probl ...

  7. dispatching(bzoj 2008)

    Description 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 Master以外,每名忍者都有且仅有一个上级. ...

  8. HDU 3763 CDs

    http://acm.hdu.edu.cn/showproblem.php?pid=3763 题意: 两组数据 看重复的有多少 如果每输入一个就去查找的话O(n^2) 会超时 所以可以用二法 第一组数 ...

  9. CPU问题定位与解决

    CPU问题定位基本流程:   性能计数器诊断 主要用到的性能计数器 %Process Time 全实例 (主要用于查看当前服务器的CPU 情况) %Process Time sqlservr (主要用 ...

  10. Flume+kakfa+sparkStream实时处理数据测试

    flume:从数据源拉取数据 kafka:主要起到缓冲从flume拉取多了的数据 sparkStream:对数据进行处理   一.flume拉取数据   1.源数据文件读取配置   在flume目录的 ...