[elasticsearch] Checkstyle updates for Elasticsearch.

This commit is contained in:
Robert J. Moore 2015-11-08 14:15:13 -05:00
Родитель 92c69c55bb
Коммит bcae000502
3 изменённых файлов: 316 добавлений и 247 удалений

Просмотреть файл

@ -31,13 +31,6 @@ LICENSE file.
<properties>
<elasticsearch-version>0.19.8</elasticsearch-version>
</properties>
<repositories>
<repository>
<id>sonatype-nexus-snapshots</id>
<name>Sonatype releases</name>
<url>https://oss.sonatype.org/content/repositories/releases</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.yahoo.ycsb</groupId>
@ -57,4 +50,28 @@ LICENSE file.
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.15</version>
<configuration>
<consoleOutput>true</consoleOutput>
<configLocation>../checkstyle.xml</configLocation>
<failOnViolation>true</failOnViolation>
<failsOnError>true</failsOnError>
</configuration>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<goals>
<goal>checkstyle</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Просмотреть файл

@ -17,7 +17,7 @@
package com.yahoo.ycsb.db;
import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
import static org.elasticsearch.common.settings.ImmutableSettings.*;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.FilterBuilders.rangeFilter;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
@ -50,267 +50,296 @@ import java.util.Vector;
/**
* ElasticSearch client for YCSB framework.
*
* <p>Default properties to set:</p> <ul> <li>es.cluster.name = es.ycsb.cluster
* <li>es.client = true <li>es.index.key = es.ycsb</ul>
* <p>
* Default properties to set:
* </p>
* <ul>
* <li>es.cluster.name = es.ycsb.cluster
* <li>es.client = true
* <li>es.index.key = es.ycsb
* </ul>
*
* @author Sharmarke Aden
*
*/
public class ElasticSearchClient extends DB {
public static final String DEFAULT_CLUSTER_NAME = "es.ycsb.cluster";
public static final String DEFAULT_INDEX_KEY = "es.ycsb";
public static final String DEFAULT_REMOTE_HOST = "localhost:9300";
private Node node;
private Client client;
private String indexKey;
public static final String DEFAULT_CLUSTER_NAME = "es.ycsb.cluster";
public static final String DEFAULT_INDEX_KEY = "es.ycsb";
public static final String DEFAULT_REMOTE_HOST = "localhost:9300";
private Node node;
private Client client;
private String indexKey;
private Boolean remoteMode;
private Boolean remoteMode;
/**
* Initialize any state for this DB. Called once per DB instance; there is
* one DB instance per client thread.
*/
@Override
public void init() throws DBException {
// initialize OrientDB driver
Properties props = getProperties();
this.indexKey = props.getProperty("es.index.key", DEFAULT_INDEX_KEY);
String clusterName = props.getProperty("cluster.name", DEFAULT_CLUSTER_NAME);
//Check if transport client needs to be used (To connect to multiple elasticsearch nodes)
remoteMode = Boolean.parseBoolean(props.getProperty("elasticsearch.remote", "false"));
Boolean newdb = Boolean.parseBoolean(props.getProperty("elasticsearch.newdb", "false"));
Builder settings = settingsBuilder()
.put("node.local", "true")
.put("path.data", System.getProperty("java.io.tmpdir") + "/esdata")
.put("discovery.zen.ping.multicast.enabled", "false")
.put("index.mapping._id.indexed", "true")
.put("index.gateway.type", "none")
.put("gateway.type", "none")
.put("index.number_of_shards", "1")
.put("index.number_of_replicas", "0");
/**
* Initialize any state for this DB. Called once per DB instance; there is one
* DB instance per client thread.
*/
@Override
public void init() throws DBException {
// initialize OrientDB driver
Properties props = getProperties();
this.indexKey = props.getProperty("es.index.key", DEFAULT_INDEX_KEY);
String clusterName =
props.getProperty("cluster.name", DEFAULT_CLUSTER_NAME);
// Check if transport client needs to be used (To connect to multiple
// elasticsearch nodes)
remoteMode = Boolean
.parseBoolean(props.getProperty("elasticsearch.remote", "false"));
Boolean newdb =
Boolean.parseBoolean(props.getProperty("elasticsearch.newdb", "false"));
Builder settings = settingsBuilder().put("node.local", "true")
.put("path.data", System.getProperty("java.io.tmpdir") + "/esdata")
.put("discovery.zen.ping.multicast.enabled", "false")
.put("index.mapping._id.indexed", "true")
.put("index.gateway.type", "none").put("gateway.type", "none")
.put("index.number_of_shards", "1")
.put("index.number_of_replicas", "0");
// if properties file contains elasticsearch user defined properties
// add it to the settings file (will overwrite the defaults).
settings.put(props);
System.out.println(
"ElasticSearch starting node = " + settings.get("cluster.name"));
System.out
.println("ElasticSearch node data path = " + settings.get("path.data"));
System.out.println("ElasticSearch Remote Mode = " + remoteMode);
// Remote mode support for connecting to remote elasticsearch cluster
if (remoteMode) {
settings.put("client.transport.sniff", true)
.put("client.transport.ignore_cluster_name", false)
.put("client.transport.ping_timeout", "30s")
.put("client.transport.nodes_sampler_interval", "30s");
// Default it to localhost:9300
String[] nodeList =
props.getProperty("elasticsearch.hosts.list", DEFAULT_REMOTE_HOST)
.split(",");
System.out.println("ElasticSearch Remote Hosts = "
+ props.getProperty("elasticsearch.hosts.list", DEFAULT_REMOTE_HOST));
TransportClient tClient = new TransportClient(settings);
for (String h : nodeList) {
String[] nodes = h.split(":");
tClient.addTransportAddress(
new InetSocketTransportAddress(nodes[0],
Integer.parseInt(nodes[1])));
}
client = tClient;
} else { // Start node only if transport client mode is disabled
node = nodeBuilder().clusterName(clusterName).settings(settings).node();
node.start();
client = node.client();
}
//if properties file contains elasticsearch user defined properties
//add it to the settings file (will overwrite the defaults).
settings.put(props);
System.out.println("ElasticSearch starting node = " + settings.get("cluster.name"));
System.out.println("ElasticSearch node data path = " + settings.get("path.data"));
System.out.println("ElasticSearch Remote Mode = " +remoteMode);
//Remote mode support for connecting to remote elasticsearch cluster
if(remoteMode) {
settings.put("client.transport.sniff", true)
.put("client.transport.ignore_cluster_name", false)
.put("client.transport.ping_timeout", "30s")
.put("client.transport.nodes_sampler_interval", "30s");
//Default it to localhost:9300
String nodeList[] = props.getProperty("elasticsearch.hosts.list", DEFAULT_REMOTE_HOST).split(",");
System.out.println("ElasticSearch Remote Hosts = " +props.getProperty("elasticsearch.hosts.list", DEFAULT_REMOTE_HOST));
TransportClient tClient = new TransportClient(settings);
for(String h : nodeList) {
String node[] = h.split(":");
tClient.addTransportAddress(new InetSocketTransportAddress(node[0], Integer.parseInt(node[1])));
}
client = tClient;
} else { //Start node only if transport client mode is disabled
node = nodeBuilder().clusterName(clusterName).settings(settings).node();
node.start();
client = node.client();
}
if (newdb) {
client.admin().indices().prepareDelete(indexKey).execute().actionGet();
client.admin().indices().prepareCreate(indexKey).execute().actionGet();
} else {
boolean exists = client.admin().indices()
.exists(Requests.indicesExistsRequest(indexKey)).actionGet()
.isExists();
if (!exists) {
client.admin().indices().prepareCreate(indexKey).execute().actionGet();
}
}
}
@Override
public void cleanup() throws DBException {
if (!remoteMode) {
if (!node.isClosed()) {
client.close();
node.stop();
node.close();
}
} else {
client.close();
}
}
if (newdb) {
client.admin().indices().prepareDelete(indexKey).execute().actionGet();
client.admin().indices().prepareCreate(indexKey).execute().actionGet();
/**
* Insert a record in the database. Any field/value pairs in the specified
* values HashMap will be written into the record with the specified record
* key.
*
* @param table
* The name of the table
* @param key
* The record key of the record to insert.
* @param values
* A HashMap of field/value pairs to insert in the record
* @return Zero on success, a non-zero error code on error. See this class's
* description for a discussion of error codes.
*/
@Override
public Status insert(String table, String key,
HashMap<String, ByteIterator> values) {
try {
final XContentBuilder doc = jsonBuilder().startObject();
for (Entry<String, String> entry : StringByteIterator.getStringMap(values)
.entrySet()) {
doc.field(entry.getKey(), entry.getValue());
}
doc.endObject();
client.prepareIndex(indexKey, table, key).setSource(doc).execute()
.actionGet();
return Status.OK;
} catch (Exception e) {
e.printStackTrace();
}
return Status.ERROR;
}
/**
* Delete a record from the database.
*
* @param table
* The name of the table
* @param key
* The record key of the record to delete.
* @return Zero on success, a non-zero error code on error. See this class's
* description for a discussion of error codes.
*/
@Override
public Status delete(String table, String key) {
try {
client.prepareDelete(indexKey, table, key).execute().actionGet();
return Status.OK;
} catch (Exception e) {
e.printStackTrace();
}
return Status.ERROR;
}
/**
* Read a record from the database. Each field/value pair from the result will
* be stored in a HashMap.
*
* @param table
* The name of the table
* @param key
* The record key of the record to read.
* @param fields
* The list of fields to read, or null for all of them
* @param result
* A HashMap of field/value pairs for the result
* @return Zero on success, a non-zero error code on error or "not found".
*/
@Override
public Status read(String table, String key, Set<String> fields,
HashMap<String, ByteIterator> result) {
try {
final GetResponse response =
client.prepareGet(indexKey, table, key).execute().actionGet();
if (response.isExists()) {
if (fields != null) {
for (String field : fields) {
result.put(field, new StringByteIterator(
(String) response.getSource().get(field)));
}
} else {
boolean exists = client.admin().indices().exists(Requests.indicesExistsRequest(indexKey)).actionGet().isExists();
if (!exists) {
client.admin().indices().prepareCreate(indexKey).execute().actionGet();
}
for (String field : response.getSource().keySet()) {
result.put(field, new StringByteIterator(
(String) response.getSource().get(field)));
}
}
return Status.OK;
}
} catch (Exception e) {
e.printStackTrace();
}
return Status.ERROR;
}
@Override
public void cleanup() throws DBException {
if(!remoteMode) {
if (!node.isClosed()) {
client.close();
node.stop();
node.close();
}
} else {
client.close();
/**
* Update a record in the database. Any field/value pairs in the specified
* values HashMap will be written into the record with the specified record
* key, overwriting any existing values with the same field name.
*
* @param table
* The name of the table
* @param key
* The record key of the record to write.
* @param values
* A HashMap of field/value pairs to update in the record
* @return Zero on success, a non-zero error code on error. See this class's
* description for a discussion of error codes.
*/
@Override
public Status update(String table, String key,
HashMap<String, ByteIterator> values) {
try {
final GetResponse response =
client.prepareGet(indexKey, table, key).execute().actionGet();
if (response.isExists()) {
for (Entry<String, String> entry : StringByteIterator
.getStringMap(values).entrySet()) {
response.getSource().put(entry.getKey(), entry.getValue());
}
client.prepareIndex(indexKey, table, key)
.setSource(response.getSource()).execute().actionGet();
return Status.OK;
}
} catch (Exception e) {
e.printStackTrace();
}
return Status.ERROR;
}
/**
* Insert a record in the database. Any field/value pairs in the specified
* values HashMap will be written into the record with the specified record
* key.
*
* @param table The name of the table
* @param key The record key of the record to insert.
* @param values A HashMap of field/value pairs to insert in the record
* @return Zero on success, a non-zero error code on error. See this class's
* description for a discussion of error codes.
*/
@Override
public Status insert(String table, String key, HashMap<String, ByteIterator> values) {
try {
final XContentBuilder doc = jsonBuilder().startObject();
/**
* Perform a range scan for a set of records in the database. Each field/value
* pair from the result will be stored in a HashMap.
*
* @param table
* The name of the table
* @param startkey
* The record key of the first record to read.
* @param recordcount
* The number of records to read
* @param fields
* The list of fields to read, or null for all of them
* @param result
* A Vector of HashMaps, where each HashMap is a set field/value
* pairs for one record
* @return Zero on success, a non-zero error code on error. See this class's
* description for a discussion of error codes.
*/
@Override
public Status scan(String table, String startkey, int recordcount,
Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
try {
final RangeFilterBuilder filter = rangeFilter("_id").gte(startkey);
final SearchResponse response = client.prepareSearch(indexKey)
.setTypes(table).setQuery(matchAllQuery()).setFilter(filter)
.setSize(recordcount).execute().actionGet();
for (Entry<String, String> entry : StringByteIterator.getStringMap(values).entrySet()) {
doc.field(entry.getKey(), entry.getValue());
}
HashMap<String, ByteIterator> entry;
doc.endObject();
for (SearchHit hit : response.getHits()) {
entry = new HashMap<String, ByteIterator>(fields.size());
client.prepareIndex(indexKey, table, key)
.setSource(doc)
.execute()
.actionGet();
return Status.OK;
} catch (Exception e) {
e.printStackTrace();
for (String field : fields) {
entry.put(field,
new StringByteIterator((String) hit.getSource().get(field)));
}
return Status.ERROR;
}
/**
* Delete a record from the database.
*
* @param table The name of the table
* @param key The record key of the record to delete.
* @return Zero on success, a non-zero error code on error. See this class's
* description for a discussion of error codes.
*/
@Override
public Status delete(String table, String key) {
try {
client.prepareDelete(indexKey, table, key)
.execute()
.actionGet();
return Status.OK;
} catch (Exception e) {
e.printStackTrace();
}
return Status.ERROR;
}
/**
* Read a record from the database. Each field/value pair from the result
* will be stored in a HashMap.
*
* @param table The name of the table
* @param key The record key of the record to read.
* @param fields The list of fields to read, or null for all of them
* @param result A HashMap of field/value pairs for the result
* @return Zero on success, a non-zero error code on error or "not found".
*/
@Override
public Status read(String table, String key, Set<String> fields, HashMap<String, ByteIterator> result) {
try {
final GetResponse response = client.prepareGet(indexKey, table, key)
.execute()
.actionGet();
if (response.isExists()) {
if (fields != null) {
for (String field : fields) {
result.put(field, new StringByteIterator((String) response.getSource().get(field)));
}
} else {
for (String field : response.getSource().keySet()) {
result.put(field, new StringByteIterator((String) response.getSource().get(field)));
}
}
return Status.OK;
}
} catch (Exception e) {
e.printStackTrace();
}
return Status.ERROR;
}
/**
* Update a record in the database. Any field/value pairs in the specified
* values HashMap will be written into the record with the specified record
* key, overwriting any existing values with the same field name.
*
* @param table The name of the table
* @param key The record key of the record to write.
* @param values A HashMap of field/value pairs to update in the record
* @return Zero on success, a non-zero error code on error. See this class's
* description for a discussion of error codes.
*/
@Override
public Status update(String table, String key, HashMap<String, ByteIterator> values) {
try {
final GetResponse response = client.prepareGet(indexKey, table, key)
.execute()
.actionGet();
if (response.isExists()) {
for (Entry<String, String> entry : StringByteIterator.getStringMap(values).entrySet()) {
response.getSource().put(entry.getKey(), entry.getValue());
}
client.prepareIndex(indexKey, table, key)
.setSource(response.getSource())
.execute()
.actionGet();
return Status.OK;
}
} catch (Exception e) {
e.printStackTrace();
}
return Status.ERROR;
}
/**
* Perform a range scan for a set of records in the database. Each
* field/value pair from the result will be stored in a HashMap.
*
* @param table The name of the table
* @param startkey The record key of the first record to read.
* @param recordcount The number of records to read
* @param fields The list of fields to read, or null for all of them
* @param result A Vector of HashMaps, where each HashMap is a set
* field/value pairs for one record
* @return Zero on success, a non-zero error code on error. See this class's
* description for a discussion of error codes.
*/
@Override
public Status scan(String table, String startkey, int recordcount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
try {
final RangeFilterBuilder filter = rangeFilter("_id").gte(startkey);
final SearchResponse response = client.prepareSearch(indexKey)
.setTypes(table)
.setQuery(matchAllQuery())
.setFilter(filter)
.setSize(recordcount)
.execute()
.actionGet();
HashMap<String, ByteIterator> entry;
for (SearchHit hit : response.getHits()) {
entry = new HashMap<String, ByteIterator>(fields.size());
for (String field : fields) {
entry.put(field, new StringByteIterator((String) hit.getSource().get(field)));
}
result.add(entry);
}
return Status.OK;
} catch (Exception e) {
e.printStackTrace();
}
return Status.ERROR;
result.add(entry);
}
return Status.OK;
} catch (Exception e) {
e.printStackTrace();
}
return Status.ERROR;
}
}

Просмотреть файл

@ -0,0 +1,23 @@
/*
* Copyright (c) 2014, Yahoo!, Inc. All rights reserved.
*
* Licensed 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. See accompanying
* LICENSE file.
*/
/**
* The YCSB binding for
* <a href="https://www.elastic.co/products/elasticsearch">Elasticsearch</a>.
*/
package com.yahoo.ycsb.db;