Skip to content

Commit d1166a4

Browse files
local-commit
1 parent 8631993 commit d1166a4

File tree

5 files changed

+62
-2
lines changed

5 files changed

+62
-2
lines changed

src/main/java/io/cdap/plugin/gcp/bigquery/source/BigQuerySource.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@ private void configureBigQuerySource() {
261261
if (config.getFilter() != null) {
262262
configuration.set(BigQueryConstants.CONFIG_FILTER, config.getFilter());
263263
}
264+
// TODO Add code of parameter map
265+
if (config.getParameterMap() != null) {
266+
configuration.set(BigQueryConstants.CONFIG_FILTER_PARAMETER_MAP, config.getParameterMap());
267+
}
264268
if (config.getViewMaterializationProject() != null) {
265269
configuration.set(BigQueryConstants.CONFIG_VIEW_MATERIALIZATION_PROJECT, config.getViewMaterializationProject());
266270
}

src/main/java/io/cdap/plugin/gcp/bigquery/source/BigQuerySourceConfig.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public final class BigQuerySourceConfig extends BigQueryBaseConfig {
6868
public static final String NAME_PARTITION_FROM = "partitionFrom";
6969
public static final String NAME_PARTITION_TO = "partitionTo";
7070
public static final String NAME_FILTER = "filter";
71+
public static final String NAME_FILTER_PARAMETER_MAP = "parameterMap";
7172
public static final String NAME_ENABLE_QUERYING_VIEWS = "enableQueryingViews";
7273
public static final String NAME_VIEW_MATERIALIZATION_PROJECT = "viewMaterializationProject";
7374
public static final String NAME_VIEW_MATERIALIZATION_DATASET = "viewMaterializationDataset";
@@ -111,6 +112,12 @@ public final class BigQuerySourceConfig extends BigQueryBaseConfig {
111112
"and discards all rows that do not return TRUE (that is, rows that return FALSE or NULL).")
112113
private String filter;
113114

115+
@Name(NAME_FILTER_PARAMETER_MAP)
116+
@Macro
117+
@Nullable
118+
@Description("While using parameterized query, provide values in key-value pair ")
119+
private String parameterMap;
120+
114121
@Name(NAME_ENABLE_QUERYING_VIEWS)
115122
@Macro
116123
@Nullable
@@ -295,6 +302,11 @@ public String getFilter() {
295302
return filter;
296303
}
297304

305+
@Nullable
306+
public String getParameterMap() {
307+
return parameterMap;
308+
}
309+
298310
public boolean isEnableQueryingViews() {
299311
return "true".equalsIgnoreCase(enableQueryingViews);
300312
}

src/main/java/io/cdap/plugin/gcp/bigquery/source/PartitionedBigQueryInputFormat.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@
2020
import com.google.api.services.bigquery.model.JobConfiguration;
2121
import com.google.api.services.bigquery.model.JobConfigurationQuery;
2222
import com.google.api.services.bigquery.model.JobReference;
23+
import com.google.api.services.bigquery.model.QueryParameter;
24+
import com.google.api.services.bigquery.model.QueryParameterType;
25+
import com.google.api.services.bigquery.model.QueryParameterValue;
2326
import com.google.api.services.bigquery.model.Table;
2427
import com.google.api.services.bigquery.model.TableReference;
28+
import com.google.cloud.bigquery.FieldList;
2529
import com.google.cloud.bigquery.StandardTableDefinition;
2630
import com.google.cloud.bigquery.TableDefinition.Type;
2731
import com.google.cloud.bigquery.TimePartitioning;
@@ -40,6 +44,7 @@
4044
import io.cdap.cdap.api.exception.ErrorCategory;
4145
import io.cdap.cdap.api.exception.ErrorType;
4246
import io.cdap.cdap.api.exception.ErrorUtils;
47+
import io.cdap.plugin.common.ConfigUtil;
4348
import io.cdap.plugin.gcp.bigquery.util.BigQueryConstants;
4449
import io.cdap.plugin.gcp.bigquery.util.BigQueryUtil;
4550
import io.cdap.plugin.gcp.common.GCPUtils;
@@ -134,6 +139,7 @@ private void processQuery(JobContext context) throws IOException, InterruptedExc
134139
String filter = configuration.get(BigQueryConstants.CONFIG_FILTER, null);
135140
Integer readTimeout = configuration.getInt(BigQueryConstants.CONFIG_BQ_HTTP_READ_TIMEOUT,
136141
GCPUtils.BQ_DEFAULT_READ_TIMEOUT_SECONDS);
142+
String parameterMap = configuration.get(BigQueryConstants.CONFIG_FILTER_PARAMETER_MAP, null);
137143

138144
com.google.cloud.bigquery.Table bigQueryTable = BigQueryUtil.getBigQueryTable(
139145
datasetProjectId, datasetId, tableName, serviceAccount, isServiceAccountFilePath, null, readTimeout);
@@ -150,11 +156,16 @@ private void processQuery(JobContext context) throws IOException, InterruptedExc
150156
if (query != null) {
151157
TableReference sourceTable = new TableReference().setDatasetId(datasetId).setProjectId(datasetProjectId)
152158
.setTableId(tableName);
159+
com.google.cloud.bigquery.Table bqTable = BigQueryUtil.getBigQueryTable(datasetProjectId, datasetId, tableName,
160+
serviceAccount, isServiceAccountFilePath,
161+
null,
162+
null);
153163
String location = bigQueryHelper.getTable(sourceTable).getLocation();
154164
String temporaryTableName = configuration.get(BigQueryConstants.CONFIG_TEMPORARY_TABLE_NAME);
155165
TableReference exportTableReference = createExportTableReference(type, datasetProjectId, datasetId,
156166
temporaryTableName, configuration);
157-
runQuery(configuration, bigQueryHelper, projectId, exportTableReference, query, location);
167+
runQuery(configuration, bigQueryHelper, projectId, exportTableReference, query, location, bqTable,
168+
parameterMap);
158169

159170
// Default values come from BigquerySource config, and can be overridden by config.
160171
configuration.set(BigQueryConfiguration.INPUT_PROJECT_ID.getKey(),
@@ -244,14 +255,30 @@ private static void runQuery(Configuration configuration,
244255
String projectId,
245256
TableReference tableRef,
246257
String query,
247-
String location)
258+
String location,
259+
com.google.cloud.bigquery.Table bqTable,
260+
@Nullable String parameterMapString)
248261
throws IOException, InterruptedException {
249262

250263
// Create a query statement and query request object.
251264
JobConfigurationQuery queryConfig = new JobConfigurationQuery();
252265
queryConfig.setAllowLargeResults(true);
253266
queryConfig.setQuery(query);
254267
queryConfig.setUseLegacySql(false);
268+
if (!Strings.isNullOrEmpty(parameterMapString)) {
269+
Map<String, String> parameterMap = ConfigUtil.parseKeyValueConfig(parameterMapString, ",", "=");
270+
List<QueryParameter> queryParameters = new ArrayList<>();
271+
FieldList fieldList = bqTable.getDefinition().getSchema().getFields();
272+
for (String columnName : parameterMap.keySet()) {
273+
String parameterType = fieldList.get(columnName).getType().name();
274+
QueryParameter queryParameter = new QueryParameter().setName(columnName)
275+
.setParameterType(new QueryParameterType().setType(parameterType));
276+
QueryParameterValue value = new QueryParameterValue().setValue(parameterMap.get(columnName));
277+
queryParameters.add(queryParameter.setParameterValue(value));
278+
}
279+
queryConfig.setParameterMode("NAMED");
280+
queryConfig.setQueryParameters(queryParameters);
281+
}
255282

256283
// Set the table to put results into.
257284
queryConfig.setDestinationTable(tableRef);

src/main/java/io/cdap/plugin/gcp/bigquery/util/BigQueryConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public interface BigQueryConstants {
3636
String CONFIG_TABLE_FIELDS = "cdap.bq.sink.table.fields";
3737
String CONFIG_JSON_STRING_FIELDS = "cdap.bq.sink.json.string.fields";
3838
String CONFIG_FILTER = "cdap.bq.source.filter";
39+
40+
String CONFIG_FILTER_PARAMETER_MAP = "cdap.bq.source.filter.parameter.map";
3941
String CONFIG_PARTITION_FILTER = "cdap.bq.sink.partition.filter";
4042
String CONFIG_JOB_ID = "cdap.bq.sink.job.id";
4143
String CONFIG_VIEW_MATERIALIZATION_PROJECT = "cdap.bq.source.view.materialization.project";

widgets/BigQueryTable-batchsource.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,21 @@
151151
"placeholder": ""
152152
}
153153
},
154+
{
155+
"widget-type": "ds-multiplevalues",
156+
"label": "Parameterized Query Map",
157+
"name": "parameterMap",
158+
"widget-attributes": {
159+
"delimiter": ",",
160+
"values-delimiter": ":",
161+
"numValues": "3",
162+
"placeholders": [
163+
"Field Alias",
164+
"Field Name",
165+
"Field Value"
166+
]
167+
}
168+
},
154169
{
155170
"widget-type": "textbox",
156171
"label": "Temporary Bucket Name",

0 commit comments

Comments
 (0)