/** Creates a job identity given only its user-defined id. */ public static JobId of(String job) { return newBuilder().setJob(checkNotNull(job)).build(); }
/** Creates a job identity with autogenerated id and no project specified. */ public static JobId of() { return newBuilder().setRandomJob().build(); }
/** Creates a job identity given project's and job's user-defined id. */ public static JobId of(String project, String job) { return newBuilder().setProject(checkNotNull(project)).setJob(checkNotNull(job)).build(); }
static JobId fromPb(JobReference jobRef) { return newBuilder() .setProject(jobRef.getProjectId()) .setJob(jobRef.getJobId()) .setLocation(jobRef.getLocation()) .build(); } }
/** Example of creating a channel with which to write to a table. */ // [TARGET writer(JobId, WriteChannelConfiguration)] // [VARIABLE "my_dataset_name"] // [VARIABLE "my_table_name"] // [VARIABLE "StringValue1\nStringValue2\n"] // [VARIABLE "us"] public long writeToTableLocation( String datasetName, String tableName, String csvData, String location) throws IOException, InterruptedException, TimeoutException { // [START ] TableId tableId = TableId.of(datasetName, tableName); WriteChannelConfiguration writeChannelConfiguration = WriteChannelConfiguration.newBuilder(tableId).setFormatOptions(FormatOptions.csv()).build(); // The location must be specified; other fields can be auto-detected. JobId jobId = JobId.newBuilder().setLocation(location).build(); TableDataWriteChannel writer = bigquery.writer(jobId, writeChannelConfiguration); // Write data to writer try { writer.write(ByteBuffer.wrap(csvData.getBytes(Charsets.UTF_8))); } finally { writer.close(); } // Get load job Job job = writer.getJob(); job = job.waitFor(); LoadStatistics stats = job.getStatistics(); return stats.getOutputRows(); // [END ] }
/** Example of running a batch query. */ public void runBatchQuery() throws TimeoutException, InterruptedException { // [START bigquery_query_batch] // BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); String query = "SELECT corpus FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;"; QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(query) // Run at batch priority, which won't count toward concurrent rate // limit. .setPriority(QueryJobConfiguration.Priority.BATCH) .build(); // Location must match that of the dataset(s) referenced in the query. JobId jobId = JobId.newBuilder().setRandomJob().setLocation("US").build(); String jobIdString = jobId.getJob(); // API request - starts the query. bigquery.create(JobInfo.newBuilder(queryConfig).setJobId(jobId).build()); // Check on the progress by getting the job's updated state. Once the state // is `DONE`, the results are ready. Job queryJob = bigquery.getJob(JobId.newBuilder().setJob(jobIdString).setLocation("US").build()); System.out.printf( "Job %s in location %s currently in state: %s%n", queryJob.getJobId().getJob(), queryJob.getJobId().getLocation(), queryJob.getStatus().getState().toString()); // [END bigquery_query_batch] }
/** Example of writing a local file to a table. */ // [TARGET writer(WriteChannelConfiguration)] // [VARIABLE "my_dataset_name"] // [VARIABLE "my_table_name"] // [VARIABLE FileSystems.getDefault().getPath(".", "my-data.csv")] // [VARIABLE "us"] public long writeFileToTable(String datasetName, String tableName, Path csvPath, String location) throws IOException, InterruptedException, TimeoutException { // [START bigquery_load_from_file] TableId tableId = TableId.of(datasetName, tableName); WriteChannelConfiguration writeChannelConfiguration = WriteChannelConfiguration.newBuilder(tableId).setFormatOptions(FormatOptions.csv()).build(); // The location must be specified; other fields can be auto-detected. JobId jobId = JobId.newBuilder().setLocation(location).build(); TableDataWriteChannel writer = bigquery.writer(jobId, writeChannelConfiguration); // Write data to writer try (OutputStream stream = Channels.newOutputStream(writer)) { Files.copy(csvPath, stream); } // Get load job Job job = writer.getJob(); job = job.waitFor(); LoadStatistics stats = job.getStatistics(); return stats.getOutputRows(); // [END bigquery_load_from_file] }
bigquery.create( JobInfo.of( JobId.newBuilder().setLocation(location).build(), QueryJobConfiguration.of(query))); job = job.waitFor(); .query( QueryJobConfiguration.of(query), JobId.newBuilder().setLocation(location).build()) .iterateAll()) .isEmpty(); .query( QueryJobConfiguration.of(query), JobId.newBuilder().setLocation(wrongLocation).build()) .iterateAll(); fail("querying a table with wrong location shouldn't work"); try (TableDataWriteChannel writer = bigquery.writer( JobId.newBuilder().setLocation(location).build(), writeChannelConfiguration)) { writer.write(ByteBuffer.wrap("foo".getBytes())); JobId.newBuilder().setLocation(wrongLocation).build(), writeChannelConfiguration); fail("writing to a table with wrong location shouldn't work"); } catch (BigQueryException e) {
/** Creates a job identity given only its user-defined id. */ public static JobId of(String job) { return newBuilder().setJob(checkNotNull(job)).build(); }
/** Creates a job identity with autogenerated id and no project specified. */ public static JobId of() { return newBuilder().setRandomJob().build(); }
/** Creates a job identity given project's and job's user-defined id. */ public static JobId of(String project, String job) { return newBuilder().setProject(checkNotNull(project)).setJob(checkNotNull(job)).build(); }
static JobId fromPb(JobReference jobRef) { return newBuilder() .setProject(jobRef.getProjectId()) .setJob(jobRef.getJobId()) .setLocation(jobRef.getLocation()) .build(); } }