本文整理匯總了Python中airflow.operators.bash_operator.BashOperator類的典型用法代碼示例。如果您正苦於以下問題:Python BashOperator類的具體用法?Python BashOperator怎麽用?Python BashOperator使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了BashOperator類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_return_value
def test_return_value(self):
bash_operator = BashOperator(
bash_command='echo "stdout"',
task_id='test_return_value',
dag=None
)
return_value = bash_operator.execute(context={})
self.assertEqual(return_value, u'stdout')
示例2: test_echo_env_variables
def test_echo_env_variables(self):
"""
Test that env variables are exported correctly to the
task bash environment.
"""
now = datetime.utcnow()
now = now.replace(tzinfo=timezone.utc)
self.dag = DAG(
dag_id='bash_op_test', default_args={
'owner': 'airflow',
'retries': 100,
'start_date': DEFAULT_DATE
},
schedule_interval='@daily',
dagrun_timeout=timedelta(minutes=60))
self.dag.create_dagrun(
run_id='manual__' + DEFAULT_DATE.isoformat(),
execution_date=DEFAULT_DATE,
start_date=now,
state=State.RUNNING,
external_trigger=False,
)
import tempfile
with tempfile.NamedTemporaryFile() as f:
fname = f.name
t = BashOperator(
task_id='echo_env_vars',
dag=self.dag,
bash_command='echo $AIRFLOW_HOME>> {0};'
'echo $PYTHONPATH>> {0};'
'echo $AIRFLOW_CTX_DAG_ID >> {0};'
'echo $AIRFLOW_CTX_TASK_ID>> {0};'
'echo $AIRFLOW_CTX_EXECUTION_DATE>> {0};'
'echo $AIRFLOW_CTX_DAG_RUN_ID>> {0};'.format(fname)
)
original_AIRFLOW_HOME = os.environ['AIRFLOW_HOME']
os.environ['AIRFLOW_HOME'] = 'MY_PATH_TO_AIRFLOW_HOME'
t.run(DEFAULT_DATE, DEFAULT_DATE,
ignore_first_depends_on_past=True, ignore_ti_state=True)
with open(fname, 'r') as fr:
output = ''.join(fr.readlines())
self.assertIn('MY_PATH_TO_AIRFLOW_HOME', output)
# exported in run_unit_tests.sh as part of PYTHONPATH
self.assertIn('tests/test_utils', output)
self.assertIn('bash_op_test', output)
self.assertIn('echo_env_vars', output)
self.assertIn(DEFAULT_DATE.isoformat(), output)
self.assertIn('manual__' + DEFAULT_DATE.isoformat(), output)
os.environ['AIRFLOW_HOME'] = original_AIRFLOW_HOME
示例3: test_return_value_to_xcom
def test_return_value_to_xcom(self):
bash_operator = BashOperator(
bash_command='echo "stdout"',
xcom_push=True,
task_id='test_return_value_to_xcom',
dag=None
)
xcom_return_value = bash_operator.execute(context={})
self.assertEqual(xcom_return_value, u'stdout')
示例4: datetime
# 'queue': 'bash_queue',
# 'pool': 'backfill',
# 'priority_weight': 10,
# 'end_date': datetime(2016, 4, 24),
}
dag = DAG('undeploy_prediction_pmml', default_args=default_args)
# TODO: dockerFileTag and dockerFilePath should be passed in from webhook
switch_to_aws = BashOperator(
task_id='switch_to_aws',
bash_command='sudo kubectl config use-context awsdemo',
dag=dag)
undeploy_container_aws = BashOperator(
task_id='undeploy_container_to_aws',
bash_command='sudo kubectl delete prediction-pmml',
dag=dag)
switch_to_gcp = BashOperator(
task_id='switch_to_gcp',
bash_command='sudo kubectl config use-context gcpdemo',
dag=dag)
undeploy_container_gcp = BashOperator(
task_id='undeploy_container_gcp',
bash_command='sudo kubectl delete prediction-pmml',
dag=dag)
# Setup Airflow DAG
undeploy_container_aws.set_upstream(switch_to_aws)
switch_to_gcp.set_upstream(undeploy_container_aws)
示例5: my_py_command
def my_py_command(ds, **kwargs):
# Print out the "foo" param passed in via
# `airflow test example_passing_params_via_test_command run_this <date>
# -tp '{"foo":"bar"}'`
if kwargs["test_mode"]:
print(" 'foo' was passed in via test={} command : kwargs[params][foo] \
= {}".format(kwargs["test_mode"], kwargs["params"]["foo"]))
# Print out the value of "miff", passed in below via the Python Operator
print(" 'miff' was passed in via task params = {}".format(kwargs["params"]["miff"]))
return 1
my_templated_command = """
echo " 'foo was passed in via Airflow CLI Test command with value {{ params.foo }} "
echo " 'miff was passed in via BashOperator with value {{ params.miff }} "
"""
run_this = PythonOperator(
task_id='run_this',
provide_context=True,
python_callable=my_py_command,
params={"miff":"agg"},
dag=dag)
also_run_this = BashOperator(
task_id='also_run_this',
bash_command=my_templated_command,
params={"miff":"agg"},
dag=dag)
also_run_this.set_upstream(run_this)
示例6: datetime
# 'pool': 'backfill',
# 'priority_weight': 10,
# 'end_date': datetime(2016, 4, 24),
}
dag = DAG('update_prediction_pmml', default_args=default_args)
#, schedule_interval=timedelta(0))
pull_git = BashOperator(
task_id='pull_git',
bash_command='cd /root/pipeline && git pull',
dag=dag)
# TODO: dockerFileTag and dockerFilePath should be passed in from webhook
build_image = BashOperator(
task_id='build_docker_image',
bash_command='sudo docker build -t fluxcapacitor/prediction-pmml /root/pipeline/prediction.ml/pmml/',
dag=dag)
push_image = BashOperator(
task_id='push_docker_image',
bash_command='sudo docker push fluxcapacitor/prediction-pmml',
dag=dag)
#switch_to_aws = BashOperator(
# task_id='switch_to_aws',
# bash_command='sudo kubectl config use-context awsdemo',
# dag=dag)
update_container_aws = BashOperator(
task_id='update_container_aws',
bash_command='kubectl rolling-update prediction-pmml --context=awsdemo --image-pull-policy=Always --image=fluxcapacitor/prediction-pmml',
示例7: create_sde_tasks
def create_sde_tasks(dag,
folder,
layer,
datasd_name,
md,
path_to_file,
sde_to_shp):
"""Dynamically create SDE Airflow tasks.
dag: DAG defined in _dags file.
folder: subfolder in the sde folder on S3.
layer: layer name.
datasd_name: layer name + _datasd.
md: name of md file on Seaboard.
path_to_file: poseidon path + datasd_name.
sde_to_shp: _jobs specific sde_to_shp function
"""
#: Latest Only Operator for sde layer
sde_latest_only = LatestOnlyOperator(task_id='{layer}_latest_only'
.format(layer=layer),
dag=dag)
#: Convert sde table to shapefile format
to_shp = PythonOperator(
task_id='{layer}_to_shp'.format(layer=layer),
python_callable=sde_to_shp,
on_failure_callback=notify,
on_retry_callback=notify,
on_success_callback=notify,
dag=dag)
#: Convert shapefile to GeoJSON format
to_geojson = BashOperator(
task_id='{layer}_to_geojson'.format(layer=layer),
bash_command=shp_to_geojson(path_to_file),
on_failure_callback=notify,
on_retry_callback=notify,
on_success_callback=notify,
dag=dag)
#: Convert shapefile to TopoJSON format
to_topojson = BashOperator(
task_id='{layer}_to_topojson'.format(layer=layer),
bash_command=shp_to_topojson(path_to_file),
on_failure_callback=notify,
on_retry_callback=notify,
on_success_callback=notify,
dag=dag)
#: Compress shapefile components
to_zip = PythonOperator(
task_id='{layer}_shp_to_zip'.format(layer=layer),
python_callable=shp_to_zip,
op_kwargs={'datasd_name': datasd_name},
on_failure_callback=notify,
on_retry_callback=notify,
on_success_callback=notify,
dag=dag)
#: Upload shapefile to S3
shp_to_S3 = S3FileTransferOperator(
task_id='{layer}_shp_to_S3'.format(layer=layer),
source_base_path=conf['prod_data_dir'],
source_key='{datasd_name}.zip'.format(datasd_name=datasd_name),
dest_s3_conn_id=conf['default_s3_conn_id'],
dest_s3_bucket=conf['dest_s3_bucket'],
dest_s3_key='sde/{folder}/{datasd_name}.zip'
.format(folder=folder, datasd_name=datasd_name),
on_failure_callback=notify,
on_retry_callback=notify,
on_success_callback=notify,
replace=True,
dag=dag)
#: Upload geojson to S3
geojson_to_S3 = S3FileTransferOperator(
task_id='{layer}_geojson_to_S3'.format(layer=layer),
source_base_path=conf['prod_data_dir'],
source_key='{datasd_name}.geojson'.format(datasd_name=datasd_name),
dest_s3_conn_id=conf['default_s3_conn_id'],
dest_s3_bucket=conf['dest_s3_bucket'],
dest_s3_key='sde/{folder}/{datasd_name}.geojson'
.format(folder=folder, datasd_name=datasd_name),
on_failure_callback=notify,
on_retry_callback=notify,
on_success_callback=notify,
replace=True,
dag=dag)
#: Upload topojson to S3
topojson_to_S3 = S3FileTransferOperator(
task_id='{layer}_topojson_to_S3'.format(layer=layer),
source_base_path=conf['prod_data_dir'],
source_key='{datasd_name}.topojson'.format(datasd_name=datasd_name),
dest_s3_conn_id=conf['default_s3_conn_id'],
dest_s3_bucket=conf['dest_s3_bucket'],
dest_s3_key='sde/{folder}/{datasd_name}.topojson'
.format(folder=folder, datasd_name=datasd_name),
on_failure_callback=notify,
on_retry_callback=notify,
#.........這裏部分代碼省略.........
示例8: get_seaboard_update_dag
dag=dag)
#: Update portal modified date
update_code_enf_md = get_seaboard_update_dag('code-enforcement-violations.md', dag)
#: Execution rules
#: dsd_code_enf_latest_only must run before get_code_enf_files
get_code_enf_files.set_upstream(dsd_ce_latest_only)
for i in fname_list:
#: Create fme shell command
build_csv_task = BashOperator(
task_id='get_' + i,
bash_command=get_bash_command(i),
on_failure_callback=notify,
on_retry_callback=notify,
on_success_callback=notify,
dag=dag)
#: Set Task as Downstream for downloading files
build_csv_task.set_upstream(get_code_enf_files)
#: Create S3 Upload task
s3_task = S3FileTransferOperator(
task_id='upload_' + i,
source_base_path=conf['prod_data_dir'],
source_key=i + '_datasd.csv',
dest_s3_bucket=conf['dest_s3_bucket'],
dest_s3_conn_id=conf['default_s3_conn_id'],
dest_s3_key='dsd/' + i + '_datasd.csv',
示例9: DAG
start_date = general.start_date['pd_cfs']
dag = DAG(
dag_id='pd_cfs', default_args=args, start_date=start_date, schedule_interval=schedule['pd_cfs'])
#: Latest Only Operator for pd_cfs
pd_cfs_latest_only = LatestOnlyOperator(
task_id='pd_cfs_latest_only', dag=dag)
#: Get CFS data from FTP and save to temp folder
get_cfs_data = BashOperator(
task_id='get_cfs_data',
bash_command=get_cfs_data(),
on_failure_callback=notify,
on_retry_callback=notify,
on_success_callback=notify,
dag=dag)
#: Process CFS data and save result to prod folder
process_cfs_data = PythonOperator(
task_id='process_cfs_data',
python_callable=process_cfs_data,
on_failure_callback=notify,
on_retry_callback=notify,
on_success_callback=notify,
dag=dag)
#: Upload prod file to S3
cfs_to_S3 = S3FileTransferOperator(
示例10: DAG
# 'on_success_callback': some_other_function,
# 'on_retry_callback': another_function,
# 'trigger_rule': 'all_success'
}
dag = DAG(
'tutorial',
default_args=default_args,
description='A simple tutorial DAG',
schedule_interval=timedelta(days=1),
)
# t1, t2 and t3 are examples of tasks created by instantiating operators
t1 = BashOperator(
task_id='print_date',
bash_command='date',
dag=dag,
)
t1.doc_md = """\
#### Task Documentation
You can document your task using the attributes `doc_md` (markdown),
`doc` (plain text), `doc_rst`, `doc_json`, `doc_yaml` which gets
rendered in the UI's Task Instance Details page.
![img](http://montcs.bloomu.edu/~bobmon/Semesters/2012-01/491/import%20soul.png)
"""
dag.doc_md = __doc__
t2 = BashOperator(
task_id='sleep',
示例11: DAG
start_date = general.start_date['tsw_integration']
#: Dag spec
dag = DAG(dag_id='tsw_integration', default_args=args, start_date=start_date, schedule_interval=schedule)
violations_latest_only = LatestOnlyOperator(task_id='violations_latest_only', dag=dag)
# VPM Extraction Support Tasks
#: Download VPM dump from FTP
get_vpm_violations = BashOperator(
task_id='get_vpm_violations',
bash_command=get_vpm_violations_wget(),
on_failure_callback=notify,
on_retry_callback=notify,
on_success_callback=notify,
dag=dag)
#: Download VPM dump from FTP
#get_vpm_dump = BashOperator(
# task_id='get_vpm_dump',
# bash_command=ftp_download_wget(),
# on_failure_callback=notify,
# on_retry_callback=notify,
# on_success_callback=notify,
# dag=dag)
#
#
示例12: BashOperator
dag=BAKE_OFF_PIPE,
task_id="{region}_split".format(**locals())
)
freebayes_command = """freebayes -f {{ reference }} --vcf {{ outfile }} --targets {{ region }} {{ opts }} {{ in_bam }}"""
freebayes_operators = {}
for toople in chromosome_split_operators.iteritems():
region, operator = toople
outfile = "{WORK_DIR}/{region}.vcf"
freebayes_by_region = BashOperator(bash_command=freebayes_command,
params={
'reference': "/path/to/human.fasta",
'outfile': outfile,
'region': region,
'opts': default_args['freebayes'],
'in_bam': "{WORK_DIR}/{region}.bam".format(**locals())
},
dag=BAKE_OFF_PIPE,
task_id="{region}_freebayes".format(**locals())
)
freebayes_operators[region] = freebayes_by_region
freebayes_by_region.set_upstream(operator)
# now merge
vcf_concat_command = """vcf-concat-parts {{ in_files }} | vcf-sort > {{ outfile }}"""
infiles = []
for toople in freebayes_operators.iteritems():
region, operator = toople
infiles.append("{WORK_DIR}/{region}.vcf".format(**locals()))
示例13: DAG
#COUNTRY='PL'
dag = DAG('project-workflow',description='Project Workflow DAG',
schedule_interval = '*/5 0 * * *',
start_date=datetime(2017,7,1),
catchup=False)
xlsx_to_csv_task = BashOperator(
task_id='xlsx_to_csv',
bash_command='"$src"/test.sh "$country" 2nd_param_xlsx',
env={'src': SRC, 'country': COUNTRY},
dag=dag)
merge_command = SRC + '/test.sh ' + COUNTRY + ' 2nd_param_merge'
merge_task = BashOperator(
task_id='merge',
bash_command=merge_command ,
dag=dag)
my_templated_command = """
{{ params.src }}/test.sh {{ params.country}} 2nd_param_cleansing
"""
cleansing_task = BashOperator(
task_id='cleansing',
bash_command=my_templated_command,
params={'src': SRC, 'country': COUNTRY},
dag=dag)
x1_task = BashOperator(
task_id='x1',
bash_command='sleep 1 && echo [x1 start]',
dag=dag)
示例14: BashOperator
# Run a simple PySpark Script
pyspark_local_task_one = BashOperator(
task_id = "pyspark_local_task_one",
bash_command = """spark-submit \
--master {{ params.master }}
{{ params.base_path }}/{{ params.filename }} {{ ts }} {{ params.base_path }}""",
params = {
"master": "local[8]",
"filename": "ch02/pyspark_task_one.py",
"base_path": "{}/".format(project_home)
},
dag=dag
)
# Run another simple PySpark Script that depends on the previous one
pyspark_local_task_two = BashOperator(
task_id = "pyspark_local_task_two",
bash_command = """spark-submit \
--master {{ params.master }}
{{ params.base_path }}/{{ params.filename }} {{ ts }} {{ params.base_path }}""",
params = {
"master": "local[8]",
"filename": "ch02/pyspark_task_two.py",
"base_path": "{}/".format(project_home)
},
dag=dag
)
# Add the dependency from the second to the first task
pyspark_local_task_two.set_upstream(pyspark_local_task_one)
示例15: DAG
dag = DAG(
dag_id='parking_meters',
default_args=args,
start_date=start_date,
schedule_interval=schedule)
#: Latest Only Operator for parking meters
parking_meters_latest_only = LatestOnlyOperator(
task_id='parking_meters_latest_only', dag=dag)
#: Downloads all parking files from FTP
get_parking_files = BashOperator(
task_id='get_parking_files',
bash_command=ftp_download_wget(),
on_failure_callback=notify,
on_retry_callback=notify,
on_success_callback=notify,
dag=dag)
#: Joins downloaded files from ftp to production
build_prod_file = PythonOperator(
task_id='build_prod_file',
python_callable=build_prod_file,
provide_context=True,
on_failure_callback=notify,
on_retry_callback=notify,
on_success_callback=notify,
dag=dag)