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常用的包可以全导 代码如上,原创转载注明出处!
MongoDB数据导入hbase + 代码的更多相关文章
- python 全栈开发,Day126(创业故事,软件部需求,内容采集,显示内容图文列表,MongoDB数据导入导出JSON)
作业讲解 下载代码: HBuilder APP和flask后端登录 链接:https://pan.baidu.com/s/1eBwd1sVXTNLdHwKRM2-ytg 密码:4pcw 如何打开APP ...
- 软件部需求,内容采集,显示内容图文列表,MongoDB数据导入导出JSON
全局变量 由于多个html页面,需要引用同一个变量.这个时候,需要定义一个全局变量!如何定义呢? 默认包含了mui的html文件都导入mui.js文件.那么将变量写在mui.js中,就可以实现所有页面 ...
- MapReduce将HDFS文本数据导入HBase中
HBase本身提供了很多种数据导入的方式,通常有两种常用方式: 使用HBase提供的TableOutputFormat,原理是通过一个Mapreduce作业将数据导入HBase 另一种方式就是使用HB ...
- Sqoop将mysql数据导入hbase的血与泪
Sqoop将mysql数据导入hbase的血与泪(整整搞了大半天) 版权声明:本文为yunshuxueyuan原创文章.如需转载请标明出处: https://my.oschina.net/yunsh ...
- 通过logstash-input-mongodb插件将mongodb数据导入ElasticSearch
目的很简单,就是将mongodb数据导入es建立相应索引.数据是从特定的网站扒下来,然后进行二次处理,也就是数据去重.清洗,接着再保存到mongodb里,那么如何将数据搞到ElasticSearch中 ...
- sqoop将mysql数据导入hbase、hive的常见异常处理
原创不易,如需转载,请注明出处https://www.cnblogs.com/baixianlong/p/10700700.html,否则将追究法律责任!!! 一.需求: 1.将以下这张表(test_ ...
- 使用sqoop将MySQL数据库中的数据导入Hbase
使用sqoop将MySQL数据库中的数据导入Hbase 前提:安装好 sqoop.hbase. 下载jbdc驱动:mysql-connector-java-5.1.10.jar 将 mysql-con ...
- mongodb数据导入导出以及备份恢复
昨日在公司收到游戏方发来一个1G多的数据文件,要求导入联运账号中.细细一看,纳尼!文件竟然是BSON格式. 哇塞,这不是去年给大家分享的NoSql中的MongoDB的备份文件吗? 于是搭好环境 1.启 ...
- Oracle数据导入Hbase操作步骤
——本文非本人原创,为公司同事整理,发布至此以便查阅 一.入库前数据准备 1.入hbase详细要求及rowkey生成规则,参考文档“_入HBase库要求 20190104.docx”. 2.根据标准库 ...
随机推荐
- Stacktack overview
class Lifecycle(models.Model): """The Lifecycle table is the Master for a group of ...
- Spring Boot入门——thymeleaf模板使用
使用步骤 1.在pom.xml中引入thymeleaf <!-- thymeleaf插件 --> <dependency> <groupId>org.springf ...
- thinkphp中如何使用phpspreadsheet插件
thinkphp中如何使用phpspreadsheet插件 一.总结 一句话总结:多百度,百度什么都有 1.thinkphp中用composer安装的插件的命名空间是什么? use PhpOffice ...
- linux设置开机自动进入命令模式
1)打开终端,输入命令: su – root 2)输入密码 3)再输入如下命令进入到命令行模式: init 3 4)修改启动模式为永久命令行模式: vi /etc ...
- KMSpico软件下载:激活Windows系统和Office工具
每次都为激活Windows系统 和 Microsoft Offilce 工具头疼,为此特意记录并分享一下激活工具. 上资源:https://pan.baidu.com/s/1kI5YpCO2aYl-r ...
- Eclipse中Maven的本地仓库引导配置
简单整理一下,方便理解操作. 1.本地拷贝maven文件后,打开maven中的.setting 文件: 2.配置文件: <?xml version="1.0" encodin ...
- 22 Python 模块与包
一 模块 1 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编 ...
- idea使用git上传项目到coding
1.VCS -> import into version control -> create git repository 2.选择要上传的项目根目录 3.选择后,项目里的文件名字变成了绿 ...
- GC(Garbage Collection)垃圾回收机制
1.在垃圾回收器中,程序员没有执行权,只有通知它的权利. 2.程序员可以通过System.gc().通知GC运行,但是Java规范并不能保证立刻运行. 3.finalize()方法,是java提供给程 ...
- Net Core 中使用 Consul 来存储配置
Net Core 中使用 Consul 来存储配置 https://www.cnblogs.com/Rwing/p/consul-configuration-aspnet-core.html 原文: ...