Skip to content

Commit

Permalink
add SchemaSerializer and IdentifierSerializer to to support json format
Browse files Browse the repository at this point in the history
  • Loading branch information
jerry-024 committed Dec 23, 2024
1 parent 25b6147 commit 0f9cccb
Show file tree
Hide file tree
Showing 12 changed files with 375 additions and 189 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.paimon.rest.requests.AlterDatabaseRequest;
import org.apache.paimon.rest.requests.CreateDatabaseRequest;
import org.apache.paimon.rest.requests.CreateTableRequest;
import org.apache.paimon.rest.requests.SchemaChanges;
import org.apache.paimon.rest.requests.UpdateTableRequest;
import org.apache.paimon.rest.responses.AlterDatabaseResponse;
import org.apache.paimon.rest.responses.ConfigResponse;
Expand Down Expand Up @@ -414,7 +415,8 @@ protected TableSchema getDataTableSchema(Identifier identifier) throws TableNotE

// todo: how know which exception to throw
private void updateTable(Identifier fromTable, Identifier toTable, List<SchemaChange> changes) {
UpdateTableRequest request = new UpdateTableRequest(fromTable, toTable, changes);
UpdateTableRequest request =
new UpdateTableRequest(fromTable, toTable, new SchemaChanges(changes));
client.post(
resourcePaths.table(fromTable.getDatabaseName(), fromTable.getTableName()),
request,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

package org.apache.paimon.rest;

import org.apache.paimon.catalog.Identifier;
import org.apache.paimon.rest.serializer.IdentifierSerializer;
import org.apache.paimon.rest.serializer.SchemaSerializer;
import org.apache.paimon.schema.Schema;
import org.apache.paimon.types.DataField;
import org.apache.paimon.types.DataType;
import org.apache.paimon.types.DataTypeJsonParser;
Expand Down Expand Up @@ -49,8 +53,15 @@ public static Module createPaimonRestJacksonModule() {
DataField.class,
DataField::serializeJson,
DataTypeJsonParser::parseDataField);
registerJsonObjects(
module, Schema.class, SchemaSerializer.INSTANCE, SchemaSerializer.INSTANCE);
registerJsonObjects(
module, DataType.class, DataType::serializeJson, DataTypeJsonParser::parseDataType);
registerJsonObjects(
module,
Identifier.class,
IdentifierSerializer.INSTANCE,
IdentifierSerializer.INSTANCE);
return module;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,60 +29,29 @@
/** Request for creating table. */
public class CreateTableRequest implements RESTRequest {

private static final String FIELD_DATABASE_NAME = "database";
private static final String FIELD_TABLE_NAME = "table";
private static final String FIELD_BRANCH_NAME = "branch";
private static final String FIELD_IDENTIFIER_NAME = "identifier";
private static final String FIELD_SCHEMA = "schema";

@JsonProperty(FIELD_DATABASE_NAME)
private String databaseName;

@JsonProperty(FIELD_TABLE_NAME)
private String tableName;

@JsonProperty(FIELD_BRANCH_NAME)
private String branchName;
@JsonProperty(FIELD_IDENTIFIER_NAME)
private Identifier identifier;

@JsonProperty(FIELD_SCHEMA)
private TableSchema schema;
private Schema schema;

@JsonCreator
public CreateTableRequest(
@JsonProperty(FIELD_DATABASE_NAME) String databaseName,
@JsonProperty(FIELD_TABLE_NAME) String tableName,
@JsonProperty(FIELD_BRANCH_NAME) String branchName,
@JsonProperty(FIELD_SCHEMA) TableSchema schema) {
this.databaseName = databaseName;
this.tableName = tableName;
this.branchName = branchName;
@JsonProperty(FIELD_IDENTIFIER_NAME) Identifier identifier,
@JsonProperty(FIELD_SCHEMA) Schema schema) {
this.schema = schema;
}

public CreateTableRequest(Identifier identifier, Schema schema) {
this(
identifier.getDatabaseName(),
identifier.getTableName(),
identifier.getBranchName(),
new TableSchema(schema));
}

@JsonGetter(FIELD_DATABASE_NAME)
public String getDatabaseName() {
return databaseName;
}

@JsonGetter(FIELD_TABLE_NAME)
public String getTableName() {
return tableName;
}

@JsonGetter(FIELD_BRANCH_NAME)
public String getBranchName() {
return branchName;
@JsonGetter(FIELD_IDENTIFIER_NAME)
public Identifier getIdentifier() {
return identifier;
}

@JsonGetter(FIELD_SCHEMA)
public TableSchema getSchema() {
public Schema getSchema() {
return schema;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.paimon.rest.requests;

import org.apache.paimon.schema.SchemaChange;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SchemaChanges {
private static final String FIELD_SET_OPTIONS_NAME = "set-options";
private static final String FIELD_REMOVE_OPTIONS_NAME = "remove-options";
private static final String FIELD_COMMENT_NAME = "comment";
private static final String FIELD_ADD_COLUMNS_NAME = "add-columns";
private static final String FIELD_RENAME_COLUMNS_NAME = "rename-columns";

private Map<String, String> setOptions;
private List<String> removeOptions;
private String comment;
private List<SchemaChange.AddColumn> addColumns;
private List<SchemaChange.RenameColumn> renameColumns;
private List<SchemaChange.DropColumn> dropColumns;
private List<SchemaChange.UpdateColumnType> updateColumnTypes;
private List<SchemaChange.UpdateColumnNullability> updateColumnNullabilities;
private List<SchemaChange.UpdateColumnComment> updateColumnComments;
private List<SchemaChange.UpdateColumnPosition> updateColumnPositions;

public SchemaChanges(
Map<String, String> setOptions,
List<String> removeOptions,
String comment,
List<SchemaChange.AddColumn> addColumns,
List<SchemaChange.RenameColumn> renameColumns,
List<SchemaChange.DropColumn> dropColumns,
List<SchemaChange.UpdateColumnType> updateColumnTypes,
List<SchemaChange.UpdateColumnNullability> updateColumnNullabilities,
List<SchemaChange.UpdateColumnComment> updateColumnComments,
List<SchemaChange.UpdateColumnPosition> updateColumnPositions) {
this.setOptions = setOptions;
this.removeOptions = removeOptions;
this.comment = comment;
this.addColumns = addColumns;
this.renameColumns = renameColumns;
this.dropColumns = dropColumns;
this.updateColumnTypes = updateColumnTypes;
this.updateColumnNullabilities = updateColumnNullabilities;
this.updateColumnComments = updateColumnComments;
this.updateColumnPositions = updateColumnPositions;
}

public SchemaChanges(List<SchemaChange> changes) {
Map<String, String> setOptions = null;
List<String> removeOptions = new ArrayList<>();
String comment = null;
List<SchemaChange.AddColumn> addColumns = new ArrayList<>();
for (SchemaChange change : changes) {
if (change instanceof SchemaChange.SetOption) {
setOptions.put(
((SchemaChange.SetOption) change).key(),
((SchemaChange.SetOption) change).value());
} else if (change instanceof SchemaChange.RemoveOption) {
removeOptions.add(((SchemaChange.RemoveOption) change).key());
} else if (change instanceof SchemaChange.UpdateComment) {
comment = ((SchemaChange.UpdateComment) change).comment();
} else if (change instanceof SchemaChange.AddColumn) {
addColumns.add((SchemaChange.AddColumn) change);
}
}
this.setOptions = setOptions;
this.removeOptions = removeOptions;
this.comment = comment;
this.addColumns = addColumns;
}

public Map<String, String> getSetOptions() {
return setOptions;
}

public List<String> getRemoveOptions() {
return removeOptions;
}

public String getComment() {
return comment;
}

public List<SchemaChange.AddColumn> getAddColumns() {
return addColumns;
}

public List<SchemaChange.RenameColumn> getRenameColumns() {
return renameColumns;
}

public List<SchemaChange.DropColumn> getDropColumns() {
return dropColumns;
}

public List<SchemaChange.UpdateColumnType> getUpdateColumnTypes() {
return updateColumnTypes;
}

public List<SchemaChange.UpdateColumnNullability> getUpdateColumnNullabilities() {
return updateColumnNullabilities;
}

public List<SchemaChange.UpdateColumnComment> getUpdateColumnComments() {
return updateColumnComments;
}

public List<SchemaChange.UpdateColumnPosition> getUpdateColumnPositions() {
return updateColumnPositions;
}
}

This file was deleted.

Loading

0 comments on commit 0f9cccb

Please sign in to comment.