Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add MongoDB's "zips" data set, sqlline, and HOWTO guide.

  • Loading branch information...
commit 1c2beab9a05c25665f8cc030dbe9fb69e6fece99 1 parent a68c975
@julianhyde authored
View
69 HOWTO.md
@@ -0,0 +1,69 @@
+# Optiq HOWTO
+
+Here's some miscellaneous documentation about using Optiq and its various
+adapters.
+
+# CSV adapter
+
+See <a href="https://github.com/julianhyde/optiq-csv/TUTORIAL.csv">optiq-csv
+tutorial</a>.
+
+# MongoDB adapter
+
+First, download and install Optiq,
+and <a href="http://www.mongodb.org/downloads">install MongoDB</a>.
+
+Import MongoDB's zipcode data set into MongoDB:
+
+```bash
+$ curl -o /tmp/zips.json http://media.mongodb.org/zips.json
+$ mongoimport --db test --collection zips --file /tmp/zips.json
+Tue Jun 4 16:24:14.190 check 9 29470
+Tue Jun 4 16:24:14.469 imported 29470 objects
+```
+
+Log into MongoDB to check it's there:
+
+```bash
+$ mongo
+MongoDB shell version: 2.4.3
+connecting to: test
+> db.zips.find().limit(3)
+{ "city" : "ACMAR", "loc" : [ -86.51557, 33.584132 ], "pop" : 6055, "state" : "AL", "_id" : "35004" }
+{ "city" : "ADAMSVILLE", "loc" : [ -86.959727, 33.588437 ], "pop" : 10616, "state" : "AL", "_id" : "35005" }
+{ "city" : "ADGER", "loc" : [ -87.167455, 33.434277 ], "pop" : 3205, "state" : "AL", "_id" : "35006" }
+> exit
+bye
+```
+
+Connect using the <a href="src/test/resources/mongo-zips-model.json">mongo-zips-model.json</a> Optiq model:
+```bash
+$ ./sqlline
+sqlline> !connect
+sqlline> !connect jdbc:optiq:model=target/test-classes/mongo-zips-model.json admin admin
+Connecting to jdbc:optiq:model=target/test-classes/mongo-zips-model.json
+Connected to: Optiq (version 0.4.2)
+Driver: Optiq JDBC Driver (version 0.4.2)
+Autocommit status: true
+Transaction isolation: TRANSACTION_REPEATABLE_READ
+sqlline> !tables
++------------+--------------+-----------------+---------------+
+| TABLE_CAT | TABLE_SCHEM | TABLE_NAME | TABLE_TYPE |
++------------+--------------+-----------------+---------------+
+| null | mongo_raw | zips | TABLE |
+| null | mongo_raw | system.indexes | TABLE |
+| null | mongo | ZIPS | VIEW |
+| null | metadata | COLUMNS | SYSTEM_TABLE |
+| null | metadata | TABLES | SYSTEM_TABLE |
++------------+--------------+-----------------+---------------+
+sqlline> select count(*) from zips;
++---------+
+| EXPR$0 |
++---------+
+| 29467 |
++---------+
+1 row selected (0.746 seconds)
+sqlline> !quit
+Closing: net.hydromatic.optiq.jdbc.FactoryJdbc41$OptiqConnectionJdbc41
+$
+```
View
7 README.md
@@ -141,7 +141,11 @@ project provides a CSV adapter, which is fully functional for use in application
but is also simple enough to serve as a good template if you are writing
your own adapter.
-See the <a href="https://github.com/julianhyde/optiq-csv/blob/master/TUTORIAL.md">tutorial</a>.
+See the <a href="https://github.com/julianhyde/optiq-csv/blob/master/TUTORIAL.md">optiq-csv tutorial</a>
+for information on using optiq-csv and writing adapters.
+
+See the <a href="HOWTO.md">HOWTO</a> for more information about using other
+adapters, and about using Optiq in general.
## Status
@@ -179,6 +183,7 @@ The following features are complete.
* Project page: http://www.hydromatic.net/optiq
* Source code: http://github.com/julianhyde/optiq
* Developers list: http://groups.google.com/group/optiq-dev
+* <a href="HOWTO.md">HOWTO</a>
### Presentations
View
8 pom.xml
@@ -9,7 +9,7 @@
<!-- Version number. The version number committed to github should be
TRUNK-SNAPSHOT except, briefly, when making a release. Next: 0.4.3. -->
- <version>0.4.2</version>
+ <version>TRUNK-SNAPSHOT</version>
<!-- More project information. -->
<name>optiq</name>
@@ -142,6 +142,12 @@
<version>5.1.20</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>sqlline</groupId>
+ <artifactId>sqlline</artifactId>
+ <version>1.1.0</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<reporting>
View
11 sqlline
@@ -0,0 +1,11 @@
+#!/bin/bash
+# sqlline - Script to launch SQL shell
+
+# Build classpath on first call. (To force rebuild, remove .classpath.txt.)
+if [ ! -f .classpath.txt ]; then
+ mvn dependency:build-classpath -Dmdep.outputFile=.classpath.txt
+fi
+
+exec java -cp "target/classes:target/test-classes:$(cat .classpath.txt)" sqlline.SqlLine "$@"
+
+# End sqlline
View
45 src/test/java/net/hydromatic/optiq/test/MongoAdapterTest.java
@@ -17,9 +17,12 @@
*/
package net.hydromatic.optiq.test;
+import net.hydromatic.optiq.jdbc.OptiqConnection;
+
import junit.framework.TestCase;
-import java.util.Random;
+import java.sql.DriverManager;
+import java.util.Properties;
/**
* Tests for the {@link net.hydromatic.optiq.impl.mongodb} package.
@@ -72,10 +75,12 @@
/** Disabled by default, because we do not expect Mongo to be installed and
* populated with the FoodMart data set. */
- private static final boolean ENABLED = new Random().nextInt(2) < 0;
+ private boolean enabled() {
+ return false;
+ }
public void testUnionPlan() {
- if (!ENABLED) {
+ if (!enabled()) {
return;
}
OptiqAssert.assertThat()
@@ -94,7 +99,7 @@ public void testUnionPlan() {
}
public void testFilterUnionPlan() {
- if (!ENABLED) {
+ if (!enabled()) {
return;
}
OptiqAssert.assertThat()
@@ -109,7 +114,7 @@ public void testFilterUnionPlan() {
}
public void testSelectWhere() {
- if (!ENABLED) {
+ if (!enabled()) {
return;
}
OptiqAssert.assertThat()
@@ -119,14 +124,15 @@ public void testSelectWhere() {
.explainContains(
"PLAN=EnumerableCalcRel(expr#0=[{inputs}], expr#1=['warehouse_id'], expr#2=[ITEM($t0, $t1)], expr#3=[CAST($t2):DOUBLE NOT NULL], expr#4=['warehouse_state_province'], expr#5=[ITEM($t0, $t4)], expr#6=[CAST($t5):VARCHAR(20) CHARACTER SET \"ISO-8859-1\" COLLATE \"ISO-8859-1$en_US$primary\" NOT NULL], expr#7=['CA'], expr#8=[=($t6, $t7)], warehouse_id=[$t3], warehouse_state_province=[$t6], $condition=[$t8])\n"
+ " EnumerableTableAccessRel(table=[[_foodmart, warehouse]])")
- .returns("warehouse_id=6.0; warehouse_state_province=CA\n"
+ .returns(
+ "warehouse_id=6.0; warehouse_state_province=CA\n"
+ "warehouse_id=7.0; warehouse_state_province=CA\n"
+ "warehouse_id=14.0; warehouse_state_province=CA\n"
+ "warehouse_id=24.0; warehouse_state_province=CA\n");
}
public void testInPlan() {
- if (!ENABLED) {
+ if (!enabled()) {
return;
}
OptiqAssert.assertThat()
@@ -144,6 +150,31 @@ public void testInPlan() {
+ "store_id=16.0; store_name=Store 16\n"
+ "store_id=24.0; store_name=Store 24\n");
}
+
+ /** Query based on the "mongo-zips" model. */
+ public void testZips() {
+ if (!enabled()) {
+ return;
+ }
+ OptiqAssert.assertThat()
+ .with(
+ new OptiqAssert.ConnectionFactory() {
+ public OptiqConnection createConnection() throws Exception {
+ Class.forName("net.hydromatic.optiq.jdbc.Driver");
+ final Properties info = new Properties();
+ info.setProperty("model",
+ "target/test-classes/mongo-zips-model.json");
+ return (OptiqConnection) DriverManager.getConnection(
+ "jdbc:optiq:", info);
+ }
+ })
+ .query("select count(*) from zips")
+ .returns("EXPR$0=29467\n")
+ .explainContains(
+ "PLAN=EnumerableAggregateRel(group=[{}], EXPR$0=[COUNT()])\n"
+ + " EnumerableCalcRel(expr#0=[{inputs}], expr#1=[0], $f0=[$t1])\n"
+ + " EnumerableTableAccessRel(table=[[mongo_raw, zips]])");
+ }
}
// End MongoAdapterTest.java
View
25 src/test/resources/mongo-zips-model.json
@@ -0,0 +1,25 @@
+{
+ version: '1.0',
+ defaultSchema: 'mongo',
+ schemas: [
+ {
+ type: 'custom',
+ name: 'mongo_raw',
+ factory: 'net.hydromatic.optiq.impl.mongodb.MongoSchemaFactory',
+ operand: {
+ host: 'localhost',
+ database: 'test'
+ }
+ },
+ {
+ name: 'mongo',
+ tables: [
+ {
+ name: 'ZIPS',
+ type: 'view',
+ sql: 'select cast(_MAP[\'city\'] AS varchar(20)) AS city,\n cast(_MAP[\'loc\'][0] AS float) AS longitude, cast(_MAP[\'loc\'][1] AS float) AS latitude, cast(_MAP[\'pop\'] AS integer) AS pop, cast(_MAP[\'state\'] AS varchar(2)) AS state, cast(_MAP[\'_id\'] AS integer) AS id from \"mongo_raw\".\"zips\"'
+ }
+ ]
+ }
+ ]
+}
Please sign in to comment.
Something went wrong with that request. Please try again.