diff --git a/README.md b/README.md index 2efa790df4..4361d4daa8 100644 --- a/README.md +++ b/README.md @@ -280,7 +280,7 @@ analytics data warehouse. View the [BigQuery Node.js samples][bigquery_samples]. [bigquery_docs]: https://cloud.google.com/bigquery/docs/ -[bigquery_samples]: bigquery +[bigquery_samples]: https://github.com/googleapis/nodejs-bigquery/tree/master/samples #### Google Cloud Pub/Sub @@ -356,7 +356,7 @@ content. View the [Cloud Vision API Node.js samples][vision_samples]. [vision_docs]: https://cloud.google.com/vision/docs/ -[vision_samples]: vision +[vision_samples]: https://github.com/googleapis/nodejs-vision/tree/master/samples ### Management Tools diff --git a/appengine/cloudtasks/createTask.js b/appengine/cloudtasks/createTask.js index 4ac341a9ae..03312bc7b2 100644 --- a/appengine/cloudtasks/createTask.js +++ b/appengine/cloudtasks/createTask.js @@ -127,5 +127,4 @@ if (module === require.main) { createTask(opts.project, opts.location, opts.queue, opts); } -exports.authorize = authorize; exports.createTask = createTask; diff --git a/bigquery/README.md b/bigquery/README.md index e847a9ea3d..40ff4dd944 100644 --- a/bigquery/README.md +++ b/bigquery/README.md @@ -1,160 +1,5 @@ -Google Cloud Platform logo +Samples for the [BigQuery API Node.js Client][client] have moved to +[github.com/googleapis/nodejs-bigquery/tree/master/samples/][samples]. -# Google BigQuery Node.js Samples - -[![Build](https://storage.googleapis.com/cloud-docs-samples-badges/GoogleCloudPlatform/nodejs-docs-samples/nodejs-docs-samples-bigquery.svg)]() - -[BigQuery](https://cloud.google.com/bigquery/docs) is Google's fully managed, petabyte scale, low cost analytics data warehouse. BigQuery is NoOps—there is no infrastructure to manage and you don't need a database administrator—so you can focus on analyzing data to find meaningful insights, use familiar SQL, and take advantage of our pay-as-you-go model. - -## Table of Contents - -* [Setup](#setup) -* [Samples](#samples) - * [Datasets](#datasets) - * [Tables](#tables) - * [Queries](#queries) -* [Running the tests](#running-the-tests) - -## Setup - -1. Read [Prerequisites][prereq] and [How to run a sample][run] first. -1. Install dependencies: - - With **npm**: - - npm install - - With **yarn**: - - yarn install - -[prereq]: ../README.md#prerequisites -[run]: ../README.md#how-to-run-a-sample - -## Samples - -### Datasets - -View the [documentation][datasets_0_docs] or the [source code][datasets_0_code]. - -__Usage:__ `node datasets.js --help` - -``` -Commands: - create Creates a new dataset. - delete Deletes a dataset. - list Lists datasets. - -Options: - --projectId, -p The Project ID to use. Defaults to the value of the GCLOUD_PROJECT or GOOGLE_CLOUD_PROJECT - environment variables. [string] - --help Show help [boolean] - -Examples: - node datasets.js create my_dataset Creates a new dataset named "my_dataset". - node datasets.js delete my_dataset Deletes a dataset named "my_dataset". - node datasets.js list Lists all datasets in the project specified by the - GCLOUD_PROJECT or GOOGLE_CLOUD_PROJECT environments variables. - node datasets.js list --projectId=bigquery-public-data Lists all datasets in the "bigquery-public-data" project. - -For more information, see https://cloud.google.com/bigquery/docs -``` - -[datasets_0_docs]: https://cloud.google.com/bigquery/docs -[datasets_0_code]: datasets.js - -### Tables - -View the [documentation][tables_1_docs] or the [source code][tables_1_code]. - -__Usage:__ `node tables.js --help` - -``` -Commands: - create Creates a new table. - list Lists all tables in a dataset. - delete Deletes a table. - copy Makes a copy of a table. - - browse Lists rows in a table. - import Imports data from a local file into a table. - import-gcs Imports data from a Google Cloud Storage file into a - table. - export Export a table from BigQuery to Google Cloud Storage. - insert Insert a JSON array (as a string or newline-delimited - file) into a BigQuery table. - -Options: - --projectId, -p The Project ID to use. Defaults to the value of the GCLOUD_PROJECT or GOOGLE_CLOUD_PROJECT - environment variables. [string] - --help Show help [boolean] - -Examples: - node tables.js create my_dataset my_table "Name:string, Creates a new table named "my_table" in "my_dataset". - Age:integer, Weight:float, IsMagic:boolean" - node tables.js list my_dataset Lists tables in "my_dataset". - node tables.js browse my_dataset my_table Displays rows from "my_table" in "my_dataset". - node tables.js delete my_dataset my_table Deletes "my_table" from "my_dataset". - node tables.js import my_dataset my_table ./data.csv Imports a local file into a table. - node tables.js import-gcs my_dataset my_table my-bucket Imports a GCS file into a table. - data.csv - node tables.js export my_dataset my_table my-bucket my-file Exports my_dataset:my_table to gcs://my-bucket/my-file - as raw CSV. - node tables.js export my_dataset my_table my-bucket my-file Exports my_dataset:my_table to gcs://my-bucket/my-file - -f JSON --gzip as gzipped JSON. - node tables.js insert my_dataset my_table json_string Inserts the JSON array represented by json_string into - my_dataset:my_table. - node tables.js insert my_dataset my_table json_file Inserts the JSON objects contained in json_file (one per - line) into my_dataset:my_table. - node tables.js copy src_dataset src_table dest_dataset Copies src_dataset:src_table to dest_dataset:dest_table. - dest_table - -For more information, see https://cloud.google.com/bigquery/docs -``` - -[tables_1_docs]: https://cloud.google.com/bigquery/docs -[tables_1_code]: tables.js - -### Queries - -View the [documentation][queries_2_docs] or the [source code][queries_2_code]. - -__Usage:__ `node queries.js --help` - -``` -Commands: - sync Run the specified synchronous query. - async Start the specified asynchronous query. - shakespeare Queries a public Shakespeare dataset. - -Options: - --projectId, -p The Project ID to use. Defaults to the value of the GCLOUD_PROJECT or GOOGLE_CLOUD_PROJECT - environment variables. [string] - --help Show help [boolean] - -Examples: - node queries.js sync "SELECT * FROM Synchronously queries the natality dataset. - publicdata.samples.natality LIMIT 5;" - node queries.js async "SELECT * FROM Queries the natality dataset as a job. - publicdata.samples.natality LIMIT 5;" - node queries.js shakespeare Queries a public Shakespeare dataset. - -For more information, see https://cloud.google.com/bigquery/docs -``` - -[queries_2_docs]: https://cloud.google.com/bigquery/docs -[queries_2_code]: queries.js - -## Running the tests - -1. Set the **GCLOUD_PROJECT** and **GOOGLE_APPLICATION_CREDENTIALS** environment variables. - -1. Run the tests: - - With **npm**: - - npm test - - With **yarn**: - - yarn test +[client]: https://github.com/googleapis/nodejs-bigquery +[samples]: https://github.com/googleapis/nodejs-bigquery/tree/master/samples diff --git a/bigquery/datasets.js b/bigquery/datasets.js deleted file mode 100644 index 7b543fb0b1..0000000000 --- a/bigquery/datasets.js +++ /dev/null @@ -1,143 +0,0 @@ -/** - * Copyright 2017, Google, Inc. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; - -function createDataset (datasetId, projectId) { - // [START bigquery_create_dataset] - // Imports the Google Cloud client library - const BigQuery = require('@google-cloud/bigquery'); - - // The project ID to use, e.g. "your-project-id" - // const projectId = "your-project-id"; - - // Instantiates a client - const bigquery = BigQuery({ - projectId: projectId - }); - - // The ID for the new dataset, e.g. "my_new_dataset" - // const datasetId = "my_new_dataset"; - - // Creates a new dataset - bigquery.createDataset(datasetId) - .then((results) => { - const dataset = results[0]; - console.log(`Dataset ${dataset.id} created.`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END bigquery_create_dataset] -} - -function deleteDataset (datasetId, projectId) { - // [START bigquery_delete_dataset] - // Imports the Google Cloud client library - const BigQuery = require('@google-cloud/bigquery'); - - // The project ID to use, e.g. "your-project-id" - // const projectId = "your-project-id"; - - // Instantiates a client - const bigquery = BigQuery({ - projectId: projectId - }); - - // The ID of the dataset to delete, e.g. "my_new_dataset" - // const datasetId = "my_new_dataset"; - - // Creates a reference to the existing dataset - const dataset = bigquery.dataset(datasetId); - - // Deletes the dataset - dataset.delete() - .then(() => { - console.log(`Dataset ${dataset.id} deleted.`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END bigquery_delete_dataset] -} - -function listDatasets (projectId) { - // [START bigquery_list_datasets] - // Imports the Google Cloud client library - const BigQuery = require('@google-cloud/bigquery'); - - // The project ID to use, e.g. "your-project-id" - // const projectId = "your-project-id"; - - // Instantiates a client - const bigquery = BigQuery({ - projectId: projectId - }); - - // Lists all datasets in the specified project - bigquery.getDatasets() - .then((results) => { - const datasets = results[0]; - console.log('Datasets:'); - datasets.forEach((dataset) => console.log(dataset.id)); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END bigquery_list_datasets] -} - -const cli = require(`yargs`) - .demand(1) - .options({ - projectId: { - alias: 'p', - default: process.env.GCLOUD_PROJECT || process.env.GOOGLE_CLOUD_PROJECT, - description: 'The Project ID to use. Defaults to the value of the GCLOUD_PROJECT or GOOGLE_CLOUD_PROJECT environment variables.', - requiresArg: true, - type: 'string' - } - }) - .command( - `create `, - `Creates a new dataset.`, - {}, - (opts) => createDataset(opts.datasetId, opts.projectId) - ) - .command( - `delete `, - `Deletes a dataset.`, - {}, - (opts) => deleteDataset(opts.datasetId, opts.projectId) - ) - .command( - `list`, - `Lists datasets.`, - {}, - (opts) => listDatasets(opts.projectId) - ) - .example(`node $0 create my_dataset`, `Creates a new dataset named "my_dataset".`) - .example(`node $0 delete my_dataset`, `Deletes a dataset named "my_dataset".`) - .example(`node $0 list`, `Lists all datasets in the project specified by the GCLOUD_PROJECT or GOOGLE_CLOUD_PROJECT environments variables.`) - .example(`node $0 list --projectId=bigquery-public-data`, `Lists all datasets in the "bigquery-public-data" project.`) - .wrap(120) - .recommendCommands() - .epilogue(`For more information, see https://cloud.google.com/bigquery/docs`) - .help() - .strict(); - -if (module === require.main) { - cli.parse(process.argv.slice(2)); -} diff --git a/bigquery/package.json b/bigquery/package.json deleted file mode 100644 index 82b7700272..0000000000 --- a/bigquery/package.json +++ /dev/null @@ -1,59 +0,0 @@ -{ - "name": "nodejs-docs-samples-bigquery", - "version": "0.0.1", - "private": true, - "license": "Apache-2.0", - "author": "Google Inc.", - "repository": { - "type": "git", - "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" - }, - "engines": { - "node": ">=4.3.2" - }, - "scripts": { - "lint": "samples lint", - "pretest": "npm run lint", - "test": "samples test run --cmd ava -- -T 3m --verbose system-test/*.test.js" - }, - "dependencies": { - "@google-cloud/bigquery": "0.9.6", - "@google-cloud/storage": "1.2.1", - "yargs": "8.0.2" - }, - "devDependencies": { - "@google-cloud/nodejs-repo-tools": "1.4.17", - "ava": "0.21.0", - "proxyquire": "1.8.0", - "sinon": "3.2.0", - "uuid": "3.1.0" - }, - "cloud-repo-tools": { - "requiresKeyFile": true, - "requiresProjectId": true, - "product": "bigquery", - "samples": [ - { - "id": "datasets", - "name": "Datasets", - "file": "datasets.js", - "docs_link": "https://cloud.google.com/bigquery/docs", - "usage": "node datasets.js --help" - }, - { - "id": "tables", - "name": "Tables", - "file": "tables.js", - "docs_link": "https://cloud.google.com/bigquery/docs", - "usage": "node tables.js --help" - }, - { - "id": "queries", - "name": "Queries", - "file": "queries.js", - "docs_link": "https://cloud.google.com/bigquery/docs", - "usage": "node queries.js --help" - } - ] - } -} diff --git a/bigquery/queries.js b/bigquery/queries.js deleted file mode 100644 index 3bcfa8348c..0000000000 --- a/bigquery/queries.js +++ /dev/null @@ -1,223 +0,0 @@ -/** - * Copyright 2017, Google, Inc. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; - -// [START bigquery_simple_app_all] -function printResult (rows) { - // [START bigquery_simple_app_print] - console.log('Query Results:'); - rows.forEach(function (row) { - let str = ''; - for (let key in row) { - if (str) { - str = `${str}\n`; - } - str = `${str}${key}: ${row[key]}`; - } - console.log(str); - }); - // [END bigquery_simple_app_print] -} - -function queryShakespeare (projectId) { - // [START bigquery_simple_app_query] - // Imports the Google Cloud client library - const BigQuery = require('@google-cloud/bigquery'); - - // The project ID to use, e.g. "your-project-id" - // const projectId = "your-project-id"; - - // The SQL query to run - const sqlQuery = `SELECT - corpus, COUNT(*) as unique_words - FROM publicdata.samples.shakespeare - GROUP BY - corpus - ORDER BY - unique_words DESC LIMIT 10;`; - - // Instantiates a client - const bigquery = BigQuery({ - projectId: projectId - }); - - // 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] -} -// [END bigquery_simple_app_all] - -function syncQuery (sqlQuery, projectId) { - // [START bigquery_sync_query] - // Imports the Google Cloud client library - const BigQuery = require('@google-cloud/bigquery'); - - // The project ID to use, e.g. "your-project-id" - // const projectId = "your-project-id"; - - // The SQL query to run, e.g. "SELECT * FROM publicdata.samples.natality LIMIT 5;" - // const sqlQuery = "SELECT * FROM publicdata.samples.natality LIMIT 5;"; - - // Instantiates a client - const bigquery = 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); - }); - // [END bigquery_sync_query] -} - -function asyncQuery (sqlQuery, projectId) { - // [START bigquery_async_query] - // [START bigquery_build_client] - // Imports the Google Cloud client library - const BigQuery = require('@google-cloud/bigquery'); - - // The project ID to use, e.g. "your-project-id" - // const projectId = "your-project-id"; - - // Instantiates a client - const bigquery = BigQuery({ - projectId: projectId - }); - // [END bigquery_build_client] - - // The SQL query to run, e.g. "SELECT * FROM publicdata.samples.natality LIMIT 5;" - // const sqlQuery = "SELECT * FROM publicdata.samples.natality LIMIT 5;"; - - // 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. - }; - - let job; - - // Runs the query as a job - bigquery - .startQuery(options) - .then((results) => { - job = results[0]; - console.log(`Job ${job.id} started.`); - return job.promise(); - }) - .then((results) => { - // Get the job's status - return job.getMetadata(); - }) - .then((metadata) => { - // Check the job's status for errors - const errors = metadata[0].status.errors; - if (errors && errors.length > 0) { - throw errors; - } - }) - .then(() => { - console.log(`Job ${job.id} completed.`); - return job.getQueryResults(); - }) - .then((results) => { - const rows = results[0]; - console.log('Rows:'); - rows.forEach((row) => console.log(row)); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END bigquery_async_query] -} - -const cli = require(`yargs`) - .demand(1) - .options({ - projectId: { - alias: 'p', - default: process.env.GCLOUD_PROJECT || process.env.GOOGLE_CLOUD_PROJECT, - description: 'The Project ID to use. Defaults to the value of the GCLOUD_PROJECT or GOOGLE_CLOUD_PROJECT environment variables.', - requiresArg: true, - type: 'string' - } - }) - .command( - `sync `, - `Run the specified synchronous query.`, - {}, - (opts) => syncQuery(opts.sqlQuery, opts.projectId) - ) - .command( - `async `, - `Start the specified asynchronous query.`, - {}, - (opts) => asyncQuery(opts.sqlQuery, opts.projectId) - ) - .command( - `shakespeare`, - `Queries a public Shakespeare dataset.`, - {}, - (opts) => queryShakespeare(opts.projectId) - ) - .example( - `node $0 sync "SELECT * FROM publicdata.samples.natality LIMIT 5;"`, - `Synchronously queries the natality dataset.` - ) - .example( - `node $0 async "SELECT * FROM publicdata.samples.natality LIMIT 5;"`, - `Queries the natality dataset as a job.` - ) - .example( - `node $0 shakespeare`, - `Queries a public Shakespeare dataset.` - ) - .wrap(120) - .recommendCommands() - .epilogue(`For more information, see https://cloud.google.com/bigquery/docs`) - .help() - .strict(); - -if (module === require.main) { - cli.parse(process.argv.slice(2)); -} diff --git a/bigquery/quickstart.js b/bigquery/quickstart.js deleted file mode 100644 index ddf69e5e7b..0000000000 --- a/bigquery/quickstart.js +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright 2017, Google, Inc. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; - -// [START bigquery_quickstart] -// Imports the Google Cloud client library -const BigQuery = require('@google-cloud/bigquery'); - -// Your Google Cloud Platform project ID -const projectId = 'YOUR_PROJECT_ID'; - -// Instantiates a client -const bigquery = BigQuery({ - projectId: projectId -}); - -// The name for the new dataset -const datasetName = 'my_new_dataset'; - -// Creates the new dataset -bigquery.createDataset(datasetName) - .then((results) => { - const dataset = results[0]; - - console.log(`Dataset ${dataset.id} created.`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); -// [END bigquery_quickstart] diff --git a/bigquery/resources/data.csv b/bigquery/resources/data.csv deleted file mode 100644 index 2f0ac57448..0000000000 --- a/bigquery/resources/data.csv +++ /dev/null @@ -1 +0,0 @@ -Gandalf,2000,140.0,TRUE \ No newline at end of file diff --git a/bigquery/resources/data.json b/bigquery/resources/data.json deleted file mode 100644 index cecbbf1582..0000000000 --- a/bigquery/resources/data.json +++ /dev/null @@ -1 +0,0 @@ -{"Name":"Gandalf","Age":2000,"Weight":140.0,"IsMagic":true} \ No newline at end of file diff --git a/bigquery/resources/schema.json b/bigquery/resources/schema.json deleted file mode 100644 index 68876fb604..0000000000 --- a/bigquery/resources/schema.json +++ /dev/null @@ -1 +0,0 @@ -[{"type":"STRING","name":"Name"},{"type":"INTEGER","name":"Age"},{"type":"FLOAT","name":"Weight"},{"type":"BOOLEAN","name":"IsMagic"}] \ No newline at end of file diff --git a/bigquery/resources/streamrows.json b/bigquery/resources/streamrows.json deleted file mode 100644 index 51b111272d..0000000000 --- a/bigquery/resources/streamrows.json +++ /dev/null @@ -1,7 +0,0 @@ -[ - {"Name":"test","Age":0,"Weight":100.0,"IsMagic":false}, - {"Name":"test","Age":1,"Weight":100.0,"IsMagic":false}, - {"Name":"test","Age":2,"Weight":100.0,"IsMagic":false}, - {"Name":"test","Age":3,"Weight":100.0,"IsMagic":false}, - {"Name":"test","Age":0,"Weight":100.0,"IsMagic":false} -] \ No newline at end of file diff --git a/bigquery/system-test/datasets.test.js b/bigquery/system-test/datasets.test.js deleted file mode 100644 index e346245268..0000000000 --- a/bigquery/system-test/datasets.test.js +++ /dev/null @@ -1,58 +0,0 @@ -/** - * Copyright 2017, Google, Inc. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; - -const bigquery = require(`@google-cloud/bigquery`)(); -const path = require(`path`); -const test = require(`ava`); -const tools = require(`@google-cloud/nodejs-repo-tools`); -const uuid = require(`uuid`); - -const cwd = path.join(__dirname, `..`); -const cmd = `node datasets.js`; -const datasetId = (`nodejs-docs-samples-test-${uuid.v4()}`).replace(/-/gi, '_'); - -test.before(tools.checkCredentials); -test.beforeEach(tools.stubConsole); -test.afterEach.always(tools.restoreConsole); -test.after.always(async () => { - try { - await bigquery.dataset(datasetId).delete({ force: true }); - } catch (err) {} // ignore error -}); - -test.serial(`should create a dataset`, async (t) => { - const output = await tools.runAsync(`${cmd} create ${datasetId}`, cwd); - t.is(output, `Dataset ${datasetId} created.`); - const [exists] = await bigquery.dataset(datasetId).exists(); - t.true(exists); -}); - -test.serial(`should list datasets`, async (t) => { - t.plan(0); - await tools.tryTest(async (assert) => { - const output = await tools.runAsync(`${cmd} list`, cwd); - assert(output.includes(`Datasets:`)); - assert(output.includes(datasetId)); - }).start(); -}); - -test.serial(`should delete a dataset`, async (t) => { - const output = await tools.runAsync(`${cmd} delete ${datasetId}`, cwd); - t.is(output, `Dataset ${datasetId} deleted.`); - const [exists] = await bigquery.dataset(datasetId).exists(); - t.false(exists); -}); diff --git a/bigquery/system-test/queries.test.js b/bigquery/system-test/queries.test.js deleted file mode 100644 index 1dbefaf9b5..0000000000 --- a/bigquery/system-test/queries.test.js +++ /dev/null @@ -1,73 +0,0 @@ -/** - * Copyright 2017, Google, Inc. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; - -const path = require(`path`); -const test = require(`ava`); -const tools = require(`@google-cloud/nodejs-repo-tools`); - -const cwd = path.join(__dirname, `..`); -const cmd = `node queries.js`; - -const expectedShakespeareResult = `Query Results: -corpus: hamlet -unique_words: 5318 -corpus: kinghenryv -unique_words: 5104 -corpus: cymbeline -unique_words: 4875 -corpus: troilusandcressida -unique_words: 4795 -corpus: kinglear -unique_words: 4784 -corpus: kingrichardiii -unique_words: 4713 -corpus: 2kinghenryvi -unique_words: 4683 -corpus: coriolanus -unique_words: 4653 -corpus: 2kinghenryiv -unique_words: 4605 -corpus: antonyandcleopatra -unique_words: 4582`; - -const sqlQuery = `SELECT * FROM publicdata.samples.natality LIMIT 5;`; -const badQuery = `SELECT * FROM INVALID`; - -test(`should query shakespeare`, async (t) => { - const output = await tools.runAsync(`${cmd} shakespeare`, cwd); - t.is(output, expectedShakespeareResult); -}); - -test(`should run a sync query`, async (t) => { - const output = await tools.runAsync(`${cmd} sync "${sqlQuery}"`, cwd); - t.true(output.includes(`Rows:`)); - t.true(output.includes(`source_year`)); -}); - -test(`should run an async query`, async (t) => { - const output = await tools.runAsync(`${cmd} async "${sqlQuery}"`, cwd); - t.true(output.includes(`Rows:`)); - t.true(output.includes(`source_year`)); -}); - -test.skip(`should handle sync query errors`, async (t) => { - await t.throws(tools.runAsync(`${cmd} sync "${badQuery}"`, cwd), /ERROR:/); -}); - -test.skip(`should handle async query errors`, async (t) => { - await t.throws(tools.runAsync(`${cmd} async "${badQuery}"`, cwd), /ERROR:/); -}); diff --git a/bigquery/system-test/quickstart.test.js b/bigquery/system-test/quickstart.test.js deleted file mode 100644 index edffde86f8..0000000000 --- a/bigquery/system-test/quickstart.test.js +++ /dev/null @@ -1,68 +0,0 @@ -/** - * Copyright 2017, Google, Inc. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; - -const proxyquire = require(`proxyquire`).noPreserveCache(); -const sinon = require(`sinon`); -const test = require(`ava`); -const tools = require(`@google-cloud/nodejs-repo-tools`); -const uuid = require(`uuid`); - -const bigquery = proxyquire(`@google-cloud/bigquery`, {})(); - -const expectedDatasetId = `my_new_dataset`; -let datasetId = `nodejs-docs-samples-test-${uuid.v4()}`; -datasetId = datasetId.replace(/-/gi, `_`); - -test.after.always(async () => { - try { - bigquery.dataset(datasetId).delete({ force: true }); - } catch (err) {} // ignore error -}); - -test.beforeEach(tools.stubConsole); -test.afterEach.always(tools.restoreConsole); - -test(`quickstart should create a dataset`, async (t) => { - await new Promise((resolve, reject) => { - const bigqueryMock = { - createDataset: (_datasetId) => { - t.is(_datasetId, expectedDatasetId); - - return bigquery.createDataset(datasetId) - .then(([dataset]) => { - t.not(dataset, undefined); - - setTimeout(() => { - try { - t.true(console.log.calledOnce); - t.deepEqual(console.log.firstCall.args, [`Dataset ${dataset.id} created.`]); - resolve(); - } catch (err) { - reject(err); - } - }, 200); - - return [dataset]; - }).catch(reject); - } - }; - - proxyquire(`../quickstart`, { - '@google-cloud/bigquery': sinon.stub().returns(bigqueryMock) - }); - }); -}); diff --git a/bigquery/system-test/tables.test.js b/bigquery/system-test/tables.test.js deleted file mode 100644 index 89c682b8f9..0000000000 --- a/bigquery/system-test/tables.test.js +++ /dev/null @@ -1,160 +0,0 @@ -/** - * Copyright 2017, Google, Inc. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; - -const bigquery = require(`@google-cloud/bigquery`)(); -const path = require(`path`); -const storage = require(`@google-cloud/storage`)(); -const test = require(`ava`); -const tools = require(`@google-cloud/nodejs-repo-tools`); -const uuid = require(`uuid`); - -const cwd = path.join(__dirname, `..`); -const cmd = `node tables.js`; -const generateUuid = () => `nodejs_docs_samples_${uuid.v4().replace(/-/gi, '_')}`; - -const datasetId = generateUuid(); -const srcDatasetId = datasetId; -const destDatasetId = generateUuid(); -const tableId = generateUuid(); -const srcTableId = tableId; -const destTableId = generateUuid(); -const schema = `Name:string, Age:integer, Weight:float, IsMagic:boolean`; -const bucketName = generateUuid(); -const exportFileName = `data.json`; -const importFileName = `data.csv`; -const localFilePath = path.join(__dirname, `../resources/${importFileName}`); -const rows = [ - { Name: `foo`, Age: 27, Weight: 80.3, IsMagic: true }, - { Name: `bar`, Age: 13, Weight: 54.6, IsMagic: false } -]; - -test.before(tools.checkCredentials); -test.before(async () => { - const [bucket] = await storage.createBucket(bucketName); - await Promise.all([ - bucket.upload(localFilePath), - bigquery.createDataset(srcDatasetId), - bigquery.createDataset(destDatasetId) - ]); -}); -test.beforeEach(tools.stubConsole); -test.afterEach.always(tools.restoreConsole); -test.after.always(async () => { - try { - await bigquery.dataset(srcDatasetId).delete({ force: true }); - } catch (err) {} // ignore error - try { - await bigquery.dataset(destDatasetId).delete({ force: true }); - } catch (err) {} // ignore error - try { - await storage.bucket(bucketName).deleteFiles({ force: true }); - } catch (err) {} // ignore error - try { - // Try deleting files a second time - await storage.bucket(bucketName).deleteFiles({ force: true }); - } catch (err) {} // ignore error - try { - await bigquery.dataset(srcDatasetId).delete({ force: true }); - } catch (err) {} // ignore error - try { - await storage.bucket(bucketName).delete(); - } catch (err) {} // ignore error -}); - -test.serial(`should create a table`, async (t) => { - const output = await tools.runAsync(`${cmd} create ${datasetId} ${tableId} "${schema}"`, cwd); - t.is(output, `Table ${tableId} created.`); - const [exists] = await bigquery.dataset(datasetId).table(tableId).exists(); - t.true(exists); -}); - -test.serial(`should list tables`, async (t) => { - t.plan(0); - await tools.tryTest(async (assert) => { - const output = await tools.runAsync(`${cmd} list ${datasetId}`, cwd); - assert(output.includes(`Tables:`)); - assert(output.includes(tableId)); - }).start(); -}); - -test.serial(`should import a local file`, async (t) => { - t.plan(2); - const output = await tools.runAsync(`${cmd} import ${datasetId} ${tableId} ${localFilePath}`, cwd); - t.true(output.includes(`started.`)); - t.true(output.includes(`completed.`)); - await tools.tryTest(async (assert) => { - const [rows] = await bigquery.dataset(datasetId).table(tableId).getRows(); - assert.equal(rows.length, 1); - }).start(); -}); - -test.serial(`should browse table rows`, async (t) => { - const output = await tools.runAsync(`${cmd} browse ${datasetId} ${tableId}`, cwd); - t.is(output, `Rows:\n{ Name: 'Gandalf', Age: 2000, Weight: 140, IsMagic: true }`); -}); - -test.serial(`should export a table to GCS`, async (t) => { - t.plan(2); - const output = await tools.runAsync(`${cmd} export ${datasetId} ${tableId} ${bucketName} ${exportFileName}`, cwd); - t.true(output.includes(`started.`)); - t.true(output.includes(`completed.`)); - await tools.tryTest(async (assert) => { - const [exists] = await storage.bucket(bucketName).file(exportFileName).exists(); - assert(exists); - }).start(); -}); - -test.serial(`should import a GCS file`, async (t) => { - t.plan(2); - const output = await tools.runAsync(`${cmd} import-gcs ${datasetId} ${tableId} ${bucketName} ${importFileName}`, cwd); - t.true(output.includes(`started.`)); - t.true(output.includes(`completed.`)); - await tools.tryTest(async (assert) => { - const [rows] = await bigquery.dataset(datasetId).table(tableId).getRows(); - assert.equal(rows.length, 2); - }).start(); -}); - -test.serial(`should copy a table`, async (t) => { - t.plan(2); - const output = await tools.runAsync(`${cmd} copy ${srcDatasetId} ${srcTableId} ${destDatasetId} ${destTableId}`, cwd); - t.true(output.includes(`started.`)); - t.true(output.includes(`completed.`)); - await tools.tryTest(async (assert) => { - const [rows] = await bigquery.dataset(destDatasetId).table(destTableId).getRows(); - assert.equal(rows.length, 2); - }).start(); -}); - -test.serial(`should insert rows`, async (t) => { - t.plan(3); - const err = await t.throws(tools.runAsync(`${cmd} insert ${datasetId} ${tableId} 'foo.bar'`, cwd)); - t.true(err.message.includes(`"json_or_file" (or the file it points to) is not a valid JSON array.`)); - const output = await tools.runAsync(`${cmd} insert ${datasetId} ${tableId} '${JSON.stringify(rows)}'`, cwd); - t.is(output.includes(`Inserted:\n{ Name: 'foo', Age: 27, Weight: 80.3, IsMagic: true }\n{ Name: 'bar', Age: 13, Weight: 54.6, IsMagic: false }`), true); - await tools.tryTest(async (assert) => { - const [rows] = await bigquery.dataset(datasetId).table(tableId).getRows(); - assert.equal(rows.length, 4); - }).start(); -}); - -test.serial(`should delete a table`, async (t) => { - const output = await tools.runAsync(`${cmd} delete ${datasetId} ${tableId}`, cwd); - t.is(output, `Table ${tableId} deleted.`); - const [exists] = await bigquery.dataset(datasetId).table(tableId).exists(); - t.false(exists); -}); diff --git a/bigquery/tables.js b/bigquery/tables.js deleted file mode 100644 index 653c5acd94..0000000000 --- a/bigquery/tables.js +++ /dev/null @@ -1,547 +0,0 @@ -/** - * Copyright 2017, Google, Inc. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; - -function createTable (datasetId, tableId, schema, projectId) { - // [START bigquery_create_table] - // Imports the Google Cloud client library - const BigQuery = require('@google-cloud/bigquery'); - - // The project ID to use, e.g. "your-project-id" - // const projectId = "your-project-id"; - - // The ID of the dataset in which to create the table, e.g. "my_dataset" - // const datasetId = "my_new_dataset"; - - // The ID for the new table, e.g. "my_new_table" - // const tableId = "my_new_table"; - - // The schema of the new table, e.g. "Name:string, Age:integer, Weight:float, IsMagic:boolean" - // const schema = "Name:string, Age:integer, Weight:float, IsMagic:boolean"; - - // Instantiates a client - const bigquery = BigQuery({ - projectId: projectId - }); - - // For all options, see https://cloud.google.com/bigquery/docs/reference/v2/tables#resource - const options = { - schema: schema - }; - - // Create a new table in the dataset - bigquery - .dataset(datasetId) - .createTable(tableId, options) - .then((results) => { - const table = results[0]; - console.log(`Table ${table.id} created.`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END bigquery_create_table] -} - -function deleteTable (datasetId, tableId, projectId) { - // [START bigquery_delete_table] - // Imports the Google Cloud client library - const BigQuery = require('@google-cloud/bigquery'); - - // The project ID to use, e.g. "your-project-id" - // const projectId = "your-project-id"; - - // The ID of the dataset of the table to delete, e.g. "my_dataset" - // const datasetId = "my_dataset"; - - // The ID of the table to delete, e.g. "my_table" - // const tableId = "my_table"; - - // Instantiates a client - const bigquery = BigQuery({ - projectId: projectId - }); - - // Deletes the table - bigquery - .dataset(datasetId) - .table(tableId) - .delete() - .then(() => { - console.log(`Table ${tableId} deleted.`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END bigquery_delete_table] -} - -function listTables (datasetId, projectId) { - // [START bigquery_list_tables] - // Imports the Google Cloud client library - const BigQuery = require('@google-cloud/bigquery'); - - // The project ID to use, e.g. "your-project-id" - // const projectId = "your-project-id"; - - // The ID of the dataset to list tables in, e.g. "my_dataset" - // const datasetId = "my_dataset"; - - // Instantiates a client - const bigquery = BigQuery({ - projectId: projectId - }); - - // Lists all tables in the dataset - bigquery - .dataset(datasetId) - .getTables() - .then((results) => { - const tables = results[0]; - console.log('Tables:'); - tables.forEach((table) => console.log(table.id)); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END bigquery_list_tables] -} - -function browseRows (datasetId, tableId, projectId) { - // [START bigquery_browse_table] - // Imports the Google Cloud client library - const BigQuery = require('@google-cloud/bigquery'); - - // The project ID to use, e.g. "your-project-id" - // const projectId = "your-project-id"; - - // The ID of the dataset of the table to browse, e.g. "my_dataset" - // const datasetId = "my_dataset"; - - // The ID of the table to browse, e.g. "my_table" - // const tableId = "my_table"; - - // Instantiates a client - const bigquery = BigQuery({ - projectId: projectId - }); - - // Lists rows in the table - bigquery - .dataset(datasetId) - .table(tableId) - .getRows() - .then((results) => { - const rows = results[0]; - console.log('Rows:'); - rows.forEach((row) => console.log(row)); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END bigquery_browse_table] -} - -function copyTable (srcDatasetId, srcTableId, destDatasetId, destTableId, projectId) { - // [START bigquery_copy_table] - // Imports the Google Cloud client library - const BigQuery = require('@google-cloud/bigquery'); - - // The project ID to use, e.g. "your-project-id" - // const projectId = "your-project-id"; - - // The ID of the dataset of the table to copy, e.g. "my_src_dataset" - // const srcDatasetId = "my_src_dataset"; - - // The ID of the table to copy, e.g. "my_src_table" - // const srcTableId = "my_src_table"; - - // The ID of the destination dataset, e.g. "my_dest_dataset" - // const destDatasetId = "my_dest_dataset"; - - // The ID of the destination table, e.g. "my_dest_table" - // const destTableId = "my_dest_table"; - - // Instantiates a client - const bigquery = BigQuery({ - projectId: projectId - }); - - let job; - - // Copies the table contents into another table - bigquery - .dataset(srcDatasetId) - .table(srcTableId) - .copy(bigquery.dataset(destDatasetId).table(destTableId)) - .then((results) => { - job = results[0]; - console.log(`Job ${job.id} started.`); - - // Wait for the job to finish - return job.promise(); - }) - .then((results) => { - // Get the job's status - return job.getMetadata(); - }).then((metadata) => { - // Check the job's status for errors - const errors = metadata[0].status.errors; - if (errors && errors.length > 0) { - throw errors; - } - }).then(() => { - console.log(`Job ${job.id} completed.`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END bigquery_copy_table] -} - -function importLocalFile (datasetId, tableId, filename, projectId) { - // [START bigquery_import_from_file] - // Imports the Google Cloud client library - const BigQuery = require('@google-cloud/bigquery'); - - // The project ID to use, e.g. "your-project-id" - // const projectId = "your-project-id"; - - // The name of the file from which data should be imported, e.g. "/path/to/file.csv" - // const filename = "/path/to/file.csv"; - - // The ID of the dataset of the table into which data should be imported, e.g. "my_dataset" - // const datasetId = "my_dataset"; - - // The ID of the table into which data should be imported, e.g. "my_table" - // const tableId = "my_table"; - - // Instantiates a client - const bigquery = BigQuery({ - projectId: projectId - }); - - let job; - - // Imports data from a local file into the table - bigquery - .dataset(datasetId) - .table(tableId) - .import(filename) - .then((results) => { - job = results[0]; - console.log(`Job ${job.id} started.`); - - // Wait for the job to finish - return job.promise(); - }) - .then((results) => { - // Get the job's status - return job.getMetadata(); - }).then((metadata) => { - // Check the job's status for errors - const errors = metadata[0].status.errors; - if (errors && errors.length > 0) { - throw errors; - } - }).then(() => { - console.log(`Job ${job.id} completed.`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END bigquery_import_from_file] -} - -function importFileFromGCS (datasetId, tableId, bucketName, filename, projectId) { - // [START bigquery_import_from_gcs] - // Imports the Google Cloud client libraries - const BigQuery = require('@google-cloud/bigquery'); - const Storage = require('@google-cloud/storage'); - - // The project ID to use, e.g. "your-project-id" - // const projectId = "your-project-id"; - - // The ID of the dataset of the table into which data should be imported, e.g. "my_dataset" - // const datasetId = "my_dataset"; - - // The ID of the table into which data should be imported, e.g. "my_table" - // const tableId = "my_table"; - - // The name of the Google Cloud Storage bucket where the file is located, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // The name of the file from which data should be imported, e.g. "file.csv" - // const filename = "file.csv"; - - // Instantiates clients - const bigquery = BigQuery({ - projectId: projectId - }); - - const storage = Storage({ - projectId: projectId - }); - - let job; - - // Imports data from a Google Cloud Storage file into the table - bigquery - .dataset(datasetId) - .table(tableId) - .import(storage.bucket(bucketName).file(filename)) - .then((results) => { - job = results[0]; - console.log(`Job ${job.id} started.`); - - // Wait for the job to finish - return job.promise(); - }) - .then((results) => { - // Get the job's status - return job.getMetadata(); - }).then((metadata) => { - // Check the job's status for errors - const errors = metadata[0].status.errors; - if (errors && errors.length > 0) { - throw errors; - } - }).then(() => { - console.log(`Job ${job.id} completed.`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END bigquery_import_from_gcs] -} - -function exportTableToGCS (datasetId, tableId, bucketName, filename, projectId) { - // [START bigquery_export_gcs] - // Imports the Google Cloud client libraries - const BigQuery = require('@google-cloud/bigquery'); - const Storage = require('@google-cloud/storage'); - - // The project ID to use, e.g. "your-project-id" - // const projectId = "your-project-id"; - - // The ID of the dataset of the table into which data should be imported, e.g. "my_dataset" - // const datasetId = "my_dataset"; - - // The ID of the table into which data should be imported, e.g. "my_table" - // const tableId = "my_table"; - - // The name of the Google Cloud Storage bucket where the file is located, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // The name of the file from which data should be imported, e.g. "file.csv" - // const filename = "file.csv"; - - // Instantiates clients - const bigquery = BigQuery({ - projectId: projectId - }); - - const storage = Storage({ - projectId: projectId - }); - - let job; - - // Exports data from the table into a Google Cloud Storage file - bigquery - .dataset(datasetId) - .table(tableId) - .export(storage.bucket(bucketName).file(filename)) - .then((results) => { - job = results[0]; - console.log(`Job ${job.id} started.`); - - // Wait for the job to finish - return job.promise(); - }) - .then((results) => { - // Get the job's status - return job.getMetadata(); - }).then((metadata) => { - // Check the job's status for errors - const errors = metadata[0].status.errors; - if (errors && errors.length > 0) { - throw errors; - } - }).then(() => { - console.log(`Job ${job.id} completed.`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END bigquery_export_gcs] -} - -function insertRowsAsStream (datasetId, tableId, rows, projectId) { - // [START bigquery_insert_stream] - // Imports the Google Cloud client library - const BigQuery = require('@google-cloud/bigquery'); - - // The project ID to use, e.g. "your-project-id" - // const projectId = "your-project-id"; - - // The ID of the dataset of the table into which data should be inserted, e.g. "my_dataset" - // const datasetId = "my_dataset"; - - // The ID of the table into which data should be inserted, e.g. "my_table" - // const tableId = "my_table"; - - // The rows to insert into the table - // Customize this object to match your table's schema - // const rows = [{name: "Tom", age: 30}, {name: "Jane", age: 32}]; - - // Instantiates a client - const bigquery = BigQuery({ - projectId: projectId - }); - - // Inserts data into a table - bigquery - .dataset(datasetId) - .table(tableId) - .insert(rows) - .then((insertErrors) => { - console.log('Inserted:'); - rows.forEach((row) => console.log(row)); - - if (insertErrors && insertErrors.length > 0) { - console.log('Insert errors:'); - insertErrors.forEach((err) => console.error(err)); - } - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END bigquery_insert_stream] -} - -const fs = require(`fs`); - -const cli = require(`yargs`) - .demand(1) - .options({ - projectId: { - alias: 'p', - default: process.env.GCLOUD_PROJECT || process.env.GOOGLE_CLOUD_PROJECT, - description: 'The Project ID to use. Defaults to the value of the GCLOUD_PROJECT or GOOGLE_CLOUD_PROJECT environment variables.', - requiresArg: true, - type: 'string' - } - }) - .command(`create `, `Creates a new table.`, {}, (opts) => { - createTable(opts.datasetId, opts.tableId, opts.schema, opts.projectId); - }) - .command(`list `, `Lists all tables in a dataset.`, {}, (opts) => { - listTables(opts.datasetId, opts.projectId); - }) - .command(`delete `, `Deletes a table.`, {}, (opts) => { - deleteTable(opts.datasetId, opts.tableId, opts.projectId); - }) - .command(`copy `, `Makes a copy of a table.`, {}, (opts) => { - copyTable(opts.srcDatasetId, opts.srcTableId, opts.destDatasetId, opts.destTableId, opts.projectId); - }) - .command(`browse `, `Lists rows in a table.`, {}, (opts) => { - browseRows(opts.datasetId, opts.tableId, opts.projectId); - }) - .command(`import `, `Imports data from a local file into a table.`, {}, (opts) => { - importLocalFile(opts.datasetId, opts.tableId, opts.fileName, opts.projectId); - }) - .command(`import-gcs `, `Imports data from a Google Cloud Storage file into a table.`, {}, (opts) => { - importFileFromGCS(opts.datasetId, opts.tableId, opts.bucketName, opts.fileName, opts.projectId); - }) - .command(`export `, `Export a table from BigQuery to Google Cloud Storage.`, {}, (opts) => { - exportTableToGCS(opts.datasetId, opts.tableId, opts.bucketName, opts.fileName, opts.projectId); - }) - .command(`insert `, - `Insert a JSON array (as a string or newline-delimited file) into a BigQuery table.`, {}, - (opts) => { - let content; - try { - content = fs.readFileSync(opts.json_or_file); - } catch (err) { - content = opts.json_or_file; - } - - let rows = null; - try { - rows = JSON.parse(content); - } catch (err) {} - - if (!Array.isArray(rows)) { - throw new Error(`"json_or_file" (or the file it points to) is not a valid JSON array.`); - } - - insertRowsAsStream(opts.datasetId, opts.tableId, rows, opts.projectId || process.env.GCLOUD_PROJECT); - } - ) - .example( - `node $0 create my_dataset my_table "Name:string, Age:integer, Weight:float, IsMagic:boolean"`, - `Creates a new table named "my_table" in "my_dataset".` - ) - .example( - `node $0 list my_dataset`, - `Lists tables in "my_dataset".` - ) - .example( - `node $0 browse my_dataset my_table`, - `Displays rows from "my_table" in "my_dataset".` - ) - .example( - `node $0 delete my_dataset my_table`, - `Deletes "my_table" from "my_dataset".` - ) - .example( - `node $0 import my_dataset my_table ./data.csv`, - `Imports a local file into a table.` - ) - .example( - `node $0 import-gcs my_dataset my_table my-bucket data.csv`, - `Imports a GCS file into a table.` - ) - .example( - `node $0 export my_dataset my_table my-bucket my-file`, - `Exports my_dataset:my_table to gcs://my-bucket/my-file as raw CSV.` - ) - .example( - `node $0 export my_dataset my_table my-bucket my-file -f JSON --gzip`, - `Exports my_dataset:my_table to gcs://my-bucket/my-file as gzipped JSON.` - ) - .example( - `node $0 insert my_dataset my_table json_string`, - `Inserts the JSON array represented by json_string into my_dataset:my_table.` - ) - .example( - `node $0 insert my_dataset my_table json_file`, - `Inserts the JSON objects contained in json_file (one per line) into my_dataset:my_table.` - ) - .example( - `node $0 copy src_dataset src_table dest_dataset dest_table`, - `Copies src_dataset:src_table to dest_dataset:dest_table.` - ) - .wrap(120) - .recommendCommands() - .epilogue(`For more information, see https://cloud.google.com/bigquery/docs`) - .help() - .strict(); - -if (module === require.main) { - cli.parse(process.argv.slice(2)); -} diff --git a/circle.yml b/circle.yml index 54b0fcc2ba..6f809b3a78 100644 --- a/circle.yml +++ b/circle.yml @@ -87,7 +87,6 @@ deployment: - GCLOUD_STORAGE_BUCKET=docs-samples-gae-test-$(uuid); node scripts/build "appengine/storage" - node scripts/build "auth" - node scripts/build "appengine/pubsub" - - node scripts/build "bigquery" - export GCP_QUEUE=nodejs-test-queue-do-not-delete; node scripts/build "cloudtasks" - node scripts/build "containerengine/hello-world" - node scripts/build "datastore" @@ -103,4 +102,3 @@ deployment: - node scripts/build "pubsub" - node scripts/build "storage-transfer" - node scripts/build "trace" - - node scripts/build "vision" diff --git a/vision/.gitignore b/vision/.gitignore deleted file mode 100644 index 0aedda85e3..0000000000 --- a/vision/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -out.png -out.* diff --git a/vision/README.md b/vision/README.md index 4b5688b356..f4757128d6 100644 --- a/vision/README.md +++ b/vision/README.md @@ -1,130 +1,5 @@ -Google Cloud Platform logo +Samples for the [Vision API Node.js Client][client] have moved to +[github.com/googleapis/nodejs-vision/tree/master/samples/][samples]. -# Google Cloud Vision API Node.js Samples - -The [Cloud Vision API][vision_docs] allows developers to easily integrate vision -detection features within applications, including image labeling, face and -landmark detection, optical character recognition (OCR), and tagging of explicit -content. - -[vision_docs]: https://cloud.google.com/vision/docs/ - -## Table of Contents - -* [Setup](#setup) -* [Samples](#samples) - * [Detection snippets](#detection-snippets) - * [Face detection](#face-detection) - * [Text detection](#text-detection) - -## Setup - -1. Read [Prerequisites][prereq] and [How to run a sample][run] first. -1. Install dependencies: - - npm install - -[prereq]: ../README.md#prerequisities -[run]: ../README.md#how-to-run-a-sample - -## Samples - -### Detection snippets - -View the [documentation][detect_docs] or the [source code][detect_code]. - -__Usage:__ `node detect.js --help` - -``` -Commands: - faces Detects faces in a local image file. - faces-gcs Detects faces in an image in Google Cloud Storage. - labels Detects labels in a local image file. - labels-gcs Detects labels in an image in Google Cloud Storage. - landmarks Detects landmarks in a local image file. - landmarks-gcs Detects landmarks in an image in Google Cloud Storage. - text Detects text in a local image file. - text-gcs Detects text in an image in Google Cloud Storage. - logos Detects logos in a local image file. - logos-gcs Detects logos in an image in Google Cloud Storage. - properties Detects image properties in a local image file. - properties-gcs Detects image properties in an image in Google Cloud Storage. - safe-search Detects safe search properties in a local image file. - safe-search-gcs Detects safe search properties in an image in Google Cloud Storage. - crops Detects crop hints in a local image file. - crops-gcs Detects crop hints in an image in Google Cloud Storage. - web Finds similar photos on the web for a local image file. - web-gcs Finds similar photos on the web for an image in Google Cloud Storage. - fulltext Extracts full text from a local image file. - fulltext-gcs Extracts full text from an image in Google Cloud Storage. - -Options: - --help Show help [boolean] - -Examples: - node detect.js faces ./resources/face_no_surprise.jpg - node detect.js faces-gcs my-bucket your-image.jpg - node detect.js labels ./resources/wakeupcat.jpg - node detect.js labels-gcs my-bucket your-image.jpg - node detect.js landmarks ./resources/landmark.jpg - node detect.js landmarks-gcs my-bucket your-image.jpg - node detect.js text ./resources/wakeupcat.jpg - node detect.js text-gcs my-bucket your-image.jpg - node detect.js logos ./resources/logos.png - node detect.js logos-gcs my-bucket your-image.jpg.png - node detect.js properties ./resources/landmark.jpg - node detect.js properties-gcs my-bucket your-image.jpg - node detect.js safe-search ./resources/wakeupcat.jpg - node detect.js safe-search-gcs my-bucket your-image.jpg - node detect.js crops ./resources/wakeupcat.jpg - node detect.js crops-gcs my-bucket your-image.jpg - node detect.js web ./resources/wakeupcat.jpg - node detect.js web-gcs my-bucket your-image.jpg - node detect.js fulltext ./resources/wakeupcat.jpg - node detect.js fulltext-gcs my-bucket your-image.jpg - -For more information, see https://cloud.google.com/vision/docs -``` - -[detect_docs]: https://cloud.google.com/vision/docs -[detect_code]: detect.js - -### Face detection tutorial - -View the [documentation][face_docs] or the [source code][face_code]. - -This sample uses [node-canvas](https://github.com/Automattic/node-canvas) to -draw an output image. node-canvas depends on Cairo, which may require separate -installation. See the node-canvas [installation section][canvas-install] for -details. - -[canvas-install]: https://github.com/Automattic/node-canvas#installation - -__Run the sample:__ - -Usage: `node faceDetection ` - -Example: - - node faceDetection "./resources/face.png" - -[face_docs]: https://cloud.google.com/vision/docs/face-tutorial -[face_code]: faceDetection.js - -### Text detection tutorial - -View the [source code][text_code]. - -__Run the sample:__ - -Usage: `node textDetection [args]...` - -Analyze images: - - node textDetection analyze "./resources/" - -Look up words: - - node textDetection lookup the sunbeams in - -[text_code]: textDetection.js +[client]: https://github.com/googleapis/nodejs-vision +[samples]: https://github.com/googleapis/nodejs-vision/tree/master/samples diff --git a/vision/detect.js b/vision/detect.js deleted file mode 100644 index 0cbf847a26..0000000000 --- a/vision/detect.js +++ /dev/null @@ -1,827 +0,0 @@ -/** - * Copyright 2017, Google, Inc. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; - -function detectFaces (fileName) { - // [START vision_face_detection] - // Imports the Google Cloud client library - const Vision = require('@google-cloud/vision'); - - // Creates a client - const vision = new Vision(); - - /** - * TODO(developer): Uncomment the following line before running the sample. - */ - // const fileName = 'Local image file, e.g. /path/to/image.png'; - - vision.faceDetection({ source: { filename: fileName } }) - .then((results) => { - const faces = results[0].faceAnnotations; - - console.log('Faces:'); - faces.forEach((face, i) => { - console.log(` Face #${i + 1}:`); - console.log(` Joy: ${face.joyLikelihood}`); - console.log(` Anger: ${face.angerLikelihood}`); - console.log(` Sorrow: ${face.sorrowLikelihood}`); - console.log(` Surprise: ${face.surpriseLikelihood}`); - }); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END vision_face_detection] -} - -function detectFacesGCS (bucketName, fileName) { - // [START vision_face_detection_gcs] - // Imports the Google Cloud client libraries - const Vision = require('@google-cloud/vision'); - - // Creates a client - const vision = new Vision(); - - /** - * TODO(developer): Uncomment the following lines before running the sample. - */ - // const bucketName = 'Bucket where the file resides, e.g. my-bucket'; - // const fileName = 'Path to file within bucket, e.g. path/to/image.png'; - - const request = { - source: { - imageUri: `gs://${bucketName}/${fileName}` - } - }; - - // Performs face detection on the gcs file - vision.faceDetection(request) - .then((results) => { - const faces = results[0].faceAnnotations; - - console.log('Faces:'); - faces.forEach((face, i) => { - console.log(` Face #${i + 1}:`); - console.log(` Joy: ${face.joyLikelihood}`); - console.log(` Anger: ${face.angerLikelihood}`); - console.log(` Sorrow: ${face.sorrowLikelihood}`); - console.log(` Surprise: ${face.surpriseLikelihood}`); - }); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END vision_face_detection_gcs] -} - -function detectLabels (fileName) { - // [START vision_label_detection] - // Imports the Google Cloud client library - const Vision = require('@google-cloud/vision'); - - // Creates a client - const vision = new Vision(); - - /** - * TODO(developer): Uncomment the following line before running the sample. - */ - // const fileName = 'Local image file, e.g. /path/to/image.png'; - - // Performs label detection on the local file - vision.labelDetection({ source: { filename: fileName } }) - .then((results) => { - const labels = results[0].labelAnnotations; - console.log('Labels:'); - labels.forEach((label) => console.log(label)); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END vision_label_detection] -} - -function detectLabelsGCS (bucketName, fileName) { - // [START vision_label_detection_gcs] - // Imports the Google Cloud client libraries - const Vision = require('@google-cloud/vision'); - - // Creates a client - const vision = new Vision(); - - /** - * TODO(developer): Uncomment the following lines before running the sample. - */ - // const bucketName = 'Bucket where the file resides, e.g. my-bucket'; - // const fileName = 'Path to file within bucket, e.g. path/to/image.png'; - - const request = { - source: { - imageUri: `gs://${bucketName}/${fileName}` - } - }; - - // Performs label detection on the gcs file - vision.labelDetection(request) - .then((results) => { - const labels = results[0].labelAnnotations; - console.log('Labels:'); - labels.forEach((label) => console.log(label)); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END vision_label_detection_gcs] -} - -function detectLandmarks (fileName) { - // [START vision_landmark_detection] - const Vision = require('@google-cloud/vision'); - - // Creates a client - const vision = new Vision(); - - /** - * TODO(developer): Uncomment the following line before running the sample. - */ - // const fileName = 'Local image file, e.g. /path/to/image.png'; - - // Performs landmark detection on the local file - vision.landmarkDetection({ source: {filename: fileName} }) - .then((results) => { - const landmarks = results[0].landmarkAnnotations; - console.log('Landmarks:'); - landmarks.forEach((landmark) => console.log(landmark)); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END vision_landmark_detection] -} - -function detectLandmarksGCS (bucketName, fileName) { - // [START vision_landmark_detection_gcs] - // Imports the Google Cloud client libraries - const Vision = require('@google-cloud/vision'); - - // Creates a client - const vision = new Vision(); - - /** - * TODO(developer): Uncomment the following lines before running the sample. - */ - // const bucketName = 'Bucket where the file resides, e.g. my-bucket'; - // const fileName = 'Path to file within bucket, e.g. path/to/image.png'; - - const request = { - source: { - imageUri: `gs://${bucketName}/${fileName}` - } - }; - - // Performs landmark detection on the gcs file - vision.landmarkDetection(request) - .then((results) => { - const landmarks = results[0].landmarkAnnotations; - console.log('Landmarks:'); - landmarks.forEach((landmark) => console.log(landmark)); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END vision_landmark_detection_gcs] -} - -function detectText (fileName) { - // [START vision_text_detection] - const Vision = require('@google-cloud/vision'); - - // Creates a client - const vision = new Vision(); - - /** - * TODO(developer): Uncomment the following line before running the sample. - */ - // const fileName = 'Local image file, e.g. /path/to/image.png'; - - // Performs text detection on the local file - vision.textDetection({ source: { filename: fileName } }) - .then((results) => { - const detections = results[0].textAnnotations; - console.log('Text:'); - detections.forEach((text) => console.log(text)); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END vision_text_detection] -} - -function detectTextGCS (bucketName, fileName) { - // [START vision_text_detection_gcs] - // Imports the Google Cloud client libraries - const Vision = require('@google-cloud/vision'); - - // Creates a client - const vision = new Vision(); - - /** - * TODO(developer): Uncomment the following lines before running the sample. - */ - // const bucketName = 'Bucket where the file resides, e.g. my-bucket'; - // const fileName = 'Path to file within bucket, e.g. path/to/image.png'; - - const request = { - source: { - imageUri: `gs://${bucketName}/${fileName}` - } - }; - - // Performs text detection on the gcs file - vision.textDetection(request) - .then((results) => { - const detections = results[0].textAnnotations; - console.log('Text:'); - detections.forEach((text) => console.log(text)); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END vision_text_detection_gcs] -} - -function detectLogos (fileName) { - // [START vision_logo_detection] - const Vision = require('@google-cloud/vision'); - - // Creates a client - const vision = new Vision(); - - /** - * TODO(developer): Uncomment the following line before running the sample. - */ - // const fileName = 'Local image file, e.g. /path/to/image.png'; - - // Performs logo detection on the local file - vision.logoDetection({ source: { filename: fileName } }) - .then((results) => { - const logos = results[0].logoAnnotations; - console.log('Logos:'); - logos.forEach((logo) => console.log(logo)); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END vision_logo_detection] -} - -function detectLogosGCS (bucketName, fileName) { - // [START vision_logo_detection_gcs] - // Imports the Google Cloud client libraries - const Vision = require('@google-cloud/vision'); - - // Creates a client - const vision = new Vision(); - - /** - * TODO(developer): Uncomment the following lines before running the sample. - */ - // const bucketName = 'Bucket where the file resides, e.g. my-bucket'; - // const fileName = 'Path to file within bucket, e.g. path/to/image.png'; - - const request = { - source: { - imageUri: `gs://${bucketName}/${fileName}` - } - }; - - // Performs logo detection on the gcs file - vision.logoDetection(request) - .then((results) => { - const logos = results[0].logoAnnotations; - console.log('Logos:'); - logos.forEach((logo) => console.log(logo)); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END vision_logo_detection_gcs] -} - -function detectProperties (fileName) { - // [START vision_image_property_detection] - const Vision = require('@google-cloud/vision'); - - // Creates a client - const vision = new Vision(); - - /** - * TODO(developer): Uncomment the following line before running the sample. - */ - // const fileName = 'Local image file, e.g. /path/to/image.png'; - - // Performs property detection on the local file - vision.imageProperties({ source: { filename: fileName } }) - .then((results) => { - const properties = results[0].imagePropertiesAnnotation; - const colors = properties.dominantColors.colors; - colors.forEach((color) => console.log(color)); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END vision_image_property_detection] -} - -function detectPropertiesGCS (bucketName, fileName) { - // [START vision_image_property_detection_gcs] - // Imports the Google Cloud client libraries - const Vision = require('@google-cloud/vision'); - - // Creates a client - const vision = new Vision(); - - /** - * TODO(developer): Uncomment the following lines before running the sample. - */ - // const bucketName = 'Bucket where the file resides, e.g. my-bucket'; - // const fileName = 'Path to file within bucket, e.g. path/to/image.png'; - - const request = { - source: { - imageUri: `gs://${bucketName}/${fileName}` - } - }; - - // Performs property detection on the gcs file - vision.imageProperties(request) - .then((results) => { - const properties = results[0].imagePropertiesAnnotation; - const colors = properties.dominantColors.colors; - colors.forEach((color) => console.log(color)); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END vision_image_property_detection_gcs] -} - -function detectSafeSearch (fileName) { - // [START vision_safe_search_detection] - const Vision = require('@google-cloud/vision'); - - // Creates a client - const vision = new Vision(); - - /** - * TODO(developer): Uncomment the following line before running the sample. - */ - // const fileName = 'Local image file, e.g. /path/to/image.png'; - - // Performs safe search detection on the local file - vision.safeSearchDetection({ source: { filename: fileName } }) - .then((results) => { - const detections = results[0].safeSearchAnnotation; - - console.log(`Adult: ${detections.adult}`); - console.log(`Spoof: ${detections.spoof}`); - console.log(`Medical: ${detections.medical}`); - console.log(`Violence: ${detections.violence}`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END vision_safe_search_detection] -} - -function detectSafeSearchGCS (bucketName, fileName) { - // [START vision_safe_search_detection_gcs] - // Imports the Google Cloud client libraries - const Vision = require('@google-cloud/vision'); - - // Creates a client - const vision = new Vision(); - - /** - * TODO(developer): Uncomment the following lines before running the sample. - */ - // const bucketName = 'Bucket where the file resides, e.g. my-bucket'; - // const fileName = 'Path to file within bucket, e.g. path/to/image.png'; - - const request = { - source: { - imageUri: `gs://${bucketName}/${fileName}` - } - }; - - // Performs safe search property detection on the remote file - vision.safeSearchDetection(request) - .then((results) => { - const detections = results[0].safeSearchAnnotation; - - console.log(`Adult: ${detections.adult}`); - console.log(`Spoof: ${detections.spoof}`); - console.log(`Medical: ${detections.medical}`); - console.log(`Violence: ${detections.violence}`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END vision_safe_search_detection_gcs] -} - -function detectCropHints (fileName) { - // [START vision_crop_hint_detection] - - // Imports the Google Cloud client library - const Vision = require('@google-cloud/vision'); - - // Creates a client - const vision = new Vision(); - - /** - * TODO(developer): Uncomment the following line before running the sample. - */ - // const fileName = 'Local image file, e.g. /path/to/image.png'; - - // Find crop hints for the local file - vision.cropHints({ source: { filename: fileName } }) - .then((results) => { - const cropHints = results[0].cropHintsAnnotation; - - cropHints.cropHints.forEach((hintBounds, hintIdx) => { - console.log(`Crop Hint ${hintIdx}:`); - hintBounds.boundingPoly.vertices.forEach((bound, boundIdx) => { - console.log(` Bound ${boundIdx}: (${bound.x}, ${bound.y})`); - }); - }); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END vision_crop_hint_detection] -} - -function detectCropHintsGCS (bucketName, fileName) { - // [START vision_crop_hint_detection_gcs] - - // Imports the Google Cloud client libraries - const Vision = require('@google-cloud/vision'); - - // Creates a client - const vision = new Vision(); - - /** - * TODO(developer): Uncomment the following lines before running the sample. - */ - // const bucketName = 'Bucket where the file resides, e.g. my-bucket'; - // const fileName = 'Path to file within bucket, e.g. path/to/image.png'; - - const request = { - source: { - imageUri: `gs://${bucketName}/${fileName}` - } - }; - - // Find crop hints for the remote file - vision.cropHints(request) - .then((results) => { - const cropHints = results[0].cropHintsAnnotation; - - cropHints.cropHints.forEach((hintBounds, hintIdx) => { - console.log(`Crop Hint ${hintIdx}:`); - hintBounds.boundingPoly.vertices.forEach((bound, boundIdx) => { - console.log(` Bound ${boundIdx}: (${bound.x}, ${bound.y})`); - }); - }); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END vision_crop_hint_detection_gcs] -} - -function detectWeb (fileName) { - // [START vision_web_detection] - - // Imports the Google Cloud client library - const Vision = require('@google-cloud/vision'); - - // Creates a client - const vision = new Vision(); - - /** - * TODO(developer): Uncomment the following line before running the sample. - */ - // const fileName = 'Local image file, e.g. /path/to/image.png'; - - // Detect similar images on the web to a local file - vision.webDetection({ source: { filename: fileName } }) - .then((results) => { - const webDetection = results[0].webDetection; - - if (webDetection.fullMatchingImages.length) { - console.log(`Full matches found: ${webDetection.fullMatchingImages.length}`); - webDetection.fullMatchingImages.forEach((image) => { - console.log(` URL: ${image.url}`); - console.log(` Score: ${image.score}`); - }); - } - - if (webDetection.partialMatchingImages.length) { - console.log(`Partial matches found: ${webDetection.partialMatchingImages.length}`); - webDetection.partialMatchingImages.forEach((image) => { - console.log(` URL: ${image.url}`); - console.log(` Score: ${image.score}`); - }); - } - - if (webDetection.webEntities.length) { - console.log(`Web entities found: ${webDetection.webEntities.length}`); - webDetection.webEntities.forEach((webEntity) => { - console.log(` Description: ${webEntity.description}`); - console.log(` Score: ${webEntity.score}`); - }); - } - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END vision_web_detection] -} - -function detectWebGCS (bucketName, fileName) { - // [START vision_web_detection_gcs] - - // Imports the Google Cloud client libraries - const Vision = require('@google-cloud/vision'); - - // Creates a client - const vision = new Vision(); - - /** - * TODO(developer): Uncomment the following lines before running the sample. - */ - // const bucketName = 'Bucket where the file resides, e.g. my-bucket'; - // const fileName = 'Path to file within bucket, e.g. path/to/image.png'; - - const request = { - source: { - imageUri: `gs://${bucketName}/${fileName}` - } - }; - - // Detect similar images on the web to a remote file - vision.webDetection(request) - .then((results) => { - const webDetection = results[0].webDetection; - - if (webDetection.fullMatchingImages.length) { - console.log(`Full matches found: ${webDetection.fullMatchingImages.length}`); - webDetection.fullMatchingImages.forEach((image) => { - console.log(` URL: ${image.url}`); - console.log(` Score: ${image.score}`); - }); - } - - if (webDetection.partialMatchingImages.length) { - console.log(`Partial matches found: ${webDetection.partialMatchingImages.length}`); - webDetection.partialMatchingImages.forEach((image) => { - console.log(` URL: ${image.url}`); - console.log(` Score: ${image.score}`); - }); - } - - if (webDetection.webEntities.length) { - console.log(`Web entities found: ${webDetection.webEntities.length}`); - webDetection.webEntities.forEach((webEntity) => { - console.log(` Description: ${webEntity.description}`); - console.log(` Score: ${webEntity.score}`); - }); - } - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END vision_web_detection_gcs] -} - -function detectFulltext (fileName) { - // [START vision_fulltext_detection] - - // Imports the Google Cloud client library - const Vision = require('@google-cloud/vision'); - - // Creates a client - const vision = new Vision(); - - /** - * TODO(developer): Uncomment the following line before running the sample. - */ - // const fileName = 'Local image file, e.g. /path/to/image.png'; - - // Read a local image as a text document - vision.documentTextDetection({ source: { filename: fileName } }) - .then((results) => { - const fullTextAnnotation = results[0].fullTextAnnotation; - console.log(fullTextAnnotation.text); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END vision_fulltext_detection] -} - -function detectFulltextGCS (bucketName, fileName) { - // [START vision_fulltext_detection_gcs] - - // Imports the Google Cloud client libraries - const Vision = require('@google-cloud/vision'); - - // Creates a client - const vision = new Vision(); - - /** - * TODO(developer): Uncomment the following lines before running the sample. - */ - // const bucketName = 'Bucket where the file resides, e.g. my-bucket'; - // const fileName = 'Path to file within bucket, e.g. path/to/image.png'; - - const request = { - source: { - imageUri: `gs://${bucketName}/${fileName}` - } - }; - - // Read a remote image as a text document - vision.documentTextDetection(request) - .then((results) => { - const fullTextAnnotation = results[0].fullTextAnnotation; - console.log(fullTextAnnotation.text); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END vision_fulltext_detection_gcs] -} - -require(`yargs`) // eslint-disable-line - .demand(1) - .command( - `faces `, - `Detects faces in a local image file.`, - {}, - (opts) => detectFaces(opts.fileName) - ) - .command( - `faces-gcs `, - `Detects faces in an image in Google Cloud Storage.`, - {}, - (opts) => detectFacesGCS(opts.bucketName, opts.fileName) - ) - .command( - `labels `, - `Detects labels in a local image file.`, - {}, - (opts) => detectLabels(opts.fileName) - ) - .command( - `labels-gcs `, - `Detects labels in an image in Google Cloud Storage.`, - {}, - (opts) => detectLabelsGCS(opts.bucketName, opts.fileName) - ) - .command( - `landmarks `, - `Detects landmarks in a local image file.`, - {}, - (opts) => detectLandmarks(opts.fileName) - ) - .command( - `landmarks-gcs `, - `Detects landmarks in an image in Google Cloud Storage.`, - {}, - (opts) => detectLandmarksGCS(opts.bucketName, opts.fileName) - ) - .command( - `text `, - `Detects text in a local image file.`, - {}, - (opts) => detectText(opts.fileName) - ) - .command( - `text-gcs `, - `Detects text in an image in Google Cloud Storage.`, - {}, - (opts) => detectTextGCS(opts.bucketName, opts.fileName) - ) - .command( - `logos `, - `Detects logos in a local image file.`, - {}, - (opts) => detectLogos(opts.fileName) - ) - .command( - `logos-gcs `, - `Detects logos in an image in Google Cloud Storage.`, - {}, - (opts) => detectLogosGCS(opts.bucketName, opts.fileName) - ) - .command( - `properties `, - `Detects image properties in a local image file.`, - {}, - (opts) => detectProperties(opts.fileName) - ) - .command( - `properties-gcs `, - `Detects image properties in an image in Google Cloud Storage.`, - {}, - (opts) => detectPropertiesGCS(opts.bucketName, opts.fileName) - ) - .command( - `safe-search `, - `Detects safe search properties in a local image file.`, - {}, - (opts) => detectSafeSearch(opts.fileName) - ) - .command( - `safe-search-gcs `, - `Detects safe search properties in an image in Google Cloud Storage.`, - {}, - (opts) => detectSafeSearchGCS(opts.bucketName, opts.fileName) - ) - .command( - `crops `, - `Detects crop hints in a local image file.`, - {}, - (opts) => detectCropHints(opts.fileName) - ) - .command( - `crops-gcs `, - `Detects crop hints in an image in Google Cloud Storage.`, - {}, - (opts) => detectCropHintsGCS(opts.bucketName, opts.fileName) - ) - .command( - `web `, - `Finds similar photos on the web for a local image file.`, - {}, - (opts) => detectWeb(opts.fileName) - ) - .command( - `web-gcs `, - `Finds similar photos on the web for an image in Google Cloud Storage.`, - {}, - (opts) => detectWebGCS(opts.bucketName, opts.fileName) - ) - .command( - `fulltext `, - `Extracts full text from a local image file.`, - {}, - (opts) => detectFulltext(opts.fileName) - ) - .command( - `fulltext-gcs `, - `Extracts full text from an image in Google Cloud Storage.`, - {}, - (opts) => detectFulltextGCS(opts.bucketName, opts.fileName) - ) - .example(`node $0 faces ./resources/face_no_surprise.jpg`) - .example(`node $0 faces-gcs my-bucket your-image.jpg`) - .example(`node $0 labels ./resources/wakeupcat.jpg`) - .example(`node $0 labels-gcs my-bucket your-image.jpg`) - .example(`node $0 landmarks ./resources/landmark.jpg`) - .example(`node $0 landmarks-gcs my-bucket your-image.jpg`) - .example(`node $0 text ./resources/wakeupcat.jpg`) - .example(`node $0 text-gcs my-bucket your-image.jpg`) - .example(`node $0 logos ./resources/logos.png`) - .example(`node $0 logos-gcs my-bucket your-image.jpg.png`) - .example(`node $0 properties ./resources/landmark.jpg`) - .example(`node $0 properties-gcs my-bucket your-image.jpg`) - .example(`node $0 safe-search ./resources/wakeupcat.jpg`) - .example(`node $0 safe-search-gcs my-bucket your-image.jpg`) - .example(`node $0 crops ./resources/wakeupcat.jpg`) - .example(`node $0 crops-gcs my-bucket your-image.jpg`) - .example(`node $0 web ./resources/wakeupcat.jpg`) - .example(`node $0 web-gcs my-bucket your-image.jpg`) - .example(`node $0 fulltext ./resources/wakeupcat.jpg`) - .example(`node $0 fulltext-gcs my-bucket your-image.jpg`) - .wrap(120) - .recommendCommands() - .epilogue(`For more information, see https://cloud.google.com/vision/docs`) - .help() - .strict() - .argv; diff --git a/vision/faceDetection.js b/vision/faceDetection.js deleted file mode 100644 index 6163ccfb7a..0000000000 --- a/vision/faceDetection.js +++ /dev/null @@ -1,128 +0,0 @@ -/** - * Copyright 2016, Google, Inc. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; - -// [START auth] -// By default, the client will authenticate using the service account file -// specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use -// the project specified by the GCLOUD_PROJECT environment variable. See -// https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/guides/authentication -var Vision = require('@google-cloud/vision'); - -// Instantiate a vision client -var vision = Vision(); -// [END auth] - -var fs = require('fs'); - -/** - * Uses the Vision API to detect faces in the given file. - */ -function detectFaces (inputFile, callback) { - // Make a call to the Vision API to detect the faces - const request = { source: { filename: inputFile } }; - vision.faceDetection(request) - .then((results) => { - const faces = results[0].faceAnnotations; - var numFaces = faces.length; - console.log('Found ' + numFaces + (numFaces === 1 ? ' face' : ' faces')); - callback(null, faces); - }) - .catch((err) => { - console.error('ERROR:', err); - callback(err); - }); -} - -/** - * Draws a polygon around the faces, then saves to outputFile. - */ -function highlightFaces (inputFile, faces, outputFile, Canvas, callback) { - fs.readFile(inputFile, (err, image) => { - if (err) { - return callback(err); - } - - var Image = Canvas.Image; - // Open the original image into a canvas - var img = new Image(); - img.src = image; - var canvas = new Canvas(img.width, img.height); - var context = canvas.getContext('2d'); - context.drawImage(img, 0, 0, img.width, img.height); - - // Now draw boxes around all the faces - context.strokeStyle = 'rgba(0,255,0,0.8)'; - context.lineWidth = '5'; - - faces.forEach((face) => { - context.beginPath(); - let origX = 0; - let origY = 0; - face.boundingPoly.vertices.forEach((bounds, i) => { - if (i === 0) { - origX = bounds.x; - origY = bounds.y; - } - context.lineTo(bounds.x, bounds.y); - }); - context.lineTo(origX, origY); - context.stroke(); - }); - - // Write the result to a file - console.log('Writing to file ' + outputFile); - var writeStream = fs.createWriteStream(outputFile); - var pngStream = canvas.pngStream(); - - pngStream.on('data', (chunk) => { - writeStream.write(chunk); - }); - pngStream.on('error', console.log); - pngStream.on('end', callback); - }); -} - -// Run the example -function main (inputFile, outputFile, Canvas, callback) { - outputFile = outputFile || 'out.png'; - detectFaces(inputFile, (err, faces) => { - if (err) { - return callback(err); - } - - console.log('Highlighting...'); - highlightFaces(inputFile, faces, outputFile, Canvas, (err) => { - if (err) { - return callback(err); - } - console.log('Finished!'); - callback(null, faces); - }); - }); -} - -exports.main = main; - -if (module === require.main) { - if (process.argv.length < 3) { - console.log('Usage: node faceDetection [outputFile]'); - process.exit(1); - } - var inputFile = process.argv[2]; - var outputFile = process.argv[3]; - exports.main(inputFile, outputFile, require('canvas'), console.log); -} diff --git a/vision/package.json b/vision/package.json deleted file mode 100644 index be92f874eb..0000000000 --- a/vision/package.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "name": "nodejs-docs-samples-vision", - "version": "0.0.1", - "private": true, - "license": "Apache-2.0", - "author": "Google Inc.", - "repository": { - "type": "git", - "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" - }, - "engines": { - "node": ">=4.3.2" - }, - "scripts": { - "lint": "samples lint", - "pretest": "npm run lint", - "test": "samples test run --cmd ava -- -T 1m --verbose system-test/*.test.js" - }, - "dependencies": { - "@google-cloud/storage": "1.1.0", - "@google-cloud/vision": "^0.12.0", - "async": "2.3.0", - "natural": "0.5.1", - "redis": "2.7.1", - "yargs": "7.1.0" - }, - "devDependencies": { - "@google-cloud/nodejs-repo-tools": "1.4.14", - "ava": "0.19.1", - "proxyquire": "1.7.11", - "sinon": "2.1.0" - }, - "optionalDependencies": { - "canvas": "1.6.5" - }, - "cloud-repo-tools": { - "requiresKeyFile": true, - "requiresProjectId": true - } -} diff --git a/vision/quickstart.js b/vision/quickstart.js deleted file mode 100644 index 60d9ea4ce8..0000000000 --- a/vision/quickstart.js +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Copyright 2017, Google, Inc. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; - -// [START vision_quickstart] -// Imports the Google Cloud client library -const Vision = require('@google-cloud/vision'); - -// Creates a client -const vision = new Vision(); - -// The name of the image file to annotate -const fileName = './resources/wakeupcat.jpg'; - -// Prepare the request object -const request = { - source: { - filename: fileName - } -}; - -// Performs label detection on the image file -vision.labelDetection(request) - .then((results) => { - const labels = results[0].labelAnnotations; - - console.log('Labels:'); - labels.forEach((label) => console.log(label.description)); - }) - .catch((err) => { - console.error('ERROR:', err); - }); -// [END vision_quickstart] diff --git a/vision/resources/bonito.gif b/vision/resources/bonito.gif deleted file mode 100644 index bea0b6ebd4..0000000000 Binary files a/vision/resources/bonito.gif and /dev/null differ diff --git a/vision/resources/cat.jpg b/vision/resources/cat.jpg deleted file mode 100644 index 76af906f0a..0000000000 Binary files a/vision/resources/cat.jpg and /dev/null differ diff --git a/vision/resources/face.png b/vision/resources/face.png deleted file mode 100644 index b613a94e34..0000000000 Binary files a/vision/resources/face.png and /dev/null differ diff --git a/vision/resources/face_no_surprise.jpg b/vision/resources/face_no_surprise.jpg deleted file mode 100644 index 0e2894adb8..0000000000 Binary files a/vision/resources/face_no_surprise.jpg and /dev/null differ diff --git a/vision/resources/faulkner.jpg b/vision/resources/faulkner.jpg deleted file mode 100644 index 93b8ac3ad2..0000000000 Binary files a/vision/resources/faulkner.jpg and /dev/null differ diff --git a/vision/resources/landmark.jpg b/vision/resources/landmark.jpg deleted file mode 100644 index 41c3d0fc93..0000000000 Binary files a/vision/resources/landmark.jpg and /dev/null differ diff --git a/vision/resources/logos.png b/vision/resources/logos.png deleted file mode 100644 index dcfb4ac955..0000000000 Binary files a/vision/resources/logos.png and /dev/null differ diff --git a/vision/resources/mountain.jpg b/vision/resources/mountain.jpg deleted file mode 100644 index f9505df38f..0000000000 Binary files a/vision/resources/mountain.jpg and /dev/null differ diff --git a/vision/resources/no-text.jpg b/vision/resources/no-text.jpg deleted file mode 100644 index 8b77575de7..0000000000 Binary files a/vision/resources/no-text.jpg and /dev/null differ diff --git a/vision/resources/sabertooth.gif b/vision/resources/sabertooth.gif deleted file mode 100644 index 2cee28eeb1..0000000000 Binary files a/vision/resources/sabertooth.gif and /dev/null differ diff --git a/vision/resources/succulents.jpg b/vision/resources/succulents.jpg deleted file mode 100644 index 197fe6ac16..0000000000 Binary files a/vision/resources/succulents.jpg and /dev/null differ diff --git a/vision/resources/sunbeamkitties.jpg b/vision/resources/sunbeamkitties.jpg deleted file mode 100644 index b9a584ef55..0000000000 Binary files a/vision/resources/sunbeamkitties.jpg and /dev/null differ diff --git a/vision/resources/text.jpg b/vision/resources/text.jpg deleted file mode 100644 index 3b17d55de0..0000000000 Binary files a/vision/resources/text.jpg and /dev/null differ diff --git a/vision/resources/wakeupcat.jpg b/vision/resources/wakeupcat.jpg deleted file mode 100644 index 139cf461ec..0000000000 Binary files a/vision/resources/wakeupcat.jpg and /dev/null differ diff --git a/vision/resources/water.jpg b/vision/resources/water.jpg deleted file mode 100644 index 1554b63df0..0000000000 Binary files a/vision/resources/water.jpg and /dev/null differ diff --git a/vision/system-test/detect.test.js b/vision/system-test/detect.test.js deleted file mode 100644 index ac6693b97b..0000000000 --- a/vision/system-test/detect.test.js +++ /dev/null @@ -1,174 +0,0 @@ -/** - * Copyright 2017, Google, Inc. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; - -const path = require(`path`); -const storage = require(`@google-cloud/storage`)(); -const test = require(`ava`); -const tools = require(`@google-cloud/nodejs-repo-tools`); -const uuid = require(`uuid`); - -const bucketName = `nodejs-docs-samples-test-${uuid.v4()}`; -const cmd = `node detect.js`; -const cwd = path.join(__dirname, `..`); -const files = [ - `face_no_surprise.jpg`, - `landmark.jpg`, - `logos.png`, - `text.jpg`, - `wakeupcat.jpg`, - `faulkner.jpg` -].map((name) => { - return { - name, - localPath: path.resolve(path.join(__dirname, `../resources/${name}`)) - }; -}); - -test.before(tools.checkCredentials); -test.before(async () => { - const [bucket] = await storage.createBucket(bucketName); - await Promise.all(files.map((file) => bucket.upload(file.localPath))); -}); - -test.after.always(async () => { - const bucket = storage.bucket(bucketName); - await bucket.deleteFiles({ force: true }); - await bucket.deleteFiles({ force: true }); // Try a second time... - await bucket.delete(); -}); - -test(`should detect faces in a local file`, async (t) => { - const output = await tools.runAsync(`${cmd} faces ${files[0].localPath}`, cwd); - t.true(output.includes(`Faces:`)); - t.true(output.includes(`Face #1:`)); -}); - -test(`should detect faces in a remote file`, async (t) => { - const output = await tools.runAsync(`${cmd} faces-gcs ${bucketName} ${files[0].name}`, cwd); - t.true(output.includes(`Faces:`)); - t.true(output.includes(`Face #1:`)); -}); - -test(`should detect labels in a local file`, async (t) => { - const output = await tools.runAsync(`${cmd} labels ${files[4].localPath}`, cwd); - t.true(output.includes(`Labels:`)); - t.true(output.includes(`cat`)); -}); - -test(`should detect labels in a remote file`, async (t) => { - const output = await tools.runAsync(`${cmd} labels-gcs ${bucketName} ${files[4].name}`, cwd); - t.true(output.includes(`Labels:`)); - t.true(output.includes(`cat`)); -}); - -test(`should detect landmarks in a local file`, async (t) => { - const output = await tools.runAsync(`${cmd} landmarks ${files[1].localPath}`, cwd); - t.true(output.includes(`Landmarks:`)); - t.true(output.includes(`Palace of Fine Arts`)); -}); - -test(`should detect landmarks in a remote file`, async (t) => { - const output = await tools.runAsync(`${cmd} landmarks-gcs ${bucketName} ${files[1].name}`, cwd); - t.true(output.includes(`Landmarks:`)); - t.true(output.includes(`Palace of Fine Arts`)); -}); - -test(`should detect text in a local file`, async (t) => { - const output = await tools.runAsync(`${cmd} text ${files[3].localPath}`, cwd); - t.true(output.includes(`Text:`)); - t.true(output.includes(`System Software Update`)); -}); - -test(`should detect text in a remote file`, async (t) => { - const output = await tools.runAsync(`${cmd} text-gcs ${bucketName} ${files[3].name}`, cwd); - t.true(output.includes(`Text:`)); - t.true(output.includes(`System Software Update`)); -}); - -test(`should detect logos in a local file`, async (t) => { - const output = await tools.runAsync(`${cmd} logos ${files[2].localPath}`, cwd); - t.true(output.includes(`Logos:`)); - t.true(output.includes(`Google`)); -}); - -test(`should detect logos in a remote file`, async (t) => { - const output = await tools.runAsync(`${cmd} logos-gcs ${bucketName} ${files[2].name}`, cwd); - t.true(output.includes(`Logos:`)); - t.true(output.includes(`Google`)); -}); - -test(`should detect properties in a local file`, async (t) => { - const output = await tools.runAsync(`${cmd} properties ${files[1].localPath}`, cwd); - t.true(output.includes(`{ color: { red: 69, green: 42, blue: 27`)); - t.true(output.split(`\n`).length > 4, `Multiple colors were detected.`); -}); - -test(`should detect properties in a remote file`, async (t) => { - const output = await tools.runAsync(`${cmd} properties-gcs ${bucketName} ${files[1].name}`, cwd); - t.true(output.includes(`{ color: { red: 69, green: 42, blue: 27`)); - t.true(output.split(`\n`).length > 4, `Multiple colors were detected.`); -}); - -test(`should detect safe-search in a local file`, async (t) => { - const output = await tools.runAsync(`${cmd} safe-search ${files[4].localPath}`, cwd); - t.true(output.includes(`Medical:`)); -}); - -test(`should detect safe-search in a remote file`, async (t) => { - const output = await tools.runAsync(`${cmd} safe-search-gcs ${bucketName} ${files[4].name}`, cwd); - t.true(output.includes(`Medical:`)); -}); - -test(`should detect crop hints in a local file`, async (t) => { - const output = await tools.runAsync(`${cmd} crops ${files[2].localPath}`, cwd); - t.true(output.includes(`Crop Hint 0:`)); - t.true(output.includes(`Bound 2: (280, 43)`)); -}); - -test(`should detect crop hints in a remote file`, async (t) => { - const output = await tools.runAsync(`${cmd} crops-gcs ${bucketName} ${files[2].name}`, cwd); - t.true(output.includes(`Crop Hint 0:`)); - t.true(output.includes(`Bound 2: (280, 43)`)); -}); - -test(`should detect similar web images in a local file`, async (t) => { - const output = await tools.runAsync(`${cmd} web ${files[5].localPath}`, cwd); - t.true(output.includes('Full matches found:')); - t.true(output.includes('URL: https://cloud.google.com/vision/docs/images/')); - t.true(output.includes('Partial matches found:')); - t.true(output.includes('Web entities found:')); - t.true(output.includes('Description: Google Cloud Platform')); -}); - -test(`should detect similar web images in a remote file`, async (t) => { - const output = await tools.runAsync(`${cmd} web-gcs ${bucketName} ${files[5].name}`, cwd); - t.true(output.includes('Full matches found:')); - t.true(output.includes('URL: https://cloud.google.com/vision/docs/images/')); - t.true(output.includes('Partial matches found:')); - t.true(output.includes('Web entities found:')); - t.true(output.includes('Description: Google')); -}); - -test(`should read a document from a local file`, async (t) => { - const output = await tools.runAsync(`${cmd} fulltext ${files[2].localPath}`, cwd); - t.true(output.includes('Google Cloud Platform')); -}); - -test(`should read a document from a remote file`, async (t) => { - const output = await tools.runAsync(`${cmd} fulltext-gcs ${bucketName} ${files[2].name}`, cwd); - t.true(output.includes('Google Cloud Platform')); -}); diff --git a/vision/system-test/faceDetection.test.js b/vision/system-test/faceDetection.test.js deleted file mode 100644 index f1e8fdaa9a..0000000000 --- a/vision/system-test/faceDetection.test.js +++ /dev/null @@ -1,80 +0,0 @@ -/** - * Copyright 2016, Google, Inc. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; - -const fs = require(`fs`); -const path = require(`path`); -const test = require(`ava`); -const tools = require(`@google-cloud/nodejs-repo-tools`); - -class MockCanvas { - getContext () { - return { - drawImage: () => {}, - beginPath: () => {}, - lineTo: () => {}, - stroke: () => {} - }; - } - - pngStream () { - return { - on: (event, cb) => { - if (event === 'end') { - setTimeout(cb, 1000); - } else if (event === `data`) { - /* eslint-disable */ - cb(`test`); - cb(`foo`); - cb(`bar`); - /* eslint-enable */ - } - } - }; - } -} - -MockCanvas.Image = class Image {}; - -const faceDetectionExample = require(`../faceDetection`); -const inputFile = path.join(__dirname, `../resources`, `face.png`); -const outputFile = path.join(__dirname, `../../vision`, `out.png`); - -test.before(tools.checkCredentials); -test.before(tools.stubConsole); -test.after.always(tools.restoreConsole); - -test.cb(`should detect faces`, (t) => { - let done = false; - let timeout = setTimeout(() => { - if (!done) { - console.warn('Face detection timed out!'); - t.end(); - } - }, 30); - faceDetectionExample.main(inputFile, outputFile, MockCanvas, (err, faces) => { - t.ifError(err); - t.is(faces.length, 1); - const image = fs.readFileSync(outputFile); - t.is(image.toString(`utf8`), `testfoobar`); - t.true(console.log.calledWith(`Found 1 face`)); - t.true(console.log.calledWith(`Highlighting...`)); - t.true(console.log.calledWith(`Finished!`)); - done = true; - clearTimeout(timeout); - t.end(); - }); -}); diff --git a/vision/system-test/quickstart.test.js b/vision/system-test/quickstart.test.js deleted file mode 100644 index e3304caec6..0000000000 --- a/vision/system-test/quickstart.test.js +++ /dev/null @@ -1,32 +0,0 @@ -/** - * Copyright 2017, Google, Inc. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; - -const path = require(`path`); -const test = require(`ava`); -const tools = require(`@google-cloud/nodejs-repo-tools`); - -const cmd = `node quickstart.js`; -const cwd = path.join(__dirname, `..`); - -test.before(tools.stubConsole); -test.after.always(tools.restoreConsole); - -test(`should detect labels in a remote file`, async (t) => { - const output = await tools.runAsync(`${cmd}`, cwd); - t.true(output.includes(`Labels:`)); - t.true(output.includes(`cat`)); -}); diff --git a/vision/system-test/textDetection.test.js b/vision/system-test/textDetection.test.js deleted file mode 100644 index 70575cd220..0000000000 --- a/vision/system-test/textDetection.test.js +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Copyright 2016, Google, Inc. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; - -const path = require(`path`); -const test = require(`ava`); -const tools = require(`@google-cloud/nodejs-repo-tools`); - -test.before(tools.checkCredentials); - -test.cb(`should detect texts`, (t) => { - const redis = require('redis'); - const client = redis.createClient(); - client - .on('error', (err) => { - if (err && err.code === 'ECONNREFUSED') { - console.error('Redis is unavailable. Skipping vision textDetection test.'); - t.end(); - } else { - t.end(err); - } - }) - .on('ready', () => { - const inputDir = path.join(__dirname, `../resources`); - const textDetectionSample = require(`../textDetection`); - textDetectionSample.main(inputDir, (err, textResponse) => { - t.ifError(err); - t.true(Object.keys(textResponse).length > 0); - textDetectionSample.lookup(['the', 'sunbeams', 'in'], (err, hits) => { - t.ifError(err); - t.true(hits.length > 0); - t.true(hits[0].length > 0); - t.end(); - }); - }); - }); -}); diff --git a/vision/textDetection.js b/vision/textDetection.js deleted file mode 100644 index 65201266e8..0000000000 --- a/vision/textDetection.js +++ /dev/null @@ -1,284 +0,0 @@ -/** - * Copyright 2016, Google, Inc. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; - -// [START app] -// [START import_libraries] -var async = require('async'); -var fs = require('fs'); -var path = require('path'); - -// By default, the client will authenticate using the service account file -// specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use -// the project specified by the GCLOUD_PROJECT environment variable. See -// https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/guides/authentication -var Vision = require('@google-cloud/vision'); -var natural = require('natural'); -var redis = require('redis'); - -// Instantiate a vision client -var vision = Vision(); -// [END import_libraries] - -function Index () { - // Connect to a redis server. - var TOKEN_DB = 0; - var DOCS_DB = 1; - var PORT = process.env.REDIS_PORT || '6379'; - var HOST = process.env.REDIS_HOST || '127.0.0.1'; - - this.tokenClient = redis.createClient(PORT, HOST, { - db: TOKEN_DB - }).on('error', function (err) { - console.error('ERR:REDIS: ' + err); - }); - this.docsClient = redis.createClient(PORT, HOST, { - db: DOCS_DB - }).on('error', function (err) { - console.error('ERR:REDIS: ' + err); - }); -} - -Index.prototype.quit = function () { - this.tokenClient.quit(); - this.docsClient.quit(); -}; - -Index.prototype.add = function (filename, document, callback) { - var self = this; - var PUNCTUATION = ['.', ',', ':', '']; - var tokenizer = new natural.WordTokenizer(); - var tokens = tokenizer.tokenize(document); - - // TODO: Remove stop words - - var tasks = tokens.filter(function (token) { - return PUNCTUATION.indexOf(token) === -1; - }).map(function (token) { - return function (cb) { - self.tokenClient.sadd(token, filename, cb); - }; - }); - - tasks.push(function (cb) { - self.tokenClient.set(filename, document, cb); - }); - - async.parallel(tasks, callback); -}; - -Index.prototype.lookup = function (words, callback) { - var self = this; - var tasks = words.map(function (word) { - word = word.toLowerCase(); - return function (cb) { - self.tokenClient.smembers(word, cb); - }; - }); - async.parallel(tasks, callback); -}; - -Index.prototype.documentIsProcessed = function (filename, callback) { - this.docsClient.GET(filename, function (err, value) { - if (err) { - return callback(err); - } - if (value) { - console.log(filename + ' already added to index.'); - callback(null, true); - } else if (value === '') { - console.log(filename + ' was already checked, and contains no text.'); - callback(null, true); - } else { - callback(null, false); - } - }); -}; - -Index.prototype.setContainsNoText = function (filename, callback) { - this.docsClient.set(filename, '', callback); -}; - -function lookup (words, callback) { - var index = new Index(); - index.lookup(words, function (err, hits) { - index.quit(); - if (err) { - return callback(err); - } - words.forEach(function (word, i) { - console.log('hits for "' + word + '":', hits[i].join(', ')); - }); - callback(null, hits); - }); -} - -// [START extract_descrs] -function extractDescription (texts) { - var document = ''; - texts.forEach(function (text) { - document += (text.description || ''); - }); - return document; -} - -function extractDescriptions (filename, index, response, callback) { - if (response.textAnnotations.length) { - index.add(filename, extractDescription(response.textAnnotations), callback); - } else { - console.log(filename + ' had no discernable text.'); - index.setContainsNoText(filename, callback); - } -} -// [END extract_descrs] - -// [START get_text] -function getTextFromFiles (index, inputFiles, callback) { - // Make a call to the Vision API to detect text - let requests = []; - inputFiles.forEach((filename) => { - let request = { - image: {content: fs.readFileSync(filename).toString('base64')}, - features: {type: 'TEXT_DETECTION'} - }; - requests.push(request); - }); - vision.batchAnnotateImages({requests: requests}) - .then((results) => { - let detections = results[0].responses; - var textResponse = {}; - var tasks = []; - inputFiles.forEach(function (filename, i) { - var response = detections[i]; - if (response.error) { - console.log('API Error for ' + filename, response.error); - return; - } else if (Array.isArray(response)) { - textResponse[filename] = 1; - } else { - textResponse[filename] = 0; - } - tasks.push(function (cb) { - extractDescriptions(filename, index, response, cb); - }); - }); - async.parallel(tasks, function (err) { - if (err) { - return callback(err); - } - callback(null, textResponse); - }); - }); -} - -// Run the example -function main (inputDir, callback) { - var index = new Index(); - - async.waterfall([ - // Scan the specified directory for files - function (cb) { - fs.readdir(inputDir, cb); - }, - // Separate directories from files - function (files, cb) { - async.parallel(files.map(function (file) { - var filename = path.join(inputDir, file); - return function (cb) { - fs.stat(filename, function (err, stats) { - if (err) { - return cb(err); - } - if (!stats.isDirectory()) { - return cb(null, filename); - } - cb(); - }); - }; - }), cb); - }, - // Figure out which files have already been processed - function (allImageFiles, cb) { - var tasks = allImageFiles.filter(function (filename) { - return filename; - }).map(function (filename) { - return function (cb) { - index.documentIsProcessed(filename, function (err, processed) { - if (err) { - return cb(err); - } - if (!processed) { - // Forward this filename on for further processing - return cb(null, filename); - } - cb(); - }); - }; - }); - async.parallel(tasks, cb); - }, - // Analyze any remaining unprocessed files - function (imageFilesToProcess, cb) { - imageFilesToProcess = imageFilesToProcess.filter(function (filename) { - return filename; - }); - if (imageFilesToProcess.length) { - return getTextFromFiles(index, imageFilesToProcess, cb); - } - console.log('All files processed!'); - cb(); - } - ], function (err, result) { - index.quit(); - callback(err, result); - }); -} -// [END get_text] - -// [START run_application] -if (module === require.main) { - var generalError = 'Usage: node textDetection ...\n\n' + - '\tCommands: analyze, lookup'; - if (process.argv.length < 3) { - console.log(generalError); - process.exit(1); - } - var args = process.argv.slice(2); - var command = args.shift(); - if (command === 'analyze') { - if (!args.length) { - console.log('Usage: node textDetection analyze '); - process.exit(1); - } - main(args[0], console.log); - } else if (command === 'lookup') { - if (!args.length) { - console.log('Usage: node textDetection lookup ...'); - process.exit(1); - } - lookup(args, console.log); - } else { - console.log(generalError); - process.exit(1); - } -} -// [END run_application] -// [END app] - -exports.Index = Index; -exports.lookup = lookup; -exports.getTextFromFiles = getTextFromFiles; -exports.main = main;