Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e403041

Browse files
committed
Allow dependency declarations for java.
1 parent 99b340f commit e403041

3 files changed

Lines changed: 77 additions & 4 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# coding=utf-8
2+
#
3+
# Licensed to the Apache Software Foundation (ASF) under one or more
4+
# contributor license agreements. See the NOTICE file distributed with
5+
# this work for additional information regarding copyright ownership.
6+
# The ASF licenses this file to You under the Apache License, Version 2.0
7+
# (the "License"); you may not use this file except in compliance with
8+
# the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
pipeline:
20+
type: chain
21+
transforms:
22+
- type: Create
23+
config:
24+
elements:
25+
- {sdk: MapReduce, year: 2004}
26+
- {sdk: MillWheel, year: 2008}
27+
- {sdk: Flume, year: 2010}
28+
- {sdk: Dataflow, year: 2014}
29+
- {sdk: Apache Beam, year: 2016}
30+
- type: MapToFields
31+
name: ToRoman
32+
config:
33+
language: java
34+
fields:
35+
tool_name: sdk
36+
year:
37+
callable: |
38+
import org.apache.beam.sdk.values.Row;
39+
import java.util.function.Function;
40+
import com.github.chaosfirebolt.converter.RomanInteger;
41+
42+
public class MyFunction implements Function<Row, String> {
43+
public String apply(Row row) {
44+
return RomanInteger.parse(
45+
String.valueOf(row.getInt64("year"))).toString();
46+
}
47+
}
48+
dependencies:
49+
- 'com.github.chaosfirebolt.converter:roman-numeral-converter:2.1.0'
50+
- type: LogForTesting
51+
52+
# Expected:
53+
# Row(tool_name='MapReduce', year='MMIV')
54+
# Row(tool_name='MillWheel', year='MMVIII')
55+
# Row(tool_name='Flume', year='MMX')
56+
# Row(tool_name='Dataflow', year='MMXIV')
57+
# Row(tool_name='Apache Beam', year='MMXVI')

sdks/python/apache_beam/yaml/yaml_provider.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,10 +356,13 @@ def cache_artifacts(self):
356356

357357

358358
class ExternalJavaProvider(ExternalProvider):
359-
def __init__(self, urns, jar_provider):
359+
def __init__(self, urns, jar_provider, classpath=None):
360360
super().__init__(
361-
urns, lambda: external.JavaJarExpansionService(jar_provider()))
361+
urns,
362+
lambda: external.JavaJarExpansionService(
363+
jar_provider(), classpath=classpath))
362364
self._jar_provider = jar_provider
365+
self._classpath = classpath
363366

364367
def available(self):
365368
# pylint: disable=subprocess-run-check
@@ -381,6 +384,15 @@ def try_decode(bs):
381384
def cache_artifacts(self):
382385
return [self._jar_provider()]
383386

387+
def _with_extra_dependencies(self, dependencies: Iterable[str]):
388+
jars = sum((
389+
external.JavaJarExpansionService._expand_jars(dep)
390+
for dep in dependencies), [])
391+
return ExternalJavaProvider(
392+
self._urns,
393+
jar_provider=self._jar_provider,
394+
classpath=(list(self._classpath or []) + list(jars)))
395+
384396

385397
@ExternalProvider.register_provider_type('python')
386398
def python(urns, provider_base_path, packages=()):

sdks/python/apache_beam/yaml/yaml_transform.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ def expand(pcolls):
368368
if pcoll in providers_by_input
369369
]
370370
provider = self.best_provider(spec, input_providers)
371-
extra_dependencies = extract_extra_dependencies(spec)
371+
extra_dependencies, spec = extract_extra_dependencies(spec)
372372
if extra_dependencies:
373373
provider = provider.with_extra_dependencies(frozenset(extra_dependencies))
374374

@@ -713,9 +713,13 @@ def extract_name(spec):
713713

714714
def extract_extra_dependencies(spec):
715715
deps = spec.get('config', {}).get('dependencies', [])
716+
if not deps:
717+
return [], spec
716718
if not isinstance(deps, list):
717719
raise TypeErrorError(f'Dependencies must be a list of strings, got {deps}')
718-
return deps
720+
return deps, dict(
721+
spec,
722+
config={k: v for k, v in spec['config'].items() if k != 'dependencies'})
719723

720724

721725
def push_windowing_to_roots(spec):

0 commit comments

Comments
 (0)