MongoDB数据导入hbase + 代码
需求: 从mongoDB里面查出来数据,判断是否有该列簇,如果有则导入此条数据+列簇,如果没有,则该条数据不包含该列簇 直接贴出代码: package Test; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Set; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.util.Bytes; import org.bson.Document; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; import com.mongodb.ServerAddress; import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoCursor; import com.mongodb.client.MongoDatabase; public class OperateTable2 { private static Configuration 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 void addRow(String tableName, String row, String columnFamily, String column, String value) throws Exception { HTable hTable = new HTable(conf, tableName); Put put = new Put(Bytes.toBytes(row)); // 参数出分别:列族、列、值 put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value)); hTable.put(put); } public static void main(String[] args) { // 声明静态配置 String tableName = "house"; String columnFamilys = "info"; int a = 0; try { ServerAddress serverAddress = new ServerAddress("42.62.66.9",27017); List addrs = new ArrayList(); addrs.add(serverAddress); MongoCredential credential = MongoCredential.createScramSha1Credential("admin", "admin", "bigmaster654321".toCharArray()); List credentials = new ArrayList(); credentials.add(credential); //通过认证获取mongoDB的连接 MongoClient mongoClient = new MongoClient(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; Document 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 exception System.err.println(e.getClass().getName()+ ":" +e.getMessage()); } } } 我这里是用_id为hbase的rowkey,列为info,代码无误,根据需求可以改动,关键在于两个while判断的地方,那里最容易出错,导入mysql也可以用此代码改动取值的地方和jdbc连接进行导入 需要用到的包:mongo-java-driver-3.2.2.jar,hbase常用的包可以全导 代码如上,原创转载注明出处!
转载注明出处
如果本文对你有帮助,请帮忙啦~~
打开支付宝首页搜“522901509”领红包,领到大红包的小伙伴赶紧使用哦!