In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Here, the editor will first provide you with the dependence on pom.xml: (modify it according to your own version)
UTF-8 1.7 1.7 2.3.2 junit junit 4.10 org.elasticsearch.client transport 6.2.0 org.scala-lang scala-library 2.10. 3 org.json json 20180813 org.elasticsearch elasticsearch-hadoop 6.2.4 org.apache.spark spark-core_2.11 ${spark.version} org. Apache.spark spark-sql_2.11 ${spark.version} 1. Create a programming entry for ES
mainly provides a Utils, which is a programming entry to create an ES by reading the configuration file.
# elasticSearch.conf
Elastic.host=192.168.130.131elastic.port=9300elastic.cluster.name=zzy-application
# Constants
Public interface Constants {String ELASTIC_HOST = "elastic.host"; String ELASTIC_PORT= "elastic.port"; String ELASTIC_CLUSTER_NAME = "elastic.cluster.name";}
# ElasticSearchUtil
Import com.zy.es.constant.Constants;import org.elasticsearch.client.transport.TransportClient;import org.elasticsearch.common.settings.Setting;import org.elasticsearch.common.settings.Settings;import org.elasticsearch.common.transport.TransportAddress;import org.elasticsearch.transport.client.PreBuiltTransportClient;import java.io.IOException;import java.io.InputStream;import java.net.InetAddress;import java.util.Properties / * in general, utility classes are singletons * some of the methods are generally static * if the cluster name does not match when connecting to the cluster: * NoNodeAvailableException [None of the configured nodes are available: * / public class ElasticSearchUtil {private static TransportClient client; private static Properties ps; static {InputStream resourceAsStream = ElasticSearchUtil.class.getClassLoader (). GetResourceAsStream ("elasticsearch.conf") Ps = new Properties (); ps.load (resourceAsStream); String host=ps.getProperty (Constants.ELASTIC_HOST); int port = Integer.parseInt (ps.getProperty (Constants.ELASTIC_PORT)); String clusterName=ps.getProperty (Constants.ELASTIC_CLUSTER_NAME) Settings settings = Settings.builder () .put ("cluster.name", clusterName) .build (); client=new PreBuiltTransportClient (settings); / / there can be multiple, cluster mode TransportAddress ta=new TransportAddress (InetAddress.getByName (host), port) / / addTransportAddresses (TransportAddress... TransportAddress), the parameter is a variable parameter client.addTransportAddresses (ta);} catch (IOException e) {e.printStackTrace ();}} public static TransportClient getTransportClient () {return client;} public static void close (TransportClient client) {if (clientproof null) {client.close ();}} 2. Create an index
editor here provides four creation methods: json, map, javabean and XContentBuilder.
Import java.utilimport com.zy.es.pojo.Bookimport com.zy.es.utils.ElasticSearchUtilimport org.elasticsearch.action.index.IndexResponseimport org.elasticsearch.cluster.metadata.MetaData.XContentContextimport org.elasticsearch.common.xcontent. {XContentBuilder XContentType} import org.elasticsearch.common.xcontent.json.JsonXContentimport org.json.JSONObjectobject createIndex {private var index= "library" private var `type` = "books" privateval client = ElasticSearchUtil.getTransportClient () def main (args: Array [String]): Unit = {createIndexByJson () / / createIndexByMap () / / createIndexByBean () / / createIndexByXContentBuilder () / / close es connection object ElasticSearchUtil.close (client)} / * 1. Create * java.lang.IllegalArgumentException: The number of object passed must be even but was [1] * above es5.x using XContentType.JSON to make * setSource (json.toString (), XContentType.JSON) must specify the second parameter. * / def createIndexByJson () = {val json=new JSONObject json.put ("name", "I love you China") json.put ("author", "Zhou Xun") json.put ("date", "2018-6-6") / / return the created result var response: IndexResponse = client.prepareIndex (index, `type`, "9") .setSource (json.toString XContentType.JSON) .get () / / View version println (response.getVersion)} / * 2.map mode * / def createIndexByMap (): Unit = {val sourceMap=new util.HashMap [String,String] () sourceMap.put ("name", "picking up flowers at night") sourceMap.put ("author", "Lu Xun") sourceMap.put ("date", "2009-4-5") var response: IndexResponse = client.prepareIndex (index, `type` "2") .setSource (sourceMap) .get () / / View version println (response.getVersion)} / * 3. Use ordinary javabean * / def createIndexByBean () = {val book:Book=new Book ("fight through the sky", "Bombyx mori potato", "2012-2-6") Val json=new JSONObject (book) / / returns the created result var response: IndexResponse = client.prepareIndex (index, `type`, "3") .setSource (json.toString, XContentType.JSON). Get () / View version println (response.getVersion)} / * * 4.XContentBuilder mode * / def createIndexByXContentBuilder () = {var builder: XContentBuilder = JsonXContent.contentBuilder () builder.startObject () .field ("name") "Journey to the West") .field ("author", "Wu Chengen") .field ("version", "1.0") .endObject () var response: IndexResponse = client.prepareIndex (index, `type`, "4") .setSource (builder) .get () println (response.getVersion)}} 3. Delete data & update data & batch processing
editor here provides delete data, update data, batch operation.
Import java.utilimport com.zy.es.utils.ElasticSearchUtilimport org.elasticsearch.action.bulk.BulkResponseimport org.elasticsearch.action.delete.DeleteResponseimport org.elasticsearch.action.update. {UpdateRequestBuilder, UpdateResponse} import org.elasticsearch.common.xcontent. {XContentBuilder XContentType} import org.elasticsearch.common.xcontent.json.JsonXContentimport org.json.JSONObjectobject ElasticsearchCRUD {private var index= "library" private var `type` = "books" privateval client = ElasticSearchUtil.getTransportClient () def main (args: Array [String]): Unit = {/ / delete data testDelete () / update / / testUpdate () / / batch operation / / testBulk () / / close connection object ElasticSearchUtil.close (client)} / / delete data def testDelete () = {var response: DeleteResponse = client.prepareDelete (index `type`, "2"). Get () println ("version:" + response.getVersion)} / / Update def testUpdate () = {var builder: XContentBuilder = JsonXContent.contentBuilder () builder.startObject () .field ("version", "3.0") .endObject () var response: UpdateResponse = client.prepareUpdate (index, `type` "4") .setDoc (builder) .get () println ("version:" + response.getVersion)} / / bulk operation def testBulk () = {val map=new util.HashMap [String,String] () map.put ("name", "unparalleled") map.put ("author", "Chow Yun Fat") map.put ("version", "2") val json=new JSONObject json.put ("name") "A Dream of Red Mansions") json.put ("author", "Cao Xueqin") json.put ("version", "1.0") var responses: BulkResponse = client.prepareBulk (). Add (client.prepareIndex (index, `type`, "7") .setSource (map) .add (client.prepareIndex (index, `type`, "8") .setSource (json.toString (), XContentType.JSON)) .get () for (response)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.