Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将新列动态添加到bigquery中已存在的表中。。?_Java_Google App Engine_Google Bigquery - Fatal编程技术网

Java 如何将新列动态添加到bigquery中已存在的表中。。?

Java 如何将新列动态添加到bigquery中已存在的表中。。?,java,google-app-engine,google-bigquery,Java,Google App Engine,Google Bigquery,我创建了一个csv文件,一行有三列..在google bigquery中,我创建了一个数据集,其中一个表有csv文件..为此,我完成了我的java代码…但现在我必须在java代码中向现有行动态添加一个新列 // Main method for data print. @SuppressWarnings({ "rawtypes", "unchecked" }) public static void main(String[] args) throws IOException, Interrupte

我创建了一个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);

}

有两个表操作和

您需要使用Update命令将新列添加到架构中

重要的旁注:

秩序很重要。如果更改顺序,它将看起来像一个不兼容的模式。 只能在表的末尾添加新字段。在旧列上,您可以选择将required更改为nullable。 不能将必填字段添加到现有架构中。 您无法删除旧字段,一旦指定了表的架构,您就无法在不首先删除与之关联的所有数据的情况下对其进行更改。如果要更改表的架构,必须指定WRITE_TRUNCATE的writeDisposition。有关更多信息,请参阅作业资源。 将字段添加到架构的curl会话的。适应Java应该相对容易。它使用来自的auth.py


使用Table.Update时,必须再次包含完整的表架构。若不提供精确匹配的模式,则可以得到:提供的模式与表不匹配。例如,我没有注意细节,在我的一次更新调用中,我没有包含像created这样的旧字段,因此失败了

实际上,我在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;

    }
}

实际上,我在java代码中没有使用任何作业。我只是创建了一个数据集,其中一个表有三列中的一行…现在我必须在java代码中添加新列,而不是在csv文件中。。我正在发布我的完整源代码。@duozmo添加了链接请不要发布答案,您可以编辑您的原始问题,并确保您做了一些事情。在您的代码中,我没有看到更新调用的任何引用。另外,请理解,我们不会为您编写代码,我们将为您提供解决方案。