Skip to content

Commit 4d423a7

Browse files
authored
[#2612] feat(client): Add Java client support for messaging catalog (#2722)
### What changes were proposed in this pull request? Add Java client support for the messaging catalog ### Why are the changes needed? Fix: #2612 ### Does this PR introduce _any_ user-facing change? Add Java client support for the messaging catalog ### How was this patch tested? UTs
1 parent a1c1d53 commit 4d423a7

File tree

9 files changed

+641
-3
lines changed

9 files changed

+641
-3
lines changed

api/src/main/java/com/datastrato/gravitino/Catalog.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ enum Type {
2727
FILESET,
2828

2929
/** Catalog Type for Message Queue, like kafka://topic */
30-
MESSAGING
30+
MESSAGING,
31+
32+
/** Catalog Type for test only. */
33+
UNSUPPORTED
3134
}
3235

3336
/**

api/src/main/java/com/datastrato/gravitino/NameIdentifier.java

+11
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,17 @@ public static void checkFileset(NameIdentifier ident) {
185185
Namespace.checkFileset(ident.namespace);
186186
}
187187

188+
/**
189+
* Check the given {@link NameIdentifier} is a topic identifier. Throw an {@link
190+
* IllegalNameIdentifierException} if it's not.
191+
*
192+
* @param ident The topic {@link NameIdentifier} to check.
193+
*/
194+
public static void checkTopic(NameIdentifier ident) {
195+
check(ident != null, "Topic identifier must not be null");
196+
Namespace.checkTopic(ident.namespace);
197+
}
198+
188199
/**
189200
* Create a {@link NameIdentifier} from the given identifier string.
190201
*

api/src/main/java/com/datastrato/gravitino/Namespace.java

+13
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,19 @@ public static void checkFileset(Namespace namespace) {
182182
namespace);
183183
}
184184

185+
/**
186+
* Check if the given topic namespace is legal, throw an {@link IllegalNamespaceException} if it's
187+
* illegal.
188+
*
189+
* @param namespace The topic namespace
190+
*/
191+
public static void checkTopic(Namespace namespace) {
192+
check(
193+
namespace != null && namespace.length() == 3,
194+
"Topic namespace must be non-null and have 3 levels, the input namespace is %s",
195+
namespace);
196+
}
197+
185198
private Namespace(String[] levels) {
186199
this.levels = levels;
187200
}

clients/client-java/src/main/java/com/datastrato/gravitino/client/DTOConverters.java

+28
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
import com.datastrato.gravitino.dto.requests.MetalakeUpdateRequest;
1616
import com.datastrato.gravitino.dto.requests.SchemaUpdateRequest;
1717
import com.datastrato.gravitino.dto.requests.TableUpdateRequest;
18+
import com.datastrato.gravitino.dto.requests.TopicUpdateRequest;
1819
import com.datastrato.gravitino.file.FilesetChange;
20+
import com.datastrato.gravitino.messaging.TopicChange;
1921
import com.datastrato.gravitino.rel.SchemaChange;
2022
import com.datastrato.gravitino.rel.TableChange;
2123

@@ -82,6 +84,15 @@ static Catalog toCatalog(CatalogDTO catalog, RESTClient client) {
8284
.build();
8385

8486
case MESSAGING:
87+
return MessagingCatalog.builder()
88+
.withName(catalog.name())
89+
.withType(catalog.type())
90+
.withProvider(catalog.provider())
91+
.withComment(catalog.comment())
92+
.withProperties(catalog.properties())
93+
.withAudit((AuditDTO) catalog.auditInfo())
94+
.withRestClient(client)
95+
.build();
8596
default:
8697
throw new UnsupportedOperationException("Unsupported catalog type: " + catalog.type());
8798
}
@@ -183,6 +194,23 @@ static FilesetUpdateRequest toFilesetUpdateRequest(FilesetChange change) {
183194
}
184195
}
185196

