├── .gitignore ├── Kafka-Streams-Country-TopN ├── pom.xml └── src │ └── main │ └── java │ └── nl │ └── amis │ └── streams │ ├── JsonPOJODeserializer.java │ ├── JsonPOJOSerializer.java │ └── countries │ └── App.java ├── Kafka_Streams_TopN.pptx ├── README.md ├── kafka-node-TopN-Report ├── KafkaCountryStreamsConsumer.js └── package.json ├── kafka-node-countries ├── KafkaCountryProducer.js ├── countries2.csv └── package.json ├── kafka-node-express-topN-sse ├── package.json ├── public │ └── index.html ├── sse.js └── topNreport.js └── kafka-node-mongodb-topN ├── package.json ├── topNKafkaConsumerMongoDBWriter.js └── topNmongodbWriter.js /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | pom.xml.tag 3 | pom.xml.releaseBackup 4 | pom.xml.versionsBackup 5 | pom.xml.next 6 | release.properties 7 | dependency-reduced-pom.xml 8 | buildNumber.properties 9 | .mvn/timing.properties 10 | # do not include dependent node modules in Git Repo 11 | node_modules/ -------------------------------------------------------------------------------- /Kafka-Streams-Country-TopN/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | nl.amis.streams.countries 5 | Kafka-Streams-Country-TopN 6 | jar 7 | 1.0-SNAPSHOT 8 | Kafka-Streams-Country-TopN 9 | http://maven.apache.org 10 | 11 | 12 | org.apache.kafka 13 | kafka-streams 14 | 0.10.0.0 15 | 16 | 17 | 18 | org.rocksdb 19 | rocksdbjni 20 | 4.9.0 21 | 22 | 23 | junit 24 | junit 25 | 3.8.1 26 | test 27 | 28 | 29 | 30 | 31 | 32 | org.apache.maven.plugins 33 | maven-compiler-plugin 34 | 3.1 35 | 36 | 1.8 37 | 1.8 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /Kafka-Streams-Country-TopN/src/main/java/nl/amis/streams/JsonPOJODeserializer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | *

9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | *

11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | 17 | Downloaded from: https://www.codatlas.com/github.com/apache/kafka/trunk/streams/examples/src/main/java/org/apache/kafka/streams/examples/pageview/JsonPOJODeserializer.java 18 | 19 | **/ 20 | package nl.amis.streams; 21 | 22 | import com.fasterxml.jackson.databind.ObjectMapper; 23 | import org.apache.kafka.common.errors.SerializationException; 24 | import org.apache.kafka.common.serialization.Deserializer; 25 | 26 | import java.util.Map; 27 | 28 | public class JsonPOJODeserializer implements Deserializer { 29 | private ObjectMapper objectMapper = new ObjectMapper(); 30 | 31 | private Class tClass; 32 | 33 | /** 34 | * Default constructor needed by Kafka 35 | */ 36 | public JsonPOJODeserializer() { 37 | } 38 | 39 | @SuppressWarnings("unchecked") 40 | @Override 41 | public void configure(Map props, boolean isKey) { 42 | tClass = (Class) props.get("JsonPOJOClass"); 43 | } 44 | 45 | @Override 46 | public T deserialize(String topic, byte[] bytes) { 47 | if (bytes == null) 48 | return null; 49 | 50 | T data; 51 | try { 52 | data = objectMapper.readValue(bytes, tClass); 53 | } catch (Exception e) { 54 | throw new SerializationException(e); 55 | } 56 | 57 | return data; 58 | } 59 | 60 | @Override 61 | public void close() { 62 | 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /Kafka-Streams-Country-TopN/src/main/java/nl/amis/streams/JsonPOJOSerializer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | *

9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | *

