Merge remote-tracking branch 'upstream/master'
This commit is contained in:
commit
a84c742aa7
|
@ -1,38 +1,29 @@
|
||||||
package com.baeldung.decimalformat;
|
package com.baeldung.decimalformat;
|
||||||
|
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
import java.text.NumberFormat;
|
import java.text.NumberFormat;
|
||||||
import java.util.Locale;
|
|
||||||
|
|
||||||
public class DoubletoString {
|
public class DoubletoString {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
double doubleValue = 345.56;
|
double doubleValue = 345.56;
|
||||||
|
|
||||||
System.out.println(String.valueOf((int) doubleValue));
|
System.out.println(String.valueOf((int) doubleValue));
|
||||||
|
|
||||||
System.out.println(String.format("%.0f", doubleValue));
|
System.out.println(String.format("%.0f", doubleValue));
|
||||||
|
|
||||||
doubleValue = Math.floor(doubleValue);
|
NumberFormat nf = NumberFormat.getInstance();
|
||||||
DecimalFormat df = new DecimalFormat("#");
|
nf.setMaximumFractionDigits(0);
|
||||||
df.setRoundingMode(RoundingMode.FLOOR);
|
nf.setRoundingMode(RoundingMode.FLOOR);
|
||||||
System.out.println(df.format(doubleValue));
|
System.out.println(nf.format(doubleValue));
|
||||||
|
|
||||||
Locale enlocale = new Locale("en", "US");
|
|
||||||
String pattern = "###,##";
|
|
||||||
df = (DecimalFormat) NumberFormat.getNumberInstance(enlocale);
|
|
||||||
df.applyPattern(pattern);
|
|
||||||
String format = df.format(doubleValue);
|
|
||||||
System.out.println(format);
|
|
||||||
|
|
||||||
Locale dalocale = new Locale("da", "DK");
|
doubleValue = Math.floor(doubleValue);
|
||||||
df = (DecimalFormat) NumberFormat.getNumberInstance(dalocale);
|
DecimalFormat df = new DecimalFormat("#,###");
|
||||||
df.applyPattern(pattern);
|
df.setRoundingMode(RoundingMode.FLOOR);
|
||||||
System.out.println(df.format(doubleValue));
|
System.out.println(df.format(doubleValue));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
|
@ -933,7 +933,7 @@
|
||||||
<caffeine.version>2.5.5</caffeine.version>
|
<caffeine.version>2.5.5</caffeine.version>
|
||||||
<google-api.version>1.23.0</google-api.version>
|
<google-api.version>1.23.0</google-api.version>
|
||||||
<google-sheets.version>v4-rev493-1.21.0</google-sheets.version>
|
<google-sheets.version>v4-rev493-1.21.0</google-sheets.version>
|
||||||
<kafka.version>1.0.0</kafka.version>
|
<kafka.version>2.0.0</kafka.version>
|
||||||
<smooks.version>1.7.0</smooks.version>
|
<smooks.version>1.7.0</smooks.version>
|
||||||
<docker.version>3.0.14</docker.version>
|
<docker.version>3.0.14</docker.version>
|
||||||
<async.http.client.version>2.2.0</async.http.client.version>
|
<async.http.client.version>2.2.0</async.http.client.version>
|
||||||
|
|
|
@ -0,0 +1,102 @@
|
||||||
|
package com.baeldung.kafka;
|
||||||
|
|
||||||
|
import org.apache.kafka.clients.consumer.ConsumerRecord;
|
||||||
|
import org.apache.kafka.clients.consumer.ConsumerRecords;
|
||||||
|
import org.apache.kafka.clients.consumer.KafkaConsumer;
|
||||||
|
import org.apache.kafka.clients.consumer.OffsetAndMetadata;
|
||||||
|
import org.apache.kafka.clients.producer.KafkaProducer;
|
||||||
|
import org.apache.kafka.clients.producer.ProducerConfig;
|
||||||
|
import org.apache.kafka.clients.producer.ProducerRecord;
|
||||||
|
import org.apache.kafka.common.KafkaException;
|
||||||
|
import org.apache.kafka.common.TopicPartition;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import static java.time.Duration.ofSeconds;
|
||||||
|
import static java.util.Collections.singleton;
|
||||||
|
import static org.apache.kafka.clients.consumer.ConsumerConfig.*;
|
||||||
|
import static org.apache.kafka.clients.consumer.ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG;
|
||||||
|
import static org.apache.kafka.clients.producer.ProducerConfig.*;
|
||||||
|
|
||||||
|
public class TransactionalApp {
|
||||||
|
|
||||||
|
private static final String CONSUMER_GROUP_ID = "test";
|
||||||
|
private static final String OUTPUT_TOPIC = "output";
|
||||||
|
private static final String INPUT_TOPIC = "input";
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
KafkaConsumer<String, String> consumer = initConsumer();
|
||||||
|
KafkaProducer<String, String> producer = initProducer();
|
||||||
|
|
||||||
|
producer.initTransactions();
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
|
||||||
|
ConsumerRecords<String, String> records = consumer.poll(ofSeconds(20));
|
||||||
|
|
||||||
|
producer.beginTransaction();
|
||||||
|
|
||||||
|
for (ConsumerRecord record : records)
|
||||||
|
producer.send(new ProducerRecord(OUTPUT_TOPIC, record));
|
||||||
|
|
||||||
|
Map<TopicPartition, OffsetAndMetadata> offsetsToCommit = new HashMap<>();
|
||||||
|
|
||||||
|
for (TopicPartition partition : records.partitions()) {
|
||||||
|
List<ConsumerRecord<String, String>> partitionedRecords = records.records(partition);
|
||||||
|
long offset = partitionedRecords.get(partitionedRecords.size() - 1).offset();
|
||||||
|
|
||||||
|
offsetsToCommit.put(partition, new OffsetAndMetadata(offset));
|
||||||
|
}
|
||||||
|
|
||||||
|
producer.sendOffsetsToTransaction(offsetsToCommit, CONSUMER_GROUP_ID);
|
||||||
|
producer.commitTransaction();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (KafkaException e) {
|
||||||
|
|
||||||
|
producer.abortTransaction();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static KafkaConsumer<String, String> initConsumer() {
|
||||||
|
Properties props = new Properties();
|
||||||
|
props.put(BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
|
||||||
|
props.put(GROUP_ID_CONFIG, CONSUMER_GROUP_ID);
|
||||||
|
props.put(ENABLE_AUTO_COMMIT_CONFIG, "false");
|
||||||
|
props.put(KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
|
||||||
|
props.put(VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
|
||||||
|
|
||||||
|
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
|
||||||
|
consumer.subscribe(singleton(INPUT_TOPIC));
|
||||||
|
return consumer;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static KafkaProducer<String, String> initProducer() {
|
||||||
|
|
||||||
|
Properties props = new Properties();
|
||||||
|
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
|
||||||
|
props.put(ACKS_CONFIG, "all");
|
||||||
|
props.put(RETRIES_CONFIG, 3);
|
||||||
|
props.put(BATCH_SIZE_CONFIG, 16384);
|
||||||
|
props.put(LINGER_MS_CONFIG, 1);
|
||||||
|
props.put(BUFFER_MEMORY_CONFIG, 33554432);
|
||||||
|
props.put(ENABLE_IDEMPOTENCE_CONFIG, "true");
|
||||||
|
props.put(TRANSACTIONAL_ID_CONFIG, "prod-1");
|
||||||
|
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
|
||||||
|
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
|
||||||
|
|
||||||
|
return new KafkaProducer(props);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -4,10 +4,12 @@ import org.apache.kafka.clients.consumer.ConsumerConfig;
|
||||||
import org.apache.kafka.common.serialization.Serde;
|
import org.apache.kafka.common.serialization.Serde;
|
||||||
import org.apache.kafka.common.serialization.Serdes;
|
import org.apache.kafka.common.serialization.Serdes;
|
||||||
import org.apache.kafka.streams.KafkaStreams;
|
import org.apache.kafka.streams.KafkaStreams;
|
||||||
|
import org.apache.kafka.streams.StreamsBuilder;
|
||||||
import org.apache.kafka.streams.StreamsConfig;
|
import org.apache.kafka.streams.StreamsConfig;
|
||||||
|
import org.apache.kafka.streams.Topology;
|
||||||
import org.apache.kafka.streams.kstream.KStream;
|
import org.apache.kafka.streams.kstream.KStream;
|
||||||
import org.apache.kafka.streams.kstream.KStreamBuilder;
|
|
||||||
import org.apache.kafka.streams.kstream.KTable;
|
import org.apache.kafka.streams.kstream.KTable;
|
||||||
|
import org.apache.kafka.streams.kstream.Produced;
|
||||||
import org.apache.kafka.test.TestUtils;
|
import org.apache.kafka.test.TestUtils;
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -36,20 +38,20 @@ public class KafkaStreamsLiveTest {
|
||||||
streamsConfiguration.put(StreamsConfig.STATE_DIR_CONFIG, TestUtils.tempDirectory().getAbsolutePath());
|
streamsConfiguration.put(StreamsConfig.STATE_DIR_CONFIG, TestUtils.tempDirectory().getAbsolutePath());
|
||||||
|
|
||||||
// when
|
// when
|
||||||
KStreamBuilder builder = new KStreamBuilder();
|
StreamsBuilder builder = new StreamsBuilder();
|
||||||
KStream<String, String> textLines = builder.stream(inputTopic);
|
KStream<String, String> textLines = builder.stream(inputTopic);
|
||||||
Pattern pattern = Pattern.compile("\\W+", Pattern.UNICODE_CHARACTER_CLASS);
|
Pattern pattern = Pattern.compile("\\W+", Pattern.UNICODE_CHARACTER_CLASS);
|
||||||
|
|
||||||
KTable<String, Long> wordCounts = textLines.flatMapValues(value -> Arrays.asList(pattern.split(value.toLowerCase()))).groupBy((key, word) -> word).count();
|
KTable<String, Long> wordCounts = textLines.flatMapValues(value -> Arrays.asList(pattern.split(value.toLowerCase()))).groupBy((key, word) -> word).count();
|
||||||
|
|
||||||
wordCounts.foreach((word, count) -> System.out.println("word: " + word + " -> " + count));
|
textLines.foreach((word, count) -> System.out.println("word: " + word + " -> " + count));
|
||||||
|
|
||||||
String outputTopic = "outputTopic";
|
String outputTopic = "outputTopic";
|
||||||
final Serde<String> stringSerde = Serdes.String();
|
final Serde<String> stringSerde = Serdes.String();
|
||||||
final Serde<Long> longSerde = Serdes.Long();
|
final Serde<String> longSerde = Serdes.String();
|
||||||
wordCounts.to(stringSerde, longSerde, outputTopic);
|
textLines.to(outputTopic, Produced.with(stringSerde,longSerde));
|
||||||
|
|
||||||
KafkaStreams streams = new KafkaStreams(builder, streamsConfiguration);
|
KafkaStreams streams = new KafkaStreams(new Topology(), streamsConfiguration);
|
||||||
streams.start();
|
streams.start();
|
||||||
|
|
||||||
// then
|
// then
|
||||||
|
|
Loading…
Reference in New Issue