欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

HBase基礎(chǔ)操作,包括表的增刪改查過濾等

package com.snglw.basic;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.coprocessor.AggregationClient;
import org.apache.hadoop.hbase.client.coprocessor.LongColumnInterpreter;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.FilterList.Operator;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;

import com.snglw.util.ConnectInit;

public class BasicOperator {

	ConnectInit ci = new ConnectInit();
	Configuration conf = ci.getConfiguration();
	
	
	/*建表與刪表*/
	public void createTable(){

		String tableName = "user";
		HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));
		HColumnDescriptor hcd = new HColumnDescriptor("info");
		htd.addFamily(hcd);
		
		HBaseAdmin admin = null;
		try
        {
            admin = new HBaseAdmin(conf);
            if (admin.tableExists(tableName)){
                admin.disableTable(tableName);
                admin.deleteTable(tableName);
            }else{
            	admin.createTable(htd);
            	System.out.println("Table Created!");
            }
		}catch(IOException e){
			e.printStackTrace();
		}
		finally{
			if(admin != null){
				try{
					admin.close();
				}catch(IOException e){
					e.printStackTrace();
				}
			}
		}
	}
	
	
	/*修改表(注意:修改表或者列時,只有表的Enabled屬性為false時才能生效)*/
	public void modifyTable(){
		//指定表名
		String tableName = "user";
		//指定列族名
		byte[] familyName = Bytes.toBytes("education");
		
		HBaseAdmin admin = null;
		try{
			//實例化HBaseAdmin對象
			admin = new HBaseAdmin(conf);
			//獲取表描述信息對象
			HTableDescriptor htd = admin.getTableDescriptor(Bytes.toBytes(tableName));
			//修改前,判斷表是否有指定列族
			if(!htd.hasFamily(familyName)){
				//創(chuàng)建列描述信息
				HColumnDescriptor hcd = new HColumnDescriptor(familyName);
				htd.addFamily(hcd);
				
				//修改表前,需要disable表,使其下線
				admin.disableTable(tableName);
				//提交modifyTable請求
				admin.modifyTable(tableName, htd);
				//修改完成之后,使表上線
				admin.enableTable(tableName);
			}
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			if(admin != null){
				try {
					admin.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	
	/*插入數(shù)據(jù)*/
	public void put(){
		//指定表名
		String tableName = "user";
		//指定列族名
		byte[] familyName = Bytes.toBytes("info");
		//指定列名
		byte[][] qualifiers = {Bytes.toBytes("name"),Bytes.toBytes("gender"),
							   Bytes.toBytes("age"),Bytes.toBytes("address")};
		HTable table = null;
		try{
			//實例化一個HTable對象
			table = new HTable(conf,tableName);
			List<Put> puts = new ArrayList<Put>();
			//實例化一個Put對象
			Put put = new Put(Bytes.toBytes("012005000201"));
			put.addImmutable(familyName, qualifiers[0],Bytes.toBytes("張三"));
			put.addImmutable(familyName, qualifiers[1],Bytes.toBytes("男"));
			put.addImmutable(familyName, qualifiers[2],Bytes.toBytes(new Long(19)));
			put.addImmutable(familyName, qualifiers[3],Bytes.toBytes("廣東省深圳市"));
			puts.add(put);
			
			put = new Put(Bytes.toBytes("012005000202"));
			put.addImmutable(familyName, qualifiers[0],Bytes.toBytes("李"));
			put.addImmutable(familyName, qualifiers[1],Bytes.toBytes("女"));
			put.addImmutable(familyName, qualifiers[2],Bytes.toBytes(new Long(23)));
			put.addImmutable(familyName, qualifiers[3],Bytes.toBytes("山西省大同市"));
			puts.add(put);
			
			put = new Put(Bytes.toBytes("012005000203"));
			put.addImmutable(familyName, qualifiers[0],Bytes.toBytes("王"));
			put.addImmutable(familyName, qualifiers[1],Bytes.toBytes("男"));
			put.addImmutable(familyName, qualifiers[2],Bytes.toBytes(new Long(26)));
			put.addImmutable(familyName, qualifiers[3],Bytes.toBytes("浙江省寧波市"));
			puts.add(put);
			
			//提交put數(shù)據(jù)請求
			table.put(puts);
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			if(table != null){
				try{
					//關(guān)閉HTable對象
					table.close();
				}catch(IOException e){
					e.printStackTrace();
				}
			}
		}
	}
	
	
	/*刪除數(shù)據(jù)*/
	public void delete(){
		String tableName = "user";
		//指定rowKey值,即編號為012005000201
		byte[] rowKey = Bytes.toBytes("012005000201");
		
		HTable table = null;
		try{
			table = new HTable(conf,tableName);
			Delete delete = new Delete(rowKey);
			//提交一次delete數(shù)據(jù)請求
			table.delete(delete);
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			if(table != null){
				try {
					table.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	
	/*使用Get讀取數(shù)據(jù)*/
	public void get(){
		String tableName = "user";
		//指定列族名
		byte[] familyName = Bytes.toBytes("info");
		//指定列名
		byte[][] qualifier = {Bytes.toBytes("name"),Bytes.toBytes("address")};
		//指定rowKey值
		byte[] rowKey = Bytes.toBytes("012005000202");
		
		HTable table = null;
		try{
			table = new HTable(conf,tableName);
			//實例化get對象
			Get get = new Get(rowKey);
			//設(shè)置列族和列名
			get.addColumn(familyName,qualifier[0]);
			get.addColumn(familyName,qualifier[1]);
			//提交請求
			Result result = table.get(get);
			for(Cell cell:result.rawCells()){
				System.out.println(Bytes.toString(CellUtil.cloneRow(cell))
						+ ":" + Bytes.toString(CellUtil.cloneFamily(cell))
						+ ":" + Bytes.toString(CellUtil.cloneQualifier(cell))
						+ ":" + Bytes.toString(CellUtil.cloneValue(cell)));
			}
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			if(table != null){
				try {
					table.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	
	/*使用scan讀取數(shù)據(jù)*/
	public void scan(){
		String tableName = "webPage";
		HTable table = null;
		try{
			table = new HTable(conf,tableName);
			Scan scan = new Scan();
			scan.addColumn(Bytes.toBytes("webPageInfo"),Bytes.toBytes("doc"));
			//設(shè)置緩存大小
			scan.setCaching(5000);
			scan.setBatch(2);
			//實例化一個ResultScanner對象
			ResultScanner rScanner = null;
			//提交請求
			rScanner = table.getScanner(scan);
			for(Result r = rScanner.next();r != null;r = rScanner.next()){
				for(Cell cell:r.rawCells()){
					System.out.println(Bytes.toString(CellUtil.cloneRow(cell))
							+ ":" + Bytes.toString(CellUtil.cloneFamily(cell))
							+ ":" + Bytes.toString(CellUtil.cloneQualifier(cell))
							+ ":" + Bytes.toString(CellUtil.cloneValue(cell)));
				}
			}
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			if(table != null){
				try {
					table.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	
	/*使用過濾器*/
	public void singleColumnValueFilter(){
		String tableName = "user";
		HTable table = null;
		try{
			table = new HTable(conf,tableName);
			//實例化一個Scan對象
			Scan scan = new Scan();
			scan.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"));
			//設(shè)置過濾條件
			SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("info"),Bytes.toBytes("name"),
								CompareOp.EQUAL,Bytes.toBytes("王"));
			scan.setFilter(filter);
			//實例化ResultScanner對象
			ResultScanner rScanner = null;
			rScanner = table.getScanner(scan);
			for(Result r = rScanner.next();r != null;r = rScanner.next()){
				for(Cell cell:r.rawCells()){
					System.out.println(Bytes.toString(CellUtil.cloneRow(cell))
							+ ":" + Bytes.toString(CellUtil.cloneFamily(cell))
							+ ":" + Bytes.toString(CellUtil.cloneQualifier(cell))
							+ ":" + Bytes.toString(CellUtil.cloneValue(cell)));
				}
			}
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			if(table != null){
				try {
					table.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	
	/*使用FilterList過濾器*/
	public void filterList(){
		String tableName = "user";
		HTable table = null;
		try{
			table = new HTable(conf,tableName);
			//實例化一個Scan對象
			Scan scan = new Scan();
			scan.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"));
			//實例化FilterList對象,里各個filter的是"and"關(guān)系
			FilterList list = new FilterList(Operator.MUST_PASS_ALL);
			//設(shè)置過濾條件(age>20的數(shù)據(jù))
			list.addFilter(new SingleColumnValueFilter(Bytes.toBytes("info"),Bytes.toBytes("age"),
								CompareOp.GREATER_OR_EQUAL,Bytes.toBytes(new Long(20))));
			//獲取age<=29的數(shù)據(jù)
			list.addFilter(new SingleColumnValueFilter(Bytes.toBytes("info"),Bytes.toBytes("age"),
					CompareOp.GREATER_OR_EQUAL,Bytes.toBytes(new Long(29))));
			scan.setFilter(list);
			//實例化ResultScanner對象
			ResultScanner rScanner = null;
			rScanner = table.getScanner(scan);
			for(Result r = rScanner.next();r != null;r = rScanner.next()){
				for(Cell cell:r.rawCells()){
					System.out.println(Bytes.toString(CellUtil.cloneRow(cell))
							+ ":" + Bytes.toString(CellUtil.cloneFamily(cell))
							+ ":" + Bytes.toString(CellUtil.cloneQualifier(cell))
							+ ":" + Bytes.toString(CellUtil.cloneValue(cell)));
				}
			}
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			if(table != null){
				try {
					table.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	
	/*聚合函數(shù)Aggregate*/
	public void aggregate(){
		//指定表名
		byte[] tableName = Bytes.toBytes("user");
		//指定列族名
		byte[] family = Bytes.toBytes("info");
		
		AggregationClient aggregationClient = new AggregationClient(conf);
		//實例化scan對象
		Scan scan = new Scan();
		scan.addFamily(family);
		scan.addColumn(family,Bytes.toBytes("age"));
		try{
			//獲取行數(shù)
			long rowCount = aggregationClient.rowCount(TableName.valueOf(tableName),null,scan);
			System.out.println("row count is "+rowCount);
			//獲取最大值
			long max = aggregationClient.max(TableName.valueOf(tableName),new LongColumnInterpreter(),scan);
			System.out.println("max number is "+max);
			//獲取最小值
			long min = aggregationClient.min(TableName.valueOf(tableName),new LongColumnInterpreter(),scan);
			System.out.println("min number is "+min);
		}catch(Throwable e){
			e.printStackTrace();
		}
	}
	
	
	public static void main(String[] args){
		BasicOperator bo = new BasicOperator();
		bo.aggregate();
	}
}

本文標(biāo)題:HBase基礎(chǔ)操作,包括表的增刪改查過濾等
標(biāo)題鏈接:http://aaarwkj.com/article32/igcppc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊手機(jī)網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計、小程序開發(fā)云服務(wù)器、靜態(tài)網(wǎng)站

廣告

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

成都做網(wǎng)站
粗长挺进新婚人妻诗岚| 丰满人妻一区二三区av| 久热精品视频在线观看| 日本韩国亚洲欧美一区二区| 琪琪精品免费一区二区三区| 国产欧美激情一区二区| 日韩深夜成人在线视频| 日本一区二区高清网址| 狼人综合狼人综合网站| 日本一本一道高清不卡视频| 亚洲国产欧美日韩久久| 欧美日韩一区二区综合性色| 国产精品日本欧美久久久| 久久av免费一区二区观看| 天天色天天色天天色综合网| 欧美精品国产一区二区免费| 青青草视频在线针对华人| 日韩欧美亚洲一级黄片| 97超碰国产在线观看| 久视频这里只有精品99| 国产一区av剧情巨作| 很黄无遮挡在线免费网站| 日本午夜精品在线观看| 岛国大片一区二区三区| 欧美在线观看香蕉视频| 日韩在线欧美在线一区二区| 日本在线一区二区中文| 人妇乱系列中文字幕人妻| 成人免费在线视频不卡| 后入视频国产在线观看| 久久草福利视频在线观看| 日本国产福利视频在线观看| 丰满少妇一区二区自拍区| 区一区二区三视频日韩| 91精品国产综合久久香蕉麻豆| av影片在线观看不卡| 蜜臀一二区免费在线视频| 2021天天操夜夜爽| 四虎永久精品国产毛片| 少妇太爽高潮在线播放| 欧美日韩另类综合91|