-
Notifications
You must be signed in to change notification settings - Fork 4.5k
[BEAM-2657] Create Solr IO #3618
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
53318ca
6cf1777
5f96b95
fc4af7a
e31724d
0cf4fce
1a9c785
9361deb
a5a4862
90541d3
25f2801
4704c7a
f38ed6f
f0cc049
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, how can I know ahead the length of
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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.