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(connector): add webhdfs sink #18293

Merged
merged 5 commits into from
Aug 28, 2024
Merged
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
1 change: 1 addition & 0 deletions src/connector/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ opendal = { workspace = true, features = [
"services-gcs",
"services-memory",
"services-s3",
"services-webhdfs",
] }
openssl = "0.10"
parking_lot = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions src/connector/src/sink/file_sink/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ pub mod fs;
pub mod gcs;
pub mod opendal_sink;
pub mod s3;
pub mod webhdfs;
1 change: 1 addition & 0 deletions src/connector/src/sink/file_sink/opendal_sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ pub enum EngineType {
S3,
Fs,
Azblob,
Webhdfs,
}

impl<S: OpendalSinkBackend> Sink for FileSink<S> {
Expand Down
107 changes: 107 additions & 0 deletions src/connector/src/sink/file_sink/webhdfs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright 2024 RisingWave Labs
//
// 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.
use std::collections::{BTreeMap, HashMap};

use anyhow::anyhow;
use opendal::layers::LoggingLayer;
use opendal::services::Webhdfs;
use opendal::Operator;
use serde::Deserialize;
use serde_with::serde_as;
use with_options::WithOptions;

use super::opendal_sink::FileSink;
use crate::sink::file_sink::opendal_sink::OpendalSinkBackend;
use crate::sink::{Result, SinkError, SINK_TYPE_APPEND_ONLY, SINK_TYPE_OPTION, SINK_TYPE_UPSERT};
use crate::source::UnknownFields;
#[derive(Deserialize, Debug, Clone, WithOptions)]
pub struct WebhdfsCommon {
#[serde(rename = "webhdfs.endpoint")]
pub endpoint: String,
/// The directory where the sink file is located.
#[serde(rename = "webhdfs.path")]
pub path: String,
}

#[serde_as]
#[derive(Clone, Debug, Deserialize, WithOptions)]
pub struct WebhdfsConfig {
#[serde(flatten)]
pub common: WebhdfsCommon,

pub r#type: String, // accept "append-only"

#[serde(flatten)]
pub unknown_fields: HashMap<String, String>,
}

pub const WEBHDFS_SINK: &str = "webhdfs";

impl<S: OpendalSinkBackend> FileSink<S> {
pub fn new_webhdfs_sink(config: WebhdfsConfig) -> Result<Operator> {
// Create webhdfs backend builder.
let mut builder = Webhdfs::default();
// Set the name node for hdfs.
builder.endpoint(&config.common.endpoint);
builder.root(&config.common.path);

let operator: Operator = Operator::new(builder)?
.layer(LoggingLayer::default())
.finish();

Ok(operator)
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct WebhdfsSink;

impl UnknownFields for WebhdfsConfig {
fn unknown_fields(&self) -> HashMap<String, String> {
self.unknown_fields.clone()
}
}

impl OpendalSinkBackend for WebhdfsSink {
type Properties = WebhdfsConfig;

const SINK_NAME: &'static str = WEBHDFS_SINK;

fn from_btreemap(btree_map: BTreeMap<String, String>) -> Result<Self::Properties> {
let config =
serde_json::from_value::<WebhdfsConfig>(serde_json::to_value(btree_map).unwrap())
.map_err(|e| SinkError::Config(anyhow!(e)))?;
if config.r#type != SINK_TYPE_APPEND_ONLY && config.r#type != SINK_TYPE_UPSERT {
return Err(SinkError::Config(anyhow!(
"`{}` must be {}, or {}",
SINK_TYPE_OPTION,
SINK_TYPE_APPEND_ONLY,
SINK_TYPE_UPSERT
)));
}
Ok(config)
}

fn new_operator(properties: WebhdfsConfig) -> Result<Operator> {
FileSink::<WebhdfsSink>::new_webhdfs_sink(properties)
}

fn get_path(properties: Self::Properties) -> String {
properties.common.path
}

fn get_engine_type() -> super::opendal_sink::EngineType {
super::opendal_sink::EngineType::Webhdfs
}
}
1 change: 1 addition & 0 deletions src/connector/src/sink/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ macro_rules! for_all_sinks {

{ Gcs, $crate::sink::file_sink::opendal_sink::FileSink<$crate::sink::file_sink::gcs::GcsSink> },
{ Azblob, $crate::sink::file_sink::opendal_sink::FileSink<$crate::sink::file_sink::azblob::AzblobSink>},
{ Webhdfs, $crate::sink::file_sink::opendal_sink::FileSink<$crate::sink::file_sink::webhdfs::WebhdfsSink>},

{ Fs, $crate::sink::file_sink::opendal_sink::FileSink<FsSink> },
{ Snowflake, $crate::sink::snowflake::SnowflakeSink },
Expand Down
12 changes: 12 additions & 0 deletions src/connector/with_options_sink.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1022,3 +1022,15 @@ StarrocksConfig:
- name: r#type
field_type: String
required: true
WebhdfsConfig:
fields:
- name: webhdfs.endpoint
field_type: String
required: true
- name: webhdfs.path
field_type: String
comments: The directory where the sink file is located.
required: true
- name: r#type
field_type: String
required: true
4 changes: 4 additions & 0 deletions src/frontend/src/handler/create_sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,7 @@ static CONNECTORS_COMPATIBLE_FORMATS: LazyLock<HashMap<String, HashMap<Format, V
use risingwave_connector::sink::file_sink::gcs::GcsSink;
use risingwave_connector::sink::file_sink::opendal_sink::FileSink;
use risingwave_connector::sink::file_sink::s3::S3Sink;
use risingwave_connector::sink::file_sink::webhdfs::WebhdfsSink;
use risingwave_connector::sink::google_pubsub::GooglePubSubSink;
use risingwave_connector::sink::kafka::KafkaSink;
use risingwave_connector::sink::kinesis::KinesisSink;
Expand All @@ -900,6 +901,9 @@ static CONNECTORS_COMPATIBLE_FORMATS: LazyLock<HashMap<String, HashMap<Format, V
FileSink::<AzblobSink>::SINK_NAME => hashmap!(
Format::Plain => vec![Encode::Parquet],
),
FileSink::<WebhdfsSink>::SINK_NAME => hashmap!(
Format::Plain => vec![Encode::Parquet],
),
FileSink::<FsSink>::SINK_NAME => hashmap!(
Format::Plain => vec![Encode::Parquet],
),
Expand Down
Loading