197+
static TopicUpdateRequest toTopicUpdateRequest(TopicChange change) {
198+
if (change instanceof TopicChange.UpdateTopicComment) {
199+
return new TopicUpdateRequest.UpdateTopicCommentRequest(
200+
((TopicChange.UpdateTopicComment) change).getNewComment());
201+
} else if (change instanceof TopicChange.SetProperty) {
202+
return new TopicUpdateRequest.SetTopicPropertyRequest(
203+
((TopicChange.SetProperty) change).getProperty(),
204+
((TopicChange.SetProperty) change).getValue());
205+
} else if (change instanceof TopicChange.RemoveProperty) {
206+
return new TopicUpdateRequest.RemoveTopicPropertyRequest(
207+
((TopicChange.RemoveProperty) change).getProperty());
208+
} else {
209+
throw new IllegalArgumentException(
210+
"Unknown change type: " + change.getClass().getSimpleName());
211+
}
212+
}
213+
186214
private static TableUpdateRequest toColumnUpdateRequest(TableChange.ColumnChange change) {
187215
if (change instanceof TableChange.AddColumn) {
188216
TableChange.AddColumn addColumn = (TableChange.AddColumn) change;

clients/client-java/src/main/java/com/datastrato/gravitino/client/ErrorHandlers.java

+46
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
import com.datastrato.gravitino.exceptions.NoSuchPartitionException;
1818
import com.datastrato.gravitino.exceptions.NoSuchSchemaException;
1919
import com.datastrato.gravitino.exceptions.NoSuchTableException;
20+
import com.datastrato.gravitino.exceptions.NoSuchTopicException;
2021
import com.datastrato.gravitino.exceptions.NonEmptySchemaException;
2122
import com.datastrato.gravitino.exceptions.NotFoundException;
2223
import com.datastrato.gravitino.exceptions.PartitionAlreadyExistsException;
2324
import com.datastrato.gravitino.exceptions.RESTException;
2425
import com.datastrato.gravitino.exceptions.SchemaAlreadyExistsException;
2526
import com.datastrato.gravitino.exceptions.TableAlreadyExistsException;
27+
import com.datastrato.gravitino.exceptions.TopicAlreadyExistsException;
2628
import com.datastrato.gravitino.exceptions.UnauthorizedException;
2729
import com.fasterxml.jackson.databind.ObjectMapper;
2830
import com.google.common.base.Joiner;
@@ -112,6 +114,15 @@ public static Consumer<ErrorResponse> filesetErrorHandler() {
112114
return FilesetErrorHandler.INSTANCE;
113115
}
114116

117+
/**
118+
* Creates an error handler specific to Topic operations.
119+
*
120+
* @return A Consumer representing the Topic error handler.
121+
*/
122+
public static Consumer<ErrorResponse> topicErrorHandler() {
123+
return TopicErrorHandler.INSTANCE;
124+
}
125+
115126
private ErrorHandlers() {}
116127

117128
/**
@@ -410,6 +421,41 @@ public void accept(ErrorResponse errorResponse) {
410421
}
411422
}
412423

424+
/** Error handler specific to Topic operations. */
425+
@SuppressWarnings("FormatStringAnnotation")
426+
private static class TopicErrorHandler extends RestErrorHandler {
427+
428+
private static final TopicErrorHandler INSTANCE = new TopicErrorHandler();
429+
430+
@Override
431+
public void accept(ErrorResponse errorResponse) {
432+
String errorMessage = formatErrorMessage(errorResponse);
433+
434+
switch (errorResponse.getCode()) {
435+
case ErrorConstants.ILLEGAL_ARGUMENTS_CODE:
436+
throw new IllegalArgumentException(errorMessage);
437+
438+
case ErrorConstants.NOT_FOUND_CODE:
439+
if (errorResponse.getType().equals(NoSuchSchemaException.class.getSimpleName())) {
440+
throw new NoSuchSchemaException(errorMessage);
441+
} else if (errorResponse.getType().equals(NoSuchTopicException.class.getSimpleName())) {
442+
throw new NoSuchTopicException(errorMessage);
443+
} else {
444+
throw new NotFoundException(errorMessage);
445+
}
446+
447+
case ErrorConstants.ALREADY_EXISTS_CODE:
448+
throw new TopicAlreadyExistsException(errorMessage);
449+
450+
case ErrorConstants.INTERNAL_ERROR_CODE:
451+
throw new RuntimeException(errorMessage);
452+
453+
default:
454+
super.accept(errorResponse);
455+
}
456+
}
457+
}
458+
413459
/** Generic error handler for REST requests. */
414460
private static class RestErrorHandler extends ErrorHandler {
415461
private static final ErrorHandler INSTANCE = new RestErrorHandler();

clients/client-java/src/main/java/com/datastrato/gravitino/client/FilesetCatalog.java

+1
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ public Fileset alterFileset(NameIdentifier ident, FilesetChange... changes)
164164
.map(DTOConverters::toFilesetUpdateRequest)
165165
.collect(Collectors.toList());
166166
FilesetUpdatesRequest req = new FilesetUpdatesRequest(updates);
167+
req.validate();
167168

168169
FilesetResponse resp =
169170
restClient.put(

0 commit comments

Comments
 (0)