使用服务账号密钥文件创建客户端
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
使用服务账号密钥文件创建 BigQuery 客户端。
代码示例
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis page provides code samples for creating a BigQuery client using a service account key file in C#, Java, Node.js, and Python.\u003c/p\u003e\n"],["\u003cp\u003eEach language example requires setting up Application Default Credentials for BigQuery authentication and references the relevant BigQuery quickstart guide and API documentation.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples demonstrate how to instantiate a BigQuery client by loading credentials from a JSON service account key file and specifying the project ID.\u003c/p\u003e\n"],["\u003cp\u003eThese examples provide users with a way to access the BigQuery service via a client with the required authentication.\u003c/p\u003e\n"],["\u003cp\u003eUsers are directed to a tool to search for similar examples using the Google cloud sample browser.\u003c/p\u003e\n"]]],[],null,["# Create a client with a service account key file\n\nCreate a BigQuery client using a service account key file.\n\nCode sample\n-----------\n\n### C#\n\n\nBefore trying this sample, follow the C# setup instructions in the\n[BigQuery quickstart using\nclient libraries](/bigquery/docs/quickstarts/quickstart-client-libraries).\n\n\nFor more information, see the\n[BigQuery C# API\nreference documentation](/dotnet/docs/reference/Google.Cloud.BigQuery.V2/latest).\n\n\nTo authenticate to BigQuery, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/bigquery/docs/authentication#client-libs).\n\n var credentials = GoogleCredential.FromFile(jsonPath);\n var client = BigQueryClient.Create(projectId, credentials);\n\n### Java\n\n\nBefore trying this sample, follow the Java setup instructions in the\n[BigQuery quickstart using\nclient libraries](/bigquery/docs/quickstarts/quickstart-client-libraries).\n\n\nFor more information, see the\n[BigQuery Java API\nreference documentation](/java/docs/reference/google-cloud-bigquery/latest/overview).\n\n\nTo authenticate to BigQuery, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/bigquery/docs/authentication#client-libs).\n\n public static void explicit() throws IOException {\n // TODO(developer): Replace these variables before running the sample.\n String projectId = \"MY_PROJECT_ID\";\n File credentialsPath = new File(\"path/to/your/service_account.json\");\n\n // Load credentials from JSON key file. If you can't set the GOOGLE_APPLICATION_CREDENTIALS\n // environment variable, you can explicitly load the credentials file to construct the\n // credentials.\n GoogleCredentials credentials;\n try (FileInputStream serviceAccountStream = new FileInputStream(credentialsPath)) {\n credentials = ServiceAccountCredentials.fromStream(serviceAccountStream);\n }\n\n // Instantiate a client.\n BigQuery bigquery =\n BigQueryOptions.newBuilder()\n .setCredentials(credentials)\n .setProjectId(projectId)\n .build()\n .getService();\n\n // Use the client.\n System.out.println(\"Datasets:\");\n for (Dataset dataset : bigquery.listDatasets().iterateAll()) {\n System.out.printf(\"%s%n\", dataset.getDatasetId().getDataset());\n }\n }\n\n### Node.js\n\n\nBefore trying this sample, follow the Node.js setup instructions in the\n[BigQuery quickstart using\nclient libraries](/bigquery/docs/quickstarts/quickstart-client-libraries).\n\n\nFor more information, see the\n[BigQuery Node.js API\nreference documentation](https://googleapis.dev/nodejs/bigquery/latest/index.html).\n\n\nTo authenticate to BigQuery, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/bigquery/docs/authentication#client-libs).\n\n // Create a BigQuery client explicitly using service account credentials.\n // by specifying the private key file.\n const {BigQuery} = require('https://cloud.google.com/nodejs/docs/reference/bigquery/latest/overview.html');\n\n const options = {\n keyFilename: 'path/to/service_account.json',\n projectId: 'my_project',\n };\n\n const bigquery = new https://cloud.google.com/nodejs/docs/reference/bigquery/latest/bigquery/bigquery.html(options);\n\n### Python\n\n\nBefore trying this sample, follow the Python setup instructions in the\n[BigQuery quickstart using\nclient libraries](/bigquery/docs/quickstarts/quickstart-client-libraries).\n\n\nFor more information, see the\n[BigQuery Python API\nreference documentation](/python/docs/reference/bigquery/latest).\n\n\nTo authenticate to BigQuery, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/bigquery/docs/authentication#client-libs).\n\n from google.cloud import https://cloud.google.com/python/docs/reference/bigquery/latest/\n from google.oauth2 import service_account\n\n # TODO(developer): Set key_path to the path to the service account key\n # file.\n # key_path = \"path/to/service_account.json\"\n\n credentials = service_account.Credentials.from_service_account_file(\n key_path,\n scopes=[\"https://www.googleapis.com/auth/cloud-platform\"],\n )\n\n # Alternatively, use service_account.Credentials.from_service_account_info()\n # to set credentials directly via a json object rather than set a filepath\n # TODO(developer): Set key_json to the content of the service account key file.\n # credentials = service_account.Credentials.from_service_account_info(key_json)\n\n client = https://cloud.google.com/python/docs/reference/bigquery/latest/.https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.client.Client.html(\n credentials=credentials,\n project=credentials.project_id,\n )\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=bigquery)."]]