如何将新列动态添加到 bigquery 中已存在的表..?
Posted
技术标签:
【中文标题】如何将新列动态添加到 bigquery 中已存在的表..?【英文标题】:How to Add new column dynamically to already existed table in bigquery..? 【发布时间】:2014-06-13 06:09:52 【问题描述】:我创建了一个连续三列的 csv 文件..在 google bigquery 中创建了一个带有 csv 文件的表的数据集 ....为此我完成了我的 java 代码...但现在我必须添加一个在java代码中动态存在行的新列..?
// Main method for data print.
@SuppressWarnings( "rawtypes", "unchecked" )
public static void main(String[] args) throws IOException, InterruptedException
// Create a new BigQuery client authorized via OAuth 2.0 protocol
Bigquery bigquery = createAuthorizedClient();
TableRow row = new TableRow();
row.set("Column1", "Sample23");
row.set("Column2", 7);
row.set("Column3", "Sample25");
TableDataInsertAllRequest.Rows rows = new TableDataInsertAllRequest.Rows();
rows.setJson(row);
List rowList = new ArrayList();
rowList.add(rows);
TableDataInsertAllRequest content =
new TableDataInsertAllRequest().setRows(rowList);
TableDataInsertAllResponse response = bigquery.tabledata().insertAll(PROJECT_ID, DATASET_ID, TABLE_ID, content).execute();
System.out.println("Final response = " + response);
【问题讨论】:
【参考方案1】:有两个表操作Update和Patch。
您需要使用 Update 命令将新列添加到您的架构中。
重要的旁注:
-
顺序很重要。如果您更改顺序,它将看起来像一个不兼容的模式。
您只能在表格末尾添加新字段。在旧列上,您可以选择将 required 更改为 nullable。
您不能将必填字段添加到现有架构中。
您无法删除旧字段,一旦指定了表的架构,您将无法更改它,除非先删除与其关联的所有数据。如果要更改表的架构,则必须指定 WRITE_TRUNCATE 的 writeDisposition。有关详细信息,请参阅作业资源。
Here is an example 将字段添加到架构的 curl 会话。适应Java应该比较容易。它使用来自here的auth.py
使用 Table.Update() 时,您必须再次包含完整的表架构。如果您不提供完全匹配的架构,您可以获得:Provided Schema does not match Table
。例如,我没有注意细节,在我的一个更新调用中,我没有包含像 created
这样的旧字段,但它失败了。
【讨论】:
实际上我没有在我的 java 代码中使用任何作业..只是我创建了一个数据集,其中一个表在三列中有一行...现在我必须在 java 代码中添加新列而不是在 csv文件..我正在发布我的完整源代码.. @duozmo 添加了链接【参考方案2】:实际上我没有在我的 java 代码中使用任何作业。我只是创建了一个数据集,其中包含一个表,其中一行包含三列。现在我必须在 java 代码中添加新列,而不是在 csv 文件中。我正在发布我的完整源代码:
public class BigQueryJavaGettingStarted
// Define required variables.
private static final String PROJECT_ID = "nvjsnsb";
private static final String DATASET_ID = "nvjjvv";
private static final String TABLE_ID = "sampledata";
private static final String CLIENTSECRETS_LOCATION = "client_secrets.json";
static GoogleClientSecrets clientSecrets = loadClientSecrets(CLIENTSECRETS_LOCATION);
// Static variables for API scope, callback URI, and HTTP/JSON functions
private static final List<String> SCOPES = Arrays.asList(BigqueryScopes.BIGQUERY);
private static final String REDIRECT_URI = "https://www.example.com/oauth2callback";
// Global instances of HTTP transport and JSON factory objects.
private static final HttpTransport TRANSPORT = new NetHttpTransport();
private static final JsonFactory JSON_FACTORY = new JacksonFactory();
private static GoogleAuthorizationCodeFlow flow = null;
// Main method for data print.
@SuppressWarnings( "rawtypes", "unchecked" )
public static void main(String[] args) throws IOException, InterruptedException
// Create a new BigQuery client authorized via OAuth 2.0 protocol
Bigquery bigquery = createAuthorizedClient();
TableRow row = new TableRow();
row.set("Column1", "OneMoreCol1");
row.set("Column2", 79);
row.set("Column3", "OneMoreCol2");
TableDataInsertAllRequest.Rows rows = new TableDataInsertAllRequest.Rows();
rows.setJson(row);
List rowList = new ArrayList();
rowList.add(rows);
TableDataInsertAllRequest content = new TableDataInsertAllRequest().setRows(rowList);
TableDataInsertAllResponse response = bigquery.tabledata().insertAll(PROJECT_ID, DATASET_ID, TABLE_ID, content).execute();
System.out.println("Final response = " + response);
// Create Authorized client.
public static Bigquery createAuthorizedClient() throws IOException
String authorizeUrl = new GoogleAuthorizationCodeRequestUrl(
clientSecrets,
REDIRECT_URI,
SCOPES).setState("").build();
System.out.println("Paste this URL into a web browser to authorize BigQuery Access:\n" + authorizeUrl);
System.out.println("... and type the code you received here: ");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String authorizationCode = in.readLine();
// Exchange the auth code for an access token and refresh token
Credential credential = exchangeCode(authorizationCode);
return new Bigquery(TRANSPORT, JSON_FACTORY, credential);
// Exchange code method.
static Credential exchangeCode(String authorizationCode) throws IOException
GoogleAuthorizationCodeFlow flow = getFlow();
GoogleTokenResponse response =
flow.newTokenRequest(authorizationCode).setRedirectUri(REDIRECT_URI).execute();
return flow.createAndStoreCredential(response, null);
// Get flow.
static GoogleAuthorizationCodeFlow getFlow()
if (flow == null)
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport,
jsonFactory,
clientSecrets,
SCOPES)
.setAccessType("offline").setApprovalPrompt("force").build();
return flow;
// Load client secrets.
private static GoogleClientSecrets loadClientSecrets(String clientSecretsLocation)
try
clientSecrets = GoogleClientSecrets.load(new JacksonFactory(),
new InputStreamReader(BigQueryJavaGettingStarted.class.getResourceAsStream(clientSecretsLocation)));
catch (Exception e)
System.out.println("Could not load client_secrets.json");
e.printStackTrace();
return clientSecrets;
【讨论】:
请不要发布答案,您可以编辑您的原始问题,并确保您有所作为。在您的代码中,我没有看到更新调用的任何参考。另外请理解我们不会为您编写代码,我们会为您提供解决方案。以上是关于如何将新列动态添加到 bigquery 中已存在的表..?的主要内容,如果未能解决你的问题,请参考以下文章
如何将新列添加到现有表 symfony - orocommerce