Membuat klien dengan file kunci akun layanan
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Membuat klien BigQuery menggunakan file kunci akun layanan.
Contoh kode
Kecuali dinyatakan lain, konten di halaman ini dilisensikan berdasarkan Lisensi Creative Commons Attribution 4.0, sedangkan contoh kode dilisensikan berdasarkan Lisensi Apache 2.0. Untuk mengetahui informasi selengkapnya, lihat Kebijakan Situs Google Developers. Java adalah merek dagang terdaftar dari Oracle dan/atau afiliasinya.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","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)."]]