11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | **/ 17 | package nl.amis.streams; 18 | 19 | import com.fasterxml.jackson.databind.ObjectMapper; 20 | import org.apache.kafka.common.errors.SerializationException; 21 | import org.apache.kafka.common.serialization.Serializer; 22 | 23 | import java.util.Map; 24 | 25 | public class JsonPOJOSerializer implements Serializer { 26 | private final ObjectMapper objectMapper = new ObjectMapper(); 27 | 28 | private Class tClass; 29 | 30 | /** 31 | * Default constructor needed by Kafka 32 | */ 33 | public JsonPOJOSerializer() { 34 | 35 | } 36 | 37 | @SuppressWarnings("unchecked") 38 | @Override 39 | public void configure(Map props, boolean isKey) { 40 | tClass = (Class) props.get("JsonPOJOClass"); 41 | } 42 | 43 | @Override 44 | public byte[] serialize(String topic, T data) { 45 | if (data == null) 46 | return null; 47 | 48 | try { 49 | return objectMapper.writeValueAsBytes(data); 50 | } catch (Exception e) { 51 | throw new SerializationException("Error serializing JSON message", e); 52 | } 53 | } 54 | 55 | @Override 56 | public void close() { 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /Kafka-Streams-Country-TopN/src/main/java/nl/amis/streams/countries/App.java: -------------------------------------------------------------------------------- 1 | package nl.amis.streams.countries; 2 | 3 | import nl.amis.streams.JsonPOJOSerializer; 4 | import nl.amis.streams.JsonPOJODeserializer; 5 | 6 | // generic Java imports 7 | import java.util.Properties; 8 | import java.util.HashMap; 9 | import java.util.Map; 10 | import java.util.Arrays; 11 | // Kafka imports 12 | import org.apache.kafka.common.serialization.Serde; 13 | import org.apache.kafka.common.serialization.Serdes; 14 | import org.apache.kafka.common.serialization.Serializer; 15 | import org.apache.kafka.common.serialization.Deserializer; 16 | // Kafka Streams related imports 17 | import org.apache.kafka.streams.StreamsConfig; 18 | import org.apache.kafka.streams.KafkaStreams; 19 | import org.apache.kafka.streams.kstream.KStream; 20 | import org.apache.kafka.streams.kstream.KTable; 21 | import org.apache.kafka.streams.kstream.KStreamBuilder; 22 | import org.apache.kafka.streams.processor.WallclockTimestampExtractor; 23 | 24 | import org.apache.kafka.streams.kstream.Window; 25 | import org.apache.kafka.streams.kstream.Windowed; 26 | import org.apache.kafka.streams.kstream.Windows; 27 | import org.apache.kafka.streams.kstream.TimeWindows; 28 | 29 | 30 | public class App { 31 | static public class CountryMessage { 32 | /* the JSON messages produced to the countries Topic have this structure: 33 | { "name" : "The Netherlands" 34 | , "code" : "NL 35 | , "continent" : "Europe" 36 | , "population" : 17281811 37 | , "size" : 42001 38 | }; 39 | 40 | this class needs to have at least the corresponding fields to deserialize the JSON messages into 41 | */ 42 | 43 | public String code; 44 | public String name; 45 | public int population; 46 | public int size; 47 | public String continent; 48 | } 49 | 50 | static public class CountryTop3 { 51 | 52 | public CountryMessage[] nrs = new CountryMessage[4] ; 53 | public CountryTop3() {} 54 | } 55 | 56 | 57 | 58 | private static final String APP_ID = "countries-top3-kafka-streaming-analysis-app"; 59 | 60 | public static void main(String[] args) { 61 | System.out.println("Kafka Streams Top 3 Demonstration"); 62 | 63 | // Create an instance of StreamsConfig from the Properties instance 64 | StreamsConfig config = new StreamsConfig(getProperties()); 65 | final Serde < String > stringSerde = Serdes.String(); 66 | final Serde < Long > longSerde = Serdes.Long(); 67 | 68 | // define countryMessageSerde 69 | Map < String, Object > serdeProps = new HashMap < String, Object > (); 70 | final Serializer < CountryMessage > countryMessageSerializer = new JsonPOJOSerializer < > (); 71 | serdeProps.put("JsonPOJOClass", CountryMessage.class); 72 | countryMessageSerializer.configure(serdeProps, false); 73 | 74 | final Deserializer < CountryMessage > countryMessageDeserializer = new JsonPOJODeserializer < > (); 75 | serdeProps.put("JsonPOJOClass", CountryMessage.class); 76 | countryMessageDeserializer.configure(serdeProps, false); 77 | final Serde < CountryMessage > countryMessageSerde = Serdes.serdeFrom(countryMessageSerializer, countryMessageDeserializer); 78 | 79 | // define countryTop3Serde 80 | serdeProps = new HashMap(); 81 | final Serializer countryTop3Serializer = new JsonPOJOSerializer<>(); 82 | serdeProps.put("JsonPOJOClass", CountryTop3.class); 83 | countryTop3Serializer.configure(serdeProps, false); 84 | 85 | final Deserializer countryTop3Deserializer = new JsonPOJODeserializer<>(); 86 | serdeProps.put("JsonPOJOClass", CountryTop3.class); 87 | countryTop3Deserializer.configure(serdeProps, false); 88 | final Serde countryTop3Serde = Serdes.serdeFrom(countryTop3Serializer, countryTop3Deserializer ); 89 | 90 | // building Kafka Streams Model 91 | KStreamBuilder kStreamBuilder = new KStreamBuilder(); 92 | // the source of the streaming analysis is the topic with country messages 93 | KStream countriesStream = 94 | kStreamBuilder.stream(stringSerde, countryMessageSerde, "countries"); 95 | 96 | 97 | 98 | // A hopping time window with a size of 5 minutes and an advance interval of 1 minute. 99 | // The window's name -- the string parameter -- is used to e.g. name the backing state store. 100 | long windowSizeMs = 5 * 60 * 1000L; 101 | long advanceMs = 1 * 60 * 1000L; 102 | TimeWindows.of("hopping-window-example", windowSizeMs).advanceBy(advanceMs); 103 | 104 | 105 | // THIS IS THE CORE OF THE STREAMING ANALYTICS: 106 | // top 3 largest countries per continent, published to topic Top3CountrySizePerContinent 107 | KTable top3PerContinent = countriesStream 108 | // the dimension for aggregation is continent; assign the continent as the key for each message 109 | .selectKey((k, country) -> country.continent) 110 | // for each key value (every continent in the stream) perform an aggregation 111 | .aggregateByKey( 112 | // first initialize a new CountryTop3 object, initially empty 113 | CountryTop3::new 114 | , // for each country in the continent, invoke the aggregator, passing in the continent, the country element and the CountryTop3 object for the continent 115 | (continent, countryMsg, top3) -> { 116 | // add the new country as the last element in the nrs array 117 | top3.nrs[3]=countryMsg; 118 | // sort the array by country size, largest first 119 | Arrays.sort( 120 | top3.nrs, (a, b) -> { 121 | // in the initial cycles, not all nrs element contain a CountryMessage object 122 | if (a==null) return 1; 123 | if (b==null) return -1; 124 | // with two proper CountryMessage objects, do the normal comparison 125 | return Integer.compare(b.size, a.size); 126 | } 127 | ); 128 | // lose nr 4, only top 3 is relevant 129 | top3.nrs[3]=null; 130 | return (top3); 131 | } 132 | , stringSerde, countryTop3Serde 133 | , "Top3LargestCountriesPerContinent" 134 | ); 135 | // publish the Top3 messages to Kafka Topic Top3CountrySizePerContinent 136 | top3PerContinent.to(stringSerde, countryTop3Serde, "Top3CountrySizePerContinent"); 137 | 138 | // prepare Top3 messages to be printed to the console 139 | top3PerContinent.mapValues((top3) -> { 140 | String rank = " 1. "+top3.nrs[0].name+" - "+top3.nrs[0].size 141 | + ((top3.nrs[1]!=null)? ", 2. "+top3.nrs[1].name+" - "+top3.nrs[1].size:"") 142 | + ((top3.nrs[2]!=null) ? ", 3. "+top3.nrs[2].name+" - "+top3.nrs[2].size:"") 143 | ; 144 | return "List for "+ top3.nrs[0].continent +rank; 145 | } 146 | ) 147 | .print(stringSerde,stringSerde); 148 | 149 | System.out.println("Starting Kafka Streams Countries Example"); 150 | KafkaStreams kafkaStreams = new KafkaStreams(kStreamBuilder, config); 151 | kafkaStreams.start(); 152 | System.out.println("Now started CountriesStreams Example"); 153 | } 154 | 155 | private static Properties getProperties() { 156 | Properties settings = new Properties(); 157 | // Set a few key parameters 158 | settings.put(StreamsConfig.APPLICATION_ID_CONFIG, APP_ID); 159 | // Kafka bootstrap server (broker to talk to); ubuntu is the host name for my VM running Kafka, port 9092 is where the (single) broker listens 160 | settings.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "ubuntu:9092"); 161 | // Apache ZooKeeper instance keeping watch over the Kafka cluster; ubuntu is the host name for my VM running Kafka, port 2181 is where the ZooKeeper listens 162 | settings.put(StreamsConfig.ZOOKEEPER_CONNECT_CONFIG, "ubuntu:2181"); 163 | // default serdes for serialzing and deserializing key and value from and to streams in case no specific Serde is specified 164 | settings.put(StreamsConfig.KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName()); 165 | settings.put(StreamsConfig.VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName()); 166 | settings.put(StreamsConfig.STATE_DIR_CONFIG, "C:\\temp"); 167 | // to work around exception Exception in thread "StreamThread-1" java.lang.IllegalArgumentException: Invalid timestamp -1 168 | // at org.apache.kafka.clients.producer.ProducerRecord.(ProducerRecord.java:60) 169 | // see: https://groups.google.com/forum/#!topic/confluent-platform/5oT0GRztPBo 170 | settings.put(StreamsConfig.TIMESTAMP_EXTRACTOR_CLASS_CONFIG, WallclockTimestampExtractor.class); 171 | return settings; 172 | } 173 | 174 | } -------------------------------------------------------------------------------- /Kafka_Streams_TopN.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lucasjellema/kafka-streams-running-topN/41e81b98f437a8c074a07ccac17a0615a8845bad/Kafka_Streams_TopN.pptx -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # kafka-streams-running-topN 2 | Running Top N aggregation using Apache Kafka Streams 3 | 4 | Details on this application are discussed in the following blog articles: 5 | 6 | * Apache Kafka Streams – Running Top-N Aggregation grouped by Dimension – from and to Kafka Topic - https://technology.amis.nl/2017/02/12/apache-kafka-streams-running-top-n-grouped-by-dimension-from-and-to-kafka-topic/ 7 | 8 | * Getting Started with Kafka Streams – building a streaming analytics Java application against a Kafka Topic - https://technology.amis.nl/2017/02/11/getting-started-with-kafka-streams-building-a-streaming-analytics-java-application-against-a-kafka-topic/ 9 | 10 | * NodeJS – Publish messages to Apache Kafka Topic with random delays to generate sample events based on records in CSV file - https://technology.amis.nl/2017/02/09/nodejs-publish-messages-to-apache-kafka-topic-with-random-delays-to-generate-sample-events-based-on-records-in-csv-file/ 11 | 12 | * Workshop Apache Kafka – presentation and hands on labs for getting started - https://technology.amis.nl/2017/02/10/workshop-apache-kafka-presentation-and-hands-on-labs-for-getting-started/ 13 | 14 | Four applications in this repository: 15 | 16 | kafka-node-countries: Node.JS application (with kafka-node) that produces country messages from a CSV file with countries to the Kafka Topic countries 17 | 18 | kafka-node-TopN-Report: Node.JS application (with kafka-node) that produces a periodic report on the current Top N (largest countries per continent) based on the Kafka Topic Top3CountrySizePerContinent 19 | 20 | Kafka-Streams-Country-TopN: Java application (with Kafka Streams) that produces the running Top 3 standings per continent of the largest countries, published to Kafka Topic Top3CountrySizePerContinent 21 | 22 | kafka-node-express-topN-sse: Node.JS application (with kafka-node) that consumes Top N messages from Kafka Topic Top3CountrySizePerContinent and pushes them using Server Sent Events (SSE) to all SSE Clients that have registered through the Node Express framework 23 | 24 | To run these applications, go through the following steps: 25 | 26 | 1. git clone https://github.com/lucasjellema/kafka-streams-running-topN 27 | 28 | 2. kafka-node-countries 29 | 30 | * cd kafka-node-countries 31 | * npm install 32 | * node KafkaCountryProducer.js 33 | 34 | 35 | 3. Kafka-Streams-Country-TopN 36 | 37 | * cd Kafka-Streams-Country-TopN 38 | * mvn package 39 | * mvn install dependency:copy-dependencies 40 | * java -cp target/Kafka-Streams-Country-TopN-1.0-SNAPSHOT.jar;target/dependency/* nl.amis.streams.countries.App 41 | 42 | 43 | 4. Kafka-Streams-Country-TopN 44 | 45 | * cd kafka-node-TopN-Report 46 | * npm install 47 | * node KafkaCountryStreamsConsumer.js 48 | 49 | 5. kafka-node-express-topN-sse 50 | 51 | * cd kafka-node-express-topN-sse 52 | * npm install 53 | * node topNreport.js 54 | 55 | 56 | Note: the following prequisites have to be met: 57 | * a running Apache Kafka Cluster is available. The sources assume ubuntu:9092 (Broker) and ubuntu:2181 (ZooKeeper); 58 | * Java 8 JRE is installed (1.8.x) 59 | * Apache Maven is installed (3.x) 60 | * Node.JS is installed (6.x or higher) 61 | * npm is installed 62 | -------------------------------------------------------------------------------- /kafka-node-TopN-Report/KafkaCountryStreamsConsumer.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | This program consumes Kafka messages from topic Top3CountrySizePerContinent to which the Running Top3 (size of countries by continent) is produced. 4 | 5 | This program reports: top 3 largest countries per continent (periodically, with a configurable interval) 6 | */ 7 | 8 | 9 | var kafka = require('kafka-node') 10 | var Consumer = kafka.Consumer 11 | var client = new kafka.Client("ubuntu:2181/") 12 | 13 | var countriesTopic = "Top3CountrySizePerContinent"; 14 | var reportingIntervalInSecs = 4; 15 | 16 | var consumer = new Consumer( 17 | client, 18 | [], 19 | {fromOffset: true} 20 | ); 21 | 22 | consumer.on('message', function (message) { 23 | handleCountryMessage(message); 24 | }); 25 | 26 | consumer.addTopics([ 27 | { topic: countriesTopic, partition: 0, offset: 0} 28 | ], () => console.log("topic "+countriesTopic+" added to consumer for listening")); 29 | 30 | var countrySizeStandings = {}; // the global container for the most recent country size standings 31 | 32 | function handleCountryMessage(countryMessage) { 33 | var top3 = JSON.parse(countryMessage.value); 34 | var continent = new Buffer(countryMessage.key).toString('ascii'); 35 | //console.log ("key "+continent); 36 | countrySizeStandings[continent]=top3; 37 | }// handleCountryMessage 38 | 39 | // every reportingIntervalInSecs seconds, report on the current standings per continent 40 | function report() { 41 | var d = new Date(); 42 | console.log("Report at "+ d.getHours()+":"+d.getMinutes()+ ":"+d.getSeconds()); 43 | // loop over the keys (properties) in the countrySizeStandings map (object) 44 | for (var continent in countrySizeStandings) { 45 | if (countrySizeStandings.hasOwnProperty(continent)) { 46 | // do stuff 47 | //console.log("Present standings for continent "+continent); 48 | var line = continent+ ": "; 49 | var index = 1; 50 | countrySizeStandings[continent].nrs.forEach(function(c) { 51 | if (c) { 52 | line = line + (index++) +'. '+ c.name+ '('+c.size+'), '; 53 | } 54 | }); 55 | console.log(line); 56 | }//if 57 | }//for 58 | }//report 59 | 60 | // schedule execution of function report at the indicated interval 61 | setInterval(report, reportingIntervalInSecs*1000); -------------------------------------------------------------------------------- /kafka-node-TopN-Report/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "kafka-node-TopN-Report", 3 | "version": "0.0.2", 4 | "description": "Node.js programs to consume Top3 Country messages from a Kafka Topic and periodically report on the latest Top 3 standings", 5 | "main": "KafkaCountryProducer.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/lucasjellema/kafka-streams-running-topN" 12 | }, 13 | "keywords": [ 14 | "kafka", 15 | "node", 16 | "js", 17 | "produce", 18 | "consume" 19 | ], 20 | "author": "Lucas Jellema", 21 | "license": "ISC", 22 | "bugs": { 23 | "url": "https://github.com/lucasjellema/kafka-streams-running-topN/issues" 24 | }, 25 | "homepage": "https://github.com/lucasjellema/kafka-streams-running-topN#readme", 26 | "dependencies": { 27 | "csv-parse": "^1.2.0", 28 | "kafka-node": "^1.3.4" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /kafka-node-countries/KafkaCountryProducer.js: -------------------------------------------------------------------------------- 1 | /* 2 | This program reads and parses all lines from csv files countries2.csv into an array (countriesArray) of arrays; each nested array represents a country. 3 | The initial file read is synchronous. The country records are kept in memory. 4 | After the the initial read is performed, a function is invoked to publish a message to Kafka for the first country in the array. This function then uses a time out with a random delay 5 | to schedule itself to process the next country record in the same way. Depending on how the delays pan out, this program will publish country messages to Kafka every 3 seconds for about 10 minutes. 6 | */ 7 | 8 | var fs = require('fs'); 9 | var parse = require('csv-parse'); 10 | 11 | // Kafka configuration 12 | var kafka = require('kafka-node') 13 | var Producer = kafka.Producer 14 | // instantiate client with as connectstring host:port for the ZooKeeper for the Kafka cluster 15 | var client = new kafka.Client("ubuntu:2181/") 16 | 17 | // name of the topic to produce to 18 | var countriesTopic = "countries"; 19 | 20 | KeyedMessage = kafka.KeyedMessage, 21 | producer = new Producer(client), 22 | km = new KeyedMessage('key', 'message'), 23 | countryProducerReady = false ; 24 | 25 | producer.on('ready', function () { 26 | console.log("Producer for countries is ready"); 27 | countryProducerReady = true; 28 | }); 29 | 30 | producer.on('error', function (err) { 31 | console.error("Problem with producing Kafka message "+err); 32 | }) 33 | 34 | 35 | var inputFile='countries2.csv'; 36 | var averageDelay = 3000; // in miliseconds 37 | var spreadInDelay = 2000; // in miliseconds 38 | 39 | var countriesArray ; 40 | 41 | var parser = parse({delimiter: ';'}, function (err, data) { 42 | countriesArray = data; 43 | // when all countries are available,then process the first one 44 | // note: array element at index 0 contains the row of headers that we should skip 45 | handleCountry(1); 46 | }); 47 | 48 | // read the inputFile, feed the contents to the parser 49 | fs.createReadStream(inputFile).pipe(parser); 50 | 51 | // handle the current coountry record 52 | function handleCountry( currentCountry) { 53 | var line = countriesArray[currentCountry]; 54 | var country = { "name" : line[0] 55 | , "code" : line[1] 56 | , "continent" : line[2] 57 | , "population" : line[4] 58 | , "size" : line[5] 59 | }; 60 | console.log(JSON.stringify(country)); 61 | // produce country message to Kafka 62 | produceCountryMessage(country) 63 | // schedule this function to process next country after a random delay of between averageDelay plus or minus spreadInDelay ) 64 | var delay = averageDelay + (Math.random() -0.5) * spreadInDelay; 65 | //note: use bind to pass in the value for the input parameter currentCountry 66 | setTimeout(handleCountry.bind(null, currentCountry+1), delay); 67 | }//handleCountry 68 | 69 | function produceCountryMessage(country) { 70 | KeyedMessage = kafka.KeyedMessage, 71 | countryKM = new KeyedMessage(country.code, JSON.stringify(country)), 72 | payloads = [ 73 | { topic: countriesTopic, messages: countryKM, partition: 0 }, 74 | ]; 75 | if (countryProducerReady) { 76 | producer.send(payloads, function (err, data) { 77 | console.log(data); 78 | }); 79 | } else { 80 | // the exception handling can be improved, for example schedule this message to be tried again later on 81 | console.error("sorry, CountryProducer is not ready yet, failed to produce message to Kafka."); 82 | } 83 | 84 | }//produceCountryMessage 85 | 86 | -------------------------------------------------------------------------------- /kafka-node-countries/countries2.csv: -------------------------------------------------------------------------------- 1 | Country (en);Country code;Continent;Capital;Population;Area;Coastline;Government form;Currency;Currency code;Dialing prefix;Birthrate;Deathrate;Life expectancy;Url 2 | Afghanistan;AF;Asia;;33332025;652230;0;Presidential islamic republic;Afghani;AFN;93;38.3;13.7;51.3;https://www.laenderdaten.info/Asien/Afghanistan/index.php 3 | Egypt;EG;Africa;;94666993;1001450;2450;Presidential republic;Pfund;EGP;20;30.3;4.7;72.7;https://www.laenderdaten.info/Afrika/Aegypten/index.php 4 | Aland Islands;AX;Europe;;29013;1580;0;Autonomous region of Finland;Euro;EUR;358;0;0;0;https://www.laenderdaten.info/Europa/Aland/index.php 5 | Albania;AL;Europe;;3038594;28748;362;parliamentary republic;Lek;ALL;355;13.1;6.7;78.3;https://www.laenderdaten.info/Europa/Albanien/index.php 6 | Algeria;DZ;Africa;;40263711;2381741;998;Presidential republic;Dinar;DZD;213;23;4.3;76.8;https://www.laenderdaten.info/Afrika/Algerien/index.php 7 | American Samoa;AS;Oceania;;54194;199;116;Presidential democracy (self-governing territory of the US);Dollar;USD;1-684;22.9;4.8;75.4;https://www.laenderdaten.info/Ozeanien/Amerikanisch-Samoa/index.php 8 | Andorra;AD;Europe;;85660;468;0;parliamentary democracy;Euro;EUR;376;7.8;7.1;82.8;https://www.laenderdaten.info/Europa/Andorra/index.php 9 | Angola;AO;Africa;;20172332;1246700;1600;Presidential republic;Kwanza;AOA;244;38.6;11.3;56;https://www.laenderdaten.info/Afrika/Angola/index.php 10 | Anguilla;AI;North America;;16752;91;61;Parliamentary democracy (self-governing territory of the UK);Dollar;XCD;1809;12.7;4.6;81.4;https://www.laenderdaten.info/Amerika/Anguilla/index.php 11 | Antarctica;AQ;Antarctica;;0;14000000;17968;foreign-administrated territory;;;;0;0;0;https://www.laenderdaten.info/Antarktis/Antarktis/index.php 12 | Antigua and Barbuda;AG;North America;;93581;443;153;Parliamentary democracy (under constitutional monarchy);Dollar;XCD;1809;15.8;5.7;76.5;https://www.laenderdaten.info/Amerika/Antigua-Barbuda/index.php 13 | Equatorial Guinea;GQ;Africa;;759451;28051;296;Presidential republic;Franc;XAF;240;32.8;8;64.2;https://www.laenderdaten.info/Afrika/Aequatorialguinea/index.php 14 | Argentina;AR;South America;;43886748;2780400;4989;Presidential republic;Peso;ARS;54;17;7.5;77.1;https://www.laenderdaten.info/Amerika/Argentinien/index.php 15 | Armenia;AM;Asia;;3051250;29743;0;Semi-presidential republic;Dram;AMD;374;13.3;9.4;74.6;https://www.laenderdaten.info/Asien/Armenien/index.php 16 | Aruba;AW;North America;;113648;180;69;Parliamentary democracy (territory of the Kingdom of the Netherlands);Florin;AWG;297;12.5;8.3;76.8;https://www.laenderdaten.info/Amerika/Aruba/index.php 17 | Azerbaijan;AZ;Asia;;9872765;86600;0;Presidential republic;Manat;AZN;994;16.2;7.1;72.5;https://www.laenderdaten.info/Asien/Aserbaidschan/index.php 18 | Ethiopia;ET;Africa;;102374044;1104300;0;Federal parliamentary republic;Birr;ETB;251;36.9;7.9;62.2;https://www.laenderdaten.info/Afrika/Aethiopien/index.php 19 | Australia;AU;Australia;;22992654;7741220;25760;Parliamentary democracy (under constitutional monarchy);Dollar;AUD;61;12.1;7.2;82.2;https://www.laenderdaten.info/Australien/Australien/index.php 20 | Bahamas;BS;North America;;327316;13880;3542;Parliamentary democracy (under constitutional monarchy);Dollar;BSD;1809;15.4;7.1;72.4;https://www.laenderdaten.info/Amerika/Bahamas/index.php 21 | Bahrain;BH;Asia;;1378904;760;161;constitutional monarchy;Dinar;BHD;973;13.5;2.7;78.9;https://www.laenderdaten.info/Asien/Bahrain/index.php 22 | Bangladesh;BD;Asia;;156186882;143998;580;parliamentary republic;Taka;BDT;880;19;5.3;73.2;https://www.laenderdaten.info/Asien/Bangladesch/index.php 23 | Barbados;BB;North America;;291495;430;97;Parliamentary democracy (under constitutional monarchy);Dollar;BBD;1809;11.8;8.5;75.3;https://www.laenderdaten.info/Amerika/Barbados/index.php 24 | Belarus;BY;Europe;;9570376;207600;0;Presidential republic (in fact a dictatorship);Rubel;BYR;375;10.5;13.3;72.7;https://www.laenderdaten.info/Europa/Belarus/index.php 25 | Belgium;BE;Europe;;11409077;30528;67;Federal parliamentary republic (under constitutional monarchy);Euro;EUR;32;11.4;9.7;81;https://www.laenderdaten.info/Europa/Belgien/index.php 26 | Belize;BZ;Central America;;353858;22966;386;Parliamentary democracy (under constitutional monarchy);Dollar;BZD;501;24.3;6;68.7;https://www.laenderdaten.info/Amerika/Belize/index.php 27 | Benin;BJ;Africa;;10741458;112622;121;Presidential republic;Franc;XOF;229;35.5;8;61.9;https://www.laenderdaten.info/Afrika/Benin/index.php 28 | Bermuda;BM;North America;;70537;54;103;Parliamentary democracy (self-governing territory of the UK);Dollar;BMD;1809;11.3;8.4;81.3;https://www.laenderdaten.info/Amerika/Bermuda/index.php 29 | Bhutan;BT;Asia;;750125;38394;0;constitutional monarchy;Ngultrum;BTN;975;17.5;6.6;70.1;https://www.laenderdaten.info/Asien/Bhutan/index.php 30 | Bolivia;BO;South America;;10969649;1098581;0;Presidential republic;Boliviano;BOB;591;22.4;6.5;69.2;https://www.laenderdaten.info/Amerika/Bolivien/index.php 31 | Bosnia and Herzegovina;BA;Europe;;3861912;51197;20;parliamentary republic;Konvertible Mark;BAM;387;8.8;9.9;76.7;https://www.laenderdaten.info/Europa/Bosnien-und-Herzegowina/index.php 32 | Botswana;BW;Africa;;2209208;581730;0;parliamentary republic;Pula;BWP;267;20.7;13.3;54.5;https://www.laenderdaten.info/Afrika/Botswana/index.php 33 | Bouvet Island;BV;Antarctica;;0;49;30;territory of Norway;Krone;NOK;;0;0;0;https://www.laenderdaten.info/Antarktis/Bouvetinsel/index.php 34 | Brazil;BR;South America;;205823665;8514877;7491;Federal presidential republic;Real;BRL;55;14.3;6.6;73.8;https://www.laenderdaten.info/Amerika/Brasilien/index.php 35 | British Indian Ocean Territory;IO;Africa;;0;54400;698;British overseas territory;Dollar;USD;246;0;0;0;https://www.laenderdaten.info/Afrika/Britisches-Territorium-im-Indischen-Ozean/index.php 36 | Brunei;BN;Asia;;436620;5765;161;Absolute monarchy/sultanate;Dollar;BND;673;17.2;3.6;77.2;https://www.laenderdaten.info/Asien/Brunei/index.php 37 | Bulgaria;BG;Europe;;7144653;110879;354;parliamentary republic;Lew;BGN;359;8.8;14.5;74.5;https://www.laenderdaten.info/Europa/Bulgarien/index.php 38 | Burkina Faso;BF;Africa;;19512533;274200;0;Presidential republic;Franc;XOF;226;41.6;11.5;55.5;https://www.laenderdaten.info/Afrika/Burkina-Faso/index.php 39 | Burundi;BI;Africa;;11099298;27830;0;Presidential republic;Franc;BIF;257;41.7;9;60.5;https://www.laenderdaten.info/Afrika/Burundi/index.php 40 | Chile;CL;South America;;17650114;756102;6435;Presidential republic;Peso;CLP;56;13.7;6.1;78.8;https://www.laenderdaten.info/Amerika/Chile/index.php 41 | China;CN;Asia;;1373541278;9596960;14500;People's republic (communist one-party system);Yuan;CNY;86;12.4;7.7;75.5;https://www.laenderdaten.info/Asien/China/index.php 42 | Cook Islands;CK;Oceania;;9556;236;120;parliamentary democracy;Dollar;NZD;682;14.1;8.3;75.8;https://www.laenderdaten.info/Ozeanien/Cookinseln/index.php 43 | Costa Rica;CR;Central America;;4872543;51100;1290;Presidential republic;Colón;CRC;506;15.7;4.6;78.6;https://www.laenderdaten.info/Amerika/Costa-Rica/index.php 44 | Curacao;CW;America;;149035;444;364;parliamentary republic;Gulden;ANG;599-9;13.8;8.3;78.3;https://www.laenderdaten.info/Amerika/Curacao/index.php 45 | Denmark;DK;Europe;;5593785;43094;7314;Parliamentary constitutional monarchy;Krone;DKK;45;10.4;10.3;79.4;https://www.laenderdaten.info/Europa/Daenemark/index.php 46 | Democratic Republic of the Congo;CD;Africa;;81331050;2344858;37;Semi-presidential republic;Franc;CDF;242;34.2;9.9;57.3;https://www.laenderdaten.info/Afrika/Kongo-Kinshasa/index.php 47 | Germany;DE;Europe;;80722792;357022;2389;Federal parliamentary republic;Euro;EUR;49;8.5;11.6;80.7;https://www.laenderdaten.info/Europa/Deutschland/index.php 48 | Dominica;DM;North America;;73757;751;148;parliamentary republic;Dollar;XCD;1809;15.2;7.9;77;https://www.laenderdaten.info/Amerika/Dominica/index.php 49 | Dominican Republic;DO;North America;;10606865;48670;1288;Presidential republic;Peso;DOP;1809;18.6;4.6;78.1;https://www.laenderdaten.info/Amerika/Dominikanische-Republik/index.php 50 | Djibouti;DJ;Africa;;846687;23200;314;Semi-presidential republic;Franc;DJF;253;23.6;7.6;63.2;https://www.laenderdaten.info/Afrika/Dschibuti/index.php 51 | Ecuador;EC;South America;;16080778;283561;2237;Presidential republic;Dollar;USD;593;18.2;5.1;76.8;https://www.laenderdaten.info/Amerika/Ecuador/index.php 52 | El Salvador;SV;Central America;;6156670;21041;307;Presidential republic;Dollar;USD;503;16.3;5.7;74.7;https://www.laenderdaten.info/Amerika/El-Salvador/index.php 53 | Ivory Coast;CI;Africa;;23740424;322463;515;Presidential republic;Franc;XOF;225;28.2;9.5;58.7;https://www.laenderdaten.info/Afrika/Elfenbeinkueste/index.php 54 | Eritrea;ER;Africa;;5869869;117600;2234;Presidential republic;Nakfa;ERN;291;30.1;7.3;64.9;https://www.laenderdaten.info/Afrika/Eritrea/index.php 55 | Estonia;EE;Europe;;1258545;45228;3794;parliamentary republic;Euro;EUR;372;10.3;12.5;76.7;https://www.laenderdaten.info/Europa/Estland/index.php 56 | Falkland Islands;FK;South America;;2931;12173;1288;Parliamentary democracy (self-governing territory of the UK);Pfund;FKP;500;10.9;4.9;0;https://www.laenderdaten.info/Amerika/Falklandinseln/index.php 57 | Faroe Islands;FO;Europe;;50456;1393;1117;Parliamentary democracy (territory of the Kingdom of Denmark);Krone;DKK;298;14;8.7;80.4;https://www.laenderdaten.info/Europa/Faeroeer-Inseln/index.php 58 | Fiji;FJ;Oceania;;915303;18274;1129;parliamentary republic;Dollar;FJD;679;19;6.1;72.7;https://www.laenderdaten.info/Ozeanien/Fidschi/index.php 59 | Finland;FI;Europe;;5498211;338145;1250;parliamentary republic;Euro;EUR;358;10.7;9.9;80.9;https://www.laenderdaten.info/Europa/Finnland/index.php 60 | France;FR;Europe;;66836154;643801;3427;Semi-presidential republic;Euro;EUR;33;12.3;9.3;81.8;https://www.laenderdaten.info/Europa/Frankreich/index.php 61 | French Guiana;GF;South America;;181000;83534;0;overseas territory of France;Euro;EUR;594;0;0;76.1;https://www.laenderdaten.info/Amerika/Franzoesisch-Guayana/index.php 62 | French Polynesia;PF;Oceania;;285321;4167;2525;Parliamentary democracy (territory of France);Franc;XPF;689;15;5.1;77.2;https://www.laenderdaten.info/Ozeanien/Franzoesisch-Polynesien/index.php 63 | French Southern and Antarctic Lands;TF;Antarctica;;0;439672;2800;overseas territory of France;Euro;EUR;;0;0;0;https://www.laenderdaten.info/Antarktis/Franzoesische-Antarktis/index.php 64 | Gabon;GA;Africa;;1738541;267667;885;Presidential republic;Franc;XAF;241;34.3;13.1;52.1;https://www.laenderdaten.info/Afrika/Gabun/index.php 65 | Gambia;GM;Africa;;2009648;11295;80;Presidential republic;Dalasi;GMD;220;30.1;7.1;64.9;https://www.laenderdaten.info/Afrika/Gambia/index.php 66 | Georgia;GE;Asia;;4928052;69700;310;Semi-presidential republic;Lari;GEL;995;12.5;10.9;76.2;https://www.laenderdaten.info/Asien/Georgien/index.php 67 | Ghana;GH;Africa;;26908262;238533;539;Presidential republic;Ghana Cedi;GHS;233;30.8;7.1;66.6;https://www.laenderdaten.info/Afrika/Ghana/index.php 68 | Gibraltar;GI;Europe;;29328;7;12;Parliamentary democracy (self-governing territory of the UK);Pfund;GIP;350;14.1;8.4;79.4;https://www.laenderdaten.info/Europa/Gibraltar/index.php 69 | Grenada;GD;North America;;111219;344;121;parliamentary democracy;Dollar;XCD;1809;15.8;8.1;74.3;https://www.laenderdaten.info/Amerika/Grenada/index.php 70 | Greece;GR;Europe;;10773253;131957;13676;parliamentary republic;Euro;EUR;30;8.5;11.2;80.5;https://www.laenderdaten.info/Europa/Griechenland/index.php 71 | Greenland;GL;North America;;57728;2166086;44087;Parliamentary democracy (territory of the Kingdom of Denmark);Krone;DKK;299;14.4;8.6;72.4;https://www.laenderdaten.info/Amerika/Groenland/index.php 72 | Guadeloupe;GP;North America;;456000;1628;0;overseas territory of France;Euro;EUR;590;0;0;77;https://www.laenderdaten.info/Amerika/Guadeloupe/index.php 73 | Guam;GU;Oceania;;162742;544;126;Presidential democracy (self-governing unincorporated territory of the US);Dollar;USD;671;16.7;5.2;79.1;https://www.laenderdaten.info/Ozeanien/Guam/index.php 74 | Guatemala;GT;Central America;;15189958;108889;400;Presidential republic;Quetzal;GTQ;502;24.5;4.7;72.3;https://www.laenderdaten.info/Amerika/Guatemala/index.php 75 | Guernsey;GG;Europe;;66297;78;50;Parliamentary democracy (autonomous Crown dependency of the UK);Pfund;GGP;44;9.8;8.9;82.5;https://www.laenderdaten.info/Europa/Guernsey/index.php 76 | Guinea;GN;Africa;;12093349;245857;320;Presidential republic;Franc;GNF;224;35.4;9.2;60.6;https://www.laenderdaten.info/Afrika/Guinea/index.php 77 | Guinea-Bissau;GW;Africa;;1759159;36125;350;Semi-presidential republic;Franc;XOF;245;32.9;14.1;50.6;https://www.laenderdaten.info/Afrika/Guinea-Bissau/index.php 78 | Guyana;GY;South America;;735909;214969;459;parliamentary republic;Dollar;GYD;592;15.5;7.4;68.4;https://www.laenderdaten.info/Amerika/Guyana/index.php 79 | Haiti;HT;North America;;10485800;27750;1771;Semi-presidential republic;Gourde;HTG;509;23.3;7.7;63.8;https://www.laenderdaten.info/Amerika/Haiti/index.php 80 | Heard Island and McDonald Islands;HM;Antarctica;;0;412;102;territory of Australia;Dollar;AUD;;0;0;0;https://www.laenderdaten.info/Antarktis/Heard-und-McDonaldinseln/index.php 81 | Honduras;HN;Central America;;8893259;112090;669;Presidential republic;Lempira;HNL;504;22.8;5.2;71.1;https://www.laenderdaten.info/Amerika/Honduras/index.php 82 | Hong Kong;HK;Asia;;7167403;1108;733;Presidential limited democracy (special administrative region of China);Dollar;HKD;852;9.1;7.2;82.9;https://www.laenderdaten.info/Asien/Hongkong/index.php 83 | India;IN;Asia;;1266883598;3287263;7000;Federal parliamentary republic;Rupie;INR;91;19.3;7.3;68.5;https://www.laenderdaten.info/Asien/Indien/index.php 84 | Indonesia;ID;Asia;;258316051;1904569;54716;Presidential republic;Rupiah;IDR;62;16.4;6.4;72.7;https://www.laenderdaten.info/Asien/Indonesien/index.php 85 | Isle of Man;IM;Europe;;88195;572;160;Parliamentary democracy (autonomous Crown dependency of the UK);Pfund;IMP;44-1624;11;10.1;81.2;https://www.laenderdaten.info/Europa/Insel-Man/index.php 86 | Iraq;IQ;Asia;;38146025;438317;58;Federal parliamentary republic;Dinar;IQD;964;30.9;3.8;74.9;https://www.laenderdaten.info/Asien/Irak/index.php 87 | Iran;IR;Asia;;82801633;1648195;2440;islamic republic;Rial;IRR;98;17.8;5.9;71.4;https://www.laenderdaten.info/Asien/Iran/index.php 88 | Ireland;IE;Europe;;4952473;70273;1448;parliamentary republic;Euro;EUR;353;14.5;6.5;80.8;https://www.laenderdaten.info/Europa/Irland/index.php 89 | Iceland;IS;Europe;;335878;103000;4970;parliamentary republic;Krone;ISK;354;13.8;6.3;83;https://www.laenderdaten.info/Europa/Island/index.php 90 | Israel;IL;Asia;;8174527;20770;273;parliamentary democracy;Schekel;ILS;972;18.3;5.2;82.4;https://www.laenderdaten.info/Asien/Israel/index.php 91 | Italy;IT;Europe;;62007540;301340;7600;parliamentary republic;Euro;EUR;39;8.7;10.3;82.2;https://www.laenderdaten.info/Europa/Italien/index.php 92 | Jamaica;JM;North America;;2970340;10991;1022;Parliamentary democracy (under constitutional monarchy);Dollar;JMD;1876;18;6.7;73.6;https://www.laenderdaten.info/Amerika/Jamaika/index.php 93 | Japan;JP;Asia;;126702133;377915;29751;Parliamentary constitutional monarchy;Yen;JPY;81;7.8;9.6;85;https://www.laenderdaten.info/Asien/Japan/index.php 94 | Yemen;YE;Asia;;27392779;527968;1906;republic;Rial;YER;967;29.2;6.1;65.5;https://www.laenderdaten.info/Asien/Jemen/index.php 95 | Jersey;JE;Europe;;98069;116;70;Parliamentary democracy (autonomous Crown dependency of the UK);Pfund-Sterling;JEP;44;12.1;7.7;81.9;https://www.laenderdaten.info/Europa/Jersey/index.php 96 | Jordan;JO;Asia;;8185384;89342;26;Parliamentary constitutional monarchy;Dinar;JOD;962;25.5;3.8;74.6;https://www.laenderdaten.info/Asien/Jordanien/index.php 97 | British Virgin Islands;VG;North America;;34232;151;80;Parliamentary democracy (self-governing territory of the UK);Dollar;USD;1-284;11;5.1;78.6;https://www.laenderdaten.info/Amerika/Jungferninseln-UK/index.php 98 | Virgin Islands;VI;North America;;102951;1910;188;Presidential democracy (self-governing territory of the US);Dollar;USD;1809;10.2;8.9;80;https://www.laenderdaten.info/Amerika/Jungferninseln-US/index.php 99 | Cayman Islands;KY;North America;;57268;264;160;Parliamentary democracy (self-governing territory of the UK);Cayman-Dollar;KYD;1809;12.1;5.7;81.2;https://www.laenderdaten.info/Amerika/Kaimaninseln/index.php 100 | Cambodia;KH;Asia;;15957223;181035;443;Parliamentary constitutional monarchy;Riel;KHR;855;23.4;7.6;64.5;https://www.laenderdaten.info/Asien/Kambodscha/index.php 101 | Cameroon;CM;Africa;;24360803;475440;402;Presidential republic;Franc;XAF;237;35.8;9.8;58.5;https://www.laenderdaten.info/Afrika/Kamerun/index.php 102 | Canada;CA;North America;;35362905;9984670;202080;Federal parliamentary republic (under constitutional monarchy);Dollar;CAD;1;10.3;8.5;81.9;https://www.laenderdaten.info/Amerika/Kanada/index.php 103 | Cape Verde;CV;Africa;;553432;4033;965;parliamentary republic;Escudo;CVE;238;20.2;6.1;72.1;https://www.laenderdaten.info/Afrika/Kap-Verde/index.php 104 | Kazakhstan;KZ;Asia;;18360353;2724900;0;Presidential republic;Tenge;KZT;7;18.7;8.2;70.8;https://www.laenderdaten.info/Asien/Kasachstan/index.php 105 | Qatar;QA;Asia;;2258283;11586;563;absolute monarchy;Dollar;QAR;974;9.7;1.5;78.7;https://www.laenderdaten.info/Asien/Katar/index.php 106 | Kenya;KE;Africa;;46790758;580367;536;Presidential republic;Schilling;KES;254;25.1;6.8;64;https://www.laenderdaten.info/Afrika/Kenia/index.php 107 | Kyrgyzstan;KG;Asia;;5727553;199951;0;parliamentary republic;Som;KGS;996;22.6;6.6;70.7;https://www.laenderdaten.info/Asien/Kirgisistan/index.php 108 | Kiribati;KI;Oceania;;106925;811;1143;Presidential republic;Dollar;AUD;686;21.3;7.1;66.2;https://www.laenderdaten.info/Ozeanien/Kiribati/index.php 109 | Cocos Islands;CC;Australia;;596;14;26;(non-self-governing territory of Australia);Dollar;AUD;891;0;0;0;https://www.laenderdaten.info/Australien/Kokosinseln/index.php 110 | Colombia;CO;South America;;47220856;1138910;3208;Presidential republic;Peso;COP;57;16.3;5.4;75.7;https://www.laenderdaten.info/Amerika/Kolumbien/index.php 111 | Comoros;KM;Africa;;794678;2235;340;Federal presidential republic;Franc;KMF;269;26.9;7.4;64.2;https://www.laenderdaten.info/Afrika/Komoren/index.php 112 | Republic of the Congo;CG;Africa;;4852412;342000;169;Presidential republic;Franc;CDF;242;35.1;9.7;59.3;https://www.laenderdaten.info/Afrika/Kongo-Brazzaville/index.php 113 | Kosovo;XK;Europe;;1883018;10887;0;parliamentary republic;Euro;EUR;383;0;0;69;https://www.laenderdaten.info/Europa/Kosovo/index.php 114 | Croatia;HR;Europe;;4313707;56594;5835;parliamentary republic;Kuna;HRK;385;9;12.1;75.9;https://www.laenderdaten.info/Europa/Kroatien/index.php 115 | Cuba;CU;North America;;11179995;110860;3735;Republic (communist one-party system);Peso;CUP;53;10.8;8.6;78.7;https://www.laenderdaten.info/Amerika/Kuba/index.php 116 | Kuwait;KW;Asia;;2832776;17818;499;constitutional monarchy;Dinar;KWD;965;19.6;2.2;78;https://www.laenderdaten.info/Asien/Kuwait/index.php 117 | Laos;LA;Asia;;7019073;236800;0;Republic (communist one-party system);Kip;LAK;856;23.9;7.5;64.3;https://www.laenderdaten.info/Asien/Laos/index.php 118 | Lesotho;LS;Africa;;1953070;30355;0;Parliamentary constitutional monarchy;Loti;LSL;266;25.1;14.9;53;https://www.laenderdaten.info/Afrika/Lesotho/index.php 119 | Latvia;LV;Europe;;1965686;64589;498;parliamentary republic;Euro;EUR;371;9.9;14.4;74.5;https://www.laenderdaten.info/Europa/Lettland/index.php 120 | Lebanon;LB;Asia;;6237738;10400;225;parliamentary republic;Pfund;LBP;961;14.4;4.9;77.6;https://www.laenderdaten.info/Asien/Libanon/index.php 121 | Liberia;LR;Africa;;4299944;111369;579;Presidential republic;Dollar;LRD;231;33.9;9.5;59;https://www.laenderdaten.info/Afrika/Liberia/index.php 122 | Libya;LY;Africa;;6541948;1759540;1770;Republic (transitional government after dictatorship);Dinar;LYD;218;17.8;3.6;76.5;https://www.laenderdaten.info/Afrika/Libyen/index.php 123 | Liechtenstein;LI;Europe;;37937;160;0;constitutional monarchy;Franken;CHF;423;10.4;7.3;81.9;https://www.laenderdaten.info/Europa/Liechtenstein/index.php 124 | Lithuania;LT;Europe;;2854235;65300;90;Semi-presidential republic;Litas;LTL;370;10;14.5;74.9;https://www.laenderdaten.info/Europa/Litauen/index.php 125 | Luxembourg;LU;Europe;;582291;2586;0;constitutional monarchy;Euro;EUR;352;11.4;7.3;82.3;https://www.laenderdaten.info/Europa/Luxemburg/index.php 126 | Macau;MO;Asia;;597425;28;41;Presidential limited democracy (special administrative region of China);Pataca;MOP;853;8.8;4.4;84.5;https://www.laenderdaten.info/Asien/Macau/index.php 127 | Madagascar;MG;Africa;;24430325;587041;4828;Semi-presidential republic;Ariary;MGA;261;32.1;6.7;65.9;https://www.laenderdaten.info/Afrika/Madagaskar/index.php 128 | Malawi;MW;Africa;;18570321;118484;0;Presidential republic;Kwacha;MWK;265;41.3;8.1;61.2;https://www.laenderdaten.info/Afrika/Malawi/index.php 129 | Malaysia;MY;Asia;;30949962;329847;4675;Federal constitutional monarchy;Ringgit;MYR;60;19.4;5.1;75;https://www.laenderdaten.info/Asien/Malaysia/index.php 130 | Maldives;MV;Asia;;392960;298;644;Presidential republic;Rufiyaa;MVR;960;16;3.9;75.6;https://www.laenderdaten.info/Asien/Malediven/index.php 131 | Mali;ML;Africa;;17467108;1240192;0;Semi-presidential republic;Franc;XOF;223;44.4;12.6;55.8;https://www.laenderdaten.info/Afrika/Mali/index.php 132 | Malta;MT;Europe;;415196;316;197;parliamentary republic;Euro;EUR;356;10.1;9.2;80.4;https://www.laenderdaten.info/Europa/Malta/index.php 133 | Morocco;MA;Africa;;33655786;446550;1835;Parliamentary constitutional monarchy;Dirham;MAD;212;18;4.8;76.9;https://www.laenderdaten.info/Afrika/Marokko/index.php 134 | Marshall Islands;MH;Oceania;;73376;181;370;Presidential republic;Dollar;USD;692;25;4.2;73.1;https://www.laenderdaten.info/Ozeanien/Marshallinseln/index.php 135 | Martinique;MQ;North America;;395000;1128;0;overseas territory of France;Euro;EUR;596;0;0;78.3;https://www.laenderdaten.info/Amerika/Martinique/index.php 136 | Mauritania;MR;Africa;;3677293;1030700;754;Presidential republic;Ouguiya;MRO;222;30.9;8.1;63;https://www.laenderdaten.info/Afrika/Mauretanien/index.php 137 | Mauritius;MU;Africa;;1348242;2040;177;parliamentary republic;Rupie;MUR;230;13.1;7;75.6;https://www.laenderdaten.info/Afrika/Mauritius/index.php 138 | Mayotte;YT;Africa;;223765;374;0;overseas territory of France;Euro;EUR;269;39.3;0;62.9;https://www.laenderdaten.info/Afrika/Mayotte/index.php 139 | Macedonia;MK;Europe;;2100025;25713;0;parliamentary republic;Denar;MKD;389;11.5;9.1;76.2;https://www.laenderdaten.info/Europa/Mazedonien/index.php 140 | Mexico;MX;North America;;123166749;1964375;9330;Federal presidential republic;Peso;MXN;52;18.5;5.3;75.9;https://www.laenderdaten.info/Amerika/Mexiko/index.php 141 | Micronesia, Federated States of;FM;Oceania;;104719;702;6112;Federal republic;Dollar;USD;691;20.3;4.2;72.9;https://www.laenderdaten.info/Ozeanien/Mikronesien/index.php 142 | Moldova;MD;Europe;;3510485;33851;0;parliamentary republic;Leu;MDL;373;11.8;12.6;70.7;https://www.laenderdaten.info/Europa/Moldau/index.php 143 | Monaco;MC;Europe;;30581;2;4;constitutional monarchy;Euro;EUR;3393;6.6;9.6;89.5;https://www.laenderdaten.info/Europa/Monaco/index.php 144 | Mongolia;MN;Asia;;3031330;1564116;0;Semi-presidential republic;Tögrög;MNT;976;19.6;6.3;69.6;https://www.laenderdaten.info/Asien/Mongolei/index.php 145 | Montenegro;ME;Europe;;644578;13812;294;parliamentary republic;Euro;EUR;382;10.2;9.6;0;https://www.laenderdaten.info/Europa/Montenegro/index.php 146 | Montserrat;MS;North America;;5267;102;40;Parliamentary democracy (self-governing territory of the UK);Dollar;XCD;1809;11;6.3;74.4;https://www.laenderdaten.info/Amerika/Montserrat/index.php 147 | Mozambique;MZ;Africa;;25930150;799380;2470;Presidential republic;Metical;MZN;258;38.3;11.9;53.3;https://www.laenderdaten.info/Afrika/Mosambik/index.php 148 | Burma;MM;Asia;;56890418;676578;1930;parliamentary republic;Kyat;MMK;95;18.2;7.9;66.6;https://www.laenderdaten.info/Asien/Myanmar/index.php 149 | Namibia;NA;Africa;;2436469;824292;1572;Presidential republic;Dollar;NAD;264;27.9;8.1;63.6;https://www.laenderdaten.info/Afrika/Namibia/index.php 150 | Nauru;NR;Oceania;;9591;21;30;parliamentary republic;Dollar;AUD;674;24.4;5.9;67.1;https://www.laenderdaten.info/Ozeanien/Nauru/index.php 151 | Nepal;NP;Asia;;29033914;147181;0;Federal parliamentary republic;Rupie;NPR;977;19.9;5.7;70.7;https://www.laenderdaten.info/Asien/Nepal/index.php 152 | New Caledonia;NC;Oceania;;275355;18575;2254;Parliamentary democracy (territory of France);Franc;XPF;687;15.2;5.6;77.7;https://www.laenderdaten.info/Ozeanien/Neukaledonien/index.php 153 | New Zealand;NZ;Australia;;4474549;267710;15134;Parliamentary democracy (under constitutional monarchy);Dollar;NZD;64;13.3;7.4;81.2;https://www.laenderdaten.info/Australien/Neuseeland/index.php 154 | Nicaragua;NI;Central America;;5966798;130370;910;Presidential republic;Córdoba Oro;NIO;505;17.9;5.1;73.2;https://www.laenderdaten.info/Amerika/Nicaragua/index.php 155 | Netherlands;NL;Europe;;17016967;41543;451;Parliamentary constitutional monarchy;Euro;EUR;31;10.9;8.8;81.3;https://www.laenderdaten.info/Europa/Niederlande/index.php 156 | Netherlands Antilles;AN;North America;;227049;800;0;territory of the Netherlands;Gulden;ANG;599;14.2;0;76.7;https://www.laenderdaten.info/Amerika/Niederlaendische-Antillen/index.php 157 | Niger;NE;Africa;;18638600;1267000;0;Semi-presidential republic;Franc;XOF;227;44.8;12.1;55.5;https://www.laenderdaten.info/Afrika/Niger/index.php 158 | Nigeria;NG;Africa;;186053386;923768;853;Federal presidential republic;Naira;NGN;234;37.3;12.7;53.4;https://www.laenderdaten.info/Afrika/Nigeria/index.php 159 | Niue;NU;Oceania;;1190;260;64;parliamentary democracy;Dollar;NZD;683;0;0;0;https://www.laenderdaten.info/Ozeanien/Niue/index.php 160 | North Korea;KP;Asia;;25115311;120538;2495;"People's republic (communist one-party system)";Won;KPW;850;14.6;9.3;70.4;https://www.laenderdaten.info/Asien/Nordkorea/index.php 161 | Northern Mariana Islands;MP;Oceania;;53467;464;1482;Presidential democracy;Dollar;USD;1-670;17.2;3.8;78;https://www.laenderdaten.info/Ozeanien/Noerdliche-Marianen/index.php 162 | Norfolk Island;NF;Australia;;2210;36;32;Parliamentary democracy (territory of Australia);Dollar;AUD;6723;0;0;0;https://www.laenderdaten.info/Australien/Norfolkinsel/index.php 163 | Norway;NO;Europe;;5265158;323802;25148;Parliamentary constitutional monarchy;Krone;NOK;47;12.2;8.1;81.8;https://www.laenderdaten.info/Europa/Norwegen/index.php 164 | Oman;OM;Asia;;3355262;309500;2092;absolute monarchy;Rial;OMR;968;24.3;3.3;75.5;https://www.laenderdaten.info/Asien/Oman/index.php 165 | Austria;AT;Europe;;8711770;83871;0;Federal parliamentary republic;Euro;EUR;43;9.5;9.5;81.5;https://www.laenderdaten.info/Europa/Oesterreich/index.php 166 | Timor-Leste;TL;Asia;;1261072;14874;706;Semi-presidential republic;Dollar;USD;670;33.8;6;68.1;https://www.laenderdaten.info/Asien/Osttimor/index.php 167 | Pakistan;PK;Asia;;201995540;796095;1046;Federal parliamentary republic;Rupie;PKR;92;22.3;6.4;67.7;https://www.laenderdaten.info/Asien/Pakistan/index.php 168 | Palestine;PS;Asia;;2731052;5860;0;autonomous region;Schekel;ILS;970;23.4;3.5;75.7;https://www.laenderdaten.info/Asien/Palaestina/index.php 169 | Palau;PW;Oceania;;21347;459;1519;Presidential republic;Dollar;USD;680;11.2;8;73.1;https://www.laenderdaten.info/Ozeanien/Palau/index.php 170 | Panama;PA;Central America;;3705246;75420;2490;Presidential republic;Balboa;PAB;507;18.1;4.9;78.6;https://www.laenderdaten.info/Amerika/Panama/index.php 171 | Papua New Guinea;PG;Oceania;;6791317;462840;5152;Parliamentary democracy (under constitutional monarchy);Kina;PGK;675;24;6.5;67.2;https://www.laenderdaten.info/Ozeanien/Papua-Neuguinea/index.php 172 | Paraguay;PY;South America;;6862812;406752;0;Presidential republic;Guaraní;PYG;595;16.5;4.7;77.2;https://www.laenderdaten.info/Amerika/Paraguay/index.php 173 | Peru;PE;South America;;30741062;1285216;2414;Presidential republic;Nuevo Sol;PEN;51;18;6;73.7;https://www.laenderdaten.info/Amerika/Peru/index.php 174 | Philippines;PH;Asia;;102624209;300000;36289;Presidential republic;Peso;PHP;63;24;6.1;69.2;https://www.laenderdaten.info/Asien/Philippinen/index.php 175 | Pitcairn Islands;PN;Oceania;;54;47;51;Parliamentary democracy (territory of the US);Pfund;GBP;649;0;0;0;https://www.laenderdaten.info/Ozeanien/Pitcairninseln/index.php 176 | Poland;PL;Europe;;38523261;312685;440;parliamentary republic;Zloty;PLN;48;9.6;10.3;77.6;https://www.laenderdaten.info/Europa/Polen/index.php 177 | Portugal;PT;Europe;;10833816;92090;1793;Semi-presidential republic;Euro;EUR;351;9.1;11.1;79.3;https://www.laenderdaten.info/Europa/Portugal/index.php 178 | Puerto Rico;PR;North America;;3578056;13790;501;Presidential democracy;Dollar;USD;1809;10.8;8.8;79.4;https://www.laenderdaten.info/Amerika/Puerto-Rico/index.php 179 | Reunion;RE;Africa;;699000;2512;0;overseas territory of France;Euro;EUR;262;0;0;72.7;https://www.laenderdaten.info/Afrika/Reunion/index.php 180 | Rwanda;RW;Africa;;12988423;26338;0;Presidential republic;Franc;RWF;250;33.3;8.8;60.1;https://www.laenderdaten.info/Afrika/Ruanda/index.php 181 | Romania;RO;Europe;;21599736;238391;225;Semi-presidential republic;Leu;RON;40;9;11.9;75.1;https://www.laenderdaten.info/Europa/Rumaenien/index.php 182 | Russia;RU;Europe;;142355415;17098242;37653;Federal republic;Rubel;RUB;7;11.3;13.6;70.8;https://www.laenderdaten.info/Europa/Russland/index.php 183 | Solomon Islands;SB;Oceania;;635027;28896;5313;Parliamentary democracy (under constitutional monarchy);Dollar;SBD;677;25.3;3.8;75.3;https://www.laenderdaten.info/Ozeanien/Salomonen/index.php 184 | Zambia;ZM;Africa;;15510711;752618;0;Presidential republic;Kwacha;ZMK;260;41.8;12.4;52.5;https://www.laenderdaten.info/Afrika/Sambia/index.php 185 | Samoa;WS;Oceania;;198926;2831;403;parliamentary republic;Tala;WST;685;20.6;5.3;73.7;https://www.laenderdaten.info/Ozeanien/Samoa/index.php 186 | San Marino;SM;Europe;;33285;61;0;parliamentary republic;Euro;EUR;378;8.6;8.6;83.3;https://www.laenderdaten.info/Europa/San-Marino/index.php 187 | Saint Barthelemy;BL;America;;7209;21;0;territory of France;Euro;EUR;590;0;0;0;https://www.laenderdaten.info/Amerika/Sankt-Bartholomaeus/index.php 188 | Sao Tome and Principe;ST;Africa;;197541;964;209;Semi-presidential republic;Dobra;STD;239;33.3;7;64.9;https://www.laenderdaten.info/Afrika/Sao-Tome-und-Principe/index.php 189 | Saudi Arabia;SA;Asia;;28160273;2149690;2640;absolute monarchy;Riyal;SAR;966;18.4;3.3;75.3;https://www.laenderdaten.info/Asien/Saudi-Arabien/index.php 190 | Sweden;SE;Europe;;9880604;450295;3218;Parliamentary constitutional monarchy;Krone;SEK;46;12;9.4;82.1;https://www.laenderdaten.info/Europa/Schweden/index.php 191 | Switzerland;CH;Europe;;8179294;41277;0;Federal republic;Franken;CHF;41;10.5;8.2;82.6;https://www.laenderdaten.info/Europa/Schweiz/index.php 192 | Senegal;SN;Africa;;14320055;196722;531;Presidential republic;Franc;XOF;221;34;8.3;61.7;https://www.laenderdaten.info/Afrika/Senegal/index.php 193 | Serbia;RS;Europe;;7143921;77474;0;parliamentary republic;Dinar;RSD;381;9;13.6;75.5;https://www.laenderdaten.info/Europa/Serbien/index.php 194 | Seychelles;SC;Africa;;93186;455;491;Presidential republic;Rupie;SCR;248;13.9;6.9;74.7;https://www.laenderdaten.info/Afrika/Seychellen/index.php 195 | Sierra Leone;SL;Africa;;6018888;71740;402;Presidential republic;Leone;SLL;232;36.7;10.6;58.2;https://www.laenderdaten.info/Afrika/Sierra-Leone/index.php 196 | Zimbabwe;ZW;Africa;;14546961;390757;0;Semi-presidential republic;Dollar;ZWL;263;31.9;9.9;58;https://www.laenderdaten.info/Afrika/Simbabwe/index.php 197 | Singapore;SG;Asia;;5781728;697;193;parliamentary republic;Dollar;SGD;65;8.4;3.5;85;https://www.laenderdaten.info/Asien/Singapur/index.php 198 | Saint Martin;SX;America;;31949;54;59;Republic (autonomous territory of the Kingdom of the Netherlands);Gulden;ANG;1-721;13.1;5;78.1;https://www.laenderdaten.info/Amerika/Sint-Maarten/index.php 199 | Slovakia;SK;Europe;;5445802;49035;0;parliamentary republic;Euro;EUR;42;9.8;9.8;77.1;https://www.laenderdaten.info/Europa/Slowakei/index.php 200 | Slovenia;SI;Europe;;1978029;20273;47;parliamentary republic;Euro;EUR;386;8.3;11.5;78.2;https://www.laenderdaten.info/Europa/Slowenien/index.php 201 | Somalia;SO;Africa;;10817354;637657;3025;Federal parliamentary republic;Schilling;SOS;252;40;13.3;52.4;https://www.laenderdaten.info/Afrika/Somalia/index.php 202 | Spain;ES;Europe;;48563476;505370;4964;Parliamentary constitutional monarchy;Euro;EUR;34;9.4;9.1;81.7;https://www.laenderdaten.info/Europa/Spanien/index.php 203 | Sri Lanka;LK;Asia;;22235000;65610;1340;Presidential republic;Rupie;LKR;94;15.5;6.2;76.8;https://www.laenderdaten.info/Asien/Sri-Lanka/index.php 204 | Saint Helena, Ascension and Tristan da Cunha;SH;Africa;;7795;308;60;Parliamentary democracy (limited self-governing territory of the UK);Pfund;SHP;290;9.7;7.7;79.5;https://www.laenderdaten.info/Afrika/St-Helena/index.php 205 | Saint Kitts and Nevis;KN;North America;;52329;261;135;Federal parliamentary republic (under constitutional monarchy);Dollar;XCD;1809;13.3;7.1;75.7;https://www.laenderdaten.info/Amerika/St-Kitts-Nevis/index.php 206 | Saint Lucia;LC;North America;;164464;616;158;Parliamentary democracy (under constitutional monarchy);Dollar;XCD;1809;13.5;7.6;77.8;https://www.laenderdaten.info/Amerika/St-Lucia/index.php 207 | Saint Pierre and Miquelon;PM;North America;;5595;242;120;Parliamentary democracy (territory of France);Euro;EUR;508;7.2;9.8;80.5;https://www.laenderdaten.info/Amerika/St-Pierre-Miquelon/index.php 208 | Saint Vincent and the Grenadines;VC;North America;;102350;389;84;Parliamentary democracy (under constitutional monarchy);Dollar;XCD;1809;13.4;7.3;75.3;https://www.laenderdaten.info/Amerika/St-Vincent-Grenadinen/index.php 209 | South Africa;ZA;Africa;;54300704;1219090;2798;parliamentary republic;Rand;ZAR;27;20.5;9.6;63.1;https://www.laenderdaten.info/Afrika/Suedafrika/index.php 210 | Sudan;SD;Africa;;36729501;1861484;853;Presidential republic;Pfund;SDG;249;28.5;7.5;64.1;https://www.laenderdaten.info/Afrika/Sudan/index.php 211 | South Georgia and South Sandwich Islands;GS;Antarctica;;30;3903;0;British overseas territory;Pfund;GBP;500;0;0;0;https://www.laenderdaten.info/Antarktis/Suedgeorgien-Sandwichinseln/index.php 212 | South Korea;KR;Asia;;50924172;99720;2413;Presidential republic;Won;KRW;82;8.4;5.8;82.4;https://www.laenderdaten.info/Asien/Suedkorea/index.php 213 | South Sudan;SS;Africa;;12530717;644329;0;Presidential republic;Pfund;SSP;211;36.2;8;0;https://www.laenderdaten.info/Afrika/Suedsudan/index.php 214 | Suriname;SR;South America;;585824;163820;386;Presidential republic;Dollar;SRD;597;16;6.1;72.2;https://www.laenderdaten.info/Amerika/Suriname/index.php 215 | Svalbard;SJ;Europe;;1872;377;124;territory of Norway;Krone;NOK;47;0;0;0;https://www.laenderdaten.info/Europa/Svalbard-und-Jan-Mayen/index.php 216 | Swaziland;SZ;Africa;;1451428;17364;0;absolute monarchy;Lilangeni;SZL;268;24.3;13.4;51.6;https://www.laenderdaten.info/Afrika/Swasiland/index.php 217 | Syria;SY;Asia;;17185170;185180;193;Presidential republic;Pfund;SYP;963;21.7;4;74.9;https://www.laenderdaten.info/Asien/Syrien/index.php 218 | Tajikistan;TJ;Asia;;8330946;143100;0;Presidential republic;Somoni;TJS;992;23.8;6.1;67.7;https://www.laenderdaten.info/Asien/Tadschikistan/index.php 219 | Taiwan;TW;Asia;;23464787;35980;1566;Semi-presidential republic;Dollar;TWD;886;8.4;7.3;80.1;https://www.laenderdaten.info/Asien/Taiwan/index.php 220 | Tanzania;TZ;Africa;;52482726;947300;1424;Presidential republic;Schilling;TZS;255;36;7.8;62.2;https://www.laenderdaten.info/Afrika/Tansania/index.php 221 | Thailand;TH;Asia;;68200824;513120;3219;Constitutional monarchy (interim military-government since May 2014);Baht;THB;66;11.1;7.9;74.7;https://www.laenderdaten.info/Asien/Thailand/index.php 222 | Togo;TG;Africa;;7756937;56785;56;Presidential republic;Franc;XOF;228;33.7;7.1;65;https://www.laenderdaten.info/Afrika/Togo/index.php 223 | Tokelau;TK;Oceania;;1337;12;101;Parliamentary democracy (territory of New Zealand);Dollar;NZD;690;0;0;0;https://www.laenderdaten.info/Ozeanien/Tokelau/index.php 224 | Tonga;TO;Oceania;;106513;747;419;constitutional monarchy;Pa'anga;TOP;676;22.6;4.9;76.2;https://www.laenderdaten.info/Ozeanien/Tonga/index.php 225 | Trinidad and Tobago;TT;North America;;1220479;5128;362;parliamentary republic;Dollar;TTD;296;13.1;8.7;72.9;https://www.laenderdaten.info/Amerika/Trinidad-und-Tobago/index.php 226 | Chad;TD;Africa;;11852462;1284000;0;Presidential republic;Franc;XAF;235;36.1;14;50.2;https://www.laenderdaten.info/Afrika/Tschad/index.php 227 | Czech Republic;CZ;Europe;;10644842;78867;0;parliamentary republic;Krone;CZK;42;9.5;10.4;78.6;https://www.laenderdaten.info/Europa/Tschechien/index.php 228 | Tunisia;TN;Africa;;11134588;163610;1148;parliamentary republic;Dinar;TND;216;16.4;6;76.1;https://www.laenderdaten.info/Afrika/Tunesien/index.php 229 | Turkey;TR;Asia;;80274604;783562;7200;parliamentary republic;Lira;TRY;90;16;5.9;74.8;https://www.laenderdaten.info/Asien/Tuerkei/index.php 230 | Turkmenistan;TM;Asia;;5291317;488100;0;Presidential republic;Manat;TMT;993;19.3;6.1;70.1;https://www.laenderdaten.info/Asien/Turkmenistan/index.php 231 | Turks and Caicos Islands;TC;North America;;51430;948;389;Parliamentary democracy (self-governing territory of the UK);Dollar;USD;1-649;15.7;3.2;79.8;https://www.laenderdaten.info/Amerika/Turks-und-Caicosinseln/index.php 232 | Tuvalu;TV;Oceania;;10959;26;24;Parliamentary democracy (under constitutional monarchy);Dollar;AUD;688;23.8;8.6;66.5;https://www.laenderdaten.info/Ozeanien/Tuvalu/index.php 233 | Uganda;UG;Africa;;38319241;241038;0;Presidential republic;Schilling;UGX;256;43.4;10.4;55.4;https://www.laenderdaten.info/Afrika/Uganda/index.php 234 | Ukraine;UA;Europe;;44209733;603550;2782;Semi-presidential republic;Hrywnja;UAH;380;10.5;14.4;71.8;https://www.laenderdaten.info/Europa/Ukraine/index.php 235 | Hungary;HU;Europe;;9874784;93028;0;parliamentary republic;Forint;HUF;36;9.1;12.8;75.9;https://www.laenderdaten.info/Europa/Ungarn/index.php 236 | Uruguay;UY;South America;;3351016;176215;660;Presidential republic;Peso;UYU;598;13;9.4;77.2;https://www.laenderdaten.info/Amerika/Uruguay/index.php 237 | Uzbekistan;UZ;Asia;;29473614;447400;0;Presidential republic;So'm;UZS;998;16.9;5.3;73.8;https://www.laenderdaten.info/Asien/Usbekistan/index.php 238 | Vanuatu;VU;Oceania;;277554;12189;2528;parliamentary republic;Vatu;VUV;678;24.5;4.1;73.4;https://www.laenderdaten.info/Ozeanien/Vanuatu/index.php 239 | Holy See (Vatican City);VA;Europe;;1000;0;0;absolute monarchy;Euro;EUR;396;0;0;0;https://www.laenderdaten.info/Europa/Vatikan/index.php 240 | Venezuela;VE;South America;;30912302;912050;2800;Federal presidential republic;Bolívar Fuerte;VEF;58;19.2;5.2;75.8;https://www.laenderdaten.info/Amerika/Venezuela/index.php 241 | United Arab Emirates;AE;Asia;;5927482;83600;1318;Federation of autonomous monarchies;Dirham;AED;971;15.3;2;77.5;https://www.laenderdaten.info/Asien/Arabische-Emirate/index.php 242 | United States;US;North America;;323995528;9826675;19924;Federal presidential republic;Dollar;USD;1;12.5;8.2;79.8;https://www.laenderdaten.info/Amerika/USA/index.php 243 | United Kingdom;GB;Europe;;64430428;243610;12429;Parliamentary constitutional monarchy;Pfund;GBP;44;12.1;9.4;80.7;https://www.laenderdaten.info/Europa/Vereinigtes-Koenigreich/index.php 244 | Vietnam;VN;Asia;;95261021;331210;3444;Republic (communist one-party system);Dong;VND;84;15.7;5.9;73.4;https://www.laenderdaten.info/Asien/Vietnam/index.php 245 | Wallis and Futuna;WF;Oceania;;15664;142;129;Parliamentary democracy (territory of France);Franc;XPF;681;13.3;5.2;79.7;https://www.laenderdaten.info/Ozeanien/Wallis-und-Futuna/index.php 246 | Christmas Island;CX;Australia;;2205;135;139;(non-self-governing territory of Australia);Dollar;AUD;6724;0;0;0;https://www.laenderdaten.info/Australien/Weihnachtsinsel/index.php 247 | Western Sahara;EH;Africa;;587020;266000;1110;republic;Dirham;MAD;;29.8;8.2;63;https://www.laenderdaten.info/Afrika/Westsahara/index.php 248 | Central African Republic;CF;Africa;;5507257;622984;0;Presidential republic;Franc;XAF;236;34.7;13.5;52.3;https://www.laenderdaten.info/Afrika/Zentralafrikanische-Republik/index.php 249 | Cyprus;CY;Asia;;1205575;9251;648;Presidential democracy;Euro;EUR;357;11.4;6.7;78.7;https://www.laenderdaten.info/Asien/Zypern/index.php 250 | -------------------------------------------------------------------------------- /kafka-node-countries/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "kafka-node-countries", 3 | "version": "0.0.2", 4 | "description": "Node.js programs to read country records from CVS file (with random delay), produce them to Kafka, consume them from Kafka, aggregate in real time and publish results to console and file", 5 | "main": "KafkaCountryProducer.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/MaartenSmeets/kafka-workshop.git" 12 | }, 13 | "keywords": [ 14 | "kafka", 15 | "node", 16 | "js", 17 | "produce", 18 | "consume" 19 | ], 20 | "author": "Lucas Jellema", 21 | "license": "ISC", 22 | "bugs": { 23 | "url": "https://github.com/MaartenSmeets/kafka-workshop/issues" 24 | }, 25 | "homepage": "https://github.com/MaartenSmeets/kafka-workshop#readme", 26 | "dependencies": { 27 | "csv-parse": "^1.2.0", 28 | "kafka-node": "^1.3.4" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /kafka-node-express-topN-sse/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "kafka-streams-result-push-sse", 3 | "version": "0.0.1", 4 | "description": "A Data API that publishes some data sources from an Oracle Database", 5 | "main": "topNreport.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [ 10 | "rest,express,sse," 11 | ], 12 | "author": "Lucas Jellema", 13 | "license": "ISC", 14 | "devDependencies": { 15 | 16 | }, 17 | "dependencies": { 18 | "express": "~4.13.4", 19 | "kafka-node": "^1.3.4" } 20 | } 21 | -------------------------------------------------------------------------------- /kafka-node-express-topN-sse/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Continent and Country Overview 6 | 7 | 8 | 50 | 51 | 52 |

53 |

Please wait...

54 |
55 |
56 | 57 | 58 |
ContinentTop 3 Countries by Size
59 |
60 | 61 | -------------------------------------------------------------------------------- /kafka-node-express-topN-sse/sse.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | console.log("loading sse.js"); 4 | 5 | // ... with this middleware: 6 | function sseMiddleware(req, res, next) { 7 | console.log(" sseMiddleware is activated with "+ req+" res: "+res); 8 | res.sseConnection = new Connection(res); 9 | console.log(" res has now connection res: "+res.sseConnection ); 10 | next(); 11 | } 12 | exports.sseMiddleware = sseMiddleware; 13 | /** 14 | * A Connection is a simple SSE manager for 1 client. 15 | */ 16 | var Connection = (function () { 17 | function Connection(res) { 18 | console.log(" sseMiddleware construct connection for response "); 19 | 20 | this.res = res; 21 | } 22 | Connection.prototype.setup = function () { 23 | console.log("set up SSE stream for response"); 24 | this.res.writeHead(200, { 25 | 'Content-Type': 'text/event-stream', 26 | 'Cache-Control': 'no-cache', 27 | 'Connection': 'keep-alive' 28 | }); 29 | }; 30 | Connection.prototype.send = function (data) { 31 | console.log("send event to SSE stream "+JSON.stringify(data)); 32 | this.res.write("data: " + JSON.stringify(data) + "\n\n"); 33 | }; 34 | return Connection; 35 | }()); 36 | 37 | exports.Connection = Connection; 38 | /** 39 | * A Topic handles a bundle of connections with cleanup after lost connection. 40 | */ 41 | var Topic = (function () { 42 | function Topic() { 43 | console.log(" constructor for Topic"); 44 | 45 | this.connections = []; 46 | } 47 | Topic.prototype.add = function (conn) { 48 | var connections = this.connections; 49 | connections.push(conn); 50 | console.log('New client connected, now: ', connections.length); 51 | conn.res.on('close', function () { 52 | var i = connections.indexOf(conn); 53 | if (i >= 0) { 54 | connections.splice(i, 1); 55 | } 56 | console.log('Client disconnected, now: ', connections.length); 57 | }); 58 | }; 59 | Topic.prototype.forEach = function (cb) { 60 | this.connections.forEach(cb); 61 | }; 62 | return Topic; 63 | }()); 64 | exports.Topic = Topic; 65 | -------------------------------------------------------------------------------- /kafka-node-express-topN-sse/topNreport.js: -------------------------------------------------------------------------------- 1 | /* 2 | This program serves a static HTML file (through the Express framework on top of Node). The browser that loads this HTML document registers itself as an SSE client with this program. 3 | 4 | This program consumes Kafka messages from topic Top3CountrySizePerContinent to which the Running Top3 (size of countries by continent) is produced. 5 | 6 | This program reports to all its SSE clients the latest update (or potentially a periodice top 3 largest countries per continent (with a configurable interval)) 7 | 8 | 9 | */ 10 | 11 | var express = require('express') 12 | , http = require('http') 13 | , sseMW = require('./sse'); 14 | 15 | var kafka = require('kafka-node') 16 | var Consumer = kafka.Consumer 17 | var client = new kafka.Client("ubuntu:2181/") 18 | var countriesTopic = "Top3CountrySizePerContinent"; 19 | 20 | var app = express(); 21 | var server = http.createServer(app); 22 | 23 | var PORT = process.env.PORT || 3000; 24 | var APP_VERSION = '0.0.4.06'; 25 | 26 | server.listen(PORT, function () { 27 | console.log('Server running, version '+APP_VERSION+', Express is listening... at '+PORT+" "); 28 | }); 29 | 30 | // Realtime updates 31 | var sseClients = new sseMW.Topic(); 32 | 33 | 34 | app.use(express.static(__dirname + '/public')) 35 | app.get('/about', function (req, res) { 36 | res.writeHead(200, {'Content-Type': 'text/html'}); 37 | res.write("Version "+APP_VERSION+". No Data Requested, so none is returned"); 38 | res.write("Supported URLs:"); 39 | res.write("/public , /public/index.html "); 40 | res.write("incoming headers" + JSON.stringify(req.headers)); 41 | res.end(); 42 | }); 43 | app.use(sseMW.sseMiddleware) 44 | 45 | // initial registration of SSE Client Connection 46 | app.get('/topn/updates', function(req,res){ 47 | var sseConnection = res.sseConnection; 48 | sseConnection.setup(); 49 | sseClients.add(sseConnection); 50 | } ); 51 | 52 | 53 | var m; 54 | //send message to all registered SSE clients 55 | updateSseClients = function(message) { 56 | var msg = message; 57 | this.m=message; 58 | sseClients.forEach( 59 | function(sseConnection) { 60 | sseConnection.send(this.m); 61 | } 62 | , this // this second argument to forEach is the thisArg (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) 63 | ); //forEach 64 | }// updateSseClients 65 | 66 | // send a heartbeat signal to all SSE clients, once every interval seconds (or every 3 seconds if no interval is specified) 67 | initHeartbeat = function(interval) { 68 | setInterval(function() { 69 | var msg = {"label":"The latest", "time":new Date()}; 70 | updateSseClients( JSON.stringify(msg)); 71 | }//interval function 72 | , interval?interval*1000:3000 73 | ); // setInterval 74 | }//initHeartbeat 75 | 76 | // initialize heartbeat at 10 second interval 77 | initHeartbeat(10); 78 | 79 | 80 | var consumer = new Consumer( 81 | client, 82 | [], 83 | {fromOffset: true} 84 | ); 85 | 86 | consumer.on('message', function (message) { 87 | handleCountryMessage(message); 88 | }); 89 | 90 | consumer.addTopics([ 91 | { topic: countriesTopic, partition: 0, offset: 0} 92 | ], () => console.log("topic "+countriesTopic+" added to consumer for listening")); 93 | 94 | function handleCountryMessage(countryMessage) { 95 | var top3 = JSON.parse(countryMessage.value); 96 | var continent = new Buffer(countryMessage.key).toString('ascii'); 97 | top3.continent = continent; 98 | updateSseClients( top3); 99 | }// handleCountryMessage 100 | -------------------------------------------------------------------------------- /kafka-node-mongodb-topN/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "kafka-node-mondgodb-topn", 3 | "version": "0.0.1", 4 | "description": "A Kafka Topic Consumer and Mongodb writer", 5 | "main": "topNmongodbWriter.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [ 10 | "mongodb,kafka" 11 | ], 12 | "author": "Lucas Jellema", 13 | "license": "ISC", 14 | "devDependencies": {}, 15 | "dependencies": { 16 | "kafka-node": "^1.3.4", 17 | "mongodb": "^2.2.24" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /kafka-node-mongodb-topN/topNKafkaConsumerMongoDBWriter.js: -------------------------------------------------------------------------------- 1 | /* 2 | This program connects to MongoDB (using the mongodb module ) 3 | This program consumes Kafka messages from topic Top3CountrySizePerContinent to which the Running Top3 (size of countries by continent) is produced. 4 | 5 | This program records each latest update of the top 3 largest countries for a continent in MongoDB. If a document does not yet exist for a continent (based on the key which is the continent property) it is inserted. 6 | 7 | The program ensures that the MongoDB /test/top3 collection contains the latest Top 3 for each continent at any point in time. 8 | 9 | */ 10 | 11 | var MongoClient = require('mongodb').MongoClient; 12 | var assert = require('assert'); 13 | 14 | var kafka = require('kafka-node') 15 | var Consumer = kafka.Consumer 16 | var client = new kafka.Client("ubuntu:2181/") 17 | var countriesTopic = "Top3CountrySizePerContinent"; 18 | 19 | 20 | // connect string for mongodb server running locally, connecting to a database called test 21 | var url = 'mongodb://127.0.0.1:27017/test'; 22 | var mongodb; 23 | 24 | MongoClient.connect(url, function(err, db) { 25 | assert.equal(null, err); 26 | console.log("Connected correctly to MongoDB server."); 27 | mongodb = db; 28 | }); 29 | 30 | var insertDocument = function(db, doc, callback) { 31 | // first try to update; if a document could be updated, we're done 32 | updateTop3ForContinent( db, doc, function (results) { 33 | if (!results || results.result.n == 0) { 34 | // the document was not updated so presumably it does not exist; let's insert it 35 | db.collection('top3').insertOne( 36 | doc 37 | , function(err, result) { 38 | assert.equal(err, null); 39 | console.log("Inserted doc for "+doc.continent); 40 | callback(); 41 | } 42 | ); 43 | }//if 44 | else { 45 | console.log("Updated doc for "+doc.continent); 46 | callback(); 47 | } 48 | }); //updateTop3ForContinent 49 | }; //insertDocument 50 | 51 | var updateTop3ForContinent = function(db, top3 , callback) { 52 | db.collection('top3').updateOne( 53 | { "continent" : top3.continent }, 54 | { 55 | $set: { "nrs": top3.nrs }, 56 | $currentDate: { "lastModified": true } 57 | }, function(err, results) { 58 | //console.log(results); 59 | callback(results); 60 | }); 61 | }; 62 | 63 | // Configure Kafka Consumer for Kafka Top3 Topic and handle Kafka message (by calling updateSseClients) 64 | var consumer = new Consumer( 65 | client, 66 | [], 67 | {fromOffset: true} 68 | ); 69 | 70 | consumer.on('message', function (message) { 71 | handleCountryMessage(message); 72 | }); 73 | 74 | consumer.addTopics([ 75 | { topic: countriesTopic, partition: 0, offset: 0} 76 | ], () => console.log("topic "+countriesTopic+" added to consumer for listening")); 77 | 78 | function handleCountryMessage(countryMessage) { 79 | var top3 = JSON.parse(countryMessage.value); 80 | var continent = new Buffer(countryMessage.key).toString('ascii'); 81 | top3.continent = continent; 82 | // insert or update the top3 in the MongoDB server 83 | insertDocument(mongodb,top3, function() { 84 | console.log("Top3 recorded in MongoDB for "+top3.continent); 85 | }); 86 | 87 | }// handleCountryMessage 88 | -------------------------------------------------------------------------------- /kafka-node-mongodb-topN/topNmongodbWriter.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Simple application that connects to MongoDB (using the mongodb module ) and then updates two documents in the top3 collection in the test database; if a document does not yet exist (based on the key which is the continent property) it is inserted. 4 | When the application is done running, two documents exist (and have their lastModified property set if they were updated). 5 | 6 | */ 7 | 8 | var MongoClient = require('mongodb').MongoClient; 9 | var assert = require('assert'); 10 | 11 | // connect string for mongodb server running locally, connecting to a database called test 12 | var url = 'mongodb://127.0.0.1:27017/test'; 13 | 14 | MongoClient.connect(url, function(err, db) { 15 | assert.equal(null, err); 16 | console.log("Connected correctly to server."); 17 | var doc = { 18 | "continent" : "Europe", 19 | "nrs" : [ {"name":"Belgium"}, {"name":"Luxemburg"}] 20 | }; 21 | var doc2 = { 22 | "continent" : "Asia", 23 | "nrs" : [ {"name":"China"}, {"name":"India"}] 24 | }; 25 | insertDocument(db,doc, function() { 26 | console.log("returned from processing doc "+doc.continent); 27 | insertDocument(db,doc2, function() { 28 | console.log("returned from processing doc "+doc2.continent); 29 | db.close(); 30 | console.log("Connection to database is closed. Two documents should exist, either just created or updated. "); 31 | console.log("From the MongoDB shell: db.top3.find() should list the documents. "); 32 | }); 33 | }); 34 | }); 35 | 36 | var insertDocument = function(db, doc, callback) { 37 | // first try to update; if a document could be updated, we're done 38 | console.log("Processing doc for "+doc.continent); 39 | updateTop3ForContinent( db, doc, function (results) { 40 | if (!results || results.result.n == 0) { 41 | // the document was not updated so presumably it does not exist; let's insert it 42 | db.collection('top3').insertOne( 43 | doc 44 | , function(err, result) { 45 | assert.equal(err, null); 46 | callback(); 47 | } 48 | ); 49 | }//if 50 | else { 51 | callback(); 52 | } 53 | }); //updateTop3ForContinent 54 | }; //insertDocument 55 | 56 | var updateTop3ForContinent = function(db, top3 , callback) { 57 | db.collection('top3').updateOne( 58 | { "continent" : top3.continent }, 59 | { 60 | $set: { "nrs": top3.nrs }, 61 | $currentDate: { "lastModified": true } 62 | }, function(err, results) { 63 | //console.log(results); 64 | callback(results); 65 | }); 66 | }; 67 | --------------------------------------------------------------------------------