Implementation:Risingwavelabs Risingwave TableSchema
Metadata
| Property | Value |
|---|---|
| File | java/connector-node/connector-api/src/main/java/com/risingwave/connector/api/TableSchema.java
|
| Language | Java |
| Module | connector-api |
| Package | com.risingwave.connector.api
|
| Classes | TableSchema
|
| Lines | 124 |
Overview
TableSchema is a core data model class that represents the schema of a table in the RisingWave connector framework. It stores column names, their protobuf data types, column indices, column descriptors, and the list of primary key column names. It provides accessor methods for schema introspection and a static factory method fromProto to construct a TableSchema from a protobuf ConnectorServiceProto.TableSchema message.
This class is used by all sink and source connectors to understand the structure of the tables they interact with.
Code Reference
Source Location
java/connector-node/connector-api/src/main/java/com/risingwave/connector/api/TableSchema.java
Signature
public class TableSchema {
public TableSchema(List<String> columnNames, List<Data.DataType> dataTypes, List<String> primaryKeys)
public int getNumColumns()
public int getColumnIndex(String columnName)
public TypeName getColumnType(String columnName)
public ColumnDesc getColumnDesc(int index)
public Map<String, TypeName> getColumnTypes()
public String[] getColumnNames()
public List<ColumnDesc> getColumnDescs()
public Object getFromRow(String columnName, SinkRow row)
public static TableSchema fromProto(ConnectorServiceProto.TableSchema tableSchema)
public List<String> getPrimaryKeys()
public String toString()
}
Imports
import com.risingwave.connector.api.sink.SinkRow;
import com.risingwave.proto.ConnectorServiceProto;
import com.risingwave.proto.Data;
import com.risingwave.proto.Data.DataType.TypeName;
import com.risingwave.proto.PlanCommon;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
I/O Contract
Constructor
| Parameter | Type | Description |
|---|---|---|
columnNames |
List<String> |
Ordered list of column names in the table |
dataTypes |
List<Data.DataType> |
Ordered list of protobuf data types corresponding to each column |
primaryKeys |
List<String> |
List of column names that form the primary key |
The constructor populates the following internal maps:
columns- Maps column name toTypeNamecolumnIndices- Maps column name to its positional indexcolumnDescs- List ofColumnDescobjects containing name and full data type info
fromProto (static factory)
| Parameter | Type | Description |
|---|---|---|
tableSchema |
ConnectorServiceProto.TableSchema |
Protobuf table schema message |
| Direction | Type | Description |
|---|---|---|
| Output | TableSchema |
A new TableSchema instance built from the protobuf message
|
Extracts column names and types from the protobuf column descriptors. Primary key indices are resolved to column names using the pkIndices list. Logs the column names after construction.
getFromRow
| Parameter | Type | Description |
|---|---|---|
columnName |
String |
The name of the column to retrieve |
row |
SinkRow |
The row to extract the value from |
| Direction | Type | Description |
|---|---|---|
| Output | Object |
The value at the column's index in the row |
Usage Examples
// Create a TableSchema from protobuf
TableSchema schema = TableSchema.fromProto(protoTableSchema);
// Inspect the schema
int numCols = schema.getNumColumns();
String[] names = schema.getColumnNames();
TypeName type = schema.getColumnType("user_id");
int idx = schema.getColumnIndex("user_id");
// Get primary keys
List<String> pks = schema.getPrimaryKeys();
// Extract a value from a SinkRow by column name
Object value = schema.getFromRow("user_id", sinkRow);
// Get all column descriptors
List<ColumnDesc> descs = schema.getColumnDescs();
Related Pages
- SinkRow Interface - The row interface used with
getFromRowto extract column values - PkComparator - Uses primary key information from TableSchema for row comparison
- Deserializer Interface - Deserializers produce SinkRows that conform to a TableSchema
- SinkWriterV1 Interface - Sink writers that operate on rows described by TableSchema
- SinkFactory CreateWriter - Factory that uses TableSchema when creating sink writers