word文档educoder平台使用Java操作HBase

内容

1创建表packagestep1;importjavaioIOException;importorgapachehadoopconfConfiguration;importorgapachehadoophbaseHBaseConfiguration;importorgapachehadoophbaseHColumnDescriptor;importorgapachehadoophbaseHTableDescriptor;importorgapachehadoophbaseTableName;importorgapachehadoophbaseclientAdmin;importorgapachehadoophbaseclientColumnFamilyDescriptor;importorgapachehadoophbaseclientColumnFamilyDescriptorBuilder;importorgapachehadoophbaseclientConnection;importorgapachehadoophbaseclientConnectionFactory;importorgapachehadoophbaseclientGet;importorgapachehadoophbaseclientPut;importorgapachehadoophbaseclientResult;importorgapachehadoophbaseclientResultScanner;importorgapachehadoophbaseclientScan;importorgapachehadoophbaseclientTable;importorgapachehadoophbaseclientTableDescriptor;importorgapachehadoophbaseclientTableDescriptorBuilder;importorgapachehadoophbaseutilBytes;/***HBase10versionofExampleClientthatuses{@codeConnection},*{@codeAdmin}and{@codeTable}*/publicclassTask{publicvoidcreateTable()throwsException{/*********Begin*********/Configurationconfig=HBaseConfigurationcreate();Connectionconnection=ConnectionFactorycreateConnection(config);try{//CreatetableAdminadmin=connectiongetAdmin();try{TableNametableName=TableNamevalueOf("dept");//新API构建表//TableDescriptor对象通过TableDescriptorBuilder构建;TableDescriptorBuildertableDescriptor=TableDescriptorBuildernewBuilder(tableName);ColumnFamilyDescriptorfamily=ColumnFamilyDescriptorBuildernewBuilder(BytestoBytes("data"))build();//构建列族对象tableDescriptorsetColumnFamily(family);//设置列族admincreateTable(tableDescriptorbuild());//创建表精选—2TableNameemp=TableNamevalueOf("emp");//新API构建表//TableDescriptor对象通过TableDescriptorBuilder构建;TableDescriptorBuilderempDescriptor=TableDescriptorBuildernewBuilder(emp);ColumnFamilyDescriptorempfamily=ColumnFamilyDescriptorBuildernewBuilder(BytestoBytes("emp"))build();//构建列族对象empDescriptorsetColumnFamily(empfamily);//设置列族admincreateTable(empDescriptorbuild());//创建表}finally{adminclose();}}finally{connectionclose();}/*********End*********/}}命令行:start-dfssh(Hadoop启动)回车start-hbasesh(hbase启动)测评———————————————————————————————————————2添加数据packagestep2;importjavaioIOException;importorgapachehadoopconfConfiguration;importorgapachehadoophbaseHBaseConfiguration;importorgapachehadoophbaseHColumnDescriptor;importorgapachehadoophbaseHTableDescriptor;importorgapachehadoophbaseTableName;importorgapachehadoophbaseclientAdmin;importorgapachehadoophbaseclientColumnFamilyDescriptor;importorgapachehadoophbaseclientColumnFamilyDescriptorBuilder;importorgapachehadoophbaseclientConnection;importorgapachehadoophbaseclientConnectionFactory;importorgapachehadoophbaseclientGet;importorgapachehadoophbaseclientPut;importorgapachehadoophbaseclientResult;importorgapachehadoophbaseclientResultScanner;importorgapachehadoophbaseclientScan;importorgapachehadoophbaseclientTable;importorgapachehadoophbaseclientTableDescriptor;精选—3importorgapachehadoophbaseclientTableDescriptorBuilder;importorgapachehadoophbaseutilBytes;publicclassTask{publicvoidinsertInfo()throwsException{/*********Begin*********/Configurationconfig=HBaseConfigurationcreate();Connectionconnection=ConnectionFactorycreateConnection(config);Adminadmin=connectiongetAdmin();TableNametableName=TableNamevalueOf("tb_step2");TableDescriptorBuildertableDescriptor=TableDescriptorBuildernewBuilder(tableName);ColumnFamilyDescriptorfamily=ColumnFamilyDescriptorBuildernewBuilder(BytestoBytes("data"))build();//构建列族对象tableDescriptorsetColumnFamily(family);//设置列族admincreateTable(tableDescriptorbuild());//创建表//添加数据byte[]row1=BytestoBytes("row1");Putput1=newPut(row1);byte[]columnFamily1=BytestoBytes("data");//列byte[]qualifier1=BytestoBytes(StringvalueOf(1));//列族修饰词byte[]value1=BytestoBytes("张三丰");//值put1addColumn(columnFamily1,qualifier1,value1);byte[]row2=BytestoBytes("row2");Putput2=newPut(row2);byte[]columnFamily2=BytestoBytes("data");//列byte[]qualifier2=BytestoBytes(StringvalueOf(2));//列族修饰词byte[]value2=BytestoBytes("张无忌");//值put2addColumn(columnFamily2,qualifier2,value2);Tabletable=connectiongetTable(tableName);tableput(put1);tableput(put2);/*********End*********/}}测评———————————————————————————————————————3获取数据packagestep3;importjavaioIOException;importorgapachehadoopconfConfiguration;importorgapachehadoophbaseHBaseConfiguration;importorgapachehadoophbaseHColumnDescriptor;importorgapachehadoophbaseHTableDescriptor;importorgapachehadoophbaseTableName;精选—4importorgapachehadoophbaseclientAdmin;importorgapachehadoophbaseclientColumnFamilyDescriptor;importorgapachehadoophbaseclientColumnFamilyDescriptorBuilder;importorgapachehadoophbaseclientConnection;importorgapachehadoophbaseclientConnectionFactory;importorgapachehadoophbaseclientGet;importorgapachehadoophbaseclientPut;importorgapachehadoophbaseclientResult;importorgapachehadoophbaseclientResultScanner;importorgapachehadoophbaseclientScan;importorgapachehadoophbaseclientTable;importorgapachehadoophbaseclientTableDescriptor;importorgapachehadoophbaseclientTableDescriptorBuilder;importorgapachehadoophbaseutilBytes;publicclassTask{publicvoidqueryTableInfo()throwsException{/*********Begin*********/Configurationconfig=HBaseConfigurationcreate();Connectionconnection=ConnectionFactorycreateConnection(config);Adminadmin=connectiongetAdmin();TableNametableName=TableNamevalueOf("t_step3");Tabletable=connectiongetTable(tableName);//获取数据Getget=newGet(BytestoBytes("row1"));//定义get对象Resultresult=tableget(get);//通过table对象获取数据//Systemoutprintln("Result:"+result);//很多时候我们只需要获取“值”这里表示获取data:1列族的值byte[]valueBytes=resultgetValue(BytestoBytes("data"),BytestoBytes("1"));//获取到的是字节数组//将字节转成字符串StringvalueStr=newString(valueBytes,"utf-8");Systemoutprintln("value:"+valueStr);TableNametableStep3Name=TableNamevalueOf("table_step3");Tablestep3Table=connectiongetTable(tableStep3Name);//批量查询Scanscan=newScan();ResultScannerscanner=step3TablegetScanner(scan);try{inti=0;for(ResultscannerResult:scanner){//byte[]value=scannerResultgetValue(BytestoBytes("data"),BytestoBytes(1));//Systemoutprintln("Scan:"+scannerResult);byte[]row=scannerResultgetRow();精选—5Systemoutprintln("rowName:"+newString(row,"utf-8"));}}finally{scannerclose();}/*********End*********/}}测评———————————————————————————————————————4删除表packagestep4;importjavaioIOException;importorgapachehadoopconfConfiguration;importorgapachehadoophbaseHBaseConfiguration;importorgapachehadoophbaseHColumnDescriptor;importorgapachehadoophbaseHTableDescriptor;importorgapachehadoophbaseTableName;importorgapachehadoophbaseclientAdmin;importorgapachehadoophbaseclientColumnFamilyDescriptor;importorgapachehadoophbaseclientColumnFamilyDescriptorBuilder;importorgapachehadoophbaseclientConnection;importorgapachehadoophbaseclientConnectionFactory;importorgapachehadoophbaseclientGet;importorgapachehadoophbaseclientPut;importorgapachehadoophbaseclientResult;importorgapachehadoophbaseclientResultScanner;importorgapachehadoophbaseclientScan;importorgapachehadoophbaseclientTable;importorgapachehadoophbaseclientTableDescriptor;importorgapachehadoophbaseclientTableDescriptorBuilder;importorgapachehadoophbaseutilBytes;publicclassTask{publicvoiddeleteTable()throwsException{/*********Begin*********/Configurationconfig=HBaseConfigurationcreate();Connectionconnection=ConnectionFactorycreateConnection(config);Adminadmin=connectiongetAdmin();TableNametableName=TableNamevalueOf("t_step4");admindisableTable(tableName);admindeleteTable(tableName);/*********End*********/}}

浏览:34
下载量:-
下载币:30
日期:2020-11-18
6
收藏
         投诉 / 报错