Skip to content

Commit

Permalink
chore: refactor demos
Browse files Browse the repository at this point in the history
  • Loading branch information
fengjiachun committed Jan 8, 2024
1 parent baede06 commit 2063fe6
Show file tree
Hide file tree
Showing 12 changed files with 226 additions and 269 deletions.
7 changes: 7 additions & 0 deletions ingester-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,12 @@
<artifactId>log4j-slf4j-impl</artifactId>
<scope>compile</scope>
</dependency>

<!-- MySQL usage dependency -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,29 @@
import io.greptime.models.Column;
import io.greptime.models.DataType;
import io.greptime.models.Metric;
import java.util.Date;

/**
* @author jiachun.fjc
*/
@Metric(name = "my_metric2")
public class MyMetric2 {
@Column(name = "tag1", tag = true, dataType = DataType.String)
private String tag1;
@Column(name = "tag2", tag = true, dataType = DataType.String)
private String tag2;
@Metric(name = "cpu_metric")
public class Cpu {
@Column(name = "host", tag = true, dataType = DataType.String)
private String host;

@Column(name = "ts", timestamp = true, dataType = DataType.TimestampSecond)
@Column(name = "ts", timestamp = true, dataType = DataType.TimestampMillisecond)
private long ts;

@Column(name = "field1", dataType = DataType.Date)
private Date field1;
@Column(name = "field2", dataType = DataType.Float64)
private double field2;
@Column(name = "cpu_user", dataType = DataType.Float64)
private double cpuUser;
@Column(name = "cpu_sys", dataType = DataType.Float64)
private double cpuSys;

public String getTag1() {
return tag1;
public String getHost() {
return host;
}

public void setTag1(String tag1) {
this.tag1 = tag1;
}

public String getTag2() {
return tag2;
}

public void setTag2(String tag2) {
this.tag2 = tag2;
public void setHost(String host) {
this.host = host;
}

public long getTs() {
Expand All @@ -62,19 +51,19 @@ public void setTs(long ts) {
this.ts = ts;
}

public Date getField1() {
return field1;
public double getCpuUser() {
return cpuUser;
}

public void setField1(Date field1) {
this.field1 = field1;
public void setCpuUser(double cpuUser) {
this.cpuUser = cpuUser;
}

public double getField2() {
return field2;
public double getCpuSys() {
return cpuSys;
}

public void setField2(double field2) {
this.field2 = field2;
public void setCpuSys(double cpuSys) {
this.cpuSys = cpuSys;
}
}
59 changes: 59 additions & 0 deletions ingester-example/src/main/java/io/greptime/Memory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2023 Greptime Team
*
* 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 io.greptime;

import io.greptime.models.Column;
import io.greptime.models.DataType;
import io.greptime.models.Metric;

/**
* @author jiachun.fjc
*/
@Metric(name = "mem_metric")
public class Memory {
@Column(name = "host", tag = true, dataType = DataType.String)
private String host;

@Column(name = "ts", timestamp = true, dataType = DataType.TimestampMillisecond)
private long ts;

@Column(name = "mem_usage", dataType = DataType.Float64)
private double memUsage;

public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
}

public long getTs() {
return ts;
}

public void setTs(long ts) {
this.ts = ts;
}

public double getMemUsage() {
return memUsage;
}

public void setMemUsage(double memUsage) {
this.memUsage = memUsage;
}
}
110 changes: 0 additions & 110 deletions ingester-example/src/main/java/io/greptime/MyMetric1.java

This file was deleted.

56 changes: 56 additions & 0 deletions ingester-example/src/main/java/io/greptime/QueryJDBC.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2023 Greptime Team
*
* 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 io.greptime;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Objects;
import java.util.Properties;

/**
* @author jiachun.fjc
*/
public class QueryJDBC {

public static void main(String[] args) throws Exception {
GreptimeDB greptimeDB = TestConnector.connectToDefaultDB();

// Inserts data first


try (Connection conn = makeConnection()) {

}
}

public static Connection makeConnection() throws IOException, ClassNotFoundException, SQLException {
Properties prop = new Properties();
prop.load(QueryJDBC.class.getResourceAsStream("/db-connection.properties"));

String dbName = (String) prop.get("db.database-driver");

String dbConnUrl = (String) prop.get("db.url");
String dbUserName = (String) prop.get("db.username");
String dbPassword = (String) prop.get("db.password");

Class.forName(dbName);
Connection dbConn = DriverManager.getConnection(dbConnUrl, dbUserName, dbPassword);

return Objects.requireNonNull(dbConn, "Failed to make connection!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
import io.greptime.models.WriteOk;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
Expand All @@ -35,41 +33,33 @@ public class StreamWritePOJOsQuickStart {
public static void main(String[] args) throws ExecutionException, InterruptedException {
GreptimeDB greptimeDB = TestConnector.connectToDefaultDB();

List<MyMetric1> myMetric1s = new ArrayList<>();
List<Cpu> cpus = new ArrayList<>();
for (int i = 0; i < 10; i++) {
MyMetric1 m = new MyMetric1();
m.setTag1("tag_value_1_" + i);
m.setTag2("tag_value_2_" + i);
m.setTag3("tag_value_3_" + i);
m.setTs(System.currentTimeMillis());
m.setField1("field_value_1_" + i);
m.setField2(i);
m.setField3(new BigDecimal(i));
m.setField4(i);

myMetric1s.add(m);
Cpu c = new Cpu();
c.setHost("127.0.0." + i);
c.setTs(System.currentTimeMillis());
c.setCpuUser(i + 0.1);
c.setCpuSys(i + 0.12);
cpus.add(c);
}

List<MyMetric2> myMetric2s = new ArrayList<>();
List<Memory> memories = new ArrayList<>();
for (int i = 0; i < 10; i++) {
MyMetric2 m = new MyMetric2();
m.setTag1("tag_value_1_" + i);
m.setTag2("tag_value_2_" + i);
Memory m = new Memory();
m.setHost("127.0.0." + i);
m.setTs(System.currentTimeMillis() / 1000);
m.setField1(Calendar.getInstance().getTime());
m.setField2(i);

myMetric2s.add(m);
m.setMemUsage(i + 0.2);
memories.add(m);
}

StreamWriter<List<?>, WriteOk> writer = greptimeDB.streamWriterPOJOs();

// write data into stream
writer.write(myMetric1s);
writer.write(myMetric2s);
writer.write(cpus);
writer.write(memories);

// delete the first 5 rows
writer.write(myMetric1s.subList(0, 5), WriteOp.Delete);
writer.write(cpus.subList(0, 5), WriteOp.Delete);

// complete the stream
CompletableFuture<WriteOk> future = writer.completed();
Expand Down
Loading

0 comments on commit 2063fe6

Please sign in to comment.