risingwave_connector/sink/file_sink/
gcs.rs1use std::collections::{BTreeMap, HashMap};
16
17use anyhow::anyhow;
18use opendal::Operator;
19use opendal::layers::{LoggingLayer, RetryLayer};
20use opendal::services::Gcs;
21use serde::Deserialize;
22use serde_with::serde_as;
23use with_options::WithOptions;
24
25use super::opendal_sink::{BatchingStrategy, FileSink};
26use crate::sink::file_sink::opendal_sink::OpendalSinkBackend;
27use crate::sink::{Result, SINK_TYPE_APPEND_ONLY, SINK_TYPE_OPTION, SINK_TYPE_UPSERT, SinkError};
28use crate::source::UnknownFields;
29
30#[derive(Deserialize, Debug, Clone, WithOptions)]
31pub struct GcsCommon {
32 #[serde(rename = "gcs.bucket_name")]
33 pub bucket_name: String,
34
35 #[serde(rename = "gcs.credential")]
37 pub credential: String,
38
39 #[serde(rename = "gcs.service_account", default)]
41 pub service_account: String,
42
43 #[serde(rename = "gcs.path")]
45 pub path: String,
46}
47
48#[serde_as]
49#[derive(Clone, Debug, Deserialize, WithOptions)]
50pub struct GcsConfig {
51 #[serde(flatten)]
52 pub common: GcsCommon,
53
54 #[serde(flatten)]
55 pub batching_strategy: BatchingStrategy,
56
57 pub r#type: String, #[serde(flatten)]
60 pub unknown_fields: HashMap<String, String>,
61}
62
63impl UnknownFields for GcsConfig {
64 fn unknown_fields(&self) -> HashMap<String, String> {
65 self.unknown_fields.clone()
66 }
67}
68
69crate::impl_sink_unknown_fields!(GcsConfig);
70
71pub const GCS_SINK: &str = "gcs";
72
73impl<S: OpendalSinkBackend> FileSink<S> {
74 pub fn new_gcs_sink(config: GcsConfig) -> Result<Operator> {
75 let builder = Gcs::default()
77 .bucket(&config.common.bucket_name)
78 .credential(&config.common.credential)
79 .service_account(&config.common.service_account);
80
81 let operator: Operator = Operator::new(builder)?
82 .layer(LoggingLayer::default())
83 .layer(RetryLayer::default());
84 Ok(operator)
85 }
86}
87
88#[derive(Debug, Clone, Copy, PartialEq, Eq)]
89pub struct GcsSink;
90
91impl OpendalSinkBackend for GcsSink {
92 type Properties = GcsConfig;
93
94 const SINK_NAME: &'static str = GCS_SINK;
95
96 fn from_btreemap(btree_map: BTreeMap<String, String>) -> Result<Self::Properties> {
97 let config = serde_json::from_value::<GcsConfig>(serde_json::to_value(btree_map).unwrap())
98 .map_err(|e| SinkError::Config(anyhow!(e)))?;
99 if config.r#type != SINK_TYPE_APPEND_ONLY && config.r#type != SINK_TYPE_UPSERT {
100 return Err(SinkError::Config(anyhow!(
101 "`{}` must be {}, or {}",
102 SINK_TYPE_OPTION,
103 SINK_TYPE_APPEND_ONLY,
104 SINK_TYPE_UPSERT
105 )));
106 }
107 Ok(config)
108 }
109
110 fn new_operator(properties: GcsConfig) -> Result<Operator> {
111 FileSink::<GcsSink>::new_gcs_sink(properties)
112 }
113
114 fn get_path(properties: Self::Properties) -> String {
115 properties.common.path
116 }
117
118 fn get_engine_type() -> super::opendal_sink::EngineType {
119 super::opendal_sink::EngineType::Gcs
120 }
121
122 fn get_batching_strategy(properties: Self::Properties) -> BatchingStrategy {
123 BatchingStrategy {
124 max_row_count: properties.batching_strategy.max_row_count,
125 rollover_seconds: properties.batching_strategy.rollover_seconds,
126 path_partition_prefix: properties.batching_strategy.path_partition_prefix,
127 }
128 }
129}