欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

怎么往Hbase表添加測(cè)試數(shù)據(jù)

本篇內(nèi)容介紹了“怎么往Hbase表添加測(cè)試數(shù)據(jù)”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

目前成都創(chuàng)新互聯(lián)已為1000多家的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站運(yùn)營(yíng)、企業(yè)網(wǎng)站設(shè)計(jì)、林周網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

創(chuàng)建一張表,往Hbase表添加測(cè)試數(shù)據(jù),然后將數(shù)據(jù)寫入HDFS文件。

public class HBaseAndMapReduce1 {

	public static void main(String[] args) throws Exception {
		//測(cè)試數(shù)據(jù)
		testData();
		
		System.exit(run());
	}
	
	public static int run() throws IOException, ClassNotFoundException, InterruptedException{
		
		//初始化HBase配置
		Configuration conf = new Configuration();
		conf = HBaseConfiguration.create(conf);
		conf.set("hbase.zookeeper.quorum", "192.168.226.129");

		Job job = Job.getInstance(conf,"searchPerson");
		job.setJarByClass(HBaseAndMapReduce1.class);

		//實(shí)例化scan對(duì)象。
		Scan scan = new Scan();

		//限定列,只讀取關(guān)鍵數(shù)據(jù),比如:article,author
		scan.addColumn(Bytes.toBytes("article"), Bytes.toBytes("tags"));
		scan.addColumn(Bytes.toBytes("author"), Bytes.toBytes("nickname"));
		
		/**
		 * 參照源碼如下:
		 * TableMapper //對(duì)輸出hbase數(shù)據(jù)來(lái)格式分割的處理類 
		 *  public static void initTableMapperJob(String table, Scan scan,
		 *  		Class<? extends TableMapper> mapper,
		 *  		Class<?> outputKeyClass,
		 *  		Class<?> outputValueClass, Job job) throws IOException {
		 *  	initTableMapperJob(table, scan, mapper, outputKeyClass, outputValueClass,job, true);
		 *  }
		 * */
		/**
		 * Result類中有個(gè)靜態(tài)方法getFamilyMap()可以獲得以列名為key,值為value,這剛好與hadoop中map結(jié)果是一樣的。
		 * 
		 */
		TableMapReduceUtil.initTableMapperJob( "blog",scan, FindFriendMapper.class, 
				ImmutableBytesWritable.class,  Result.class, job);
		
		DateFormat df = new SimpleDateFormat( "yyyyMMddHHmmssS" );
		FileOutputFormat.setOutputPath(job, new Path("hdfs://192.168.226.129:9000/hbasemapreduce/" + df.format( new Date() )+"/"));

		return job.waitForCompletion(true) ? 0 : 1;
	}

	public static class FindFriendMapper extends TableMapper<ImmutableBytesWritable, Result>{
		//key是hbase中的行鍵
		//value是hbase中的所行鍵的所有數(shù)據(jù)
		@Override
		protected void map(ImmutableBytesWritable key,Result value,
						Mapper<ImmutableBytesWritable, Result, ImmutableBytesWritable, Result>.Context context)
								throws IOException, InterruptedException {
			//System.out.println("key-->" + Bytes.toString(key.get()) + "---> " + key );
			/*
			 * Cell (存儲(chǔ)單元)
			 * 由{row key, column(=<family> + <label>), version} 唯一確定的單元。
			 * 		cell中的數(shù)據(jù)是沒有類型的,全部是字節(jié)碼形式存貯。
			 * */
			List<Cell> cs = value.listCells();   // return  Arrays.asList(rawCells());
			//value.getFamilyMap("");
			for (Cell cell : cs) {
				System.out.println("Cell--->"+cell);
                // 存儲(chǔ)單元中的每個(gè)屬性:
				String rowKey = Bytes.toString(CellUtil.cloneRow(cell));
				long timestamp = cell.getTimestamp();
				String family = Bytes.toString(CellUtil.cloneFamily(cell));
				String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
				String val = Bytes.toString(CellUtil.cloneValue(cell));
				System.out.println("RowKey=" + rowKey + ", Timestamp=" + timestamp + 
						", Family=" + family + ", Qualifier=" + qualifier + ", Value=" + val +"\n");
			}
			System.out.println( "key---> " +key + "\nvalue---> "+value);
			super.map(key, value, context);
		}
	}

	public static void testData() {
		try {
			//初始化HBase的配置文件。
			Configuration conf = HBaseConfiguration.create();
			conf.set("hbase.zookeeper.quorum", "192.168.226.129");
			Connection con = ConnectionFactory.createConnection(conf);
			
			Admin admin = con.getAdmin();
			TableName tn = TableName.valueOf("blog");
			if (admin.tableExists(tn)) {
				admin.disableTable(tn);
				admin.deleteTable(tn);
			}

			//創(chuàng)建表結(jié)構(gòu)對(duì)象:用于描述表名和相關(guān)的列族。
			HTableDescriptor htd = new HTableDescriptor(tn);
			HColumnDescriptor hcd1 = new HColumnDescriptor("article");
			HColumnDescriptor hcd2 = new HColumnDescriptor("author");
			
			//描述相關(guān)的列族:
			htd.addFamily(hcd1);
			htd.addFamily(hcd2);
			//創(chuàng)建表:
			admin.createTable(htd); 
			Table table = con.getTable(tn);
			
			//參數(shù)為行鍵:
			Put put = new Put(Bytes.toBytes("rowkey1"));
			//Put.addColumn(byte[] family, byte[] qualifier, byte[] value)
			put.addColumn(Bytes.toBytes("article"), Bytes.toBytes("content"), Bytes.toBytes("HBase is the Hadoop database"));
			put.addColumn(Bytes.toBytes("article"), Bytes.toBytes("tags"), Bytes.toBytes("Hadoop,Zookeeper,HBase"));
			put.addColumn(Bytes.toBytes("article"), Bytes.toBytes("title"),Bytes.toBytes("Head First Hbase"));
			put.addColumn(Bytes.toBytes("author"), Bytes.toBytes("name"), Bytes.toBytes("Berg"));
			put.addColumn(Bytes.toBytes("author"), Bytes.toBytes("nickname"),Bytes.toBytes("BergBerg"));

			Put put2 = new Put(Bytes.toBytes("rowkey2"));
			put2.addColumn(Bytes.toBytes("article"), Bytes.toBytes("tags"), Bytes.toBytes("Hadoop"));
			put2.addColumn(Bytes.toBytes("author"), Bytes.toBytes("nickname"), Bytes.toBytes("Berg-OSChina"));

			Put put3 = new Put(Bytes.toBytes("rowkey3"));
			put3.addColumn(Bytes.toBytes("article"), Bytes.toBytes("tags"), Bytes.toBytes("Zookeeper,HBase"));
			put3.addColumn(Bytes.toBytes("author"), Bytes.toBytes("nickname"), Bytes.toBytes("OSChina"));

			List<Put> puts = Arrays.asList(put, put2, put3);
			
			//Table.put(List<Put> puts)
			table.put(puts);
			
			System.out.println(" \n\n***************測(cè)試數(shù)據(jù)準(zhǔn)備完成****************\n");

			if (admin != null){
				admin.close();
			}
			if(con != null){
				con.close();
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

“怎么往Hbase表添加測(cè)試數(shù)據(jù)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

網(wǎng)站名稱:怎么往Hbase表添加測(cè)試數(shù)據(jù)
文章來(lái)源:http://chinadenli.net/article28/gigejp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、電子商務(wù)、ChatGPT、關(guān)鍵詞優(yōu)化、軟件開發(fā)、網(wǎng)站營(yíng)銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營(yíng)