实际我们进行cube.js 的集成schema 是一个比较重要的东西,官方的playground 是提供了
一个自己的脚手架,但是基于代码集成就需要我们自己搞定了(比如java),解决方法很简单
我们可以基于模板引擎,以下是基于jinja模板引擎的demo(java 版本的工具)
项目准备
- pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>com.dalong</groupId>
<artifactId>myjinja2-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<encoding>UTF-8</encoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
<dependency>
<groupId>com.hubspot.jinjava</groupId>
<artifactId>jinjava</artifactId>
<version>2.5.6</version>
</dependency>
</dependencies>
<build>
<!-- Maven Shade Plugin -->
<finalName>mydemo-app</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<!-- Run shade goal on package phase -->
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<!-- add Main-Class to manifest file -->
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.dalong.Application</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
- 模板准备
完全使用了jinja 的模板语法。很简单
cube("{{cubename}}",{
measures: {
drillMembers: [{{ users | join(',')}}]
},
dimensions: {
{% for item in userconfs %}
{{item.name}}: {
type:`{{ item.age}}`,
sql:`{{ item.name}}`
},
{% endfor %}
}
})
- 依赖的一个实体
UserConf
package com.dalong;
public class UserConf {
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
- 代码集成
package com.dalong;
import com.google.common.base.Charsets;
import com.google.common.collect.Maps;
import com.google.common.io.Resources;
import com.hubspot.jinjava.Jinjava;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* @author dalong
*/
public class Application {
public static void main(String[] args) throws IOException {
demo();
}
private static void demo() throws IOException {
Jinjava jinjava = new Jinjava();
Map<String, Object> context = Maps.newHashMap();
List<UserConf> confs= new ArrayList<>();
UserConf userConf = new UserConf();
userConf.setAge("ddd");
userConf.setName("dddd");
UserConf userConf2 = new UserConf();
userConf2.setAge("ddd222");
userConf2.setName("dddd222");
confs.add(userConf);
confs.add(userConf2);
context.put("userconfs",confs);
context.put("cubename","demoapp");
context.put("users", Arrays.asList("dalong","demoapp"));
String template = Resources.toString(Resources.getResource("index.html"), Charsets.UTF_8);
String renderedTemplate = jinjava.render(template, context);
System.out.println(renderedTemplate);
}
}
运行效果
参考资料
https://cube.dev/docs/cube
https://github.com/HubSpot/jinjava
https://jinja.palletsprojects.com/en/3.0.x/