RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
怎么将HDFS文件内容数据写入存储到HBase中

这篇文章主要讲解了“怎么将HDFS文件内容数据写入存储到HBase中”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么将HDFS文件内容数据写入存储到HBase中”吧!

创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都做网站、网站设计、鱼台网络推广、小程序制作、鱼台网络营销、鱼台企业策划、鱼台品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供鱼台建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com

将HDFS文件内容数据写入存储到HBase中:
对一些大的文件,需要存入HBase中,其思想是先把文件传到HDFS上,利用map阶段读取对,可在reduce把这些键值对上传到HBase中。

这里已经是固定指定HDFS中的某一文件,然后在reduce中把这些键值对写入到HBase中。
 

public class HBaseAndMapReduce3 {
	
	public static void main(String[] args) throws Exception {
		System.exit(run());
	}

	public static int run() throws Exception {
		Configuration conf = new Configuration();
		conf = HBaseConfiguration.create(conf);
		conf.set("hbase.zookeeper.quorum", "192.168.226.129");

		Job job = Job.getInstance(conf, "findFriend");
		job.setJarByClass(HBaseAndMapReduce3.class);

		job.setInputFormatClass(KeyValueTextInputFormat.class);
		
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(Text.class);

		DateFormat df = new SimpleDateFormat( "yyyyMMddHHmmssS" );
		
		FileInputFormat.addInputPath(job, new Path("hdfs://192.168.226.129:9000/hbasemapreduce1/2016051818564427/part-r-00000"));
		
		// 把数据写入Hbase数据库
		TableMapReduceUtil.initTableReducerJob("friend",FindFriendReducer.class, job);
		checkTable(conf);
		return job.waitForCompletion(true) ? 0 : 1;
	}

	private static void checkTable(Configuration conf) throws Exception {
		Connection con = ConnectionFactory.createConnection(conf);
		Admin admin = con.getAdmin();
		TableName tn = TableName.valueOf("friend");
		if (!admin.tableExists(tn)){
			HTableDescriptor htd = new HTableDescriptor(tn);
			HColumnDescriptor hcd = new HColumnDescriptor("person");
			htd.addFamily(hcd);
			admin.createTable(htd);
			System.out.println("表不存在,新创建表成功....");
		}
	}

	public static class FindFriendReducer extends
			TableReducer {
		@Override
		protected void reduce(
				Text key,
				Iterable values,
				Reducer.Context context)
				throws IOException, InterruptedException {
			
			Put put = new Put(key.getBytes());
			put.addColumn(Bytes.toBytes("person"), Bytes.toBytes("nickname"),
					values.iterator().next().getBytes());
			context.write(new ImmutableBytesWritable(key.getBytes()), put);
		}
	}
}

//原数据文件中的内容:

hadoop	Berg-OSChina,BergBerg
hbase	OSChina,BergBerg
zookeeper	OSChina,BergBerg

///将HDFS中文件内容存入HBase中,通过客户端全表扫描知:

hbase(main):003:0> scan 'friend'
ROW                             COLUMN+CELL                                                                              
 hadoop                         column=person:nickname, timestamp=1463748372584, value=Berg-OSChina,BergBerg             
 hbasep                         column=person:nickname, timestamp=1463748372584, value=OSChina,BergBerggBerg             
 zookeeper                      column=person:nickname, timestamp=1463748372584, value=OSChina,BergBerggBerg             
3 row(s) in 0.2850 seconds

感谢各位的阅读,以上就是“怎么将HDFS文件内容数据写入存储到HBase中”的内容了,经过本文的学习后,相信大家对怎么将HDFS文件内容数据写入存储到HBase中这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!


网页名称:怎么将HDFS文件内容数据写入存储到HBase中
文章路径:http://sczitong.cn/article/ippjgc.html