Commits

Anonymous committed ac839bf

move bigquery-json-clientlogin-sample to here from other repository

Comments (0)

Files changed (15)

+syntax: regexp
+^(.*/)?target$

bigquery-json-clientlogin-sample/.classpath

+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" path="src"/>
+  <classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
+  <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+  <classpathentry kind="output" path="target/classes"/>
+</classpath>

bigquery-json-clientlogin-sample/.project

+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>bigquery-json-clientlogin-sample</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.maven.ide.eclipse.maven2Builder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+		<nature>org.maven.ide.eclipse.maven2Nature</nature>
+	</natures>
+</projectDescription>

bigquery-json-clientlogin-sample/.settings/org.eclipse.jdt.core.prefs

+#Tue Jul 13 15:56:54 EDT 2010
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.6
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.6

bigquery-json-clientlogin-sample/instructions.html

+<html>
+<title>BigQuery JSON ClientLogin Sample</title>
+<body>
+<h2>Instructions for the BigQuery JSON ClientLogin Sample</h2>
+
+<h3>Browse Online</h3>
+
+<ul>
+  <li><a
+    href="http://code.google.com/p/google-api-java-client/source/browse?repo=samples#hg/bigquery-json-clientlogin-sample">Browse
+  Source</a>, or main file <a
+    href="http://code.google.com/p/google-api-java-client/source/browse/bigquery-json-clientlogin-sample/src/com/google/api/client/sample/bigquery/BigQuerySample.java?repo=samples">BigQuerySample.java</a></li>
+</ul>
+
+<h3>Checkout Instructions</h3>
+
+<p><b>Prerequisites:</b> install <a href="http://java.com">Java 6</a>, <a
+  href="http://mercurial.selenic.com/">Mercurial</a> and <a
+  href="http://maven.apache.org/download.html">Maven</a>. You may need to set
+your <code>JAVA_HOME</code>.</p>
+
+<p><b>Important:</b> after checking out the project, and before compiling
+and running it, you need to edit <code>src/com/google/api/client/sample/bigquery/ClientLoginCredentials.java</code>
+to enter a username and password that has access to BigQuery. Otherwise, you
+will get a <code>403 Forbidden</code> exception.</p>
+
+<pre><code>cd <i>[someDirectory]</i>
+hg clone https://samples.google-api-java-client.googlecode.com/hg/ google-api-java-client-samples
+cd google-api-java-client-samples/bigquery-json-clientlogin-sample
+<i>[editor]</i> src/com/google/api/client/sample/bigquery/ClientLoginCredentials.java
+mvn compile
+mvn -q exec:java</code></pre>
+
+<h3>Setup Project in Eclipse 3.5/3.6</h3>
+
+<p><b>Prerequisites:</b> install <a href="http://www.eclipse.org/downloads/">Eclipse</a>,
+the <a href="http://javaforge.com/project/HGE">Mercurial plugin</a>, and the <a
+  href="http://m2eclipse.sonatype.org/installing-m2eclipse.html">Maven
+plugin</a>. Window &gt; Preferences...; select Maven; check on "Download Artifact
+Sources" and "Download Artifact JavaDoc".</p>
+
+<ul>
+  <li>Import <code>bigquery-json-clientlogin-sample</code> project
+  <ul>
+    <li>File &gt; Import...</li>
+    <li>Select "General &gt; Existing Project into Workspace" and click
+    "Next"</li>
+    <li>Click "Browse" next to "Select root directory", find <code><i>someDirectory</i>/bigquery-json-clientlogin-sample</code>
+    and click "Next"</li>
+    <li>Click "Finish"</li>
+  </ul>
+  </li>
+  <li>Run
+  <ul>
+    <li>Right-click on project bigquery-json-clientlogin-sample</li>
+    <li>Run As &gt; Java Application</li>
+  </ul>
+  </li>
+</ul>
+
+</body>
+</html>

bigquery-json-clientlogin-sample/pom.xml

