예제 #1
0
def main(client, routine_id):
    # [START bigquery_create_routine]
    from google.cloud import bigquery
    from google.cloud import bigquery_v2

    # TODO(developer): Construct a BigQuery client object.
    # client = bigquery.Client()

    # TODO(developer): Choose a fully-qualified ID for the routine.
    # routine_id = "my-project.my_dataset.my_routine"

    routine = bigquery.Routine(
        routine_id,
        type_="SCALAR_FUNCTION",
        language="SQL",
        body="x * 3",
        arguments=[
            bigquery.RoutineArgument(
                name="x",
                data_type=bigquery_v2.types.StandardSqlDataType(
                    type_kind=bigquery_v2.enums.StandardSqlDataType.TypeKind.INT64
                ),
            )
        ],
    )

    routine = client.create_routine(routine)

    print("Created routine {}".format(routine.reference))
    # [END bigquery_create_routine]
    return routine
예제 #2
0
def routine_id(client, dataset_id):
    now = datetime.datetime.now()
    routine_id = "python_routine_sample_{}_{}".format(
        now.strftime("%Y%m%d%H%M%S"), uuid.uuid4().hex[:8]
    )

    routine = bigquery.Routine("{}.{}".format(dataset_id, routine_id))
    routine.type_ = "SCALAR_FUNCTION"
    routine.language = "SQL"
    routine.body = "x * 3"
    routine.arguments = [
        bigquery.RoutineArgument(
            name="x",
            data_type=bigquery_v2.types.StandardSqlDataType(
                type_kind=bigquery_v2.enums.StandardSqlDataType.TypeKind.INT64
            ),
        )
    ]

    routine = client.create_routine(routine)
    yield "{}.{}.{}".format(routine.project, routine.dataset_id, routine.routine_id)
    client.delete_routine(routine, not_found_ok=True)
예제 #3
0
def test_create_routine_ddl(capsys, client, random_routine_id):
    from .. import create_routine_ddl

    create_routine_ddl.create_routine_ddl(client, random_routine_id)
    routine = client.get_routine(random_routine_id)
    out, err = capsys.readouterr()

    assert "Created routine {}".format(random_routine_id) in out
    return routine
    assert routine.type_ == "SCALAR_FUNCTION"
    assert routine.language == "SQL"
    expected_arguments = [
        bigquery.RoutineArgument(
            name="arr",
            data_type=bigquery_v2.types.StandardSqlDataType(
                type_kind=bigquery_v2.enums.StandardSqlDataType.TypeKind.ARRAY,
                array_element_type=bigquery_v2.types.StandardSqlDataType(
                    type_kind=bigquery_v2.enums.StandardSqlDataType.TypeKind.
                    STRUCT,
                    struct_type=bigquery_v2.types.StandardSqlStructType(
                        fields=[
                            bigquery_v2.types.StandardSqlField(
                                name="name",
                                type=bigquery_v2.types.StandardSqlDataType(
                                    type_kind=bigquery_v2.enums.
                                    StandardSqlDataType.TypeKind.STRING),
                            ),
                            bigquery_v2.types.StandardSqlField(
                                name="val",
                                type=bigquery_v2.types.StandardSqlDataType(
                                    type_kind=bigquery_v2.enums.
                                    StandardSqlDataType.TypeKind.INT64),
                            ),
                        ]),
                ),
            ),
        )
    ]
    assert routine.arguments == expected_arguments