精华内容
下载资源
问答
  • Java编程实例

    2012-07-24 13:54:31
    Java 编程实例
  • java编程实例

    2012-09-24 22:14:58
    java编程实例 Java实例入门(一) java实例入门(二)-----基本运算符与表达式 java实例入门(三)--获取命令行参数java实例入门(四)-----循环语句
  • java 编程实例

    2013-07-25 11:29:51
    各种很不错 的java实例 ,适合开发者参考。
  • JAVA编程实例

    2011-12-12 10:51:20
    很适合java新手练习,都是实例,都是源代码,新手想找习题的不要错过哦,主要分为以下8部分内容: 1.图形用户界面 2.applet编辑 3.多媒体处理 4.java输入输出系统 5.java网络编程 6.java数据库编程 7.java安全机制 8...
  • java编程实例100个

    2010-10-16 21:07:52
    java编程实例100个java编程实例100个java编程实例100个java编程实例100个java编程实例100个
  • JAVA编程实例源代码

    2010-11-20 17:02:59
    JAVA编程实例源代码;JAVA编程实例源代码;JAVA编程实例源代码
  • 80个JAVA编程实例

    2008-12-29 11:31:09
    80 JAVA 编程 实例 80 JAVA 编程 实例 80 JAVA 编程 实例 80 JAVA 编程 实例
  • java编程实例2

    2008-06-14 15:01:44
    java编程实例2java编程实例2
  • java编程实例150

    2011-08-18 14:47:52
    java编程实例150,锻炼java的好方法
  • java编程实例、java编程应用实例、Java实例、最新JAVA编程题全集
  • java编程实例源码

    2011-08-24 20:11:30
    java编程实例源码图书管理系统帮助大家更好的学习,相互交流可以使大家进步的更快
  • KafKa Java编程实例

    千次阅读 2018-05-28 18:04:42
    KafKa Java编程实例编写一个能发送消息,接收消息的实例(1)编写系数配置:KafkaProperties.javapackage com.imooc.spark.kafka;/*** Kafka常用配置文件*/public class KafkaProperties { public static final ...
    KafKa Java编程实例

    编写一个能发送消息,接收消息的实例
    (1)编写系数配置:KafkaProperties.java

    package com.imooc.spark.kafka;

    /**
    * Kafka常用配置文件
    */
    public class KafkaProperties {

        public static final String ZK = "140.143.236.169:2181";   //此处要和kafka配置文件中的 advertised.host.name保持一致,要么都是IP,要么都是HostName

        public static final String TOPIC = "hello_topic";

        public static final String BROKER_LIST = "140.143.236.169:9092";

        public static final String GROUP_ID = "test_group1";

    }
    (2)编写生产者:KafkaProducer.java

    package com.imooc.spark.kafka;

    import kafka.javaapi.producer.Producer;
    import kafka.producer.KeyedMessage;
    import kafka.producer.ProducerConfig;

    import java.util.Properties;

    /**
    * Kafka生产者
    */
    public class KafkaProducer extends Thread{

        private String topic;

        private Producer<Integer, String> producer;

        public KafkaProducer(String topic) {
            this.topic = topic;

            Properties properties = new Properties();

            properties.put("metadata.broker.list",KafkaProperties.BROKER_LIST);
            properties.put("serializer.class","kafka.serializer.StringEncoder");
            properties.put("request.required.acks","1");

            producer = new Producer<Integer, String>(new ProducerConfig(properties));
        }


        @Override
        public void run() {

            int messageNo = 1;

            while(true) {
                String message = "message_" + messageNo;
                producer.send(new KeyedMessage<Integer, String>(topic, message));
                System.out.println("Sent: " + message);

                messageNo ++ ;

                try{
                    Thread.sleep(2000);
                } catch (Exception e){
                    e.printStackTrace();
                }
            }

        }
    }

    (3)编写消费者:KafkaConsumer.java
    package com.imooc.spark.kafka;

    import kafka.consumer.Consumer;
    import kafka.consumer.ConsumerConfig;
    import kafka.consumer.ConsumerIterator;
    import kafka.consumer.KafkaStream;
    import kafka.javaapi.consumer.ConsumerConnector;

    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Properties;

    /**
    * Kafka消费者
    */
    public class KafkaConsumer extends Thread{

        private String topic;

        public KafkaConsumer(String topic) {
            this.topic = topic;
        }


        private ConsumerConnector createConnector(){
            Properties properties = new Properties();
            properties.put("zookeeper.connect", KafkaProperties.ZK);
            properties.put("group.id",KafkaProperties.GROUP_ID);
            return Consumer.createJavaConsumerConnector(new ConsumerConfig(properties));
        }

        @Override
        public void run() {
            ConsumerConnector consumer = createConnector();

            Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
            topicCountMap.put(topic, 1);
    //        topicCountMap.put(topic2, 1);
    //        topicCountMap.put(topic3, 1);

            // String: topic
            // List<KafkaStream<byte[], byte[]>>  对应的数据流
            Map<String, List<KafkaStream<byte[], byte[]>>> messageStream =  consumer.createMessageStreams(topicCountMap);

            KafkaStream<byte[], byte[]> stream = messageStream.get(topic).get(0);   //获取我们每次接收到的暑假

            ConsumerIterator<byte[], byte[]> iterator = stream.iterator();


            while (iterator.hasNext()) {
                String message = new String(iterator.next().message());
                System.out.println("rec: " + message);
            }
        }
    }

    (4)编写启动实例:KafkaClientApp.java
    package com.imooc.spark.kafka;

    /**
    * Kafka Java API测试
    */
    public class KafkaClientApp {

        public static void main(String[] args) {
            new KafkaProducer(KafkaProperties.TOPIC).start();

            new KafkaConsumer(KafkaProperties.TOPIC).start();

        }
    }

    pom.xml
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.imooc.spark</groupId>
        <artifactId>sparktrain</artifactId>
        <version>1.0</version>
        <inceptionYear>2008</inceptionYear>
        <properties>
            <scala.version>2.11.8</scala.version>
            <kafka.version>0.9.0.0</kafka.version>
            <spark.version>2.2.0</spark.version>
            <hadoop.version>2.6.0-cdh5.7.0</hadoop.version>
            <hbase.version>1.2.0-cdh5.7.0</hbase.version>
        </properties>

        <!--添加cloudera的repository-->
        <repositories>
            <repository>
                <id>cloudera</id>
            </repository>
        </repositories>

        <dependencies>
            <dependency>
                <groupId>org.scala-lang</groupId>
                <artifactId>scala-library</artifactId>
                <version>${scala.version}</version>
            </dependency>

            <!-- Kafka 依赖-->
            <!--
            <dependency>
                <groupId>org.apache.kafka</groupId>
                <artifactId>kafka_2.11</artifactId>
                <version>${kafka.version}</version>
            </dependency>
            -->

            <!-- Hadoop 依赖-->
            <dependency>
                <groupId>org.apache.hadoop</groupId>
                <artifactId>hadoop-client</artifactId>
                <version>${hadoop.version}</version>
            </dependency>

            <!-- HBase 依赖-->
            <dependency>
                <groupId>org.apache.hbase</groupId>
                <artifactId>hbase-client</artifactId>
                <version>${hbase.version}</version>
            </dependency>

            <dependency>
                <groupId>org.apache.hbase</groupId>
                <artifactId>hbase-server</artifactId>
                <version>${hbase.version}</version>
            </dependency>

            <!-- Spark Streaming 依赖-->
            <dependency>
                <groupId>org.apache.spark</groupId>
                <artifactId>spark-streaming_2.11</artifactId>
                <version>${spark.version}</version>
            </dependency>


            <!-- Spark Streaming整合Flume 依赖-->
            <dependency>
                <groupId>org.apache.spark</groupId>
                <artifactId>spark-streaming-flume_2.11</artifactId>
                <version>${spark.version}</version>
            </dependency>

            <dependency>
                <groupId>org.apache.spark</groupId>
                <artifactId>spark-streaming-flume-sink_2.11</artifactId>
                <version>${spark.version}</version>
            </dependency>

            <dependency>
                <groupId>org.apache.spark</groupId>
                <artifactId>spark-streaming-kafka-0-8_2.11</artifactId>
                <version>${spark.version}</version>
            </dependency>

            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>3.5</version>
            </dependency>

            <!-- Spark SQL 依赖-->
            <dependency>
                <groupId>org.apache.spark</groupId>
                <artifactId>spark-sql_2.11</artifactId>
                <version>${spark.version}</version>
            </dependency>


            <dependency>
                <groupId>com.fasterxml.jackson.module</groupId>
                <artifactId>jackson-module-scala_2.11</artifactId>
                <version>2.6.5</version>
            </dependency>

            <dependency>
                <groupId>net.jpountz.lz4</groupId>
                <artifactId>lz4</artifactId>
                <version>1.3.0</version>
            </dependency>

            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.38</version>
            </dependency>

            <dependency>
                <groupId>org.apache.flume.flume-ng-clients</groupId>
                <artifactId>flume-ng-log4jappender</artifactId>
                <version>1.6.0</version>
            </dependency>

        </dependencies>

        <build>
            <sourceDirectory>src/main/scala</sourceDirectory>
            <testSourceDirectory>src/test/scala</testSourceDirectory>
            <plugins>
                <plugin>
                    <groupId>org.scala-tools</groupId>
                    <artifactId>maven-scala-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>compile</goal>
                                <goal>testCompile</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <scalaVersion>${scala.version}</scalaVersion>
                        <args>
                            <arg>-target:jvm-1.5</arg>
                        </args>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-eclipse-plugin</artifactId>
                    <configuration>
                        <downloadSources>true</downloadSources>
                        <buildcommands>
                            <buildcommand>ch.epfl.lamp.sdt.core.scalabuilder</buildcommand>
                        </buildcommands>
                        <additionalProjectnatures>
                            <projectnature>ch.epfl.lamp.sdt.core.scalanature</projectnature>
                        </additionalProjectnatures>
                        <classpathContainers>
                            <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
                            <classpathContainer>ch.epfl.lamp.sdt.launching.SCALA_CONTAINER</classpathContainer>
                        </classpathContainers>
                    </configuration>
                </plugin>
            </plugins>
        </build>
        <reporting>
            <plugins>
                <plugin>
                    <groupId>org.scala-tools</groupId>
                    <artifactId>maven-scala-plugin</artifactId>
                    <configuration>
                        <scalaVersion>${scala.version}</scalaVersion>
                    </configuration>
                </plugin>
            </plugins>
        </reporting>
    </project>



    展开全文
  • 这是一份关于TCP协议的Java编程实例的代码,有服务器端和客户端程序的代码,可以参考一下。
  • JAVA编程实例大全及详解答案(50例)

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 14,946
精华内容 5,978
关键字:

java编程实例

java 订阅