Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search by Kafka message key #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
<spring.boot.version>1.3.6.RELEASE</spring.boot.version>
<additionalparam>-Xdoclint:none</additionalparam>
<curator.version>2.10.0</curator.version>

<kafka-client.version>0.10.2.0</kafka-client.version>
<scala.version>2.11</scala.version>
<spark.version>2.3.0</spark.version>
<!-- name of parameter changed in latest mvn javadoc plugin version-->
<additionalOptions>-Xdoclint:none</additionalOptions>

Expand Down Expand Up @@ -71,7 +73,7 @@
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.9.2</artifactId>
<artifactId>kafka_${scala.version}</artifactId>
<version>0.8.2.2</version>
</dependency>
<dependency>
Expand Down Expand Up @@ -109,6 +111,36 @@
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<!-- spark dependency -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-streaming-kafka-0-10 -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming-kafka-0-10_${scala.version}</artifactId>
<version>${spark.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-streaming -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_${scala.version}</artifactId>
<version>${spark.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_${scala.version}</artifactId>
<version>${spark.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-sql -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_${scala.version}</artifactId>
<version>${spark.version}</version>
</dependency>

<!-- Spring Boot -->
<dependency>
Expand Down Expand Up @@ -336,7 +368,7 @@
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.9.2</artifactId>
<artifactId>kafka_${scala.version}</artifactId>
<version>0.8.2.2</version>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.homeadvisor.kafdrop.config;

import javax.annotation.PostConstruct;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import com.homeadvisor.kafdrop.util.SearchType;;


@Configuration
public class MessageConfiguration {

@Component
@ConfigurationProperties(prefix = "message")
public static class MessageProperties
{

private SearchType type;
private static String consumerName = "kafka-test-ARRTP";

@PostConstruct
public void init() {
// Set a default message format if not configured.
if (type == null) {
type = SearchType.Offset;
}
}

public SearchType getType()
{
return type;
}

public void setType(SearchType type)
{
this.type = type;
}

public String getConsumerName()
{
return this.consumerName;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.homeadvisor.kafdrop.config.MessageFormatConfiguration;
import com.homeadvisor.kafdrop.config.SchemaRegistryConfiguration;
import com.homeadvisor.kafdrop.config.MessageConfiguration;
import com.homeadvisor.kafdrop.model.MessageVO;
import com.homeadvisor.kafdrop.model.TopicVO;
import com.homeadvisor.kafdrop.service.KafkaMonitor;
import com.homeadvisor.kafdrop.service.MessageInspector;
import com.homeadvisor.kafdrop.service.SparkMessageInspector;
import com.homeadvisor.kafdrop.service.TopicNotFoundException;
import com.homeadvisor.kafdrop.util.AvroMessageDeserializer;
import com.homeadvisor.kafdrop.util.DefaultMessageDeserializer;
import com.homeadvisor.kafdrop.util.MessageDeserializer;
import com.homeadvisor.kafdrop.util.MessageFormat;
import com.homeadvisor.kafdrop.util.SearchType;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
Expand Down Expand Up @@ -58,12 +61,18 @@ public class MessageController
@Autowired
private MessageInspector messageInspector;

@Autowired
private SparkMessageInspector sparkMessageInspector;

@Autowired
private MessageFormatConfiguration.MessageFormatProperties messageFormatProperties;

@Autowired
private SchemaRegistryConfiguration.SchemaRegistryProperties schemaRegistryProperties;

@Autowired
private MessageConfiguration.MessageProperties searchTypeProperties;

/**
* Human friendly view of reading messages.
* @param topicName Name of topic
Expand All @@ -79,7 +88,7 @@ public String viewMessageForm(@PathVariable("name") String topicName,
Model model)
{
final MessageFormat defaultFormat = messageFormatProperties.getFormat();

final SearchType defaultSearchType = searchTypeProperties.getType();
if (messageForm.isEmpty())
{
final PartitionOffsetInfo defaultForm = new PartitionOffsetInfo();
Expand All @@ -98,21 +107,32 @@ public String viewMessageForm(@PathVariable("name") String topicName,

model.addAttribute("defaultFormat", defaultFormat);
model.addAttribute("messageFormats", MessageFormat.values());

if (!messageForm.isEmpty() && !errors.hasErrors())
{
final MessageDeserializer deserializer = getDeserializer(
topicName, messageForm.getFormat());

model.addAttribute("messages",
messageInspector.getMessages(topicName,
messageForm.getPartition(),
messageForm.getOffset(),
messageForm.getCount(),
deserializer));

model.addAttribute("defaultSearchType", defaultSearchType);
model.addAttribute("searchType", SearchType.values());

if (messageForm.getSearchby() != null && messageForm.getSearchby().name() == SearchType.Key.toString()) {
if (!messageForm.isEmptyMessageKey() && !errors.hasErrors())
{
model.addAttribute("messages",
sparkMessageInspector.getMessagesByKey(topicName,
messageForm.getMessageKey(),
messageForm.getCount()));
}
} else {
if (!messageForm.isEmpty() && !errors.hasErrors())
{
final MessageDeserializer deserializer = getDeserializer(
topicName, messageForm.getFormat());

model.addAttribute("messages",
messageInspector.getMessages(topicName,
messageForm.getPartition(),
messageForm.getOffset(),
messageForm.getCount(),
deserializer));

}
}

return "message-inspector";
}

Expand Down Expand Up @@ -207,23 +227,24 @@ public static class PartitionOffsetInfo
*/
@NotNull
@Min(1)
@Max(100)
@JsonProperty("lastOffset")
private Long count;

private MessageFormat format;

public PartitionOffsetInfo(int partition, long offset, long count, MessageFormat format)
private SearchType searchby;
private String messageKey;
public PartitionOffsetInfo(int partition, long offset, long count, MessageFormat format, SearchType searchby)
{
this.partition = partition;
this.offset = offset;
this.count = count;
this.format = format;
this.searchby = searchby;
}

public PartitionOffsetInfo(int partition, long offset, long count)
{
this(partition, offset, count, MessageFormat.DEFAULT);
this(partition, offset, count, MessageFormat.DEFAULT, SearchType.Offset);
}

public PartitionOffsetInfo()
Expand All @@ -236,7 +257,10 @@ public boolean isEmpty()
{
return partition == null && offset == null && (count == null || count == 1);
}

public boolean isEmptyMessageKey()
{
return messageKey == null && (count == null || count == 0);
}
public Integer getPartition()
{
return partition;
Expand Down Expand Up @@ -276,5 +300,23 @@ public void setFormat(MessageFormat format)
{
this.format = format;
}
public String getMessageKey()
{
return this.messageKey;
}
public void setMessageKey(String messageKey)
{
this.messageKey = messageKey;
}

public SearchType getSearchby()
{
return searchby;
}

public void setSearchby(SearchType searchby)
{
this.searchby = searchby;
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/homeadvisor/kafdrop/model/MessageVO.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

package com.homeadvisor.kafdrop.model;

public class MessageVO
public class MessageVO implements java.io.Serializable
{
private String message;
private String key;
Expand Down
Loading