List of usage examples for org.apache.lucene.util BytesRef BytesRef
public BytesRef(CharSequence text)
From source file:io.crate.executor.transport.task.ShowCreateTableTask.java
License:Apache License
@Override public void execute(BatchConsumer consumer, Row parameters) { Row1 row;/* w w w.j a v a2 s . c o m*/ try { CreateTable createTable = MetaDataToASTNodeResolver.resolveCreateTable(tableInfo); row = new Row1(new BytesRef(SqlFormatter.formatSql(createTable))); } catch (Throwable t) { consumer.accept(null, t); return; } consumer.accept(RowsBatchIterator.newInstance(row), null); }
From source file:io.crate.executor.transport.TransportExecutorDDLTest.java
License:Apache License
@Test public void testCreateTableWithOrphanedPartitions() throws Exception { String partitionName = new PartitionName("test", Arrays.asList(new BytesRef("foo"))).asIndexName(); client().admin().indices().prepareCreate(partitionName) .addMapping(Constants.DEFAULT_MAPPING_TYPE, TEST_PARTITIONED_MAPPING).setSettings(TEST_SETTINGS) .execute().actionGet();//w w w . ja va 2 s . c o m ensureGreen(); execute("create table test (id integer, name string, names string) partitioned by (id)"); ensureYellow(); execute("select * from information_schema.tables where table_name = 'test'"); assertThat(response.rowCount(), is(1L)); execute("select count(*) from information_schema.columns where table_name = 'test'"); assertThat((Long) response.rows()[0][0], is(3L)); // check that orphaned partition has been deleted assertThat( client().admin().indices().exists(new IndicesExistsRequest(partitionName)).actionGet().isExists(), is(false)); }
From source file:io.crate.executor.transport.TransportExecutorDDLTest.java
License:Apache License
@Test public void testCreateTableWithOrphanedAlias() throws Exception { String partitionName = new PartitionName("test", Collections.singletonList(new BytesRef("foo"))) .asIndexName();// w ww . ja v a 2s . co m client().admin().indices().prepareCreate(partitionName) .addMapping(Constants.DEFAULT_MAPPING_TYPE, TEST_PARTITIONED_MAPPING).setSettings(TEST_SETTINGS) .addAlias(new Alias("test")).execute().actionGet(); ensureGreen(); execute("create table test (id integer, name string, names string) " + "clustered into 2 shards " + "partitioned by (id) with (number_of_replicas=0)"); assertThat(response.rowCount(), is(1L)); ensureGreen(); execute("select * from information_schema.tables where table_name = 'test'"); assertThat(response.rowCount(), is(1L)); execute("select count(*) from information_schema.columns where table_name = 'test'"); assertThat((Long) response.rows()[0][0], is(3L)); // check that orphaned alias has been deleted assertThat(client().admin().cluster().prepareState().execute().actionGet().getState().metaData().aliases() .containsKey("test"), is(false)); // check that orphaned partition has been deleted assertThat( client().admin().indices().exists(new IndicesExistsRequest(partitionName)).actionGet().isExists(), is(false)); }
From source file:io.crate.executor.transport.TransportExecutorDDLTest.java
License:Apache License
@Test public void testDeletePartitionTask() throws Exception { execute("create table t (id integer primary key, name string) partitioned by (id)"); ensureYellow();//from w w w . j a va 2 s . c om execute("insert into t (id, name) values (1, 'Ford')"); assertThat(response.rowCount(), is(1L)); ensureYellow(); execute("select * from information_schema.table_partitions where table_name = 't'"); assertThat(response.rowCount(), is(1L)); String partitionName = new PartitionName("t", ImmutableList.of(new BytesRef("1"))).asIndexName(); ESDeletePartitionNode deleteIndexNode = new ESDeletePartitionNode(partitionName); Plan plan = new IterablePlan(UUID.randomUUID(), deleteIndexNode); Job job = executor.newJob(plan); List<? extends ListenableFuture<TaskResult>> futures = executor.execute(job); ListenableFuture<List<TaskResult>> listenableFuture = Futures.allAsList(futures); Bucket objects = listenableFuture.get().get(0).rows(); assertThat(objects, contains(isRow(-1L))); execute("select * from information_schema.table_partitions where table_name = 't'"); assertThat(response.rowCount(), is(0L)); }
From source file:io.crate.executor.transport.TransportExecutorDDLTest.java
License:Apache License
/** * this case should not happen as closed indices aren't listed as TableInfo * but if it does maybe because of stale cluster state - validate behaviour here * * cannot prevent this task from deleting closed indices. *///from w w w . j a v a 2 s.c o m @Test public void testDeletePartitionTaskClosed() throws Exception { execute("create table t (id integer primary key, name string) partitioned by (id)"); ensureYellow(); execute("insert into t (id, name) values (1, 'Ford')"); assertThat(response.rowCount(), is(1L)); ensureYellow(); String partitionName = new PartitionName("t", ImmutableList.of(new BytesRef("1"))).asIndexName(); assertTrue(client().admin().indices().prepareClose(partitionName).execute().actionGet().isAcknowledged()); ESDeletePartitionNode deleteIndexNode = new ESDeletePartitionNode(partitionName); Plan plan = new IterablePlan(UUID.randomUUID(), deleteIndexNode); Job job = executor.newJob(plan); List<? extends ListenableFuture<TaskResult>> futures = executor.execute(job); ListenableFuture<List<TaskResult>> listenableFuture = Futures.allAsList(futures); Bucket objects = listenableFuture.get().get(0).rows(); assertThat(objects, contains(isRow(-1L))); execute("select * from information_schema.table_partitions where table_name = 't'"); assertThat(response.rowCount(), is(0L)); }
From source file:io.crate.executor.transport.TransportExecutorTest.java
License:Apache License
@Test public void testESIndexPartitionedTableTask() throws Exception { execute("create table parted (" + " id int, " + " name string, " + " date timestamp" + ") partitioned by (date)"); ensureGreen();//w w w . j av a 2 s. c om Map<String, Object> sourceMap = new MapBuilder<String, Object>().put("id", 0L).put("name", "Trillian") .map(); BytesReference source = XContentFactory.jsonBuilder().map(sourceMap).bytes(); PartitionName partitionName = new PartitionName("parted", Arrays.asList(new BytesRef("13959981214861"))); ESIndexNode indexNode = new ESIndexNode(new String[] { partitionName.stringValue() }, Arrays.asList(source), ImmutableList.of("123"), ImmutableList.of("123"), true, false); Plan plan = new Plan(); plan.add(indexNode); plan.expectsAffectedRows(true); Job job = executor.newJob(plan); assertThat(job.tasks().get(0), instanceOf(ESIndexTask.class)); List<ListenableFuture<TaskResult>> result = executor.execute(job); TaskResult taskResult = result.get(0).get(); Object[][] indexResult = taskResult.rows(); assertThat(indexResult.length, is(0)); assertThat(taskResult.rowCount(), is(1L)); refresh(); assertTrue(client().admin().indices().prepareExists(partitionName.stringValue()).execute().actionGet() .isExists()); assertTrue(client().admin().indices().prepareAliasesExist("parted").execute().actionGet().exists()); SearchHits hits = client().prepareSearch(partitionName.stringValue()) .setTypes(Constants.DEFAULT_MAPPING_TYPE).addFields("id", "name") .setQuery(new MapBuilder<String, Object>().put("match_all", new HashMap<String, Object>()).map()) .execute().actionGet().getHits(); assertThat(hits.getTotalHits(), is(1L)); assertThat((Integer) hits.getHits()[0].field("id").getValues().get(0), is(0)); assertThat((String) hits.getHits()[0].field("name").getValues().get(0), is("Trillian")); }
From source file:io.crate.executor.transport.TransportExecutorUpsertTest.java
License:Apache License
@Test public void testInsertWithSymbolBasedUpsertByIdTask() throws Exception { execute("create table characters (id int primary key, name string)"); ensureGreen();// w w w. j a v a 2s .c o m /* insert into characters (id, name) values (99, 'Marvin'); */ Planner.Context ctx = newPlannerContext(); SymbolBasedUpsertByIdNode updateNode = new SymbolBasedUpsertByIdNode(ctx.nextExecutionPhaseId(), false, false, null, new Reference[] { idRef, nameRef }); updateNode.add("characters", "99", "99", null, null, new Object[] { 99, new BytesRef("Marvin") }); Plan plan = new IterablePlan(ctx.jobId(), updateNode); Job job = executor.newJob(plan); assertThat(job.tasks().get(0), instanceOf(SymbolBasedUpsertByIdTask.class)); List<? extends ListenableFuture<TaskResult>> result = executor.execute(job); TaskResult taskResult = result.get(0).get(); Bucket rows = taskResult.rows(); assertThat(rows, contains(isRow(1L))); // verify insertion ImmutableList<Symbol> outputs = ImmutableList.<Symbol>of(idRef, nameRef); ESGetNode getNode = newGetNode("characters", outputs, "99", ctx.nextExecutionPhaseId()); plan = new IterablePlan(UUID.randomUUID(), getNode); job = executor.newJob(plan); result = executor.execute(job); Bucket objects = result.get(0).get().rows(); assertThat(objects, contains(isRow(99, "Marvin"))); }
From source file:io.crate.executor.transport.TransportExecutorUpsertTest.java
License:Apache License
@Test public void testInsertIntoPartitionedTableWithSymbolBasedUpsertByIdTask() throws Exception { execute("create table parted (" + " id int, " + " name string, " + " date timestamp" + ") partitioned by (date)"); ensureGreen();//from w w w . ja v a2 s . c om /* insert into parted (id, name, date) values(0, 'Trillian', 13959981214861); */ Planner.Context ctx = newPlannerContext(); SymbolBasedUpsertByIdNode updateNode = new SymbolBasedUpsertByIdNode(ctx.nextExecutionPhaseId(), true, false, null, new Reference[] { idRef, nameRef }); PartitionName partitionName = new PartitionName("parted", Arrays.asList(new BytesRef("13959981214861"))); updateNode.add(partitionName.asIndexName(), "123", "123", null, null, new Object[] { 0L, new BytesRef("Trillian") }); Plan plan = new IterablePlan(ctx.jobId(), updateNode); Job job = executor.newJob(plan); assertThat(job.tasks().get(0), instanceOf(SymbolBasedUpsertByIdTask.class)); List<? extends ListenableFuture<TaskResult>> result = executor.execute(job); TaskResult taskResult = result.get(0).get(); Bucket indexResult = taskResult.rows(); assertThat(indexResult, contains(isRow(1L))); refresh(); assertTrue(client().admin().indices().prepareExists(partitionName.asIndexName()).execute().actionGet() .isExists()); assertTrue(client().admin().indices().prepareAliasesExist("parted").execute().actionGet().exists()); SearchHits hits = client().prepareSearch(partitionName.asIndexName()) .setTypes(Constants.DEFAULT_MAPPING_TYPE).addFields("id", "name") .setQuery(new MapBuilder<String, Object>().put("match_all", new HashMap<String, Object>()).map()) .execute().actionGet().getHits(); assertThat(hits.getTotalHits(), is(1L)); assertThat((Integer) hits.getHits()[0].field("id").getValues().get(0), is(0)); assertThat((String) hits.getHits()[0].field("name").getValues().get(0), is("Trillian")); }
From source file:io.crate.executor.transport.TransportExecutorUpsertTest.java
License:Apache License
@Test public void testInsertMultiValuesWithSymbolBasedUpsertByIdTask() throws Exception { execute("create table characters (id int primary key, name string)"); ensureGreen();//from w w w. j a v a2 s . c o m /* insert into characters (id, name) values (99, 'Marvin'), (42, 'Deep Thought'); */ Planner.Context ctx = newPlannerContext(); SymbolBasedUpsertByIdNode updateNode = new SymbolBasedUpsertByIdNode(ctx.nextExecutionPhaseId(), false, false, null, new Reference[] { idRef, nameRef }); updateNode.add("characters", "99", "99", null, null, new Object[] { 99, new BytesRef("Marvin") }); updateNode.add("characters", "42", "42", null, null, new Object[] { 42, new BytesRef("Deep Thought") }); Plan plan = new IterablePlan(ctx.jobId(), updateNode); Job job = executor.newJob(plan); assertThat(job.tasks().get(0), instanceOf(SymbolBasedUpsertByIdTask.class)); List<? extends ListenableFuture<TaskResult>> result = executor.execute(job); TaskResult taskResult = result.get(0).get(); Bucket rows = taskResult.rows(); assertThat(rows, contains(isRow(2L))); // verify insertion ImmutableList<Symbol> outputs = ImmutableList.<Symbol>of(idRef, nameRef); ESGetNode getNode = newGetNode("characters", outputs, Arrays.asList("99", "42"), ctx.nextExecutionPhaseId()); plan = new IterablePlan(UUID.randomUUID(), getNode); job = executor.newJob(plan); result = executor.execute(job); Bucket objects = result.get(0).get().rows(); assertThat(objects, contains(isRow(99, "Marvin"), isRow(42, "Deep Thought"))); }
From source file:io.crate.executor.transport.TransportExecutorUpsertTest.java
License:Apache License
@Test public void testInsertOnDuplicateWithSymbolBasedUpsertByIdTask() throws Exception { setup.setUpCharacters();/* ww w . ja va2 s.c o m*/ /* insert into characters (id, name, female) values (5, 'Zaphod Beeblebrox', false) on duplicate key update set name = 'Zaphod Beeblebrox'; */ Object[] missingAssignments = new Object[] { 5, new BytesRef("Zaphod Beeblebrox"), false }; Planner.Context ctx = newPlannerContext(); SymbolBasedUpsertByIdNode updateNode = new SymbolBasedUpsertByIdNode(ctx.nextExecutionPhaseId(), false, false, new String[] { nameRef.ident().columnIdent().fqn() }, new Reference[] { idRef, nameRef, femaleRef }); updateNode.add("characters", "5", "5", new Symbol[] { Literal.newLiteral("Zaphod Beeblebrox") }, null, missingAssignments); Plan plan = new IterablePlan(UUID.randomUUID(), updateNode); Job job = executor.newJob(plan); assertThat(job.tasks().get(0), instanceOf(SymbolBasedUpsertByIdTask.class)); List<? extends ListenableFuture<TaskResult>> result = executor.execute(job); TaskResult taskResult = result.get(0).get(); Bucket rows = taskResult.rows(); assertThat(rows, contains(isRow(1L))); // verify insert ImmutableList<Symbol> outputs = ImmutableList.<Symbol>of(idRef, nameRef, femaleRef); ESGetNode getNode = newGetNode("characters", outputs, "5", ctx.nextExecutionPhaseId()); plan = new IterablePlan(UUID.randomUUID(), getNode); job = executor.newJob(plan); result = executor.execute(job); Bucket objects = result.get(0).get().rows(); assertThat(objects, contains(isRow(5, "Zaphod Beeblebrox", false))); }