CassandraInterpreter extends Interpreter { @Override public InterpreterResult interpret(String st, InterpreterContext context) { return helper.interpret(session, st, context); } CassandraInterpreter(Properties properties); @Override void open(); @Override void close(); @Override InterpreterResult interpret(String st, InterpreterContext context); @Override void cancel(InterpreterContext context); @Override FormType getFormType(); @Override int getProgress(InterpreterContext context); @Override List<InterpreterCompletion> completion(String buf, int cursor); @Override Scheduler getScheduler(); @Override void destroy(); static final String CASSANDRA_INTERPRETER_PARALLELISM; static final String CASSANDRA_HOSTS; static final String CASSANDRA_PORT; static final String CASSANDRA_PROTOCOL_VERSION; static final String CASSANDRA_CLUSTER_NAME; static final String CASSANDRA_KEYSPACE_NAME; static final String CASSANDRA_COMPRESSION_PROTOCOL; static final String CASSANDRA_CREDENTIALS_USERNAME; static final String CASSANDRA_CREDENTIALS_PASSWORD; static final String CASSANDRA_LOAD_BALANCING_POLICY; static final String CASSANDRA_RETRY_POLICY; static final String CASSANDRA_RECONNECTION_POLICY; static final String CASSANDRA_SPECULATIVE_EXECUTION_POLICY; static final String CASSANDRA_MAX_SCHEMA_AGREEMENT_WAIT_SECONDS; static final String CASSANDRA_POOLING_NEW_CONNECTION_THRESHOLD_LOCAL; static final String CASSANDRA_POOLING_NEW_CONNECTION_THRESHOLD_REMOTE; static final String CASSANDRA_POOLING_MAX_CONNECTION_PER_HOST_LOCAL; static final String CASSANDRA_POOLING_MAX_CONNECTION_PER_HOST_REMOTE; static final String CASSANDRA_POOLING_CORE_CONNECTION_PER_HOST_LOCAL; static final String CASSANDRA_POOLING_CORE_CONNECTION_PER_HOST_REMOTE; static final String CASSANDRA_POOLING_MAX_REQUESTS_PER_CONNECTION_LOCAL; static final String CASSANDRA_POOLING_MAX_REQUESTS_PER_CONNECTION_REMOTE; static final String CASSANDRA_POOLING_IDLE_TIMEOUT_SECONDS; static final String CASSANDRA_POOLING_POOL_TIMEOUT_MILLIS; static final String CASSANDRA_POOLING_HEARTBEAT_INTERVAL_SECONDS; static final String CASSANDRA_QUERY_DEFAULT_CONSISTENCY; static final String CASSANDRA_QUERY_DEFAULT_SERIAL_CONSISTENCY; static final String CASSANDRA_QUERY_DEFAULT_FETCH_SIZE; static final String CASSANDRA_QUERY_DEFAULT_IDEMPOTENCE; static final String CASSANDRA_SOCKET_CONNECTION_TIMEOUT_MILLIS; static final String CASSANDRA_SOCKET_KEEP_ALIVE; static final String CASSANDRA_SOCKET_READ_TIMEOUT_MILLIS; static final String CASSANDRA_SOCKET_RECEIVED_BUFFER_SIZE_BYTES; static final String CASSANDRA_SOCKET_REUSE_ADDRESS; static final String CASSANDRA_SOCKET_SEND_BUFFER_SIZE_BYTES; static final String CASSANDRA_SOCKET_SO_LINGER; static final String CASSANDRA_SOCKET_TCP_NO_DELAY; static final String DEFAULT_HOST; static final String DEFAULT_PORT; static final String DEFAULT_CLUSTER; static final String DEFAULT_KEYSPACE; static final String DEFAULT_PROTOCOL_VERSION; static final String DEFAULT_COMPRESSION; static final String DEFAULT_CREDENTIAL; static final String DEFAULT_POLICY; static final String DEFAULT_PARALLELISM; static final String DEFAULT_IDLE_TIMEOUT; static final String DEFAULT_POOL_TIMEOUT; static final String DEFAULT_HEARTBEAT_INTERVAL; static final String DEFAULT_CONSISTENCY; static final String DEFAULT_SERIAL_CONSISTENCY; static final String DEFAULT_FETCH_SIZE; static final String DEFAULT_CONNECTION_TIMEOUT; static final String DEFAULT_READ_TIMEOUT; static final String DEFAULT_TCP_NO_DELAY; static final String DOWNGRADING_CONSISTENCY_RETRY; static final String FALLTHROUGH_RETRY; static final String LOGGING_DEFAULT_RETRY; static final String LOGGING_DOWNGRADING_RETRY; static final String LOGGING_FALLTHROUGH_RETRY; static final List NO_COMPLETION; }
@Test public void should_error_describing_non_existing_udt() throws Exception { String query = "USE system;\n" + "DESCRIBE TYPE address;"; final InterpreterResult actual = interpreter.interpret(query, intrContext); assertThat(actual.code()).isEqualTo(Code.ERROR); assertThat(actual.message()).contains("Cannot find type system.address"); }
@Test public void should_show_help() throws Exception { String query = "HELP;"; final String expected = reformatHtml(readTestResource("/scalate/Help.html")); final InterpreterResult actual = interpreter.interpret(query, intrContext); assertThat(actual.code()).isEqualTo(Code.SUCCESS); assertThat(reformatHtml(actual.message())).isEqualTo(expected); }
@Test public void should_interpret_simple_select() throws Exception { final InterpreterResult actual = interpreter.interpret("SELECT * FROM " + ARTISTS_TABLE + " LIMIT 10;", intrContext); assertThat(actual).isNotNull(); assertThat(actual.code()).isEqualTo(Code.SUCCESS); assertThat(actual.message()).isEqualTo("name\tborn\tcountry\tdied\tgender\tstyles\ttype\n" + "Bogdan Raczynski\t1977-01-01\tPoland\tnull\tMale\t[Dance, Electro]\tPerson\n" + "Krishna Das\t1947-05-31\tUSA\tnull\tMale\t[Unknown]\tPerson\n" + "Sheryl Crow\t1962-02-11\tUSA\tnull\tFemale\t[Classic, Rock, Country, Blues, Pop, Folk]\tPerson\n" + "Doof\t1968-08-31\tUnited Kingdom\tnull\tnull\t[Unknown]\tPerson\n" + "House of Large Sizes\t1986-01-01\tUSA\t2003\tnull\t[Unknown]\tGroup\n" + "Fanfarlo\t2006-01-01\tUnited Kingdom\tnull\tnull\t[Rock, Indie, Pop, Classic]\tGroup\n" + "Jeff Beck\t1944-06-24\tUnited Kingdom\tnull\tMale\t[Rock, Pop, Classic]\tPerson\n" + "Los Paranoias\tnull\tUnknown\tnull\tnull\t[Unknown]\tnull\n" + "…And You Will Know Us by the Trail of Dead\t1994-01-01\tUSA\tnull\tnull\t[Rock, Pop, Classic]\tGroup\n"); }
@Test public void should_interpret_select_statement() throws Exception { final InterpreterResult actual = interpreter.interpret("SELECT * FROM " + ARTISTS_TABLE + " LIMIT 2;", intrContext); assertThat(actual).isNotNull(); assertThat(actual.code()).isEqualTo(Code.SUCCESS); assertThat(actual.message()).isEqualTo("name\tborn\tcountry\tdied\tgender\tstyles\ttype\n" + "Bogdan Raczynski\t1977-01-01\tPoland\tnull\tMale\t[Dance, Electro]\tPerson\n" + "Krishna Das\t1947-05-31\tUSA\tnull\tMale\t[Unknown]\tPerson\n"); }
@Test public void should_interpret_multiple_statements_with_single_line_logged_batch() throws Exception { String statements = "CREATE TABLE IF NOT EXISTS zeppelin.albums(\n" + " title text PRIMARY KEY,\n" + " artist text,\n" + " year int\n" + ");\n" + "BEGIN BATCH"+ " INSERT INTO zeppelin.albums(title,artist,year) VALUES('The Impossible Dream EP','Carter the Unstoppable Sex Machine',1992);"+ " INSERT INTO zeppelin.albums(title,artist,year) VALUES('The Way You Are','Tears for Fears',1983);"+ " INSERT INTO zeppelin.albums(title,artist,year) VALUES('Primitive','Soulfly',2003);"+ "APPLY BATCH;\n"+ "SELECT * FROM zeppelin.albums;"; final InterpreterResult actual = interpreter.interpret(statements, intrContext); assertThat(actual.code()).isEqualTo(Code.SUCCESS); assertThat(actual.message()).isEqualTo("title\tartist\tyear\n" + "The Impossible Dream EP\tCarter the Unstoppable Sex Machine\t1992\n" + "The Way You Are\tTears for Fears\t1983\n" + "Primitive\tSoulfly\t2003\n"); }
@Test public void should_throw_statement_not_having_semi_colon() throws Exception { String statement = "SELECT * zeppelin.albums"; final InterpreterResult actual = interpreter.interpret(statement, intrContext); assertThat(actual.code()).isEqualTo(Code.ERROR); assertThat(actual.message()) .contains("Error parsing input:\n" + "\t'SELECT * zeppelin.albums'\n" + "Did you forget to add ; (semi-colon) at the end of each CQL statement ?"); }
@Test public void should_validate_statement() throws Exception { String statement = "SELECT * zeppelin.albums;"; final InterpreterResult actual = interpreter.interpret(statement, intrContext); assertThat(actual.code()).isEqualTo(Code.ERROR); assertThat(actual.message()).contains("line 1:9 missing K_FROM at 'zeppelin' (SELECT * [zeppelin]....)"); }
@Test public void should_execute_statement_with_consistency_option() throws Exception { String statement = "@consistency=THREE\n" + "SELECT * FROM zeppelin.artists LIMIT 1;"; final InterpreterResult actual = interpreter.interpret(statement, intrContext); assertThat(actual.code()).isEqualTo(Code.ERROR); assertThat(actual.message()) .contains("Not enough replicas available for query at consistency THREE (3 required but only 1 alive)"); }
@Test public void should_execute_statement_with_serial_consistency_option() throws Exception { String statement = "@serialConsistency=SERIAL\n" + "SELECT * FROM zeppelin.artists LIMIT 1;"; final InterpreterResult actual = interpreter.interpret(statement, intrContext); assertThat(actual.code()).isEqualTo(Code.SUCCESS); }
@Test public void should_execute_statement_with_timestamp_option() throws Exception { String statement1 = "INSERT INTO zeppelin.ts(key,val) VALUES('k','v1');"; String statement2 = "@timestamp=15\n" + "INSERT INTO zeppelin.ts(key,val) VALUES('k','v2');"; interpreter.interpret(statement1, intrContext); Thread.sleep(1); interpreter.interpret(statement2, intrContext); final String actual = session.execute("SELECT * FROM zeppelin.ts LIMIT 1").one().getString("val"); assertThat(actual).isEqualTo("v1"); }
@Test public void should_execute_statement_with_retry_policy() throws Exception { String statement = "@retryPolicy=" + interpreter.LOGGING_DOWNGRADING_RETRY + "\n" + "@consistency=THREE\n" + "SELECT * FROM zeppelin.artists LIMIT 1;"; final InterpreterResult actual = interpreter.interpret(statement, intrContext); assertThat(actual.code()).isEqualTo(Code.SUCCESS); }
@Test public void should_execute_statement_with_request_timeout() throws Exception { String statement = "@requestTimeOut=10000000\n" + "SELECT * FROM zeppelin.artists;"; final InterpreterResult actual = interpreter.interpret(statement, intrContext); assertThat(actual.code()).isEqualTo(Code.SUCCESS); }
@Test public void should_execute_prepared_and_bound_statements() throws Exception { String queries = "@prepare[ps]=INSERT INTO zeppelin.prepared(key,val) VALUES(?,?)\n" + "@prepare[select]=SELECT * FROM zeppelin.prepared WHERE key=:key\n" + "@bind[ps]='myKey','myValue'\n" + "@bind[select]='myKey'"; final InterpreterResult actual = interpreter.interpret(queries, intrContext); assertThat(actual.code()).isEqualTo(Code.SUCCESS); assertThat(actual.message()).isEqualTo("key\tval\n" + "myKey\tmyValue\n"); }
@Test public void should_execute_bound_statement() throws Exception { String queries = "@prepare[users_insert]=INSERT INTO zeppelin.users" + "(login,firstname,lastname,addresses,location)" + "VALUES(:login,:fn,:ln,:addresses,:loc)\n" + "@bind[users_insert]='jdoe','John','DOE'," + "{street_number: 3, street_name: 'Beverly Hills Bld', zip_code: 90209," + " country: 'USA', extra_info: ['Right on the hills','Next to the post box']," + " phone_numbers: {'home': 2016778524, 'office': 2015790847}}," + "('USA', 90209, 'Beverly Hills')\n" + "SELECT * FROM zeppelin.users WHERE login='jdoe';"; final InterpreterResult actual = interpreter.interpret(queries, intrContext); assertThat(actual.code()).isEqualTo(Code.SUCCESS); assertThat(actual.message()).isEqualTo( "login\taddresses\tage\tdeceased\tfirstname\tlast_update\tlastname\tlocation\n" + "jdoe\t" + "{street_number:3,street_name:'Beverly Hills Bld',zip_code:90209," + "country:'USA',extra_info:['Right on the hills','Next to the post box']," + "phone_numbers:{'office':2015790847,'home':2016778524}}\tnull\t" + "null\t" + "John\t" + "null\t" + "DOE\t" + "('USA',90209,'Beverly Hills')\n"); }
@Test public void should_exception_when_executing_unknown_bound_statement() throws Exception { String queries = "@bind[select_users]='jdoe'"; final InterpreterResult actual = interpreter.interpret(queries, intrContext); assertThat(actual.code()).isEqualTo(Code.ERROR); assertThat(actual.message()) .isEqualTo("The statement 'select_users' can not be bound to values. " + "Are you sure you did prepare it with @prepare[select_users] ?"); }
@Test public void should_extract_variable_from_statement() throws Exception { AngularObjectRegistry angularObjectRegistry = new AngularObjectRegistry("cassandra", null); when(intrContext.getAngularObjectRegistry()).thenReturn(angularObjectRegistry); when(intrContext.getGui().input("login", "hsue")).thenReturn("hsue"); when(intrContext.getGui().input("age", "27")).thenReturn("27"); String queries = "@prepare[test_insert_with_variable]=" + "INSERT INTO zeppelin.users(login,firstname,lastname,age) VALUES(?,?,?,?)\n" + "@bind[test_insert_with_variable]='{{login=hsue}}','Helen','SUE',{{age=27}}\n" + "SELECT firstname,lastname,age FROM zeppelin.users WHERE login='hsue';"; final InterpreterResult actual = interpreter.interpret(queries, intrContext); assertThat(actual.code()).isEqualTo(Code.SUCCESS); assertThat(actual.message()).isEqualTo("firstname\tlastname\tage\n" + "Helen\tSUE\t27\n"); }
@Test public void should_just_prepare_statement() throws Exception { String queries = "@prepare[just_prepare]=SELECT name,country,styles FROM zeppelin.artists LIMIT 3"; final String expected = reformatHtml( readTestResource("/scalate/NoResult.html")); final InterpreterResult actual = interpreter.interpret(queries, intrContext); assertThat(actual.code()).isEqualTo(Code.SUCCESS); assertThat(reformatHtml(actual.message())).isEqualTo(expected); }
@Test public void should_execute_bound_statement_with_no_bound_value() throws Exception { String queries = "@prepare[select_no_bound_value]=SELECT name,country,styles FROM zeppelin.artists LIMIT 3\n" + "@bind[select_no_bound_value]"; final InterpreterResult actual = interpreter.interpret(queries, intrContext); assertThat(actual.code()).isEqualTo(Code.SUCCESS); assertThat(actual.message()).isEqualTo("name\tcountry\tstyles\n" + "Bogdan Raczynski\tPoland\t[Dance, Electro]\n" + "Krishna Das\tUSA\t[Unknown]\n" + "Sheryl Crow\tUSA\t[Classic, Rock, Country, Blues, Pop, Folk]\n"); }
@Test public void should_parse_date_value() throws Exception { String queries = "@prepare[parse_date]=INSERT INTO zeppelin.users(login,last_update) VALUES(?,?)\n" + "@bind[parse_date]='last_update','2015-07-30 12:00:01'\n" + "SELECT last_update FROM zeppelin.users WHERE login='last_update';"; final InterpreterResult actual = interpreter.interpret(queries, intrContext); assertThat(actual.code()).isEqualTo(Code.SUCCESS); assertThat(actual.message()).contains("last_update\n" + "Thu Jul 30 12:00:01"); }
@Test public void should_bind_null_value() throws Exception { String queries = "@prepare[bind_null]=INSERT INTO zeppelin.users(login,firstname,lastname) VALUES(?,?,?)\n" + "@bind[bind_null]='bind_null',null,'NULL'\n" + "SELECT firstname,lastname FROM zeppelin.users WHERE login='bind_null';"; final InterpreterResult actual = interpreter.interpret(queries, intrContext); assertThat(actual.code()).isEqualTo(Code.SUCCESS); assertThat(actual.message()).isEqualTo("firstname\tlastname\n" + "null\tNULL\n"); }
@Test public void should_bind_boolean_value() throws Exception { String queries = "@prepare[bind_boolean]=INSERT INTO zeppelin.users(login,deceased) VALUES(?,?)\n" + "@bind[bind_boolean]='bind_bool',false\n" + "SELECT login,deceased FROM zeppelin.users WHERE login='bind_bool';"; final InterpreterResult actual = interpreter.interpret(queries, intrContext); assertThat(actual.code()).isEqualTo(Code.SUCCESS); assertThat(actual.message()).isEqualTo("login\tdeceased\n" + "bind_bool\tfalse\n"); }
@Test public void should_fail_when_executing_a_removed_prepared_statement() throws Exception { String prepare_first = "@prepare[to_be_removed]=INSERT INTO zeppelin.users(login,deceased) VALUES(?,?)"; interpreter.interpret(prepare_first, intrContext); String remove_prepared = "@remove_prepare[to_be_removed]\n" + "@bind[to_be_removed]='bind_bool'"; final InterpreterResult actual = interpreter.interpret(remove_prepared, intrContext); assertThat(actual.code()).isEqualTo(Code.ERROR); assertThat(actual.message()).isEqualTo("The statement 'to_be_removed' can not be bound to values. " + "Are you sure you did prepare it with @prepare[to_be_removed] ?"); }
@Test public void should_display_statistics_for_non_select_statement() throws Exception { String query = "USE zeppelin;\nCREATE TABLE IF NOT EXISTS no_select(id int PRIMARY KEY);"; final String rawResult = reformatHtml(readTestResource("/scalate/NoResultWithExecutionInfo.html")); final InterpreterResult actual = interpreter.interpret(query, intrContext); final Cluster cluster = session.getCluster(); final int port = cluster.getConfiguration().getProtocolOptions().getPort(); final String address = cluster.getMetadata().getAllHosts().iterator().next() .getAddress().getHostAddress() .replaceAll("/", "").replaceAll("\\[", "").replaceAll("\\]",""); final String expected = rawResult.replaceAll("TRIED_HOSTS", address+":"+port) .replaceAll("QUERIED_HOSTS", address +":"+port); assertThat(actual.code()).isEqualTo(Code.SUCCESS); assertThat(reformatHtml(actual.message())).isEqualTo(expected); }
@Test public void should_error_and_display_stack_trace() throws Exception { String query = "@consistency=THREE\n" + "SELECT * FROM zeppelin.users LIMIT 3;"; final InterpreterResult actual = interpreter.interpret(query, intrContext); assertThat(actual.code()).isEqualTo(Code.ERROR); assertThat(actual.message()).contains("All host(s) tried for query failed"); }
@Test public void should_describe_cluster() throws Exception { String query = "DESCRIBE CLUSTER;"; final String expected = reformatHtml( readTestResource("/scalate/DescribeCluster.html")); final InterpreterResult actual = interpreter.interpret(query, intrContext); assertThat(actual.code()).isEqualTo(Code.SUCCESS); assertThat(reformatHtml(actual.message())).isEqualTo(expected); }
@Test public void should_describe_keyspaces() throws Exception { String query = "DESCRIBE KEYSPACES;"; final String expected = reformatHtml( readTestResource("/scalate/DescribeKeyspaces.html")); final InterpreterResult actual = interpreter.interpret(query, intrContext); assertThat(actual.code()).isEqualTo(Code.SUCCESS); assertThat(reformatHtml(actual.message())).isEqualTo(expected); }
@Test public void should_describe_keyspace() throws Exception { String query = "DESCRIBE KEYSPACE live_data;"; final String expected = reformatHtml( readTestResource("/scalate/DescribeKeyspace_live_data.html")); final InterpreterResult actual = interpreter.interpret(query, intrContext); assertThat(actual.code()).isEqualTo(Code.SUCCESS); assertThat(reformatHtml(actual.message())).isEqualTo(expected); }
@Test public void should_describe_table() throws Exception { String query = "DESCRIBE TABLE live_data.complex_table;"; final String expected = reformatHtml( readTestResource("/scalate/DescribeTable_live_data_complex_table.html")); final InterpreterResult actual = interpreter.interpret(query, intrContext); assertThat(actual.code()).isEqualTo(Code.SUCCESS); assertThat(reformatHtml(actual.message())).isEqualTo(expected); }
@Test public void should_describe_udt() throws Exception { String query = "DESCRIBE TYPE live_data.address;"; final String expected = reformatHtml( readTestResource("/scalate/DescribeType_live_data_address.html")); final InterpreterResult actual = interpreter.interpret(query, intrContext); assertThat(actual.code()).isEqualTo(Code.SUCCESS); assertThat(reformatHtml(actual.message())).isEqualTo(expected); }
@Test public void should_describe_udt_withing_logged_in_keyspace() throws Exception { String query = "USE live_data;\n" + "DESCRIBE TYPE address;"; final String expected = reformatHtml( readTestResource("/scalate/DescribeType_live_data_address_within_current_keyspace.html")); final InterpreterResult actual = interpreter.interpret(query, intrContext); assertThat(actual.code()).isEqualTo(Code.SUCCESS); assertThat(reformatHtml(actual.message())).isEqualTo(expected); }
@Test public void should_error_describing_non_existing_table() throws Exception { String query = "USE system;\n" + "DESCRIBE TABLE complex_table;"; final InterpreterResult actual = interpreter.interpret(query, intrContext); assertThat(actual.code()).isEqualTo(Code.ERROR); assertThat(actual.message()).contains("Cannot find table system.complex_table"); }