-
-
Notifications
You must be signed in to change notification settings - Fork 562
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: remove reverse proxy feature in bigquery example
- Loading branch information
Showing
3 changed files
with
79 additions
and
52 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
const axios = require('axios'); | ||
const { JWT } = require('google-auth-library'); | ||
const { BigQuery } = require('@google-cloud/bigquery'); | ||
const process = require('process'); | ||
|
||
|
||
const projectId = process.env.GOOGLE_CLOUD_PROJECT; | ||
if (!projectId) { | ||
|
@@ -8,27 +10,75 @@ if (!projectId) { | |
} | ||
|
||
const credentials_base64 = process.env.GOOGLE_APPLICATION_CREDENTIALS_BASE64; | ||
const credentials_json = Buffer.from(credentials_base64, 'base64').toString('utf-8'); | ||
if (!credentials_base64) { | ||
console.error('GOOGLE_APPLICATION_CREDENTIALS_BASE64 environment variable must be set.'); | ||
process.exit(1); | ||
} | ||
|
||
const credentials_json = Buffer.from(credentials_base64, 'base64').toString('utf-8'); | ||
const credentials = JSON.parse(credentials_json); | ||
|
||
// Configure the endpoint to our portal | ||
const bigqueryOptions = { | ||
projectId: projectId, | ||
apiEndpoint: 'http://127.0.0.1:8080', | ||
maxRetries: 100, | ||
credentials: credentials | ||
// Function to get Bearer token | ||
const getAuthToken = async () => { | ||
// Create a JWT client using the credentials | ||
const client = new JWT({ | ||
email: credentials.client_email, | ||
key: credentials.private_key, | ||
scopes: ['https://www.googleapis.com/auth/bigquery'], | ||
}); | ||
|
||
// Authorize the client and get the Bearer token | ||
const token = await client.authorize(); | ||
return token.access_token; | ||
}; | ||
|
||
// Create a BigQuery client | ||
const bigquery = new BigQuery(bigqueryOptions); | ||
// Custom BigQuery Client | ||
class CustomBigQueryClient extends BigQuery { | ||
constructor(projectID) { | ||
super(); | ||
this.projectId = projectID; | ||
} | ||
|
||
async request(reqOpts, callback) { | ||
try { | ||
const token = await getAuthToken(); | ||
const url = `http://127.0.0.1:8080/bigquery/v2/projects/${this.projectId}/${reqOpts.uri}`; | ||
const checkedURl = url.replace(/([^:]\/)\/+/g, "$1"); | ||
|
||
// When deleting dataset, body is sent as an object named qs | ||
const body = reqOpts.json || reqOpts.qs; | ||
|
||
const config = { | ||
method: reqOpts.method, | ||
url: checkedURl, | ||
headers: { | ||
...reqOpts.headers, | ||
'Authorization': `Bearer ${token}`, | ||
'Content-Type': 'application/json', | ||
'Host': 'bigquery.googleapis.com', | ||
}, | ||
data: body, | ||
}; | ||
|
||
const response = await axios(config); | ||
callback(null, response.data, response); | ||
} catch (error) { | ||
callback(error, null, null); | ||
} | ||
} | ||
} | ||
|
||
const bigQueryClient = new CustomBigQueryClient('effortless-cat-433609-h1'); | ||
|
||
async function createDataset(datasetId) { | ||
const [dataset] = await bigquery.createDataset(datasetId); | ||
console.log(`Creating Dataset ${datasetId}`); | ||
const [dataset] = await bigQueryClient.createDataset(datasetId); | ||
console.log(`Dataset ${dataset.id} created.`); | ||
} | ||
|
||
async function createTable(datasetId, tableId) { | ||
console.log(`Creating Table ${tableId} for dataset ${datasetId}`); | ||
|
||
const schema = [ | ||
{ name: 'name', type: 'STRING' }, | ||
{ name: 'age', type: 'INTEGER' }, | ||
|
@@ -39,20 +89,22 @@ async function createTable(datasetId, tableId) { | |
schema: schema | ||
}; | ||
|
||
const [table] = await bigquery | ||
const [table] = await bigQueryClient | ||
.dataset(datasetId) | ||
.createTable(tableId, options); | ||
|
||
console.log(`Table ${table.id} created.`); | ||
} | ||
|
||
async function insertData(datasetId, tableId) { | ||
console.log(`Inserting data to Table ${tableId} for dataset ${datasetId}`); | ||
|
||
const rows = [ | ||
{ name: 'John Doe', age: 30, email: '[email protected]' }, | ||
{ name: 'Jane Smith', age: 25, email: '[email protected]' } | ||
]; | ||
|
||
await bigquery | ||
await bigQueryClient | ||
.dataset(datasetId) | ||
.table(tableId) | ||
.insert(rows); | ||
|
@@ -61,13 +113,15 @@ async function insertData(datasetId, tableId) { | |
} | ||
|
||
async function queryData(datasetId, tableId) { | ||
console.log(`Querying data for Table ${tableId} for dataset ${datasetId}`); | ||
|
||
const query = ` | ||
SELECT name, age, email | ||
FROM \`${bigquery.projectId}.${datasetId}.${tableId}\` | ||
FROM \`${bigQueryClient.projectId}.${datasetId}.${tableId}\` | ||
WHERE age > 20 | ||
`; | ||
|
||
const [rows] = await bigquery.query({ query }); | ||
const [rows] = await bigQueryClient.query({ query }); | ||
|
||
console.log('Query Results:'); | ||
rows.forEach(row => { | ||
|
@@ -76,11 +130,12 @@ async function queryData(datasetId, tableId) { | |
} | ||
|
||
async function deleteDataset(datasetId) { | ||
await bigquery.dataset(datasetId).delete({ force: true }); // force: true deletes all tables within the dataset | ||
console.log(`Deleting dataset ${datasetId}`); | ||
await bigQueryClient.dataset(datasetId).delete({ force: true }); // force: true deletes all tables within the dataset | ||
console.log(`Dataset ${datasetId} deleted.`); | ||
} | ||
|
||
// Running all steps | ||
// Run the example | ||
(async () => { | ||
let datasetId = "ockam_" + (Math.random() + 1).toString(36).substring(7); | ||
const tableId = 'ockam_table'; | ||
|
@@ -102,3 +157,4 @@ async function deleteDataset(datasetId) { | |
|
||
await deleteDataset(datasetId); | ||
})(); | ||
|
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
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