Example usage for org.apache.solr.client.solrj.impl CloudSolrClient getById

List of usage examples for org.apache.solr.client.solrj.impl CloudSolrClient getById

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj.impl CloudSolrClient getById.

Prototype

public SolrDocument getById(String collection, String id) throws SolrServerException, IOException 

Source Link

Document

Retrieves the SolrDocument associated with the given identifier.

Usage

From source file:fr.jetoile.hadoopunit.component.SolrCloudBootstrapTest.java

License:Apache License

@Test
public void solrCloudShouldStart() throws IOException, SolrServerException, KeeperException,
        InterruptedException, NotFoundServiceException {

    String collectionName = configuration.getString(SolrCloudBootstrap.SOLR_COLLECTION_NAME);

    //        String zkHostString = configuration.getString(Config.ZOOKEEPER_HOST_KEY) + ":" + configuration.getInt(Config.ZOOKEEPER_PORT_KEY);
    //        CloudSolrClient client = new CloudSolrClient(zkHostString);
    CloudSolrClient client = ((SolrCloudBootstrap) HadoopBootstrap.INSTANCE.getService(Component.SOLRCLOUD))
            .getClient();/*from   w w  w  . ja  v a2  s .  co m*/

    for (int i = 0; i < 1000; ++i) {
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("cat", "book");
        doc.addField("id", "book-" + i);
        doc.addField("name", "The Legend of the Hobbit part " + i);
        client.add(collectionName, doc);
        if (i % 100 == 0)
            client.commit(collectionName); // periodically flush
    }
    client.commit("collection1");

    SolrDocument collection1 = client.getById(collectionName, "book-1");

    assertNotNull(collection1);

    assertThat(collection1.getFieldValue("name")).isEqualTo("The Legend of the Hobbit part 1");

    client.close();
}

From source file:fr.jetoile.hadoopunit.integrationtest.IntegrationBootstrapTest.java

License:Apache License

@Test
public void solrCloudShouldStart()
        throws IOException, SolrServerException, KeeperException, InterruptedException {

    String collectionName = configuration.getString(SolrCloudBootstrap.SOLR_COLLECTION_NAME);

    String zkHostString = configuration.getString(Config.ZOOKEEPER_HOST_KEY) + ":"
            + configuration.getInt(Config.ZOOKEEPER_PORT_KEY);
    CloudSolrClient client = new CloudSolrClient(zkHostString);

    for (int i = 0; i < 1000; ++i) {
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("cat", "book");
        doc.addField("id", "book-" + i);
        doc.addField("name", "The Legend of the Hobbit part " + i);
        client.add(collectionName, doc);
        if (i % 100 == 0)
            client.commit(collectionName); // periodically flush
    }// ww w .  j  av a  2 s . c  o m
    client.commit("collection1");

    SolrDocument collection1 = client.getById(collectionName, "book-1");

    assertNotNull(collection1);

    assertThat(collection1.getFieldValue("name")).isEqualTo("The Legend of the Hobbit part 1");

    client.close();
}

From source file:fr.jetoile.hadoopunit.integrationtest.SparkSolrIntegrationTest.java

License:Apache License

@Test
public void spark_should_read_parquet_file_and_index_into_solr() throws IOException, SolrServerException {
    //given//  ww w  .ja  va2 s.com
    SparkConf conf = new SparkConf().setMaster("local[*]").setAppName("test");

    JavaSparkContext context = new JavaSparkContext(conf);

    //read hive-site from classpath
    HiveContext hiveContext = new HiveContext(context.sc());

    DataFrame sql = hiveContext.sql("SELECT * FROM default.test");
    sql.write().parquet("hdfs://localhost:" + configuration.getInt(Config.HDFS_NAMENODE_PORT_KEY)
            + "/khanh/test_parquet/file.parquet");

    FileSystem fileSystem = HdfsUtils.INSTANCE.getFileSystem();
    assertThat(fileSystem.exists(new Path("hdfs://localhost:"
            + configuration.getInt(Config.HDFS_NAMENODE_PORT_KEY) + "/khanh/test_parquet/file.parquet")))
                    .isTrue();

    context.close();

    //when
    context = new JavaSparkContext(conf);
    SQLContext sqlContext = new SQLContext(context);

    DataFrame file = sqlContext.read().parquet("hdfs://localhost:"
            + configuration.getInt(Config.HDFS_NAMENODE_PORT_KEY) + "/khanh/test_parquet/file.parquet");
    DataFrame select = file.select("id", "value");

    JavaRDD<SolrInputDocument> solrInputDocument = select.toJavaRDD().map(r -> {
        SolrInputDocument solr = new SolrInputDocument();
        solr.setField("id", r.getInt(0));
        solr.setField("value_s", r.getString(1));
        return solr;
    });

    String collectionName = configuration.getString(SolrCloudBootstrap.SOLR_COLLECTION_NAME);
    String zkHostString = configuration.getString(Config.ZOOKEEPER_HOST_KEY) + ":"
            + configuration.getInt(Config.ZOOKEEPER_PORT_KEY);
    SolrSupport.indexDocs(zkHostString, collectionName, 1000, solrInputDocument);

    //then
    CloudSolrClient client = new CloudSolrClient(zkHostString);
    SolrDocument collection1 = client.getById(collectionName, "1");

    assertNotNull(collection1);
    assertThat(collection1.getFieldValue("value_s")).isEqualTo("value1");

    client.close();

    context.close();

}

