使用 Java 客户端,例如带参数的 curl
use Java client like curl with param
我使用的是 influxdb 0.9。在这个版本中,我可以像
这样写数据库
curl -XPOST 'http://localhost:8086/write?db=mydb' -d 'cpu,host=server01,region=uswest value=1.0'
现在我把它转换成java
URL url = new URL("http", "localhost", 8086, "/write?db=mydb");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
OutputStream wr = con.getOutputStream();
Stirng s = "cpu,host=server01,region=uswest value=51.0";
wr.write(s.getBytes(UTF_8));
wr.flush();
wr.close();
但它不起作用。 “-d”是表示 post 参数吗?我如何在 Java 中表达?
在这个例子中,curl
标志实际上应该是 --data-binary
,而不是 -d
,它可以有不同的编码。只要您的字符串未被 Java 代码更改,就应该没问题。 URL 编码之类的任何东西都会阻止线路协议插入工作。
您还需要获取 HTTP 响应。以下对我有用:
import java.net.*;
import java.io.*;
import java.util.*;
public class Client {
private static HttpURLConnection client;
public static void main(String[] args) {
try {
Random rn = new Random();
URL input;
while(true) {
input = new URL("http", "localhost", 8086, "/write?db=mydb");
client = (HttpURLConnection) input.openConnection();
client.setRequestMethod("POST");
client.setDoOutput(true);
double thermals = rn.nextDouble();
String s = "cpu_temperature value=" + thermals;
try (OutputStreamWriter writer =
new OutputStreamWriter(client.getOutputStream())) {
writer.write(s);
}
BufferedReader in = new BufferedReader(new InputStreamReader(
client.getInputStream()));
String decodedString;
while ((decodedString = in.readLine()) != null) {
System.out.println(decodedString);
}
in.close();
System.out.println(s); // for debugging
Thread.sleep(1000); // send data every 1 second
}
} catch(MalformedURLException error) {
System.out.println("Malformed URL!");
} catch(SocketTimeoutException error) {
System.out.println("Socket Timeout Exception!");
} catch (IOException error) {
System.out.println("IOException!");
System.out.println(error);
} catch(InterruptedException e) {
System.out.println("InterruptedException!");
} finally {
if(client != null) { // Make sure the connection is not null.
client.disconnect();
}
}
}
}
您的 SQL 查询需要更正。删除 "cpu,host=" 中的“,”即可。
我使用的是 influxdb 0.9。在这个版本中,我可以像
这样写数据库curl -XPOST 'http://localhost:8086/write?db=mydb' -d 'cpu,host=server01,region=uswest value=1.0'
现在我把它转换成java
URL url = new URL("http", "localhost", 8086, "/write?db=mydb");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
OutputStream wr = con.getOutputStream();
Stirng s = "cpu,host=server01,region=uswest value=51.0";
wr.write(s.getBytes(UTF_8));
wr.flush();
wr.close();
但它不起作用。 “-d”是表示 post 参数吗?我如何在 Java 中表达?
在这个例子中,curl
标志实际上应该是 --data-binary
,而不是 -d
,它可以有不同的编码。只要您的字符串未被 Java 代码更改,就应该没问题。 URL 编码之类的任何东西都会阻止线路协议插入工作。
您还需要获取 HTTP 响应。以下对我有用:
import java.net.*;
import java.io.*;
import java.util.*;
public class Client {
private static HttpURLConnection client;
public static void main(String[] args) {
try {
Random rn = new Random();
URL input;
while(true) {
input = new URL("http", "localhost", 8086, "/write?db=mydb");
client = (HttpURLConnection) input.openConnection();
client.setRequestMethod("POST");
client.setDoOutput(true);
double thermals = rn.nextDouble();
String s = "cpu_temperature value=" + thermals;
try (OutputStreamWriter writer =
new OutputStreamWriter(client.getOutputStream())) {
writer.write(s);
}
BufferedReader in = new BufferedReader(new InputStreamReader(
client.getInputStream()));
String decodedString;
while ((decodedString = in.readLine()) != null) {
System.out.println(decodedString);
}
in.close();
System.out.println(s); // for debugging
Thread.sleep(1000); // send data every 1 second
}
} catch(MalformedURLException error) {
System.out.println("Malformed URL!");
} catch(SocketTimeoutException error) {
System.out.println("Socket Timeout Exception!");
} catch (IOException error) {
System.out.println("IOException!");
System.out.println(error);
} catch(InterruptedException e) {
System.out.println("InterruptedException!");
} finally {
if(client != null) { // Make sure the connection is not null.
client.disconnect();
}
}
}
}
您的 SQL 查询需要更正。删除 "cpu,host=" 中的“,”即可。