+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <groupId>com.google</groupId>
+    <artifactId>google</artifactId>
+    <version>5</version>
+  </parent>
+  <groupId>com.google.api.client</groupId>
+  <artifactId>bigquery-sample</artifactId>
+  <version>1.0.2</version>
+  <name>bigquery-json-clientlogin-sample</name>
+  <description>
+    Example for BigQuery.
+  </description>
+
+  <url>http://code.google.com/p/google-api-java-client/</url>
+
+  <issueManagement>
+    <system>code.google.com</system>
+    <url>http://code.google.com/p/google-api-java-client/issues/list</url>
+  </issueManagement>
+
+  <inceptionYear>2010</inceptionYear>
+
+  <prerequisites>
+    <maven>2.0.9</maven>
+  </prerequisites>
+
+  <scm>
+    <connection>scm:hg:https://bigquerycljsonsample.google-api-java-client.googlecode.com/hg/</connection>
+    <developerConnection>scm:hg:https://bigquerycljsonsample.google-api-java-client.googlecode.com/hg/</developerConnection>
+    <url>http://code.google.com/p/google-api-java-client/source/browse/?repo=bigquerycljsonsample</url>
+  </scm>
+
+  <developers>
+    <developer>
+      <id>yanivi</id>
+      <name>Yaniv Inbar</name>
+      <email>yanivi@google.com</email>
+      <organization>Google</organization>
+      <organizationUrl>http://www.google.com</organizationUrl>
+      <roles>
+        <role>owner</role>
+        <role>developer</role>
+      </roles>
+      <timezone>-5</timezone>
+    </developer>
+  </developers>
+  <build>
+    <plugins>
+      <plugin>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <configuration>
+          <source>1.6</source>
+          <target>1.6</target>
+        </configuration>
+      </plugin>
+      <plugin>
+        <groupId>org.codehaus.mojo</groupId>
+        <artifactId>exec-maven-plugin</artifactId>
+        <version>1.1</version>
+        <executions>
+          <execution>
+            <goals>
+              <goal>java</goal>
+            </goals>
+          </execution>
+        </executions>
+        <configuration>
+          <mainClass>com.google.api.client.sample.bigquery.BigQuerySample</mainClass>
+        </configuration>
+      </plugin>
+    </plugins>
+    <finalName>${project.artifactId}-${project.version}</finalName>
+    <sourceDirectory>src</sourceDirectory>
+    <testSourceDirectory>tests_disabled</testSourceDirectory>
+  </build>
+  <dependencies>
+    <dependency>
+      <groupId>com.google.api.client</groupId>
+      <artifactId>google-api-client</artifactId>
+      <version>1.0.9-alpha</version>
+    </dependency>
+    <dependency>
+      <groupId>org.codehaus.jackson</groupId>
+      <artifactId>jackson-core-asl</artifactId>
+      <version>1.5.3</version>
+    </dependency>
+  </dependencies>
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+</project>

bigquery-json-clientlogin-sample/src/com/google/api/client/sample/bigquery/BigQuerySample.java

