Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,13 @@
<version>${project.version}</version>
</dependency>

<dependency>
<dependency>
<groupId>org.apache.beam</groupId>
<artifactId>beam-sdks-java-io-solr</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.apache.beam</groupId>
<artifactId>beam-sdks-java-io-hadoop-input-format</artifactId>
<version>${project.version}</version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ public interface IOTestPipelineOptions extends TestPipelineOptions {
Integer getElasticsearchHttpPort();
void setElasticsearchHttpPort(Integer value);

/* Solr */
@Description("Address of Zookeeper server for Solr")
@Default.String("zookeeper-server")
String getSolrZookeeperServer();
void setSolrZookeeperServer(String value);

/* Cassandra */
@Description("Host for Cassandra server (host name/ip address)")
@Default.String("cassandra-host")
Expand Down
1 change: 1 addition & 0 deletions sdks/java/io/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<module>kinesis</module>
<module>mongodb</module>
<module>mqtt</module>
<module>solr</module>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the module too in the parent pom (for reference for the javadoc) and in the sdks/java/javadoc/pom.xml so the doc would be also part of the release.

<module>xml</module>
</modules>

Expand Down
141 changes: 141 additions & 0 deletions sdks/java/io/solr/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>beam-sdks-java-io-parent</artifactId>
<groupId>org.apache.beam</groupId>
<version>2.2.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>beam-sdks-java-io-solr</artifactId>
<name>Apache Beam :: SDKs :: Java :: IO :: Solr</name>
<description>IO to read and write from/to Solr.</description>

<dependencies>
<dependency>
<groupId>org.apache.beam</groupId>
<artifactId>beam-sdks-java-core</artifactId>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>

<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>5.5.4</version>
</dependency>

<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
</dependency>

<!-- compile dependencies -->
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.4.1</version>
<scope>provided</scope>
</dependency>

<!-- test -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.beam</groupId>
<artifactId>beam-sdks-java-io-common</artifactId>
<scope>test</scope>
<classifier>tests</classifier>
</dependency>

<dependency>
<groupId>org.apache.beam</groupId>
<artifactId>beam-runners-direct-java</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-test-framework</artifactId>
<version>5.5.4</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-core</artifactId>
<version>5.5.4</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.carrotsearch.randomizedtesting</groupId>
<artifactId>randomizedtesting-runner</artifactId>
<version>2.3.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.beam.sdk.io.solr;

import static com.google.common.base.Preconditions.checkArgument;

import java.io.Closeable;
import java.io.IOException;

import org.apache.beam.sdk.io.solr.SolrIO.ConnectionConfiguration;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.SolrResponse;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CloudSolrClient;
import org.apache.solr.client.solrj.request.CollectionAdminRequest;
import org.apache.solr.client.solrj.request.CoreAdminRequest;
import org.apache.solr.client.solrj.request.QueryRequest;
import org.apache.solr.client.solrj.response.CoreAdminResponse;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.cloud.ClusterState;
import org.apache.solr.common.params.SolrParams;

/**
* Client for interact with Solr.
* @param <ClientT> type of SolrClient
*/
class AuthorizedSolrClient<ClientT extends SolrClient> implements Closeable {
private final ClientT solrClient;
private final String username;
private final String password;

AuthorizedSolrClient(ClientT solrClient, ConnectionConfiguration configuration) {
checkArgument(
solrClient != null,
"solrClient can not be null");
checkArgument(
configuration != null,
"configuration can not be null");
this.solrClient = solrClient;
this.username = configuration.getUsername();
this.password = configuration.getPassword();
}

QueryResponse query(String collection, SolrParams solrParams)
throws IOException, SolrServerException {
QueryRequest query = new QueryRequest(solrParams);
return process(collection, query);
}

<ResponseT extends SolrResponse> ResponseT process(String collection,
SolrRequest<ResponseT> request) throws IOException, SolrServerException {
request.setBasicAuthCredentials(username, password);
return request.process(solrClient, collection);
}

CoreAdminResponse process(CoreAdminRequest request)
throws IOException, SolrServerException {
return process(null, request);
}

SolrResponse process(CollectionAdminRequest request)
throws IOException, SolrServerException {
return process(null, request);
}

static ClusterState getClusterState(
AuthorizedSolrClient<CloudSolrClient> authorizedSolrClient) {
authorizedSolrClient.solrClient.connect();
return authorizedSolrClient.solrClient.getZkStateReader().getClusterState();
}

@Override public void close() throws IOException {
solrClient.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.beam.sdk.io.solr;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.beam.sdk.coders.AtomicCoder;
import org.apache.beam.sdk.coders.Coder;
import org.apache.beam.sdk.coders.CoderException;
import org.apache.beam.sdk.util.VarInt;
import org.apache.commons.compress.utils.BoundedInputStream;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.util.JavaBinCodec;

/**
* A {@link Coder} that encodes {@link SolrDocument SolrDocument}.
*/
class SolrDocumentCoder extends AtomicCoder<SolrDocument> {

private static final SolrDocumentCoder INSTANCE = new SolrDocumentCoder();

public static SolrDocumentCoder of() {
return INSTANCE;
}

@Override
public void encode(SolrDocument value, OutputStream outStream)
throws IOException {
if (value == null) {
throw new CoderException("cannot encode a null SolrDocument");
}

ByteArrayOutputStream baos = new ByteArrayOutputStream();
JavaBinCodec codec = new JavaBinCodec();
codec.marshal(value, baos);

byte[] bytes = baos.toByteArray();
VarInt.encode(bytes.length, outStream);
outStream.write(bytes);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about instead: codec.marshal(value, outStream) to avoid the copying of bytes, similarly to how you do it in decode (except this time you don't even need a wrapper)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, how can I know ahead the length of bytes?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed; nevermind then.

}

@Override
public SolrDocument decode(InputStream inStream) throws IOException {
DataInputStream in = new DataInputStream(inStream);

int len = VarInt.decodeInt(in);
if (len < 0) {
throw new CoderException("Invalid encoded SolrDocument length: " + len);
}

JavaBinCodec codec = new JavaBinCodec();
return (SolrDocument) codec.unmarshal(new BoundedInputStream(in, len));
}
}
Loading