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 .finish();
85 Ok(operator)
86 }
87}
88
89#[derive(Debug, Clone, Copy, PartialEq, Eq)]
90pub struct GcsSink;
91
92impl OpendalSinkBackend for GcsSink {
93 type Properties = GcsConfig;
94
95 const SINK_NAME: &'static str = GCS_SINK;
96
97 fn from_btreemap(btree_map: BTreeMap<String, String>) -> Result<Self::Properties> {
98 let config = serde_json::from_value::<GcsConfig>(serde_json::to_value(btree_map).unwrap())
99 .map_err(|e| SinkError::Config(anyhow!(e)))?;
100 if config.r#type != SINK_TYPE_APPEND_ONLY && config.r#type != SINK_TYPE_UPSERT {
101 return Err(SinkError::Config(anyhow!(
102 "`{}` must be {}, or {}",
103 SINK_TYPE_OPTION,
104 SINK_TYPE_APPEND_ONLY,
105 SINK_TYPE_UPSERT
106 )));
107 }
108 Ok(config)
109 }
110
111 fn new_operator(properties: GcsConfig) -> Result<Operator> {
112 FileSink::<GcsSink>::new_gcs_sink(properties)
113 }
114
115 fn get_path(properties: Self::Properties) -> String {
116 properties.common.path
117 }
118
119 fn get_engine_type() -> super::opendal_sink::EngineType {
120 super::opendal_sink::EngineType::Gcs
121 }
122
123 fn get_batching_strategy(properties: Self::Properties) -> BatchingStrategy {
124 BatchingStrategy {
125 max_row_count: properties.batching_strategy.max_row_count,
126 rollover_seconds: properties.batching_strategy.rollover_seconds,
127 path_partition_prefix: properties.batching_strategy.path_partition_prefix,
128 }
129 }
130}