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

feat: add obkv-table connector #53

Closed
wants to merge 1 commit into from
Closed
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
60 changes: 60 additions & 0 deletions flink-connector-obkv-table/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2023 OceanBase.

Licensed 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.oceanbase</groupId>
<artifactId>flink-connector-oceanbase-parent</artifactId>
<version>1.1-SNAPSHOT</version>
</parent>

<artifactId>flink-connector-obkv-table</artifactId>
<packaging>jar</packaging>

<properties>
<obkv.table.client.version>1.2.7</obkv.table.client.version>
</properties>

<dependencies>
<dependency>
<groupId>com.oceanbase</groupId>
<artifactId>flink-connector-oceanbase-base</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.oceanbase</groupId>
<artifactId>obkv-table-client</artifactId>
<version>${obkv.table.client.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>com.oceanbase</groupId>
<artifactId>flink-connector-oceanbase-base</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2023 OceanBase.
*
* Licensed 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 com.oceanbase.connector.flink;

import org.apache.flink.configuration.ConfigOption;
import org.apache.flink.configuration.ConfigOptions;

import java.util.Map;

public class OBKVTableConnectorOptions extends ConnectorOptions {

private static final long serialVersionUID = 1L;

public static final ConfigOption<String> SYS_USERNAME =
ConfigOptions.key("sys.username")
.stringType()
.noDefaultValue()
.withDescription("The username of system tenant.");

public static final ConfigOption<String> SYS_PASSWORD =
ConfigOptions.key("sys.password")
.stringType()
.noDefaultValue()
.withDescription("The password of system tenant");

public OBKVTableConnectorOptions(Map<String, String> config) {
super(config);
}

public String getSysUsername() {
return allConfig.get(SYS_USERNAME);
}

public String getSysPassword() {
return allConfig.get(SYS_PASSWORD);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2023 OceanBase.
*
* Licensed 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 com.oceanbase.connector.flink;

import com.oceanbase.connector.flink.sink.OBKVTableDynamicTableSink;
import com.oceanbase.connector.flink.utils.OptionUtils;

import org.apache.flink.configuration.ConfigOption;
import org.apache.flink.table.catalog.Column;
import org.apache.flink.table.catalog.ResolvedSchema;
import org.apache.flink.table.connector.sink.DynamicTableSink;
import org.apache.flink.table.factories.DynamicTableSinkFactory;
import org.apache.flink.table.factories.FactoryUtil;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class OBKVTableDynamicTableSinkFactory implements DynamicTableSinkFactory {

public static final String IDENTIFIER = "obkv-table";

@Override
public DynamicTableSink createDynamicTableSink(Context context) {
FactoryUtil.TableFactoryHelper helper = FactoryUtil.createTableFactoryHelper(this, context);
helper.validate();

ResolvedSchema resolvedSchema = context.getCatalogTable().getResolvedSchema();
ResolvedSchema physicalSchema =
new ResolvedSchema(
resolvedSchema.getColumns().stream()
.filter(Column::isPhysical)
.collect(Collectors.toList()),
resolvedSchema.getWatermarkSpecs(),
resolvedSchema.getPrimaryKey().orElse(null));
Map<String, String> options = context.getCatalogTable().getOptions();
OptionUtils.printOptions(IDENTIFIER, options);
return new OBKVTableDynamicTableSink(
physicalSchema, new OBKVTableConnectorOptions(options));
}

@Override
public String factoryIdentifier() {
return IDENTIFIER;
}

@Override
public Set<ConfigOption<?>> requiredOptions() {
Set<ConfigOption<?>> options = new HashSet<>();
return options;
}

@Override
public Set<ConfigOption<?>> optionalOptions() {
Set<ConfigOption<?>> options = new HashSet<>();
return options;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2023 OceanBase.
*
* Licensed 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 com.oceanbase.connector.flink.connection;

import com.oceanbase.connector.flink.OBKVTableConnectorOptions;
import com.oceanbase.connector.flink.table.TableId;
import com.oceanbase.connector.flink.utils.TableCache;

import com.alipay.oceanbase.rpc.ObTableClient;

public class OBKVTableConnectionProvider implements ConnectionProvider {

private static final long serialVersionUID = 1L;

private final OBKVTableConnectorOptions options;

private final TableCache<ObTableClient> tableCache;

public OBKVTableConnectionProvider(OBKVTableConnectorOptions options) {
this.options = options;
this.tableCache = new TableCache<>();
}

public ObTableClient getTableClient(TableId tableId) {
return tableCache.get(
tableId.identifier(),
() -> {
try {
ObTableClient tableClient = new ObTableClient();
tableClient.setParamURL(options.getUrl());
tableClient.setFullUserName(options.getUsername());
tableClient.setPassword(options.getPassword());
tableClient.setSysUserName(options.getSysUsername());
tableClient.setSysPassword(options.getSysPassword());
tableClient.init();
return tableClient;
} catch (Exception e) {
throw new RuntimeException("Failed to initialize ObTableClient", e);
}
});
}

@Override
public void close() throws Exception {
for (ObTableClient client : tableCache.getAll()) {
client.close();
}
tableCache.clear();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2023 OceanBase.
*
* Licensed 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 com.oceanbase.connector.flink.sink;

import com.oceanbase.connector.flink.OBKVTableConnectorOptions;
import com.oceanbase.connector.flink.table.DataChangeRecord;
import com.oceanbase.connector.flink.table.OBKVTableRowDataSerializationSchema;
import com.oceanbase.connector.flink.table.TableId;
import com.oceanbase.connector.flink.table.TableInfo;

import org.apache.flink.table.catalog.ResolvedSchema;
import org.apache.flink.table.connector.sink.DynamicTableSink;

public class OBKVTableDynamicTableSink extends AbstractDynamicTableSink {

private final OBKVTableConnectorOptions connectorOptions;

public OBKVTableDynamicTableSink(
ResolvedSchema resolvedSchema, OBKVTableConnectorOptions connectorOptions) {
super(resolvedSchema);
this.connectorOptions = connectorOptions;
}

@Override
public SinkRuntimeProvider getSinkRuntimeProvider(Context context) {
return new SinkProvider(
typeSerializer ->
new OceanBaseSink<>(
connectorOptions,
typeSerializer,
new OBKVTableRowDataSerializationSchema(
new TableInfo(
new TableId(
connectorOptions.getSchemaName(),
connectorOptions.getTableName()),
physicalSchema)),
DataChangeRecord.KeyExtractor.simple(),
new OBKVTableRecordFlusher(connectorOptions)));
}

@Override
public DynamicTableSink copy() {
return new OBKVTableDynamicTableSink(physicalSchema, connectorOptions);
}

@Override
public String asSummaryString() {
return "OBKV-TABLE";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2023 OceanBase.
*
* Licensed 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 com.oceanbase.connector.flink.sink;

import com.oceanbase.connector.flink.OBKVTableConnectorOptions;
import com.oceanbase.connector.flink.connection.OBKVTableConnectionProvider;
import com.oceanbase.connector.flink.table.DataChangeRecord;
import com.oceanbase.connector.flink.table.SchemaChangeRecord;
import com.oceanbase.connector.flink.table.TableId;
import com.oceanbase.connector.flink.table.TableInfo;

import java.util.List;

public class OBKVTableRecordFlusher implements RecordFlusher {

private final OBKVTableConnectorOptions options;
private final OBKVTableConnectionProvider connectionProvider;

public OBKVTableRecordFlusher(OBKVTableConnectorOptions options) {
this(options, new OBKVTableConnectionProvider(options));
}

public OBKVTableRecordFlusher(
OBKVTableConnectorOptions options, OBKVTableConnectionProvider connectionProvider) {
this.options = options;
this.connectionProvider = connectionProvider;
}

@Override
public void flush(SchemaChangeRecord record) throws Exception {
throw new UnsupportedOperationException();
}

@Override
public void flush(List<DataChangeRecord> records) throws Exception {
if (records == null || records.isEmpty()) {
return;
}

TableInfo tableInfo = (TableInfo) records.get(0).getTable();
TableId tableId = tableInfo.getTableId();
}

@Override
public void close() throws Exception {}
}
Loading
Loading