+/*
+ * Copyright (c) 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.google.api.client.sample.bigquery;
+
+import com.google.api.client.googleapis.GoogleTransport;
+import com.google.api.client.googleapis.auth.clientlogin.ClientLogin;
+import com.google.api.client.googleapis.json.JsonCParser;
+import com.google.api.client.http.HttpRequest;
+import com.google.api.client.http.HttpResponseException;
+import com.google.api.client.http.HttpTransport;
+import com.google.api.client.sample.bigquery.model.BigQueryUrl;
+import com.google.api.client.sample.bigquery.model.Debug;
+import com.google.api.client.sample.bigquery.model.QueryData;
+import com.google.api.client.sample.bigquery.model.QueryRow;
+import com.google.api.client.sample.bigquery.model.QueryValue;
+import com.google.api.client.sample.bigquery.model.SchemaData;
+import com.google.api.client.sample.bigquery.model.SchemaField;
+
+import java.io.IOException;
+
+/**
+ * @author Yaniv Inbar
+ */
+public class BigQuerySample {
+
+  public static void main(String[] args) {
+    Debug.enableLogging();
+    HttpTransport transport = GoogleTransport.create();
+    transport.addParser(new JsonCParser());
+    try {
+      try {
+        authenticateWithClientLogin(transport);
+        executeSchema(transport, "bigquery/samples/shakespeare");
+        executeQuery(
+            transport, "select count(*) from [bigquery/samples/shakespeare];");
+        executeQuery(
+            transport,
+            "select corpus, word, word_count from [bigquery/samples/shakespeare] where word_count > 600 order by word_count desc;");
+      } catch (HttpResponseException e) {
+        System.err.println(e.response.parseAsString());
+        throw e;
+      }
+    } catch (Throwable t) {
+      t.printStackTrace();
+      System.exit(1);
+    }
+  }
+
+  public static void authenticateWithClientLogin(HttpTransport transport)
+      throws IOException {
+    ClientLogin authenticator = new ClientLogin();
+    authenticator.authTokenType = "ndev";
+    authenticator.username = ClientLoginCredentials.ENTER_USERNAME;
+    authenticator.password = ClientLoginCredentials.ENTER_PASSWORD;
+    authenticator.authenticate().setAuthorizationHeader(transport);
+  }
+
+  static QueryData executeQuery(HttpTransport transport, String query)
+      throws IOException {
+    header("Query: " + query);
+    HttpRequest request = transport.buildGetRequest();
+    BigQueryUrl url = BigQueryUrl.fromRelativePath("query");
+    url.q = query;
+    request.url = url;
+    QueryData result = request.execute().parseAs(QueryData.class);
+    if (result.fields == null) {
+      System.out.println("No fields");
+    } else {
+      for (SchemaField field : result.fields) {
+        System.out.print(field.id + "\t");
+      }
+      System.out.println();
+      for (QueryRow row : result.rows) {
+        for (QueryValue value : row.f) {
+          System.out.print(value.value + "\t");
+        }
+        System.out.println();
+      }
+    }
+    return result;
+  }
+
+  static SchemaData executeSchema(HttpTransport transport, String tableName)
+      throws IOException {
+    header("Schema: " + tableName);
+    HttpRequest request = transport.buildGetRequest();
+    BigQueryUrl url = BigQueryUrl.fromRelativePath("tables");
+    url.pathParts.add(tableName);
+    request.url = url;
+    SchemaData result = request.execute().parseAs(SchemaData.class);
+    if (result.fields == null) {
+      System.out.println("No fields");
+    } else {
+      System.out.println(result.fields.size() + " fields:");
+      for (SchemaField field : result.fields) {
+        System.out.println(field.type + " " + field.id);
+      }
+    }
+    return result;
+  }
+
+  private static void header(String name) {
+    System.out.println();
+    System.out.println("============== " + name + " ==============");
+    System.out.println();
+  }
+}

bigquery-json-clientlogin-sample/src/com/google/api/client/sample/bigquery/ClientLoginCredentials.java

+/*
+ * Copyright (c) 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.google.api.client.sample.bigquery;
+
+/**
+ * @author Yaniv Inbar
+ */
+public class ClientLoginCredentials {
+
+  static final String ENTER_USERNAME = "enter_username";
+  static final String ENTER_PASSWORD = "enter_password";
+}

bigquery-json-clientlogin-sample/src/com/google/api/client/sample/bigquery/model/BigQueryUrl.java

