spark.GBQQueryDataset¶
spark.GBQQueryDataset loads and saves data to/from Google BigQuery tables using Spark.
kedro_datasets.spark.GBQQueryDataset ¶
GBQQueryDataset(
materialization_dataset,
sql=None,
filepath=None,
materialization_project=None,
load_args=None,
fs_args=None,
bq_credentials=None,
fs_credentials=None,
metadata=None,
)
Bases: AbstractDataset[None, DataFrame]
GBQQueryDataset loads data from Google BigQuery with a SQL query using BigQuery Spark connector.
Examples:
Using the YAML API:
my_gbq_spark_data:
type: spark.GBQQueryDataset
sql: |
SELECT * FROM your_table
materialization_dataset: your_dataset
materialization_project: your_project
bq_credentials:
file: /path/to/your/credentials.json
fs_credentials:
key: value
Using the Python API:
>>> from kedro_datasets.spark import GBQQueryDataset
>>>
>>> # Define your SQL query
>>> sql = "SELECT * FROM your_table"
>>>
>>> # Initialize dataset
>>> dataset = GBQQueryDataset(
... sql=sql,
... materialization_dataset="your_dataset",
... materialization_project="your_project", # optional
... bq_credentials=dict(file="/path/to/your/credentials.json"), # optional
... fs_credentials=dict(key="value"), # optional
... )
>>>
>>> # Load data
>>> df = dataset.load()
>>>
>>> # Example output
>>> df.show()
Parameters:
-
materialization_dataset(str) –The name of the dataset to materialize the query results.
-
sql(str | None, default:None) –The SQL query to execute
-
filepath(str | None, default:None) –A path to a file with a sql query statement.
-
materialization_project(str | None, default:None) –The name of the project to materialize the query results. Optional (defaults to the project id set by the credentials).
-
load_args(dict[str, Any] | None, default:None) –Load args passed to Spark DataFrameReader load method. It is dependent on the selected file format. You can find a list of read options for each supported format in Spark DataFrame read documentation: https://spark.apache.org/docs/latest/api/python/getting_started/quickstart_df.html
-
bq_credentials(dict[str, Any] | None, default:None) –Credentials to authenticate spark session with google bigquery. Dictionary with key specifying the type of credentials ('base64', 'file', 'json'). Alternatively, you can pass the credentials in load_args as follows:
When passing as
base64:load_args={"credentials": "your_credentials"}When passing as a
file:load_args={"credentialsFile": "/path/to/your/credentials.json"}When passing as a json object: This is not supported when passed directly in load_args. You can pass the json object (as a python dictionary) in the credentials by specifying the key as
json.Read more here: https://github.com/GoogleCloudDataproc/spark-bigquery-connector?tab=readme-ov-file#how-do-i-authenticate-outside-gce--dataproc
-
fs_credentials(dict[str, Any] | None, default:None) –Credentials to authenticate with the filesystem. The keyword args would be directly passed to fsspec.filesystem constructor.
-
metadata(dict[str, Any] | None, default:None) –Any arbitrary metadata. This is ignored by Kedro, but may be consumed by users or external plugins.
Source code in kedro_datasets/spark/gbq_dataset.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
_VALID_CREDENTIALS_KEYS
class-attribute
instance-attribute
¶
_VALID_CREDENTIALS_KEYS = {'base64', 'file', 'json'}
_describe ¶
_describe()
Source code in kedro_datasets/spark/gbq_dataset.py
227 228 229 230 231 232 233 234 | |
_get_spark_bq_credentials ¶
_get_spark_bq_credentials()
Source code in kedro_datasets/spark/gbq_dataset.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | |
_get_spark_load_args ¶
_get_spark_load_args()
Source code in kedro_datasets/spark/gbq_dataset.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | |
_get_sql ¶
_get_sql()
Source code in kedro_datasets/spark/gbq_dataset.py
181 182 183 184 185 | |
_load_sql_from_filepath ¶
_load_sql_from_filepath()
Source code in kedro_datasets/spark/gbq_dataset.py
177 178 179 | |
load ¶
load()
Loads data from Google BigQuery.
Returns:
-
DataFrame–A Spark DataFrame.
Source code in kedro_datasets/spark/gbq_dataset.py
213 214 215 216 217 218 219 220 221 222 | |
save ¶
save(data)
Source code in kedro_datasets/spark/gbq_dataset.py
224 225 | |