用java创建hbase表,在用java语言给hbase添加用户名、性别和密码,最后用java语言写一个根据用户名和密码的登录功能
时间: 2024-02-06 16:12:23 浏览: 64
好的,我会尽力回答您的问题。首先,您需要安装和配置HBase和Java HBase客户端。然后,您可以使用以下代码来创建HBase表:
```java
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.TableDescriptor;
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
import org.apache.hadoop.hbase.regionserver.BloomType;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
public class HBaseCreateTable {
public static void main(String[] args) throws Exception {
// 配置HBase连接参数
Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", "localhost");
config.set("hbase.zookeeper.property.clientPort", "2181");
// 建立HBase连接
Connection connection = ConnectionFactory.createConnection(config);
Admin admin = connection.getAdmin();
// 定义表名
TableName tableName = TableName.valueOf("user");
// 定义列族
byte[] cf1 = Bytes.toBytes("personal");
// 定义表描述符
TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(tableName)
.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(cf1)
.setCompressionType(Algorithm.NONE)
.setBloomFilterType(BloomType.ROW)
.build())
.build();
// 创建表
admin.createTable(tableDesc);
// 关闭连接
admin.close();
connection.close();
System.out.println("Table created.");
}
}
```
接下来,您可以使用以下代码向表中添加用户名、性别和密码:
```java
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
public class HBaseAddData {
public static void main(String[] args) throws Exception {
// 配置HBase连接参数
Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", "localhost");
config.set("hbase.zookeeper.property.clientPort", "2181");
// 建立HBase连接
Connection connection = ConnectionFactory.createConnection(config);
// 获取表对象
TableName tableName = TableName.valueOf("user");
Table table = connection.getTable(tableName);
// 定义行键
byte[] rowKey = Bytes.toBytes("user1");
// 定义列族和列限定符
byte[] cf1 = Bytes.toBytes("personal");
byte[] qf1 = Bytes.toBytes("name");
byte[] qf2 = Bytes.toBytes("gender");
byte[] qf3 = Bytes.toBytes("password");
// 定义列值
byte[] value1 = Bytes.toBytes("Alice");
byte[] value2 = Bytes.toBytes("female");
byte[] value3 = Bytes.toBytes("password123");
// 创建Put对象
Put put = new Put(rowKey);
// 添加列族、列限定符和列值
put.addColumn(cf1, qf1, value1);
put.addColumn(cf1, qf2, value2);
put.addColumn(cf1, qf3, value3);
// 插入数据
table.put(put);
// 关闭连接
table.close();
connection.close();
System.out.println("Data added.");
}
}
```
最后,您可以使用以下代码实现根据用户名和密码的登录功能:
```java
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
public class HBaseLogin {
public static void main(String[] args) throws Exception {
// 配置HBase连接参数
Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", "localhost");
config.set("hbase.zookeeper.property.clientPort", "2181");
// 建立HBase连接
Connection connection = ConnectionFactory.createConnection(config);
// 获取表对象
TableName tableName = TableName.valueOf("user");
Table table = connection.getTable(tableName);
// 定义行键
byte[] rowKey = Bytes.toBytes("user1");
// 创建Get对象
Get get = new Get(rowKey);
// 添加列族和列限定符
byte[] cf1 = Bytes.toBytes("personal");
byte[] qf1 = Bytes.toBytes("name");
byte[] qf3 = Bytes.toBytes("password");
get.addColumn(cf1, qf1);
get.addColumn(cf1, qf3);
// 获取数据
Result result = table.get(get);
// 检查用户名和密码是否正确
byte[] name = result.getValue(cf1, qf1);
byte[] password = result.getValue(cf1, qf3);
if (Bytes.toString(name).equals("Alice") && Bytes.toString(password).equals("password123")) {
System.out.println("Login successful.");
} else {
System.out.println("Login failed.");
}
// 关闭连接
table.close();
connection.close();
}
}
```
希望这些代码对您有所帮助!
阅读全文