From source file:fr.jetoile.hadoopunit.sample.ParquetToSolrJobIntegrationTest.java

License:Apache License

@Test
public void spark_should_read_parquet_file_and_index_into_solr() throws IOException, SolrServerException {
    //given//from ww w.j  a va  2  s.c o m
    SparkSession sqlContext = SparkSession.builder().appName("test").master("local[*]").getOrCreate();

    Dataset<Row> df = sqlContext.read().format("com.databricks.spark.csv").option("header", "true") // Use first line of all files as header
            .option("inferSchema", "true") // Automatically infer data types
            .load("hdfs://localhost:" + configuration.getInt(HadoopUnitClientConfig.HDFS_NAMENODE_PORT_KEY)
                    + "/khanh/test/test.csv");

    df.write().parquet("hdfs://localhost:" + configuration.getInt(HadoopUnitClientConfig.HDFS_NAMENODE_PORT_KEY)
            + "/khanh/test_parquet/file.parquet");

    FileSystem fileSystem = HdfsUtils.INSTANCE.getFileSystem();
    assertThat(fileSystem.exists(
            new Path("hdfs://localhost:" + configuration.getInt(HadoopUnitClientConfig.HDFS_NAMENODE_PORT_KEY)
                    + "/khanh/test_parquet/file.parquet"))).isTrue();

    sqlContext.close();

    //when
    sqlContext = SparkSession.builder().appName("test").master("local[*]").getOrCreate();

    ParquetToSolrJob parquetToSolrJob = new ParquetToSolrJob(sqlContext);
    parquetToSolrJob.run();

    String zkHostString = configuration.getString("zookeeper.host") + ":"
            + configuration.getInt("zookeeper.port");

    //then
    CloudSolrClient client = new CloudSolrClient(zkHostString);
    SolrDocument collection1 = client.getById("collection1", "1");

    assertNotNull(collection1);
    assertThat(collection1.getFieldValue("value_s")).isEqualTo("value1");

    client.close();

    sqlContext.close();

}

From source file:fr.jetoile.hadoopunit.sample.ParquetToSolrJobTest.java

License:Apache License

@Test
public void spark_should_read_parquet_file_and_index_into_solr() throws IOException, SolrServerException {
    //given/*from   w  w  w  .ja v a 2s  .  co m*/
    SparkConf conf = new SparkConf().setMaster("local[*]").setAppName("test");

    JavaSparkContext context = new JavaSparkContext(conf);

    //read hive-site from classpath
    HiveContext hiveContext = new HiveContext(context.sc());

    DataFrame sql = hiveContext.sql("SELECT * FROM default.test");
    sql.write().parquet("hdfs://localhost:" + configuration.getInt(HadoopUnitConfig.HDFS_NAMENODE_PORT_KEY)
            + "/khanh/test_parquet/file.parquet");

    FileSystem fileSystem = HdfsUtils.INSTANCE.getFileSystem();
    assertThat(fileSystem
            .exists(new Path("hdfs://localhost:" + configuration.getInt(HadoopUnitConfig.HDFS_NAMENODE_PORT_KEY)
                    + "/khanh/test_parquet/file.parquet"))).isTrue();

    context.close();

    //when
    context = new JavaSparkContext(conf);

    ParquetToSolrJob parquetToSolrJob = new ParquetToSolrJob(context);
    parquetToSolrJob.run();

    String zkHostString = configuration.getString(HadoopUnitConfig.ZOOKEEPER_HOST_KEY) + ":"
            + configuration.getInt(HadoopUnitConfig.ZOOKEEPER_PORT_KEY);

    //then
    CloudSolrClient client = new CloudSolrClient(zkHostString);
    SolrDocument collection1 = client.getById("collection1", "1");

    assertNotNull(collection1);
    assertThat(collection1.getFieldValue("value_s")).isEqualTo("value1");

    client.close();

    context.close();

}