Domanda

C'è un modo per caricare un file JSON dal file system locale a BigQuery utilizzando Google BigQuery Client API?

Tutte le opzioni che ho trovato sono:

1- Streaming dei record uno per uno.

2- Caricamento dei dati JSON da GCS.

3- Utilizzo delle richieste di post crudo per caricare JSON (I.e. Non tramite API di Google Client).

È stato utile?

Soluzione

Suppongo dal tag Python che vuoi farlo da Python.C'è un esempio di carico qui Carica i dati da un file locale (utilizza CSV, ma è facile adattarlo a JSON ... c'è un altro esempio JSON nella stessa directory).

Il flusso di base è:

# Load configuration with the destination specified.
load_config = {
  'destinationTable': {
    'projectId': PROJECT_ID,
    'datasetId': DATASET_ID,
    'tableId': TABLE_ID
  }
}

load_config['schema'] = {
  'fields': [
    {'name':'string_f', 'type':'STRING'},
    {'name':'boolean_f', 'type':'BOOLEAN'},
    {'name':'integer_f', 'type':'INTEGER'},
    {'name':'float_f', 'type':'FLOAT'},
    {'name':'timestamp_f', 'type':'TIMESTAMP'}
  ]
}
load_config['sourceFormat'] = 'NEWLINE_DELIMITED_JSON'

# This tells it to perform a resumable upload of a local file
# called 'foo.json' 
upload = MediaFileUpload('foo.json',
                         mimetype='application/octet-stream',
                         # This enables resumable uploads.
                         resumable=True)

start = time.time()
job_id = 'job_%d' % start
# Create the job.
result = jobs.insert(
  projectId=project_id,
  body={
    'jobReference': {
      'jobId': job_id
    },
    'configuration': {
      'load': load
    }
  },
  media_body=upload).execute()

 # Then you'd also want to wait for the result and check the status. (check out
 # the example at the link for more info).
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top