npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bigquery

v0.0.6

Published

This is a simple sdk for using google bigquery. Still working... if any problem, please let me know. Want to know what is BigQuery? Look my slide: http://www.slideshare.net/peihsinsu/google-bigquery-introduction

Downloads

272

Readme

bigquery

This is a simple sdk for using google bigquery. Still working... if any problem, please let me know. Want to know what is BigQuery? Look my slide: http://www.slideshare.net/peihsinsu/google-bigquery-introduction

Installation

npm install bigquery

Apply service account

Follow the doc: http://gappsnews.blogspot.tw/2013/10/connect-cloud-platform-bigquery-using.html

Convert p12 key (If use old version service account...)

From admin console, create a service account, save the client_secrets.json and it's key ex: Translate p12 to pem

openssl pkcs12 -in privatekey.p12 -out privatekey.pem -nocerts
openssl rsa -in privatekey.pem -out key.pem

or

openssl pkcs12 -in privatekey.p12 -nodes -nocerts > key.pem

Initial using Service Account json_file

If you are create the service account after 2015Q2, you will find the service account provide a json file formate download. The json file is like:

{
  "private_key_id": "e7************************************e8",
  "private_key": "-----BEGIN PRIVATE KEY-----\nMIIC***....skip...****AFW1Y\n-----END PRIVATE KEY-----\n",
  "client_email": "86*********8-if***************************[email protected]",
  "client_id": "86*********8-if***************************pq2.apps.googleusercontent.com",
  "type": "service_account"
}

The file is already include the secret file and the account information. We support to use the file for easy auth.

var bq = require('bigquery')

bq.init({
  json_file: '/path/to/your-service-account-json-file.json'
});

Initial using client_email directly

About 2015Q1 end, google start to deprecate the client_secret file download. You can change the init method like this:

bq.init({
  client_email: '[email protected]',
  key_pem: '/path-to-key.pem'
});

Initial using old client_secret.json (will deprecated)

Load bigquery lib, specify your project id then setup the service account and the client_secret.json file path, pem key file path for auth use.

var bq = require('bigquery')
  , fs = require('fs')
  , prjId = 'your-bigquery-project-id'; //you need to modify this

bq.init({
  client_secret: '/path-to-client_secret.json',
  key_pem: '/path-to-key.pem'
});

Do Query

Query the dataset that you have. The sample bellow is to query the public dataset of wikipedia.

bq.job.query(prjId, 'select count(*) from publicdata:samples.wikipedia', function(e,r,d){
  if(e) console.log(e);
  console.log(JSON.stringify(d));
});

Get Query Results

Retrieve results from a previously run query. Larger queries will timeout and respond with no data before the results are collected. Use this to retrieve the results. Below is an example of querying and waiting for results.

bq.job.query(prjId, 'select count(*) from publicdata:samples.wikipedia', function(e,r,d){
  if(e) console.log(e);
  if(d.jobIsComplete){
      console.log(JSON.stringify(d));
  } else {
       bq.job.getQueryResults(prjId, d.jobReference.jobId, function(e,r,d){
          console.log(JSON.stringify(d));
       }
  }
});

Load data

Before load data, you must create your dataset and table first. After that, you can use the following code to load data. (In this case test is the dataset id, testtb1 is the table name.)

var data = [
 {
   "insertId": "201403221228", //option
   "json": {
     "name": "simon",
     "sex": "M",
     "age": 35
   }
 }
];

bq.job.load(prjId, 'test', 'testtb1', data, function(e,r,d){
  if(e) console.log(e);
  console.log(JSON.stringify(d));
})

PS: The insertId use for prevent the duplicate insert of data.

Create Dataset & Table

Create dataset and table, the schema please ref: https://developers.google.com/bigquery/preparing-data-for-bigquery

bq.dataset.create(prjId, 'dataset_name', function(e,r,d){
  if(e) console.log(e);
  console.log(d);
});

var schema = {
  "fields": [
   {
    "name": "field1",
    "type": "string",
    "description": "test"
   },
   {
    "name": "field2",
    "type": "integer",
    "description": "test for int"
   }
  ]
 };
bq.table.create(prjId, 'dataset_name', 'table_name', schema, function(e,r,d){
  if(e) console.log(e);
  console.log(d);
});

Get Table

bq.table.get(prjId, 'test123', 'table_name', function(e,r,d){
  if(e) console.log(e);
  console.log(d);
});

Delete Table

bq.table.delete(prjId, 'dataset_name', 'table_name', function(e,r,d){
  if(e) console.log(e);
  console.log(d);
});

Get Dataset

bq.dataset.get(prjId, 'dataset_name', function(e,r,d){
  if(e) console.log(e);
  console.log(d);
});

Delete Dataset

bq.dataset.delete(prjId, 'dataset_name', 'table_name2', function(e,r,d){
  if(e) console.log(e);
  console.log(d);
});

Other request timeout parameters

If you want to add timeout parameter to restrict to your bigquery api request time. You can add timeout parameter to the init() like:

bq.init({
  client_secret: __dirname + '/client_secret.json',
  key_pem: __dirname + '/key.pem', 
  timeout:1000
});