溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么將HDFS文件內容數據寫入存儲到HBase中

發布時間:2021-12-09 10:25:16 來源:億速云 閱讀:294 作者:iii 欄目:云計算

這篇文章主要講解了“怎么將HDFS文件內容數據寫入存儲到HBase中”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“怎么將HDFS文件內容數據寫入存儲到HBase中”吧!

將HDFS文件內容數據寫入存儲到HBase中:
對一些大的文件,需要存入HBase中,其思想是先把文件傳到HDFS上,利用map階段讀取<key,value>對,可在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<Text, Text, ImmutableBytesWritable> {
		@Override
		protected void reduce(
				Text key,
				Iterable<Text> values,
				Reducer<Text, Text, ImmutableBytesWritable, Mutation>.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中這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女