MongoDB Data import HBase + code

Source: Internet
Author: User
Tags mongoclient set set string tags zookeeper

requirements: Identify the data from the MongoDB, determine if there is a cluster, if any, import this piece of data+A column cluster, if not, the data does not contain the column cluster to directly post the code: PackageTest;Importjava.util.ArrayList;ImportJava.util.Iterator;Importjava.util.List;ImportJava.util.Set;Importorg.apache.hadoop.conf.Configuration;Importorg.apache.hadoop.hbase.HBaseConfiguration;Importorg.apache.hadoop.hbase.client.HTable;ImportOrg.apache.hadoop.hbase.client.Put;Importorg.apache.hadoop.hbase.util.Bytes;Importorg.bson.Document;Importcom.mongodb.MongoClient;Importcom.mongodb.MongoCredential;Importcom.mongodb.ServerAddress;Importcom.mongodb.client.FindIterable;Importcom.mongodb.client.MongoCollection;ImportCom.mongodb.client.MongoCursor;Importcom.mongodb.client.MongoDatabase; Public classOperateTable2 {Private StaticConfiguration conf =NULL;Static{conf=hbaseconfiguration.create (); Conf.set ("Hbase.zookeeper.quorum", "Master.hadoop"); Conf.set ("Hbase.zookeeper.property.clientPort", "2181"); Conf.set ("Hbase.master", "master.hadoop:60000");} Public Static voidAddRow (String tableName, String row, String columnfamily, String column, String value)throwsException {htable htable=Newhtable (conf, tableName); Put put=NewPut (bytes.tobytes (row)); //parameter out: Column family, column, valuePut.add (Bytes.tobytes (columnfamily), bytes.tobytes (column), bytes.tobytes (value));     Htable.put (Put); } Public Static voidMain (string[] args) {//declaring a static configurationString tableName = "House"; String Columnfamilys= "Info"; intA = 0;Try{serveraddress serveraddress=NewServerAddress ("42.62.66.9", 27017); List Addrs=NewArrayList (); Addrs.add (serveraddress); Mongocredential Credential= Mongocredential.createscramsha1credential ("admin", "Admin", "bigmaster654321". ToCharArray ()); List Credentials=NewArrayList (); Credentials.add (credential);//obtaining MongoDB connections through authenticationMongoclient mongoclient =Newmongoclient (addrs,credentials); Mongodatabase Mongodatabase= Mongoclient.getdatabase ("House"); System.out.println ("MongoDB Connection-----------------successfully"); Mongocollection Collection= Mongodatabase.getcollection ("Houses2"); Finditerable finditerable=Collection.find (); Mongocursor Mongocursor=finditerable.iterator (); while(Mongocursor.hasnext ()) {a=a+1;D ocument Document=Mongocursor.next (); Set Set=Document.keyset (); Iterator it=set.iterator (); while(It.hasnext ()) {String tags=It.next ();if(Tags.equals ("_id")){ Continue; } Operatetable2.addrow (TableName, document.getstring ("_ID"), Columnfamilys, tags, document.getstring (tags));} System.out.println ("Insert into HBase Success" + "This is the" +a+ "data");} System.out.println ("Compelete All Insert");} Catch(Exception e) {//Todo:handle ExceptionSystem.err.println (E.getclass (). GetName () + ":" +e.getmessage ());}}} I am here with _id for HBase Rowkey, listed as info, the code is correct, according to the needs can be changed, the key lies in two while judgment, where the most error-prone, Import MySQL can also use this code to change the value of the place and the JDBC connection to import the required package: MONGO-java-driver-3.2.2. Jar,hbase commonly used packages can be fully guided code, such as the original reprint annotated source! 

MongoDB Data import HBase + code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.