需求:
从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常用的包可以全导 代码如上,原创转载注明出处!

MongoDB数据导入hbase + 代码的更多相关文章

  1. python 全栈开发,Day126(创业故事,软件部需求,内容采集,显示内容图文列表,MongoDB数据导入导出JSON)

    作业讲解 下载代码: HBuilder APP和flask后端登录 链接:https://pan.baidu.com/s/1eBwd1sVXTNLdHwKRM2-ytg 密码:4pcw 如何打开APP ...

  2. 软件部需求,内容采集,显示内容图文列表,MongoDB数据导入导出JSON

    全局变量 由于多个html页面,需要引用同一个变量.这个时候,需要定义一个全局变量!如何定义呢? 默认包含了mui的html文件都导入mui.js文件.那么将变量写在mui.js中,就可以实现所有页面 ...

  3. MapReduce将HDFS文本数据导入HBase中

    HBase本身提供了很多种数据导入的方式,通常有两种常用方式: 使用HBase提供的TableOutputFormat,原理是通过一个Mapreduce作业将数据导入HBase 另一种方式就是使用HB ...

  4. Sqoop将mysql数据导入hbase的血与泪

    Sqoop将mysql数据导入hbase的血与泪(整整搞了大半天)  版权声明:本文为yunshuxueyuan原创文章.如需转载请标明出处: https://my.oschina.net/yunsh ...

  5. 通过logstash-input-mongodb插件将mongodb数据导入ElasticSearch

    目的很简单,就是将mongodb数据导入es建立相应索引.数据是从特定的网站扒下来,然后进行二次处理,也就是数据去重.清洗,接着再保存到mongodb里,那么如何将数据搞到ElasticSearch中 ...

  6. sqoop将mysql数据导入hbase、hive的常见异常处理

    原创不易,如需转载,请注明出处https://www.cnblogs.com/baixianlong/p/10700700.html,否则将追究法律责任!!! 一.需求: 1.将以下这张表(test_ ...

  7. 使用sqoop将MySQL数据库中的数据导入Hbase

    使用sqoop将MySQL数据库中的数据导入Hbase 前提:安装好 sqoop.hbase. 下载jbdc驱动:mysql-connector-java-5.1.10.jar 将 mysql-con ...

  8. mongodb数据导入导出以及备份恢复

    昨日在公司收到游戏方发来一个1G多的数据文件,要求导入联运账号中.细细一看,纳尼!文件竟然是BSON格式. 哇塞,这不是去年给大家分享的NoSql中的MongoDB的备份文件吗? 于是搭好环境 1.启 ...

  9. Oracle数据导入Hbase操作步骤

    ——本文非本人原创,为公司同事整理,发布至此以便查阅 一.入库前数据准备 1.入hbase详细要求及rowkey生成规则,参考文档“_入HBase库要求 20190104.docx”. 2.根据标准库 ...

随机推荐

  1. Db2数据库在Linux下的安装和配置

    一.DB2数据库的安装和配置: 1.安装完成后,需要增加三个操作系统的组和三个操作系统用户,如下: groupadd -g 999 db2iadm1  #(管理实例的组) groupadd -g 99 ...

  2. 180. Consecutive Numbers

    问题描述 解决方案 select distinct l1.Num as ConsecutiveNums from Logs l1,Logs l2,Logs l3 where l1.Id+1=l2.Id ...

  3. CentOS 7.3 关闭默认防火墙&远程登录

             小编作为一个运维新人,踩坑之路是必不可少的.          这不,新来了一家公司,做云运维工程师,新的环境,网络和之前的都不一样,VMware Workstation虚拟机上的网 ...

  4. 使用Spring MVC表单标(转)

    概述     在低版本的Spring中,你必须通过JSTL或<spring:bind>将表单对象绑定到HTML表单页面中,对于习惯了Struts表单标签的开发者来说,Spring MVC的 ...

  5. List集合添加自定义对象

    public class Student { private String name; private int age; public Student() { super(); } public St ...

  6. git,npm,bower设置代理地址

    我們先假設代理伺服器的位址為: http://10.0.0.1:8080 設定 Git 使用代理伺服器 輸入兩行指令即可設定完畢: git config --global https.proxy ht ...

  7. linux install JDK

    安装JDK 下载jdk-6u23-linux-i586.bin,samba,FTP cd /usr/local/src/ wget http://www.aminglinux.com/bbs/data ...

  8. MyISAM引擎的特点及优化方法

    1.什么是MyISAM引擎?MyISAM引擎是MySQL关系数据库管理系统的默认存储引擎(MySQL5.5.5以前),这种MySQL的表存储结构从旧的ISAM代码扩展出许多有用的功能.在存储的时候,每 ...

  9. UGUI技巧

    http://www.cnblogs.com/suoluo/p/5427514.html Text中的可以单独指定某些文字的颜色,只需将想要变色的文本放在<color=**></co ...

  10. python之 前端HTML/CSS基础知识学习笔记

    1. 文件结构: HTML文件的固定结构: <html> <head>...</head> <body>...</body> </ht ...