+/*
+ * Copyright (c) 2010 Google Inc.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.google.api.client.sample.bigquery.model;
+
+import com.google.api.client.googleapis.GoogleUrl;
+import com.google.api.client.util.Key;
+
+/**
+ * BigQuery URL builder.
+ * 
+ * @author Yaniv Inbar
+ */
+public final class BigQueryUrl extends GoogleUrl {
+
+  private static final String ROOT_URL =
+    "https://www.googleapis.com/bigquery/v1/";
+
+  @Key
+  public String q;
+
+  /** Constructs a new BigQuery URL from the given encoded URL. */
+  public BigQueryUrl(String encodedUrl) {
+    super(encodedUrl);
+    if (Debug.ENABLED) {
+      prettyprint = true;
+    }
+  }
+
+  /**
+   * Constructs a new BigQuery URL based on the given relative path.
+   * 
+   * @param relativePath encoded path relative to the {@link #ROOT_URL}
+   * @return new BigQuery URL
+   */
+  public static BigQueryUrl fromRelativePath(String relativePath) {
+    return new BigQueryUrl(ROOT_URL + relativePath);
+  }
+}

bigquery-json-clientlogin-sample/src/com/google/api/client/sample/bigquery/model/Debug.java

+/*
+ * Copyright (c) 2010 Google Inc.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.google.api.client.sample.bigquery.model;
+
+import java.util.logging.Handler;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+import java.util.logging.Logger;
+
+/**
+ * @author Yaniv Inbar
+ */
+public class Debug {
+  public static final boolean ENABLED = false;
+
+  public static void enableLogging() {
+    if (ENABLED) {
+      Logger logger = Logger.getLogger("com.google.api.client");
+      logger.setLevel(Level.CONFIG);
+      logger.addHandler(new Handler() {
+
+        @Override
+        public void close() throws SecurityException {
+        }
+
+        @Override
+        public void flush() {
+        }
+
+        @Override
+        public void publish(LogRecord record) {
+          // default ConsoleHandler will take care of >= INFO
+          if (record.getLevel().intValue() < Level.INFO.intValue()) {
+            System.out.println(record.getMessage());
+          }
+        }
+      });
+    }
+  }
+}

bigquery-json-clientlogin-sample/src/com/google/api/client/sample/bigquery/model/QueryData.java

+/*
+ * Copyright (c) 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.google.api.client.sample.bigquery.model;
+
+import com.google.api.client.util.Key;
+
+import java.util.List;
+
+/**
+ * @author Yaniv Inbar
+ */
+public class QueryData {
+
+  @Key
+  public List<SchemaField> fields;
+  
+  @Key
+  public List<QueryRow> rows;
+}

bigquery-json-clientlogin-sample/src/com/google/api/client/sample/bigquery/model/QueryRow.java

+/*
+ * Copyright (c) 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.google.api.client.sample.bigquery.model;
+
+import com.google.api.client.util.Key;
+
+import java.util.List;
+
+/**
+ * @author Yaniv Inbar
+ */
+public class QueryRow {
+
+  @Key
+  public List<QueryValue> f;
+}

bigquery-json-clientlogin-sample/src/com/google/api/client/sample/bigquery/model/QueryValue.java

+/*
+ * Copyright (c) 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.google.api.client.sample.bigquery.model;
+
+import com.google.api.client.util.Key;
+
+/**
+ * @author Yaniv Inbar
+ */
+public class QueryValue {
+
+  @Key("v")
+  public String value;
+}

bigquery-json-clientlogin-sample/src/com/google/api/client/sample/bigquery/model/SchemaData.java

+/*
+ * Copyright (c) 2010 Google Inc.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.google.api.client.sample.bigquery.model;
+
+import com.google.api.client.util.Key;
+
+import java.util.List;
+
+/**
+ * @author Yaniv Inbar
+ */
+public class SchemaData {
+
+  @Key
+  public List<SchemaField> fields;
+}

bigquery-json-clientlogin-sample/src/com/google/api/client/sample/bigquery/model/SchemaField.java

+/*
+ * Copyright (c) 2010 Google Inc.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.google.api.client.sample.bigquery.model;
+
+import com.google.api.client.util.Key;
+
+/**
+ * @author Yaniv Inbar
+ */
+public class SchemaField {
+
+  @Key
+  public String id;
+  
+  @Key
+  public String type;
+}