-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add schema to SpannerIO read (#32008)
* Add schema to SpannerIO read Signed-off-by: Jeffrey Kinard <[email protected]> * fix spotless Signed-off-by: Jeffrey Kinard <[email protected]> * fix spotless Signed-off-by: Jeffrey Kinard <[email protected]> * address comments Signed-off-by: Jeffrey Kinard <[email protected]> * spotless Signed-off-by: Jeffrey Kinard <[email protected]> --------- Signed-off-by: Jeffrey Kinard <[email protected]>
- Loading branch information
Showing
9 changed files
with
442 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...loud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/SpannerQuerySourceDef.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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. | ||
*/ | ||
package org.apache.beam.sdk.io.gcp.spanner; | ||
|
||
import static org.apache.beam.sdk.io.gcp.spanner.StructUtils.structTypeToBeamRowSchema; | ||
|
||
import com.google.cloud.spanner.ReadContext; | ||
import com.google.cloud.spanner.ResultSet; | ||
import com.google.cloud.spanner.Statement; | ||
import org.apache.beam.sdk.schemas.Schema; | ||
|
||
class SpannerQuerySourceDef implements SpannerSourceDef { | ||
|
||
private final SpannerConfig config; | ||
private final Statement query; | ||
|
||
static SpannerQuerySourceDef create(SpannerConfig config, Statement query) { | ||
return new SpannerQuerySourceDef(config, query); | ||
} | ||
|
||
private SpannerQuerySourceDef(SpannerConfig config, Statement query) { | ||
this.config = config; | ||
this.query = query; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public Schema getBeamSchema() { | ||
Schema beamSchema; | ||
try (SpannerAccessor spannerAccessor = SpannerAccessor.getOrCreate(config)) { | ||
try (ReadContext readContext = spannerAccessor.getDatabaseClient().singleUse()) { | ||
ResultSet result = readContext.analyzeQuery(query, ReadContext.QueryAnalyzeMode.PLAN); | ||
result.next(); | ||
beamSchema = structTypeToBeamRowSchema(result.getMetadata().getRowType(), true); | ||
} | ||
} catch (Exception e) { | ||
throw new SpannerSchemaRetrievalException("Exception while trying to retrieve schema", e); | ||
} | ||
return beamSchema; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...orm/src/main/java/org/apache/beam/sdk/io/gcp/spanner/SpannerSchemaRetrievalException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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. | ||
*/ | ||
package org.apache.beam.sdk.io.gcp.spanner; | ||
|
||
/** Exception to signal that Spanner schema retrieval failed. */ | ||
public class SpannerSchemaRetrievalException extends RuntimeException { | ||
SpannerSchemaRetrievalException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
SpannerSchemaRetrievalException(String message) { | ||
super(message); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...gle-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/SpannerSourceDef.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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. | ||
*/ | ||
package org.apache.beam.sdk.io.gcp.spanner; | ||
|
||
import java.io.Serializable; | ||
import org.apache.beam.sdk.schemas.Schema; | ||
|
||
/** | ||
* Represents a source used for {@link SpannerIO#read()}. Currently, this could be either a table or | ||
* a query. Direct read sources are not yet supported. | ||
*/ | ||
interface SpannerSourceDef extends Serializable { | ||
|
||
/** | ||
* Extract the Beam {@link Schema} corresponding to this source. | ||
* | ||
* @return Beam schema of the source | ||
* @throws SpannerSchemaRetrievalException if schema retrieval fails | ||
*/ | ||
Schema getBeamSchema(); | ||
} |
63 changes: 63 additions & 0 deletions
63
...loud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/SpannerTableSourceDef.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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. | ||
*/ | ||
package org.apache.beam.sdk.io.gcp.spanner; | ||
|
||
import static org.apache.beam.sdk.io.gcp.spanner.StructUtils.structTypeToBeamRowSchema; | ||
|
||
import com.google.cloud.spanner.KeySet; | ||
import com.google.cloud.spanner.Options; | ||
import com.google.cloud.spanner.ReadContext; | ||
import com.google.cloud.spanner.ResultSet; | ||
import org.apache.beam.sdk.schemas.Schema; | ||
|
||
class SpannerTableSourceDef implements SpannerSourceDef { | ||
|
||
private final SpannerConfig config; | ||
private final String table; | ||
private final Iterable<String> columns; | ||
|
||
static SpannerTableSourceDef create( | ||
SpannerConfig config, String table, Iterable<String> columns) { | ||
return new SpannerTableSourceDef(config, table, columns); | ||
} | ||
|
||
private SpannerTableSourceDef(SpannerConfig config, String table, Iterable<String> columns) { | ||
this.table = table; | ||
this.config = config; | ||
this.columns = columns; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public Schema getBeamSchema() { | ||
Schema beamSchema; | ||
try (SpannerAccessor spannerAccessor = SpannerAccessor.getOrCreate(config)) { | ||
try (ReadContext readContext = spannerAccessor.getDatabaseClient().singleUse()) { | ||
ResultSet result = readContext.read(table, KeySet.all(), columns, Options.limit(1)); | ||
if (result.next()) { | ||
beamSchema = structTypeToBeamRowSchema(result.getMetadata().getRowType(), true); | ||
} else { | ||
throw new SpannerSchemaRetrievalException("Cannot find Spanner table."); | ||
} | ||
} | ||
} catch (Exception e) { | ||
throw new SpannerSchemaRetrievalException("Exception while trying to retrieve schema", e); | ||
} | ||
return beamSchema; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.