通過HBaseAdmin維護(hù)表(創(chuàng)建,刪除表)
成都創(chuàng)新互聯(lián)公司主要從事成都網(wǎng)站制作、做網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)浉河,十多年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):189808205752.通過Put寫入數(shù)據(jù)
import java.io.File; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; 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.util.Bytes; public class LoadDataFromFile { public static void main(String[] args) throws Exception { String tbl = "ericni_test"; String filename = "/tmp/click.log"; File file = new File(filename); BufferedReader reader = null; reader = new BufferedReader(new FileReader(file)); Configuration config = HBaseConfiguration. create(); config.set( "hbase.zookeeper.quorum", "10.100.90.203" ); config.set( "hbase.zookeeper.property.clientPort" , "2181" ); HTable table = new HTable(config, tbl); HBaseAdmin admin = new HBaseAdmin(config); if (admin.tableExists(tbl.getBytes("utf8" ))) { System. out.println("table already exists!" ); try { try { String tmpString = null; int linex = 1; while ((tmpString = reader.readLine()) != null) { //System.out.println(tmpString.getClass()); String[] lines = tmpString.split( "\\t"); //String[] newlines = {lines[0],lines[1],lines[2],lines[3],lines[4]}; //String row = StringUtils.join(new String[] {lines[0],lines[1],lines[2],lines[3],lines[4]}, "_"); StringBuffer sb = new StringBuffer(); String row = sb.append(lines[0]).append("_" ).append(lines[1]).append("_").append(lines[2]).append( "_").append(lines[3]).append("_" ).append(lines[4]).toString(); String valuex = lines[lines. length-1]; System. out.println(row); linex ++; addRecord(table,tbl,row, "Stat", "Click_cnt", valuex); } reader.close(); } catch(IOException err){ err.printStackTrace(); } finally { try{ if(reader != null) reader.close(); } catch(IOException err){ err.printStackTrace(); } } } catch(Exception err){ System. out.println("load data error" ); System. out.println("error log: " + err); err.printStackTrace(); } } else { System. out.println("table not already exists!" ); System. exit(1); } table.close(); admin.close(); } public static void addRecord(HTable table,String tableName, String rowKey, String family, String qualifier, String value) throws Exception{ Put put = new Put(Bytes.toBytes(rowKey)); put.add(Bytes. toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value)); table.put(put); System. out.println("insert recored " + rowKey + " to table " + tableName + " ok." ); } }3.Scan的Filter操作
import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.KeyValue; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.HTable; 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.filter.FilterList; import org.apache.hadoop.hbase.filter.SingleColumnValueFilter; import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp; import org.apache.hadoop.hbase.util.Bytes; public class FilterTest { private static Configuration config = null; private static HTable table; private static HTable table2; private static HTable table3; static { try { config = HBaseConfiguration.create(); config.set("hbase.zookeeper.quorum" , "xxxxx" ); config.set("hbase.zookeeper.property.clientPort" , "2181" ); } catch (Exception e){ e.printStackTrace(); } } public static void selectByRowKey(String tablename,String rowKey) throws IOException{ table = new HTable(config,tablename); Get g = new Get(Bytes.toBytes(rowKey)); Result r = table.get(g); for(KeyValue kv:r.raw()){ System. out.println("row : " +new String(kv.getRow())); System. out.println("column: " +new String(kv.getKey())); System. out.println("value: " +new String(kv.getValue())); } } public static void selectByRowKeyColumn(String tablename,String rowKey,String column,String qualifier) throws IOException{ table2 = new HTable(config,tablename); Get g = new Get(Bytes.toBytes(rowKey)); g.addColumn(Bytes. toBytes(column),qualifier.getBytes("utf8")); Result r = table2.get(g); for(KeyValue kv:r.raw()){ System. out.println("row : " +new String(kv.getRow())); System. out.println("column: " +new String(kv.getKey())); System. out.println("value: " +new String(kv.getValue())); } } public static void selectByFilter(String tablename,List另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。