import pyodbc
import os
import sys
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "H:\\venv\\SQLBDtoBigQuery\\key.json"
sqlSelect = "SELECT * FROM EMP"
connectionString = f"DRIVER={{SQL Server}};server={server};database={database};uid={username};pwd={password}"
conn = pyodbc.connect(connectionString)
cursor = conn.cursor()
cursor = conn.cursor().execute(sqlSelect)
columns = [column[0] for column in cursor.description]
print(columns)
results = []
for row in cursor.fetchall():
results.append(dict(zip(columns, row)))
def publish_messages(project_id, topic_name, inbound_data):
"""Publishes multiple messages to a Pub/Sub topic."""
# [START pubsub_quickstart_publisher]
# [START pubsub_publish]
from google.cloud import pubsub_v1
# TODO project_id = "Your Google Cloud Project ID"
# TODO topic_name = "Your Pub/Sub topic name"
publisher = pubsub_v1.PublisherClient()
# The `topic_path` method creates a fully qualified identifier
# in the form `projects/{project_id}/topics/{topic_name}`
topic_path = publisher.topic_path(project_id, topic_name)
for row in inbound_data:
# Data must be a bytestring
str_data=row
data = str_data.encode('utf-8')
# When you publish a message, the client returns a future.
future = publisher.publish(topic_path, attribute=data)
print('Published messages.')