今天主要在做大数据实验三,有个问题记录一下 代码如下
package Test3; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; public class ExampleForHBase { public static Configuration configuration; public static Connection connection; public static Admin admin; public static void main(String[] args)throws IOException{ init();//主要操作就是为了连接到数据库hbase createTable("student",new String[]{"score"});//创建表,shell命令:create '表名','列族名1','列族名2','列族名3' ... insertData("student","zhangsan","score","English","69"); //shell命令: put 'student','张三','score:English','69' insertData("student","zhangsan","score","Math","86"); insertData("student","zhangsan","score","Computer","77"); getData("student", "zhangsan", "score","English"); close(); } public static void init() { System.out.println("2"); configuration = HBaseConfiguration.create(); System.out.println("1"); configuration.set("hbase.rootdir", "hdfs://192.168.88.151:8020/base"); configuration.set("hbase.zookeeper.quorum","192.168.88.151,192.168.88.152,192.168.88.153"); configuration.set("hbase.zookeeper.property.clientPort","2181"); try { connection = ConnectionFactory.createConnection(configuration); admin = connection.getAdmin(); System.out.println(3); System.out.println("HBase连接已建立。"); } catch (IOException e) { e.printStackTrace(); } } public static void close(){ try{ if(admin != null){ admin.close(); } if(null != connection){ connection.close(); } }catch (IOException e){ e.printStackTrace(); } } public static void createTable(String myTableName,String[] colFamily) throws IOException { System.out.println("5"); TableName tableName = TableName.valueOf(myTableName); if(admin.tableExists(tableName)){ System.out.println("talbe is exists!"); }else { TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tableName); for(String str:colFamily){ ColumnFamilyDescriptor family = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(str)).build(); tableDescriptor.setColumnFamily(family); } admin.createTable(tableDescriptor.build()); } } public static void insertData(String tableName,String rowKey,String colFamily,String col,String val) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Put put = new Put(rowKey.getBytes()); put.addColumn(colFamily.getBytes(),col.getBytes(), val.getBytes()); table.put(put); table.close(); } public static void getData(String tableName,String rowKey,String colFamily, String col)throws IOException{ Table table = connection.getTable(TableName.valueOf(tableName)); Get get = new Get(rowKey.getBytes()); get.addColumn(colFamily.getBytes(),col.getBytes()); Result result = table.get(get); System.out.println(new String(result.getValue(colFamily.getBytes(),col==null?null:col.getBytes()))); table.close(); } }
其实代码很容易理解,主要是连接hbase的时候,网上很多代码是没有关于zookeeper的两行配置的,我一开始也没有进行配置,所以会报错
然后这两个配置项,第一个可以在hbase的安装路径下的conf的hbase-site.xml找到,另一个可以在zookeeper的安装路径下的conf下的zoo.cfg下,搜client就能找到
配置完成之后就可以成功实现了
标签:12,String,日报,2023.11,getBytes,static,IOException,hbase,public From: https://www.cnblogs.com/Arkiya/p/17827332.html