function syncQuery(sqlQuery, projectId) { // Imports the Google Cloud client library const BigQuery = require('@google-cloud/bigquery'); /** * TODO(developer): Uncomment the following lines before running the sample. */ // const projectId = "your-project-id"; // const sqlQuery = "SELECT * FROM publicdata.samples.natality LIMIT 5;"; // Creates a client const bigquery = new BigQuery({ projectId: projectId, }); // Query options list: https://cloud.google.com/bigquery/docs/reference/v2/jobs/query const options = { query: sqlQuery, timeoutMs: 10000, // Time out after 10 seconds. useLegacySql: false, // Use standard SQL syntax for queries. }; // Runs the query bigquery .query(options) .then(results => { const rows = results[0]; console.log('Rows:'); rows.forEach(row => console.log(row)); }) .catch(err => { console.error('ERROR:', err); }); }
// Creates the new dataset async function executeQuery(date, end) { const query = createQuery(date, end); debug('Executing query', query); const options = { query, useLegacySql: true, }; const results = await bigquery.query(options); console.log('Fetched events from', chalk.blue(date.format('YYYY-MM-DD')), 'to', chalk.blue(end.format('YYYY-MM-DD')), '#rows', chalk.green(results[0].length)); const contents = results[0] .map(parsePayload) .map(normalizeEvent) if (argv.db) { await couchDB.insert(argv.dbName, contents); console.log('Stored #', chalk.green(contents.length), 'documents in couchdb', argv.dbName); } else { const fileContents = contents .map(row => JSON.stringify(row)) .join(',\n'); await util.promisify(fs.appendFile)(filepath, fileContents); } return results[0].length; }
function queryStackOverflow(projectId) { // [START bigquery_simple_app_deps] // Imports the Google Cloud client library const BigQuery = require('@google-cloud/bigquery'); // [END bigquery_simple_app_deps] // [START bigquery_simple_app_client] /** * TODO(developer): Uncomment the following lines before running the sample. */ // const projectId = "your-project-id"; // Creates a client const bigquery = new BigQuery({ projectId: projectId, }); // [END bigquery_simple_app_client] // [START bigquery_simple_app_query] // The SQL query to run const sqlQuery = `SELECT CONCAT( 'https://stackoverflow.com/questions/', CAST(id as STRING)) as url, view_count FROM \`bigquery-public-data.stackoverflow.posts_questions\` WHERE tags like '%google-bigquery%' ORDER BY view_count DESC LIMIT 10`; // Query options list: https://cloud.google.com/bigquery/docs/reference/v2/jobs/query const options = { query: sqlQuery, useLegacySql: false, // Use standard SQL syntax for queries. }; // Runs the query bigquery .query(options) .then(results => { const rows = results[0]; printResult(rows); }) .catch(err => { console.error('ERROR:', err); }); // [END bigquery_simple_app_query] }