Skip to content

Commit d85964b

Browse files
committed
Proxy for tabledata
1 parent 2211d30 commit d85964b

File tree

5 files changed

+218
-0
lines changed

5 files changed

+218
-0
lines changed

zeppelin-interpreter/src/main/java/org/apache/zeppelin/resource/Resource.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public Object invokeMethod(
117117
Method method = r.getClass().getMethod(
118118
methodName,
119119
paramTypes);
120+
method.setAccessible(true);
120121
Object ret = method.invoke(r, params);
121122
return ret;
122123
} catch (Exception e) {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.zeppelin.tabledata;
18+
19+
import org.apache.zeppelin.resource.Resource;
20+
21+
import java.util.Iterator;
22+
23+
/**
24+
* Proxy row iterator
25+
*/
26+
public class ProxyRowIterator implements Iterator<Row> {
27+
28+
private final Resource rows;
29+
30+
public ProxyRowIterator(Resource rows) {
31+
this.rows = rows;
32+
}
33+
34+
@Override
35+
public boolean hasNext() {
36+
rows.invokeMethod("hasNext", null, null);
37+
return false;
38+
}
39+
40+
@Override
41+
public Row next() {
42+
return (Row) rows.invokeMethod("next", null, null);
43+
}
44+
45+
@Override
46+
public void remove() {
47+
// operation not supported
48+
}
49+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.zeppelin.tabledata;
18+
19+
import org.apache.zeppelin.resource.Resource;
20+
import org.apache.zeppelin.resource.ResourcePoolUtils;
21+
22+
import java.util.Iterator;
23+
24+
/**
25+
* Proxy TableData for ResourcePool
26+
*/
27+
public class TableDataProxy implements TableData {
28+
private final Resource resource;
29+
30+
public TableDataProxy(Resource tableDataRemoteResource) {
31+
this.resource = tableDataRemoteResource;
32+
}
33+
34+
@Override
35+
public ColumnDef[] columns() {
36+
return (ColumnDef[]) resource.invokeMethod(
37+
"columns", null, null);
38+
}
39+
40+
@Override
41+
public Iterator<Row> rows() {
42+
String resourceName = resource.getResourceId().getName() + ".rows";
43+
Resource rows = resource.invokeMethod("rows", null, null, resourceName);
44+
45+
ProxyRowIterator it = new ProxyRowIterator(rows);
46+
return it;
47+
}
48+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.zeppelin.tabledata;
18+
19+
import org.apache.zeppelin.interpreter.InterpreterResult;
20+
import org.apache.zeppelin.interpreter.InterpreterResultMessage;
21+
import org.junit.Test;
22+
23+
import java.util.Iterator;
24+
25+
import static junit.framework.TestCase.assertFalse;
26+
import static org.junit.Assert.assertEquals;
27+
28+
public class InterpreterResultTableDataTest {
29+
@Test
30+
public void test() {
31+
InterpreterResultMessage msg = new InterpreterResultMessage(
32+
InterpreterResult.Type.TABLE,
33+
"key\tvalue\nsun\t100\nmoon\t200\n");
34+
InterpreterResultTableData table = new InterpreterResultTableData(msg);
35+
36+
ColumnDef[] cols = table.columns();
37+
assertEquals(2, cols.length);
38+
39+
assertEquals("key", cols[0].name());
40+
assertEquals("value", cols[1].name());
41+
42+
Iterator<Row> it = table.rows();
43+
Row row = it.next();
44+
assertEquals(2, row.get().length);
45+
assertEquals("sun", row.get()[0]);
46+
assertEquals("100", row.get()[1]);
47+
48+
row = it.next();
49+
assertEquals("moon", row.get()[0]);
50+
assertEquals("200", row.get()[1]);
51+
52+
assertFalse(it.hasNext());
53+
}
54+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.zeppelin.tabledata;
18+
19+
import org.apache.zeppelin.interpreter.InterpreterResult;
20+
import org.apache.zeppelin.interpreter.InterpreterResultMessage;
21+
import org.apache.zeppelin.resource.LocalResourcePool;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
25+
import java.util.Iterator;
26+
27+
import static org.junit.Assert.assertEquals;
28+
import static org.junit.Assert.assertFalse;
29+
30+
public class TableDataProxyTest {
31+
private LocalResourcePool pool;
32+
33+
@Before
34+
public void setUp() {
35+
pool = new LocalResourcePool("p1");
36+
}
37+
38+
@Test
39+
public void testProxyTable() {
40+
InterpreterResultMessage msg = new InterpreterResultMessage(
41+
InterpreterResult.Type.TABLE,
42+
"key\tvalue\nsun\t100\nmoon\t200\n");
43+
InterpreterResultTableData table = new InterpreterResultTableData(msg);
44+
45+
pool.put("table", table);
46+
TableDataProxy proxy = new TableDataProxy(pool.get("table"));
47+
48+
ColumnDef[] cols = proxy.columns();
49+
assertEquals(2, cols.length);
50+
51+
assertEquals("key", cols[0].name());
52+
assertEquals("value", cols[1].name());
53+
54+
Iterator<Row> it = proxy.rows();
55+
Row row = it.next();
56+
assertEquals(2, row.get().length);
57+
assertEquals("sun", row.get()[0]);
58+
assertEquals("100", row.get()[1]);
59+
60+
row = it.next();
61+
assertEquals("moon", row.get()[0]);
62+
assertEquals("200", row.get()[1]);
63+
64+
assertFalse(it.hasNext());
65+
}
66+
}

0 commit comments

Comments
 (0)