使用java bigquery API时是否可以使用标准SQL查询? 我正在尝试执行查询但它会抛出
com.google.api.client.googleapis.json.GoogleJsonResponseException:400错误请求 "消息" :" 11.3 - 11.56:无法识别的类型FLOAT64"
答案 0 :(得分:4)
有两种方法可以将标准SQL与BigQuery Java API一起使用。第一种是使用 int portNumber = 8001;
try {
ServerSocket serverSocket = new ServerSocket(portNumber);
Socket clientSocket = serverSocket.accept();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) { // **** this basically means that read one full line, you have to specify condition like this, so suppose you have created a telnet session with this server now keep on typing and as soon you will hit entered then while block processing will start.
System.out.println("@@@ " + inputLine);
out.println(inputLine); // *** here same input is written back to the client, so suppose you have telnet session then same input can be seen
}
} catch (IOException e) {
System.out.println(
"Exception caught when trying to listen on port " + portNumber + " or listening for a connection");
System.out.println(e.getMessage());
}
开始查询文本,例如:
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import com.learn.Person;
/**
* @author himanshu.agrawal
*
*/
public class TestWebServer2 {
public static void main(String[] args) throws IOException {
startWebServer();
}
/**
* test "backlog" in ServerSocket constructor
test -- If <i>bindAddr</i> is null, it will default accepting
* connections on any/all local addresses.
* @throws IOException
*/
private static void startWebServer() throws IOException {
InetAddress address = InetAddress.getByName("localhost");
ServerSocket serverSocket = new ServerSocket(8001, 1, address);
// if set it to 1000 (1 sec.) then after 1 second porgram will exit with SocketTimeoutException because server socket will only listen for 1 second.
// 0 means infinite
serverSocket.setSoTimeout(/*1*/0000);
while(true){
/*Socket clientSocket = serverSocket.accept();*/ // a "blocking" call which waits until a connection is requested
System.out.println("1");
TestWebServer2.SocketThread socketThread = new TestWebServer2().new SocketThread();
try {
socketThread.setClientSocket(serverSocket.accept());
Thread thread = new Thread(socketThread);
thread.start();
System.out.println("2");
} catch (SocketTimeoutException socketTimeoutException) {
System.err.println(socketTimeoutException);
}
}
}
public class SocketThread implements Runnable{
Socket clientSocket;
public void setClientSocket(Socket clientSocket) throws SocketException {
this.clientSocket = clientSocket;
//this.clientSocket.setSoTimeout(2000); // this will set timeout for reading from client socket.
}
public void run(){
System.out.println("####### New client session started." + clientSocket.hashCode() + " | clientSocket.getLocalPort(): " + clientSocket.getLocalPort()
+ " | clientSocket.getPort(): " + clientSocket.getPort());
try {
listenToSocket(); // create this method and you implement what you want to do with the connection.
} catch (IOException e) {
System.err.println("#### EXCEPTION.");
e.printStackTrace();
}
}
}
}
第二种是将#standardSQL
设置为#standardSQL
SELECT ...
FROM YourTable;
作为useLegacySql
对象的一部分。例如(拍摄from the documentation):
false