[ZEPPELIN-1512] Support Kylin project name in interpreter runtime

This commit is contained in:
Yiming Liu 2016-11-16 16:17:48 +08:00
parent dfc530a25d
commit b58ee7fcf7
4 changed files with 57 additions and 21 deletions

View file

@ -29,7 +29,6 @@ limitations under the License.
To get start with Apache Kylin, please see [Apache Kylin Quickstart](https://kylin.apache.org/docs15/index.html).
## Configuration
<table class="table-configuration">
<table class="table-configuration">
<tr>
<th>Name</th>
@ -54,7 +53,7 @@ To get start with Apache Kylin, please see [Apache Kylin Quickstart](https://kyl
<tr>
<td>kylin.query.project</td>
<td>learn_kylin</td>
<td>String, Project to perform query.</td>
<td>String, Project to perform query. Could update at notebook level</td>
</tr>
<tr>
<td>kylin.query.ispartial</td>
@ -72,15 +71,12 @@ To get start with Apache Kylin, please see [Apache Kylin Quickstart](https://kyl
<td>int, Query offset <br/> If offset is set in sql, curIndex will be ignored.</td>
</tr>
</table>
</table>
## Using the Apache Kylin Interpreter
In a paragraph, use `%kylin` to select the **kylin** interpreter and then input sql.
In a paragraph, use `%kylin(project_name)` to select the **kylin** interpreter, **project name** and then input **sql**. If no project name defined, will use the default project name from the above configuration.
```
%kylin
%kylin(learn_project)
select count(*) from kylin_sales group by part_dt
```

View file

@ -15,7 +15,6 @@
* limitations under the License.
*/
package org.apache.zeppelin.kylin;
import org.apache.commons.codec.binary.Base64;
@ -41,7 +40,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Kylin interpreter for Zeppelin. (http://kylin.io)
* Kylin interpreter for Zeppelin. (http://kylin.apache.org)
*/
public class KylinInterpreter extends Interpreter {
Logger logger = LoggerFactory.getLogger(KylinInterpreter.class);
@ -59,6 +58,7 @@ public class KylinInterpreter extends Interpreter {
public KylinInterpreter(Properties property) {
super(property);
}
@Override
public void open() {
@ -100,8 +100,9 @@ public class KylinInterpreter extends Interpreter {
}
public HttpResponse prepareRequest(String sql) throws IOException {
String KYLIN_PROJECT = getProperty(KYLIN_QUERY_PROJECT);
logger.info("project:" + KYLIN_PROJECT);
String kylinProject = getProject(KYLIN_QUERY_PROJECT);
logger.info("project:" + kylinProject);
logger.info("sql:" + sql);
logger.info("acceptPartial:" + getProperty(KYLIN_QUERY_ACCEPT_PARTIAL));
logger.info("limit:" + getProperty(KYLIN_QUERY_LIMIT));
@ -109,7 +110,7 @@ public class KylinInterpreter extends Interpreter {
byte[] encodeBytes = Base64.encodeBase64(new String(getProperty(KYLIN_USERNAME)
+ ":" + getProperty(KYLIN_PASSWORD)).getBytes("UTF-8"));
String postContent = new String("{\"project\":" + "\"" + KYLIN_PROJECT + "\""
String postContent = new String("{\"project\":" + "\"" + kylinProject + "\""
+ "," + "\"sql\":" + "\"" + sql + "\""
+ "," + "\"acceptPartial\":" + "\"" + getProperty(KYLIN_QUERY_ACCEPT_PARTIAL) + "\""
+ "," + "\"offset\":" + "\"" + getProperty(KYLIN_QUERY_OFFSET) + "\""
@ -130,6 +131,22 @@ public class KylinInterpreter extends Interpreter {
return httpClient.execute(postRequest);
}
public String getProject(String cmd) {
boolean firstLineIndex = cmd.startsWith("(");
if (firstLineIndex) {
int configStartIndex = cmd.indexOf("(");
int configLastIndex = cmd.indexOf(")");
if (configStartIndex != -1 && configLastIndex != -1) {
return cmd.substring(configStartIndex + 1, configLastIndex);
} else {
return getProperty(KYLIN_QUERY_PROJECT);
}
} else {
return getProperty(KYLIN_QUERY_PROJECT);
}
}
private InterpreterResult executeQuery(String sql) throws IOException {
HttpResponse response = prepareRequest(sql);

View file

@ -14,37 +14,37 @@
"envName": null,
"propertyName": "kylin.api.user",
"defaultValue": "ADMIN",
"description": "username for kylin user"
"description": "Kylin username"
},
"kylin.api.password": {
"envName": null,
"propertyName": "kylin.api.password",
"defaultValue": "KYLIN",
"description": "password for kylin user"
"description": "Kylin password"
},
"kylin.query.project": {
"envName": null,
"propertyName": "kylin.query.project",
"defaultValue": "default",
"description": "kylin project name"
"defaultValue": "learn_kylin",
"description": "Default Kylin project name"
},
"kylin.query.offset": {
"envName": null,
"propertyName": "kylin.query.offset",
"defaultValue": "0",
"description": "kylin query offset"
"description": "Kylin query offset"
},
"kylin.query.limit": {
"envName": null,
"propertyName": "kylin.query.limit",
"defaultValue": "5000",
"description": "kylin query limit"
"description": "Kylin query limit"
},
"kylin.query.ispartial": {
"envName": null,
"propertyName": "kylin.query.ispartial",
"defaultValue": "true",
"description": "The kylin query partial flag"
"description": "Kylin query partial flag, deprecated"
}
},
"editor": {

View file

@ -47,13 +47,36 @@ public class KylinInterpreterTest {
}
@Test
public void test(){
KylinInterpreter t = new MockKylinInterpreter(kylinProperties);
public void testWithDefault(){
KylinInterpreter t = new MockKylinInterpreter(getDefaultProperties());
InterpreterResult result = t.interpret(
"select a.date,sum(b.measure) as measure from kylin_fact_table a " +
"inner join kylin_lookup_table b on a.date=b.date group by a.date", null);
assertEquals("default", t.getProject("select a.date,sum(b.measure) as measure " +
"from kylin_fact_table a inner join kylin_lookup_table b on a.date=b.date group by a.date"));
assertEquals(InterpreterResult.Type.TABLE,result.message().get(0).getType());
}
@Test
public void testWithProject(){
KylinInterpreter t = new MockKylinInterpreter(getDefaultProperties());
assertEquals("project2", t.getProject("(project2)\n select a.date,sum(b.measure) as measure " +
"from kylin_fact_table a inner join kylin_lookup_table b on a.date=b.date group by a.date"));
assertEquals("", t.getProject("()\n select a.date,sum(b.measure) as measure " +
"from kylin_fact_table a inner join kylin_lookup_table b on a.date=b.date group by a.date"));
}
private Properties getDefaultProperties(){
Properties prop = new Properties();
prop.put("kylin.api.username", "ADMIN");
prop.put("kylin.api.password", "KYLIN");
prop.put("kylin.api.url", "http://<host>:<port>/kylin/api/query");
prop.put("kylin.query.project", "default");
prop.put("kylin.query.offset", "0");
prop.put("kylin.query.limit", "5000");
prop.put("kylin.query.ispartial", "true");
return prop;
}
}
class MockKylinInterpreter extends KylinInterpreter {