mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-02-03 23:40:14 +08:00
Merge remote-tracking branch 'LCTT/master'
This commit is contained in:
commit
b6ecace06c
265
published/20191007 Using the Java Persistence API.md
Normal file
265
published/20191007 Using the Java Persistence API.md
Normal file
@ -0,0 +1,265 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (runningwater)
|
||||
[#]: reviewer: (wxy)
|
||||
[#]: publisher: (wxy)
|
||||
[#]: url: (https://linux.cn/article-11717-1.html)
|
||||
[#]: subject: (Using the Java Persistence API)
|
||||
[#]: via: (https://opensource.com/article/19/10/using-java-persistence-api)
|
||||
[#]: author: (Stephon Brown https://opensource.com/users/stephb)
|
||||
|
||||
使用 Java 持久化 API
|
||||
======
|
||||
|
||||
> 我们通过为自行车商店构建示例应用程序来学习如何使用 JPA。
|
||||
|
||||
![](https://img.linux.net.cn/data/attachment/album/201912/27/000705dymv92hnba2a2322.jpg)
|
||||
|
||||
对应用开发者来说,<ruby>Java 持久化 API<rt>Java Persistence API</rt></ruby>(JPA)是一项重要的 java 功能,需要透彻理解。它为 Java 开发人员定义了如何将对象的方法调用转换为访问、持久化及管理存储在 NoSQL 和关系型数据库中的数据的方案。
|
||||
|
||||
本文通过构建自行车借贷服务的教程示例来详细研究 JPA。此示例会使用 Spring Boot 框架、MongoDB 数据库([已经不开源][2])和 Maven 包管理来构建一个大型应用程序,并且构建一个创建、读取、更新和删除(CRUD)层。这儿我选择 NetBeans 11 作为我的 IDE。
|
||||
|
||||
此教程仅从开源的角度来介绍 Java 持久化 API 的工作原理,不涉及其作为工具的使用说明。这全是关于编写应用程序模式的学习,但对于理解具体的软件实现也很益处。可以从我的 [GitHub 仓库][3]来获取相关代码。
|
||||
|
||||
### Java: 不仅仅是“豆子”
|
||||
|
||||
Java 是一门面向对象的编程语言,自 1996 年发布第一版 Java 开发工具(JDK)起,已经变化了很多很多。要了解其各种发展及其虚拟机本身就是一堂历史课。简而言之,和 Linux 内核很相似,自发布以来,该语言已经向多个方向分支发展。有对社区免费的标准版本、有针对企业的企业版本及由多家供应商提供的开源替代品。主要版本每六个月发布一次,其功能往往差异很大,所以确认选用版本前得先做些研究。
|
||||
|
||||
总而言之,Java 的历史很悠久。本教程重点介绍 Java 11 的开源实现 [JDK 11][4]。因其是仍然有效的长期支持版本之一。
|
||||
|
||||
* **Spring Boot** 是由 Pivotal 公司开发的大型 Spring 框架的一个模块。Spring 是 Java 开发中一个非常流行的框架。它支持各种框架和配置,也为 WEB 应用程序及安全提供了保障。Spring Boot 为快速构建各种类型的 Java 项目提供了基本的配置。本教程使用 Spring Boot 来快速编写控制台应用程序并针对数据库编写测试用例。
|
||||
* **Maven** 是由 Apache 开发的项目/包管理工具。Maven 通过 `POM.xml` 文件来管理包及其依赖项。如果你使用过 NPM 的话,可能会非常熟悉包管理器的功能。此外 Maven 也用来进行项目构建及生成功能报告。
|
||||
* **Lombok** 是一个库,它通过在对象文件里面添加注解来自动创建 getters/setters 方法。像 C# 这些语言已经实现了此功能,Lombok 只是把此功能引入 Java 语言而已。
|
||||
* **NetBeans** 是一款很流行的开源 IDE,专门用于 Java 开发。它的许多工具都随着 Java SE 和 EE 的版本更新而更新。
|
||||
|
||||
我们会用这组工具为一个虚构自行车商店创建一个简单的应用程序。会实现对 `Customer` 和 `Bike` 对象集合的的插入操作。
|
||||
|
||||
### 酿造完美
|
||||
|
||||
导航到 [Spring Initializr][5] 页面。该网站可以生成基于 Spring Boot 和其依赖项的基本项目。选择以下选项:
|
||||
|
||||
1. **项目:** Maven 工程
|
||||
2. **语言:** Java
|
||||
3. **Spring Boot:** 2.1.8(或最稳定版本)
|
||||
4. **项目元数据:** 无论你使用什么名字,其命名约定都是像 `com.stephb` 这样的。
|
||||
* 你可以保留 Artifact 名字为 “Demo”。
|
||||
5. **依赖项:** 添加:
|
||||
* Spring Data MongoDB
|
||||
* Lombok
|
||||
|
||||
点击 **下载**,然后用你的 IDE(例如 NetBeans) 打开此新项目。
|
||||
|
||||
#### 模型层概要
|
||||
|
||||
在项目里面,<ruby>模型<rt>model</rt></ruby>代表从数据库里取出的信息的具体对象。我们关注两个对象:`Customer` 和 `Bike`。首先,在 `src` 目录创建 `dto` 目录;然后,创建两个名为 `Customer.java` 和 `Bike.java` 的 Java 类对象文件。其结构如下示:
|
||||
|
||||
```Java
|
||||
package com.stephb.JavaMongo.dto;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.data.annotation.Id;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author stephon
|
||||
*/
|
||||
@Getter @Setter
|
||||
public class Customer {
|
||||
|
||||
private @Id String id;
|
||||
private String emailAddress;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private String address;
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
*Customer.Java*
|
||||
|
||||
```Java
|
||||
package com.stephb.JavaMongo.dto;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.data.annotation.Id;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author stephon
|
||||
*/
|
||||
@Getter @Setter
|
||||
public class Bike {
|
||||
private @Id String id;
|
||||
private String modelNumber;
|
||||
private String color;
|
||||
private String description;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "This bike model is " + this.modelNumber + " is the color " + this.color + " and is " + description;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
*Bike.java*
|
||||
|
||||
如你所见,对象中使用 Lombok 注解来为定义的<ruby>属性<rt>properties</rt></ruby>/<ruby>特性<rt>attributes</rt></ruby>生成 getters/setters 方法。如果你不想对该类的所有特性都生成 getters/setters 方法,可以在属性上专门定义这些注解。这两个类会变成容器,里面携带有数据,无论在何处想显示信息都可以使用。
|
||||
|
||||
#### 配置数据库
|
||||
|
||||
我使用 [Mongo Docker][7] 容器来进行此次测试。如果你的系统上已经安装了 MongoDB,则不必运行 Docker 实例。你也可以登录其官网,选择系统信息,然后按照安装说明来安装 MongoDB。
|
||||
|
||||
安装后,就可以使用命令行、GUI(例如 MongoDB Compass)或用于连接数据源的 IDE 驱动程序来与新的 MongoDB 服务器进行交互。到目前为止,可以开始定义数据层了,用来拉取、转换和持久化数据。需要设置数据库访问属性,请导航到程序中的 `applications.properties` 文件,然后添加如下内容:
|
||||
|
||||
```
|
||||
spring.data.mongodb.host=localhost
|
||||
spring.data.mongodb.port=27017
|
||||
spring.data.mongodb.database=BikeStore
|
||||
```
|
||||
|
||||
#### 定义数据访问对象/数据访问层
|
||||
|
||||
<ruby>数据访问层<rt>data access layer</rt></ruby>(DAL)中的<ruby>数据访问对象<rt>data access objects</rt></ruby>(DAO)定义了与数据库中的数据的交互过程。令人惊叹的就是在使用 `spring-boot-starter` 后,查询数据库的大部分工作已经完成。
|
||||
|
||||
让我们从 `Customer` DAO 开始。在 `src` 下的新目录 `dao` 中创建一个接口文件,然后再创建一个名为 `CustomerRepository.java` 的 Java 类文件,其内容如下示:
|
||||
|
||||
```
|
||||
package com.stephb.JavaMongo.dao;
|
||||
|
||||
import com.stephb.JavaMongo.dto.Customer;
|
||||
import java.util.List;
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author stephon
|
||||
*/
|
||||
public interface CustomerRepository extends MongoRepository<Customer, String>{
|
||||
@Override
|
||||
public List<Customer> findAll();
|
||||
public List<Customer> findByFirstName(String firstName);
|
||||
public List<Customer> findByLastName(String lastName);
|
||||
}
|
||||
```
|
||||
|
||||
这个类是一个接口,扩展或继承于 `MongoRepository` 类,而 `MongoRepository` 类依赖于 DTO (`Customer.java`)和一个字符串,它们用来实现自定义函数查询功能。因为你已继承自此类,所以你可以访问许多方法函数,这些函数允许持久化和查询对象,而无需实现或引用自己定义的方法函数。例如,在实例化 `CustomerRepository` 对象后,你就可以直接使用 `Save` 函数。如果你需要扩展更多的功能,也可以重写这些函数。我创建了一些自定义查询来搜索我的集合,这些集合对象是我自定义的元素。
|
||||
|
||||
`Bike` 对象也有一个存储源负责与数据库交互。与 `CustomerRepository` 的实现非常类似。其实现如下所示:
|
||||
|
||||
```
|
||||
package com.stephb.JavaMongo.dao;
|
||||
|
||||
import com.stephb.JavaMongo.dto.Bike;
|
||||
import java.util.List;
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author stephon
|
||||
*/
|
||||
public interface BikeRepository extends MongoRepository<Bike,String>{
|
||||
public Bike findByModelNumber(String modelNumber);
|
||||
@Override
|
||||
public List<Bike> findAll();
|
||||
public List<Bike> findByColor(String color);
|
||||
}
|
||||
```
|
||||
|
||||
#### 运行程序
|
||||
|
||||
现在,你已经有了一种结构化数据的方式,可以对数据进行提取、转换和持久化,然后运行这个程序。
|
||||
|
||||
找到 `Application.java` 文件(有可能不是此名称,具体取决于你的应用程序名称,但都会包含有 “application” )。在定义此类的地方,在后面加上 `implements CommandLineRunner`。这将允许你实现 `run` 方法来创建命令行应用程序。重写 `CommandLineRunner` 接口提供的 `run` 方法,并包含如下内容用来测试 `BikeRepository` :
|
||||
|
||||
```
|
||||
package com.stephb.JavaMongo;
|
||||
|
||||
import com.stephb.JavaMongo.dao.BikeRepository;
|
||||
import com.stephb.JavaMongo.dao.CustomerRepository;
|
||||
import com.stephb.JavaMongo.dto.Bike;
|
||||
import java.util.Scanner;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
|
||||
@SpringBootApplication
|
||||
public class JavaMongoApplication implements CommandLineRunner {
|
||||
@Autowired
|
||||
private BikeRepository bikeRepo;
|
||||
private CustomerRepository custRepo;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(JavaMongoApplication.class, args);
|
||||
}
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
String response = "";
|
||||
boolean running = true;
|
||||
while(running){
|
||||
System.out.println("What would you like to create? \n C: The Customer \n B: Bike? \n X:Close");
|
||||
response = scan.nextLine();
|
||||
if ("B".equals(response.toUpperCase())) {
|
||||
String[] bikeInformation = new String[3];
|
||||
System.out.println("Enter the information for the Bike");
|
||||
System.out.println("Model Number");
|
||||
bikeInformation[0] = scan.nextLine();
|
||||
System.out.println("Color");
|
||||
bikeInformation[1] = scan.nextLine();
|
||||
System.out.println("Description");
|
||||
bikeInformation[2] = scan.nextLine();
|
||||
|
||||
Bike bike = new Bike();
|
||||
bike.setModelNumber(bikeInformation[0]);
|
||||
bike.setColor(bikeInformation[1]);
|
||||
bike.setDescription(bikeInformation[2]);
|
||||
|
||||
bike = bikeRepo.save(bike);
|
||||
System.out.println(bike.toString());
|
||||
|
||||
|
||||
} else if ("X".equals(response.toUpperCase())) {
|
||||
System.out.println("Bye");
|
||||
running = false;
|
||||
} else {
|
||||
System.out.println("Sorry nothing else works right now!");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
其中的 `@Autowired` 注解会自动依赖注入 `BikeRepository` 和 `CustomerRepository` Bean。我们将使用这些类来从数据库持久化和采集数据。
|
||||
|
||||
已经好了。你已经创建了一个命令行应用程序。该应用程序连接到数据库,并且能够以最少的代码执行 CRUD 操作
|
||||
|
||||
### 结论
|
||||
|
||||
从诸如对象和类之类的编程语言概念转换为用于在数据库中存储、检索或更改数据的调用对于构建应用程序至关重要。Java 持久化 API(JPA)正是为 Java 开发人员解决这一难题的重要工具。你正在使用 Java 操纵哪些数据库呢?请在评论中分享。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/19/10/using-java-persistence-api
|
||||
|
||||
作者:[Stephon Brown][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[runningwater](https://github.com/runningwater)
|
||||
校对:[wxy](https://github.com/wxy)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/stephb
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/java-coffee-beans.jpg?itok=3hkjX5We (Coffee beans)
|
||||
[2]: https://www.techrepublic.com/article/mongodb-ceo-tells-hard-truths-about-commercial-open-source/
|
||||
[3]: https://github.com/StephonBrown/SpringMongoJava
|
||||
[4]: https://openjdk.java.net/projects/jdk/11/
|
||||
[5]: https://start.spring.io/
|
||||
[6]: http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+string
|
||||
[7]: https://hub.docker.com/_/mongo
|
||||
[8]: http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+exception
|
||||
[9]: http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+system
|
@ -1,26 +1,30 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (HankChow)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: reviewer: (wxy)
|
||||
[#]: publisher: (wxy)
|
||||
[#]: url: (https://linux.cn/article-11718-1.html)
|
||||
[#]: subject: (Why your Python code needs to be beautiful and explicit)
|
||||
[#]: via: (https://opensource.com/article/19/12/zen-python-beauty-clarity)
|
||||
[#]: author: (Moshe Zadka https://opensource.com/users/moshez)
|
||||
|
||||
为什么 Python 代码要写得美观而明确
|
||||
======
|
||||
欢迎阅读“Python 光明节(Pythonukkah)”系列文章,这个系列文章将会讨论《Python 之禅》。我们首先来看《Python 之禅》里的前两个原则:美观与明确。
|
||||
![Searching for code][1]
|
||||
|
||||
早在 1999 年,Python 的贡献者之一,Tim Peters 就提出了《[Python 之禅][2]》,直到二十年后的今天,《Python 之禅》中的 19 条原则仍然对整个社区都产生着深远的影响。为此,我们举行了这一次的“Python 光明节”。首先,我们会讨论《Python 之禅》中的前两个原则:美观和明确。
|
||||
> 欢迎阅读“Python 光明节(Pythonukkah)”系列文章,这个系列文章将会讨论《Python 之禅》。我们首先来看《Python 之禅》里的前两个原则:美观与明确。
|
||||
|
||||
> "Hanukkah is the Festival of Lights,
|
||||
> Instead of one day of presents, we get eight crazy nights."
|
||||
> —亚当·桑德勒, [光明节之歌][3]
|
||||
![](https://img.linux.net.cn/data/attachment/album/201912/27/091634drq96c2fojzp6okr.png)
|
||||
|
||||
早在 1999 年,Python 的贡献者之一,Tim Peters 就提出了《[Python 之禅][2]》,直到二十年后的今天,《Python 之禅》中的 19 条原则仍然对整个社区都产生着深远的影响。为此,就像庆典光明的<ruby>光明节<rt>Hanukkah</rt></ruby>一样,我们举行了这一次的“<ruby>Python 光明节<rt>Pythonukkah</rt></ruby>”。首先,我们会讨论《Python 之禅》中的前两个原则:美观和明确。
|
||||
|
||||
> “Hanukkah is the Festival of Lights,
|
||||
>
|
||||
> Instead of one day of presents, we get eight crazy nights.”
|
||||
>
|
||||
> —亚当·桑德勒,[光明节之歌][3]
|
||||
|
||||
### 美观胜于丑陋
|
||||
|
||||
著名的《[<ruby>计算机程序的构造和解释<rt>Structure and Interpretation of Computer Programs</rt></ruby>][4]》中有这么一句话:代码是写给人看的,只是恰好能让机器运行(Programs must be written for people to read and only incidentally for machines to execute.)。毕竟机器并不在乎代码的美观性,但人类在乎。
|
||||
著名的《[<ruby>计算机程序的构造和解释<rt>Structure and Interpretation of Computer Programs</rt></ruby>][4]》中有这么一句话:<ruby>代码是写给人看的,只是恰好能让机器运行。<rt>Programs must be written for people to read and only incidentally for machines to execute.</rt></ruby>机器并不在乎代码的美观性,但人类在乎。
|
||||
|
||||
阅读美观的代码对人们来说是一种享受,这就要求在整套代码中保持一致的风格。使用诸如 [Black][5]、[flake8][6]、[Pylint][7] 这一类工具能够有效地接近这一个目标。
|
||||
|
||||
@ -28,7 +32,7 @@
|
||||
|
||||
最后,个人的主观能动性也很重要,否则一切工具和流程都会变得毫无意义。只有意识到美观的重要性,才能主动编写出美观的代码。
|
||||
|
||||
这就是为什么美观在众多原则当中排到了首位,它让“美”成为了 Python 社区的一种价值。如果有人要问,”我们真的在乎美吗?“社区会以代码给出肯定的答案。
|
||||
这就是为什么美观在众多原则当中排到了首位,它让“美”成为了 Python 社区的一种价值。如果有人要问,”我们*真的*在乎美吗?“社区会以代码给出肯定的答案。
|
||||
|
||||
### 明确胜于隐晦
|
||||
|
||||
@ -38,7 +42,7 @@
|
||||
|
||||
这个问题已经是老生常谈了,但网络上很多流传已久的回答都是不准确的。在编写<ruby>元类<rt>metaclass</rt></ruby>时,显式指定 `self` 参数就显得毫无意义。如果你没有编写过元类,希望你可以尝试一下,这是很多 Python 程序员的必经之路。
|
||||
|
||||
显式指定 `self` 参数的原因并不是 Python 的设计者不想将这样的元类视为“默认”元类,而是因为第一个参数必须是显式的。
|
||||
显式指定 `self` 参数的原因并不是 Python 的设计者不想将这样的元类视为“默认”元类,而是因为第一个参数必须是*显式*的。
|
||||
|
||||
即使 Python 中确实允许非显式的情况存在(例如上下文变量),但我们还是应该提出疑问:某个东西是不是有存在的必要呢?如果非显式地传递参数会不会出现问题呢?有些时候,由于种种原因,这是会有问题的。总之,在写代码时一旦能够优先考虑到明确性,至少意味着能对不明确的地方提出疑问并对结果作出有效的估计。
|
||||
|
||||
@ -49,7 +53,7 @@ via: https://opensource.com/article/19/12/zen-python-beauty-clarity
|
||||
作者:[Moshe Zadka][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[HankChow](https://github.com/HankChow)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
校对:[wxy](https://github.com/wxy)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
@ -1,105 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Why Vim fans love the Herbstluftwm Linux window manager)
|
||||
[#]: via: (https://opensource.com/article/19/12/herbstluftwm-linux-desktop)
|
||||
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
|
||||
|
||||
Why Vim fans love the Herbstluftwm Linux window manager
|
||||
======
|
||||
This article is part of a special series of 24 days of Linux desktops.
|
||||
If you're a Vim fan, check out herbstluftwm, a tile-based Linux window
|
||||
manager that takes the "Vim way" as inspiration.
|
||||
![OpenStack source code \(Python\) in VIM][1]
|
||||
|
||||
Everybody loves Vim (aside from Dvorak and Emacs users). Vim is so popular that there are entire web browsers dedicated to navigating the web with Vim keybindings, a Vim mode in the wildly popular [Zsh][2] terminal emulator, and even a text editor. There's also a window manager called [herbstluftwm][3] that models itself partly after the "Vim way." Herbstluftwm does away with windows, as such, and replaces them with tiles, or quadrants, into which applications are loaded and used. You use the keyboard (**Alt+h**, **Alt+j**, **Alt+k**, and **Alt+l**) to navigate from one tile to another.
|
||||
|
||||
![Herbstluftwm][4]
|
||||
|
||||
Install herbstluftwm from your distribution's software repository. After installing it, log out of your current desktop session so you can log into your new one. By default, your session manager (KDM, GDM, LightDM, or XDM, depending on your setup) will continue to log you into your previous desktop, so you must override that before logging in.
|
||||
|
||||
With GDM:
|
||||
|
||||
![][5]
|
||||
|
||||
With SDDM:
|
||||
|
||||
![][6]
|
||||
|
||||
### Herbstluftwm desktop tour
|
||||
|
||||
The first time you log into herbstluftwm, you are greeted with nothing but a green screen with a darker green border around the edges. What you're seeing is the initial tile with no application loaded into it. To start the default application, xterm, press **Alt+Return**.
|
||||
|
||||
The documentation emphasizes the **$HOME/.config/herbstluftwm/autostart** configuration file as a way to start important applications when you log in. For applications you don't necessarily want to start every time you log in, you can use xterm as your launch daemon. As usual, placing an ampersand (**&**) symbol after the command returns control of the shell to you. To start Emacs, for instance:
|
||||
|
||||
|
||||
```
|
||||
`% emacs &`
|
||||
```
|
||||
|
||||
This launches an Emacs window in a new tile and returns you to a prompt.
|
||||
|
||||
![Emacs running in Herbstluftwm][7]
|
||||
|
||||
#### Switching tiles
|
||||
|
||||
To move from one tile to another, use the classic Vim navigation combination of **h**, **j**, **k**, or **l**, along with the **Alt** key. For example, to switch from the terminal to an application in a tile below it (i.e., at the bottom of the screen), press **Alt+j**. To navigate back up, **Alt+k**. Left and right navigations are **Alt+h** and **Alt+l**, respectively.
|
||||
|
||||
#### Split screen
|
||||
|
||||
You can manually split a screen vertically with **Alt+o** and horizontally with **Alt+u**.
|
||||
|
||||
To remove an empty tile, navigate into it and press **Alt+r**.
|
||||
|
||||
### Configuring herbstluftwm
|
||||
|
||||
Aside from the **Alt** keybindings, you communicate with herbstluftwm through the **herbstclient** command. This command can be used interactively from a shell, or you can preserve your preferences in a configuration file.
|
||||
|
||||
You can view all attributes available in Herbstluftwm with:
|
||||
|
||||
|
||||
```
|
||||
`$ herbstclient attr`
|
||||
```
|
||||
|
||||
Herbstluftwm's default behavior is defined in the default config file, which you can copy to your home directory and modify. Upon launch, herbstluftwm executes the commands contained in the config file. For instance, if you find it awkward to use keybindings centered around the **Alt** key, which is traditionally a key reserved for in-application shortcuts, you can change the key used to trigger herbstluftwm actions in the config file:
|
||||
|
||||
|
||||
```
|
||||
% mkdir ~/.config/herbstluftwm
|
||||
% cp /usr/xdg/herbstluftwm/autostart \
|
||||
~/.config/herbstluftwm
|
||||
% sed -i 's/Mod=Mod1/Mod=Mod4/' ~/.config/herbstluftwm
|
||||
% herbstclient reload
|
||||
```
|
||||
|
||||
This changes the herbstluftwm modifier to the Super key (the "Windows" or "Tux" key, depending on your keyboard).
|
||||
|
||||
Using the autostart file, you can set custom keybindings, create tags for applications of a specific type so you can tile applications in a consistent way, and do much more.
|
||||
|
||||
### Why you need to try herbstluftwm
|
||||
|
||||
Herbstluftwm is a fine example of a tiling window manager. It tiles windows by default and lets the user define exceptions to global rules. It uses Vim-like navigation but allows for quick and easy overrides. It's very likely the tiling manager you've been looking for, so try it soon.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/19/12/herbstluftwm-linux-desktop
|
||||
|
||||
作者:[Seth Kenlon][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/seth
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/openstack_python_vim_2.jpg?itok=4fza48WU (OpenStack source code (Python) in VIM)
|
||||
[2]: https://opensource.com/article/19/9/getting-started-zsh
|
||||
[3]: https://herbstluftwm.org
|
||||
[4]: https://opensource.com/sites/default/files/uploads/advent-herbsluftwm.png (Herbstluftwm)
|
||||
[5]: https://opensource.com/sites/default/files/advent-gdm_1.jpg
|
||||
[6]: https://opensource.com/sites/default/files/advent-kdm_0.jpg
|
||||
[7]: https://opensource.com/sites/default/files/uploads/advent-herbsluftwm-emacs.jpg (Emacs running in Herbstluftwm)
|
@ -1,5 +1,5 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: ( )
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
|
@ -0,0 +1,86 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: ( )
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (10 Linux command tutorials for beginners and experts)
|
||||
[#]: via: (https://opensource.com/article/19/12/linux-commands)
|
||||
[#]: author: (Moshe Zadka https://opensource.com/users/moshez)
|
||||
|
||||
10 Linux command tutorials for beginners and experts
|
||||
======
|
||||
Learn how to make Linux do what you need it to do in Opensource.com's
|
||||
top 10 articles about Linux commands from 2019.
|
||||
![Penguin driving a car with a yellow background][1]
|
||||
|
||||
Using Linux _well_ means understanding what commands are available and what they're capable of doing for you. We have covered a lot of them on Opensource.com during 2019, and here are 10 favorites from the bunch.
|
||||
|
||||
### Using the force at the Linux command line
|
||||
|
||||
The Force has a light side and a dark side. Properly understanding that is crucial to true mastery. In his article [_Using the force at the Linux command line_][2], Alan Formy-Duval explains the **-f** option (also known as **\--force**) for several popular and sometimes dangerous commands.
|
||||
|
||||
### Intro to the Linux useradd command
|
||||
|
||||
Sharing accounts is a bad idea. Instead, give separate accounts to different people (and even different roles) with the quintessential **useradd** command. Part of his venerable series on basic Linux administration, Alan Formy-Duval provides an [_Intro to the Linux useradd command_][3], and, as usual, he explains it in _plain English_ so that both new and experienced admins can understand it.
|
||||
|
||||
### Linux commands to display your hardware information
|
||||
|
||||
What's _inside_ the box? Sometimes it's useful to inspect your hardware without using a screwdriver. In [_Linux commands to display your hardware information_][4], Howard Fosdick provides both popular and obscure commands to help you dig deep into the computer you're using, the computer you're testing at the store before buying, or the computer you're trying to repair.
|
||||
|
||||
### How to encrypt files with gocryptfs on Linux
|
||||
|
||||
Our files hold lots of private data, from social security numbers to personal letters to loved ones. In [_How to encrypt files with gocryptfs on Linux_][5], Brian "Bex" Exelbierd explains how to keep *private *what's meant to be private. As a bonus, he demonstrates encrypting files in a way that has little to no impact on your existing workflow. This isn't a complex PGP-style puzzle of key management and background key agents; this is quick, seamless, and secure file encryption.
|
||||
|
||||
### How to use advanced rsync for large Linux backups
|
||||
|
||||
In the New Year, many people will resolve to be more diligent about making backups. Alan Formy-Duval must have made that resolution years ago, because in [_How to use advanced rsync for large Linux backups_][6], he displays remarkable familiarity with the file synchronization command. You might not remember all the syntax right away, but the idea is to read and process the options, construct your backup command, and then automate it. That's the smart way to use **rsync**, and it's the _only_ way to do backups reliably.
|
||||
|
||||
### Using more to view text files at the Linux command line
|
||||
|
||||
In Scott Nesbitt's article [_Using more to view text files at the Linux command line_][7], the good old default pager **more** finally gets the spotlight. Many people install and use **less**, because it's more flexible than **more**. However, with more and more systems being implemented in the sparsest of containers, the luxury of fancy new tools like **less** or **most** sometimes just doesn't exist. Knowing and using **more** is simple, it's a common default, and it's the production system's debugging tool of last resort.
|
||||
|
||||
### What you probably didn't know about sudo
|
||||
|
||||
The **sudo** command is famous to a fault. People know the **sudo** term, and most of us believe we know what it does. And we're a little bit correct, but as Peter Czanik reveals in his article [_What you probably didn't know about sudo_][8], there's a lot more to the command than just "Simon says." Like that classic childhood game, the **sudo** command is powerful and also prone to silly mistakes—only with greater potential for horrible consequences. This is one game you do not want to lose!
|
||||
|
||||
### How to program with Bash: Syntax and tools
|
||||
|
||||
If you're a Linux, BSD, or Mac (and lately, Windows) user, you may have used the Bash shell interactively. It's a great shell for quick, one-off commands, which is why so many Linux users love to use it as their primary user interface. However, Bash is much more than just a command prompt. It's also a programming language, and if you're already using Bash commands, then the path to automation has never been more straightforward. Learn all about it in David Both's excellent [_How to program with Bash: Syntax and tools_][9].
|
||||
|
||||
### Master the Linux ls command
|
||||
|
||||
The **ls** command is one of those commands that merits a two-letter name; one-letter commands are an optimization for slow terminals where each letter causes a significant delay and also a nice bonus for lazy typists. Seth Kenlon explains how you can [_Master the Linux ls command_][10] and he does so with his usual clarity and pragmatism. Most significantly, in a system where "everything is a file," being able to list the files is crucial.
|
||||
|
||||
### Getting started with the Linux cat command
|
||||
|
||||
The **cat** command (short for con_cat_enate) is deceptively simple. Whether you use it to quickly see the contents of a file or to pipe the contents to another command, you may not be using **cat** to its full potential. Alan Formy-Duval's elucidating [_Getting started with the Linux cat command_][11] offers new ideas to take advantage of a command that lets you open a file without feeling like you've opened it. As a bonus, learn all about **zcat** so you can decompress files without all the trouble of decompression! It's a small and simple thing, but _this_ is what makes Linux great.
|
||||
|
||||
### Continue the journey
|
||||
|
||||
Don't let Opensource.com's 10 best articles about Linux commands of 2019 be the end of your journey. There's much more to discover about Linux and its versatile prompt, so stay tuned in 2020 for more insights. And, if there's a Linux command you want us to know about, please tell us about it in the comments, or share your knowledge with Opensource.com readers by [submitting an article][12] about your favorite Linux command.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/19/12/linux-commands
|
||||
|
||||
作者:[Moshe Zadka][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/moshez
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/car-penguin-drive-linux-yellow.png?itok=twWGlYAc (Penguin driving a car with a yellow background)
|
||||
[2]: https://opensource.com/article/19/5/may-the-force-linux
|
||||
[3]: https://opensource.com/article/19/10/linux-useradd-command
|
||||
[4]: https://opensource.com/article/19/9/linux-commands-hardware-information
|
||||
[5]: https://opensource.com/article/19/8/how-encrypt-files-gocryptfs
|
||||
[6]: https://opensource.com/article/19/5/advanced-rsync
|
||||
[7]: https://opensource.com/article/19/1/more-text-files-linux
|
||||
[8]: https://opensource.com/article/19/10/know-about-sudo
|
||||
[9]: https://opensource.com/article/19/10/programming-bash-syntax-tools
|
||||
[10]: https://opensource.com/article/19/7/master-ls-command
|
||||
[11]: https://opensource.com/article/19/2/getting-started-cat-command
|
||||
[12]: https://opensource.com/how-submit-article
|
@ -0,0 +1,123 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: ( )
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (10 articles to become more data science savvy)
|
||||
[#]: via: (https://opensource.com/article/19/12/data-science-resources)
|
||||
[#]: author: (Lauren Maffeo https://opensource.com/users/lmaffeo)
|
||||
|
||||
10 articles to become more data science savvy
|
||||
======
|
||||
Boost your data science game in 2020 with Opensource.com's top 10
|
||||
most-read articles on the topic from 2019.
|
||||
![Open data brain][1]
|
||||
|
||||
When LinkedIn released its third annual [Emerging Jobs report][2], engineers everywhere said, "Amen." More than half the list consists of engineering roles, with new fields like robotics appearing for the first time.
|
||||
|
||||
But data science had a strong showing as well. The role shows 37% annual growth, topping that aspect of the Emerging Jobs list for the third year in a row.
|
||||
|
||||
Looking at the core skills a data scientist needs—including R, Python, and Apache Spark—it's easy to find overlaps with open source. So, we're not surprised that data science was one of the most popular topics at Opensource.com in 2019.
|
||||
|
||||
We saw a need for knowledge about diverse data science topics. And our community of authors delivered answers.
|
||||
|
||||
For your reading pleasure, we've listed the top 10 data science articles of 2019. We define "top" as the data science articles that were published in 2019 and earned the most page views, starting with the most popular.
|
||||
|
||||
Whether you want to use Kubernetes for batch jobs or query 10 years' worth of GitHub data, these articles will boost your data science game in 2020.
|
||||
|
||||
### Why data scientists love Kubernetes
|
||||
|
||||
Kubernetes is having more than a moment. That's due in no small part to its versatility. You might already know that Kubernetes helps software developers and system operators deploy applications in Linux containers. But did you know how helpful it can be for data science as well?
|
||||
|
||||
In [_Why data scientists love Kubernetes_][3], our most popular data science article in 2019, William Benton and Sophie Watson share how Kubernetes supports the data science workflow. From repeatable batch jobs to debugging ML models, this article shares several ways for data scientists to leverage Kubernetes.
|
||||
|
||||
### How to use Spark SQL: A hands-on tutorial
|
||||
|
||||
Wondering how to use a cloud service for big data analytics? [_How to use Spark SQL: A hands-on tutorial_][4] uses Spark DataFrames to show how to use relational databases at scale. DJ Sarkar uses a real-world dataset to walk readers through the process of using Spark SQL.
|
||||
|
||||
Rich with screenshots and code, Sarkar's tutorial is the ideal sequel to [his first piece][5] on this subject. He shares several ways that you can use Spark to manage structured data obtained from flat files or databases.
|
||||
|
||||
### 9 resources for data science projects
|
||||
|
||||
The growth of data science in open source—from machine learning to neural networks—has left many engineers wanting to learn more. In [_9 resources for data science projects_][6], Dan Barker shares the books, tools, and online courses he thinks are a must for any engineer who wants to get started.
|
||||
|
||||
Barker is especially keen on Cathy O'Neil's book [_Weapons of Math Destruction_][7], which shares how bias creeps into data and how you can stop it. He also shares a range of websites for newbies to explore.
|
||||
|
||||
### Getting started with data science using Python
|
||||
|
||||
Alongside the rise of data science techniques, Python has seen a meteoric rise. It's now one of the most popular programming languages. When used with libraries like pandas and Seaborn, Python is an ideal entry to data science.
|
||||
|
||||
In [_Getting started with data science using Python_][8], a follow-up to his [intro to Python][9] article, Seth Kenlon shares how to create a Python virtual environment; install pandas and NumPy; create a sample dataset; and much more. This article is an especially good read if you want to learn more about data visualization.
|
||||
|
||||
### How to analyze log data with Python and Apache Spark
|
||||
|
||||
Like many articles in our top 10 list, [_How to analyze log data with Python and Apache Spark_][10] is a sequel to [an earlier article][11] on using Python and Apache Spark to wrangle data. Once you've learned how to put your data into a clean, structured format, DJ Sarkar offers this piece to help you analyze that data.
|
||||
|
||||
Whether you want to see the top 10 error endpoints or content size statistics, Sarkar shows you how to analyze several types of log data in your [DataFrame][12]. The data that he uses isn't "big data" from a size or volume standpoint. But these techniques can scale for use with larger datasets.
|
||||
|
||||
### How to wrangle log data with Python and Apache Spark
|
||||
|
||||
[_How to wrangle log data with Python and Apache Spark_][11], DJ Sarkar's prequel to his piece on analyzing log data, also made our top 10 list. It's no surprise since most organizations use a range of systems and infrastructure that run constantly. Data logs are an ideal way to make sure that everything keeps working effectively.
|
||||
|
||||
In this tutorial, Sarkar shows how to use Apache Spark on real-world production logs from NASA. He walks through the process of using Spark to do log analytics at scale on semi-structured log data. This ranges from setting up dependencies to data wrangling.
|
||||
|
||||
### Querying 10 years of GitHub data with GHTorrent and Libraries.io
|
||||
|
||||
Did you know that you can use Kibana or the Elasticsearch API to turn Amazon S3 object-storage data into a searchable Elasticsearch-type cluster? Likewise, did you know about the project that aims to build an offline version of all data available through GitHub APIs?
|
||||
|
||||
In [_Querying 10 years of GitHub data with GHTorrent and Libraries.io_][13], Pete Cheslock explores how to access and query GHTorrent data. You can do it using several formats, including CSV and Google Big Query. Cheslock uses the latter to search indexed GHTorrent data to learn which software languages, licenses, and rates of growth are most popular for GitHub projects.
|
||||
|
||||
### Predicting NFL play outcomes with Python and data science
|
||||
|
||||
Want to increase your machine learning skills in Python? With the NFL playoff season upon us, it's a great time to read [_Predicting NFL play outcomes with Python and data science_][14], which shares some data science tips to predict plays.
|
||||
|
||||
Christa Hayes shows how to spot weird values, predict downs and play types, make regression plots, and train models. Once you've read her article on [how to format data for training][15], this one is the ideal next step.
|
||||
|
||||
### Analyzing the Stack Overflow Survey with Python and Pandas
|
||||
|
||||
Stack Overflow's annual developer survey is a tech behemoth. Nearly 90,000 developers took this year's 20-minute survey and left a lot of data in their wake.
|
||||
|
||||
To find certain results, Moshe Zadka used the pandas library to search the survey's [anonymized results][16]. If you want to filter Stack Overflow's dataset for certain details (like seeing how many developers use certain languages or contribute to open source projects), Moshe's [_Analyzing the Stack Overflow Survey with Python and Pandas_][17] tutorial shows you how.
|
||||
|
||||
### 4 Python tools for getting started with astronomy
|
||||
|
||||
For readers with their heads in the clouds, NumFOCUS republished some of its blog posts on Opensource.com this year. In [_4 Python tools for getting started with astronomy_][18], Dr. Gina Helfrich shares how you can get involved in astronomy.
|
||||
|
||||
Intimidated? Don't be: Dr. Helfrich says Python packages are so advanced that building data-reduction scripts is much easier than ever before. If you want to play with astronomy imaging datasets, this piece will steer you in the right direction.
|
||||
|
||||
### What do you want to know about data science?
|
||||
|
||||
Data science is an exciting field with countless things to explore. If there's something you want to know about data science, please tell us about it in the comments so we can try to cover it in 2020. Or, if you are so inclined, please share your knowledge with Opensource.com readers by [submitting an article][19] about your favorite data science topic.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/19/12/data-science-resources
|
||||
|
||||
作者:[Lauren Maffeo][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/lmaffeo
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/osdc_520x292_opendata_0613mm.png?itok=UIjD_jhK (Open data brain)
|
||||
[2]: https://business.linkedin.com/content/dam/me/business/en-us/talent-solutions/emerging-jobs-report/Emerging_Jobs_Report_U.S._FINAL.pdf
|
||||
[3]: https://opensource.com/article/19/1/why-data-scientists-love-kubernetes
|
||||
[4]: https://opensource.com/article/19/3/apache-spark-and-dataframes-tutorial
|
||||
[5]: https://opensource.com/article/19/3/sql-scale-apache-spark-sql-and-dataframes
|
||||
[6]: https://opensource.com/article/19/2/learn-data-science-ai
|
||||
[7]: https://www.amazon.com/Weapons-Math-Destruction-Increases-Inequality/dp/0553418815
|
||||
[8]: https://opensource.com/article/19/9/get-started-data-science-python
|
||||
[9]: https://opensource.com/article/17/10/python-101
|
||||
[10]: https://opensource.com/article/19/5/visualize-log-data-apache-spark
|
||||
[11]: https://opensource.com/article/19/5/log-data-apache-spark
|
||||
[12]: https://pandas.pydata.org/pandas-docs/stable/getting_started/dsintro.html#dataframe
|
||||
[13]: https://opensource.com/article/19/5/chaossearch-github-ghtorrent
|
||||
[14]: https://opensource.com/article/19/10/predicting-nfl-plays-python
|
||||
[15]: https://opensource.com/article/19/10/formatting-nfl-data-python
|
||||
[16]: https://insights.stackoverflow.com/survey
|
||||
[17]: https://opensource.com/article/19/9/stack-overflow-survey-python-pandas
|
||||
[18]: https://opensource.com/article/19/10/python-astronomy-open-data
|
||||
[19]: https://opensource.com/how-submit-article
|
@ -0,0 +1,56 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: ( )
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (How the Zen of Python handles errors)
|
||||
[#]: via: (https://opensource.com/article/19/12/zen-python-errors)
|
||||
[#]: author: (Moshe Zadka https://opensource.com/users/moshez)
|
||||
|
||||
How the Zen of Python handles errors
|
||||
======
|
||||
This is part of a special series about the Zen of Python focusing on the
|
||||
10th and 11th principles: on the silence (or not) of errors.
|
||||
![a checklist for a team][1]
|
||||
|
||||
Handling "exceptional conditions" is one of the most debated issues in programming. That could be because the stakes are high: mishandled error values can bring down even the largest systems. Since "exception conditions," by nature, are the least tested but occur with unpleasant frequency, correctly handling them can often distinguish a system that horror stories are told about to a system that "just works."
|
||||
|
||||
From Java's **checked** exceptions through Erlang's fault isolation to Haskell's **Maybe**, different languages have remarkably different attitudes to error handling.
|
||||
|
||||
The [Zen][2] offers Python's meditation on the topic.
|
||||
|
||||
### Errors should never pass silently…
|
||||
|
||||
Before the Zen of Python was a twinkle in Tim Peters' eye, before Wikipedia became informally known as "wiki," the first WikiWiki site, [C2][3], existed as a trove of programming guidelines. These are principles that mostly came out of a [Smalltalk][4] programming community. Smalltalk's ideas influenced many object-oriented languages, Python included.
|
||||
|
||||
The C2 wiki defines the Samurai Principle: "return victorious, or not at all." In Pythonic terms, it encourages eschewing sentinel values, such as returning **None** or **-1** to indicate an inability to complete the task, in favor of raising exceptions. A **None** is silent: it looks like a value and can be put in a variable and passed around. Sometimes, it is even a _valid_ return value.
|
||||
|
||||
The principle here is that if a function cannot accomplish its contract, it should "fail loudly": raise an exception. The raised exception will never look like a possible value. It will skip past the **returned_value = call_to_function(parameter)** line and go up the stack, potentially crashing the program.
|
||||
|
||||
A crash is straightforward to debug: there is a stack trace indicating the problem as well as the call stack. The failure might mean that a necessary condition for the program was not met, and human intervention is needed. It might mean that the program's logic is faulty. In either case, the loud failure is better than a hidden, "missing" value, infecting the program's valid data with **None**, until it is used somewhere and an error message says "**None does not have method split**," which you probably already knew.
|
||||
|
||||
### Unless explicitly silenced.
|
||||
|
||||
Exceptions sometimes need to be explicitly caught. We might anticipate some of the lines in a file are misformatted and want to handle those in a special way, maybe by putting them in a "lines to be looked at by a human" file, instead of crashing the entire program.
|
||||
|
||||
Python allows us to catch exceptions with **except**. This means errors can be _explicitly_ silenced. This explicitness means that the **except** line is visible in code reviews. It makes sense to question why this is the right place to silence, and potentially recover from, the exception. It makes sense to ask if we are catching too many exceptions or too few.
|
||||
|
||||
Because this is all explicit, it is possible for someone to read the code and understand which exceptional conditions are recoverable.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/19/12/zen-python-errors
|
||||
|
||||
作者:[Moshe Zadka][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/moshez
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/checklist_hands_team_collaboration.png?itok=u82QepPk (a checklist for a team)
|
||||
[2]: https://www.python.org/dev/peps/pep-0020/
|
||||
[3]: https://wiki.c2.com/
|
||||
[4]: https://en.wikipedia.org/wiki/Smalltalk
|
@ -0,0 +1,72 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: ( )
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Top 10 Raspberry Pi articles of 2019)
|
||||
[#]: via: (https://opensource.com/article/19/12/raspberry-pi-2019)
|
||||
[#]: author: (Joshua Allen Holm https://opensource.com/users/holmja)
|
||||
|
||||
Top 10 Raspberry Pi articles of 2019
|
||||
======
|
||||
Take a look back at the year in Raspberry Pi and get excited for what's
|
||||
to come in 2020.
|
||||
![Raspberries with pi symbol overlay][1]
|
||||
|
||||
Since its introduction, the Raspberry Pi has been one of the hottest topics on Opensource.com. This trend continued in 2019 with an impressive list of over 30 articles about various Raspberry Pi projects. The top 10 articles are covered below, but if you are a true Raspberry Pi aficionado, I encourage you to check out out our entire selection of [Raspberry Pi articles][2].
|
||||
|
||||
1. _[Turn a Raspberry Pi 3B+ into a PriTunl VPN][3]_ by Stephen Bancroft demonstrates how to set up a PriTunl virtual private networking (VPN) on a Raspberry Pi 3B+. There are several sticky points about the process that Bancroft helpfully and clearly explains. Following his instructions, you can easily turn a Raspberry Pi into a VPN server.
|
||||
|
||||
2. In _[Let’s get physical: How to use GPIO pins on the Raspberry Pi][4]_, Anderson Silva provides a brief introduction to using the GPIO pins on a Raspberry Pi. The article is full of links to other resources, including some of Silva’s other pieces, for those who want to dig deeper into the topic.
|
||||
|
||||
3. Sam Bocetta’s article _[How to use your Raspberry Pi as a VPN server][5]_, demonstrates how to install and configure PiVPN on a Raspberry Pi to create a VPN server. The article covers every aspect of the installation and configuration process, so if the reader has followed all the instructions provided, they will end up with their own Raspberry Pi-powered VPN.
|
||||
|
||||
4. _[Program the real world using Rust on Raspberry Pi][6]_ by Rahul Thakoor explores how to use the Rust programming language on the Raspberry Pi. By adding **rust_gpiozero** to a Rust project, it is possible to write code in Rust that can interact with buttons, servos, and other hardware connected to a Raspberry Pi. This article shows you how to get started.
|
||||
|
||||
5. Ben Nuttall, Raspberry Pi community manager, proclaims _[Raspberry Pi 4 is here!][7]_ in this article covering the release of the Raspberry Pi 4. The Raspberry Pi 4 features many improvements over previous Raspberry Pi models, and Nuttall walks through them in detail. From the new hardware features to the software updates in the updated release of Raspbian, this article brings you up to speed on what the Raspberry Pi 4 brings to the Raspberry Pi ecosystem.
|
||||
|
||||
6. _[How to build a WiFi picture frame with a Raspberry Pi][8]_ by Manuel Dewald shows you how to create a WiFi-connected digital picture frame using a Raspberry Pi 3. This step-by-step tutorial provides all the instructions needed to configure the software, but if you want to make a fancy case for your digital picture frame, that part is up to you.
|
||||
|
||||
7. Anderson Silva’s _[How to boot up a new Raspberry Pi][9]_ explains what you need to do to boot an operating system on a Raspberry Pi. This article primarily provides an overview of NOOBS (New Out Of Box Software) and contains links to the official Raspberry Pi website for the most up-to-date installation instructions.
|
||||
|
||||
8. _[How to build a mobile particulate matter sensor with a Raspberry Pi][10]_ by Stephan Tetzel illustrates how to build a sensor that can monitor your air quality using a Raspberry Pi, a small LCD display, and an inexpensive sensor. The particulate matter sensor you create can be used anywhere when paired with a power bank.
|
||||
|
||||
9. _[3 popular programming languages you can learn with Raspberry Pi][11]_ by Anderson Silva looks at programming Python, Java, and JavaScript on the Raspberry Pi. The Thonny IDE for Python and the BlueJ IDE for Java are briefly covered. The article also encourages users who are interested in other programming languages with the assurance that "there’s a high likelihood that you can use your Raspberry Pi to compile or interpret any language of choice, including C, C++, PHP, and Ruby."
|
||||
|
||||
10. Take a trip down memory lane by _[Resurrecting the Amiga on the Raspberry Pi][12]_ with this guide by Sarah Thornton. Using the instructions provided, you can experience the Amiga again on a Raspberry Pi or several other devices. Thornton even provides a helpful set of links for those interested in learning more about the history of the Amiga.
|
||||
|
||||
|
||||
|
||||
|
||||
I hope these articles encourage you to explore the possibilities of the Raspberry Pi. Maybe they have even inspired you to [share your own Raspberry Pi story][13] with us? Your story could end up on this list next year!
|
||||
|
||||
In this month's Raspberry Pi column, Ben Nuttall introduces readers to five projects that you might...
|
||||
|
||||
Owning a little cloud has a lot of benefits and can save you over $100 per month. In this step-by-...
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/19/12/raspberry-pi-2019
|
||||
|
||||
作者:[Joshua Allen Holm][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/holmja
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/life-raspberrypi_0.png?itok=Kczz87J2 (Raspberries with pi symbol overlay)
|
||||
[2]: https://opensource.com/tags/raspberry-pi
|
||||
[3]: https://opensource.com/article/19/1/pritunl-vpn-raspberry-pi
|
||||
[4]: https://opensource.com/article/19/3/gpio-pins-raspberry-pi
|
||||
[5]: https://opensource.com/article/19/6/raspberry-pi-vpn-server
|
||||
[6]: https://opensource.com/article/19/3/physical-computing-rust-raspberry-pi
|
||||
[7]: https://opensource.com/article/19/6/raspberry-pi-4
|
||||
[8]: https://opensource.com/article/19/2/wifi-picture-frame-raspberry-pi
|
||||
[9]: https://opensource.com/article/19/3/how-boot-new-raspberry-pi
|
||||
[10]: https://opensource.com/article/19/3/mobile-particulate-matter-sensor
|
||||
[11]: https://opensource.com/article/19/3/programming-languages-raspberry-pi
|
||||
[12]: https://opensource.com/article/19/3/amiga-raspberry-pi
|
||||
[13]: https://opensource.com/how-submit-article
|
@ -0,0 +1,120 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: ( )
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Explained! Why Your Distribution Still Using an ‘Outdated’ Linux Kernel?)
|
||||
[#]: via: (https://itsfoss.com/why-distros-use-old-kernel/)
|
||||
[#]: author: (Abhishek Prakash https://itsfoss.com/author/abhishek/)
|
||||
|
||||
Explained! Why Your Distribution Still Using an ‘Outdated’ Linux Kernel?
|
||||
======
|
||||
|
||||
[Check your Linux kernel version][1]. Chances are that you’ll find that the kernel version your system is using has already reached end of life (EOL) as listed on Linux Kernel website.
|
||||
|
||||
End of life means a software won’t get bug fixes and support anymore.
|
||||
|
||||
That poses some valid questions. Why is my Linux distribution using a kernel that has reached end of life? Is this not a security risk? Is my system safe?
|
||||
|
||||
Let me explain all these questions in this article.
|
||||
|
||||
Summary
|
||||
|
||||
The upstream kernel support and the your distribution’s kernel support are two different things.
|
||||
|
||||
For example, Linux kernel 4.15 might have reached end of life (as per the official Linux kernel website) but Ubuntu 18.04 LTS release will use it and maintain it till April 2023 by backporting security patches and bug fixes.
|
||||
|
||||
### Checking Linux kernel version and finding its end of life status
|
||||
|
||||
Let’s first check the Linux kernel version on your system:
|
||||
|
||||
```
|
||||
uname -r
|
||||
```
|
||||
|
||||
I am using Ubuntu 18.04 here and it shows the Linux kernel version like this:
|
||||
|
||||
```
|
||||
[email protected]:~$ uname -r
|
||||
5.0.0-37-generic
|
||||
```
|
||||
|
||||
Now, you may go to the official Linux kernel website and see what Linux kernels are still being supported. It’s displayed on the homepage itself.
|
||||
|
||||
[Linux Kernel Official Website][2]
|
||||
|
||||
You should see a status like this:
|
||||
|
||||
![Linux Kernel Status][3]
|
||||
|
||||
If you don’t see a kernel version listed on the homepage of kernel website, it means that specific version has reached end of life.
|
||||
|
||||
As you can see, kernel 5.0 is not listed here. It indicates that this kernel version is not being supported anymore. Actually, it [reached end of life in June 2019][4].
|
||||
|
||||
The life cycle of a Linux kernel doesn’t follow a set pattern, unfortunately. It’s NOT like a regular kernel stable release will be supported for X months and a long term support(LTS) kernel will be supported for Y years.
|
||||
|
||||
Based on the demand and requirements, there could be several LTS kernel versions with different EOL. You can find them along with their projected EOL on [this page][5].
|
||||
|
||||
Now comes the big question. Why is Ubuntu providing kernel 5.0 if the Linux kernel website shows that it has reached its end of life?
|
||||
|
||||
### Your distribution uses an EOL Linux kernel but that’s Okay!
|
||||
|
||||
![][6]
|
||||
|
||||
Have you ever wondered why Ubuntu/Debian/Fedora etc are called Linux distributions? It’s because they ‘distribute’ the Linux kernel.
|
||||
|
||||
They have their own modification of the Linux kernel, they add the GUI elements (desktop environment, display server etc) and software and they make it available to their users.
|
||||
|
||||
In the typical workflow, a Linux distribution will choose a kernel to provide to its users. And then it will hold on to this kernel for months or years even after the kernel has reached end of life.
|
||||
|
||||
How is it safe then? It’s because the _**distribution maintains the kernel by backporting all the important fixes to its kernel**_.
|
||||
|
||||
In other words, your Linux distribution makes sure that your Linux kernel is patched well and has all the bug fixes and important new features backported to it. There will be thousands of changes on top of the ‘old outdated Linux kernel’.
|
||||
|
||||
When the Linux kernel website says that a certain kernel version has reached EOL, it means that the core Linux kernel maintainers are not going to update/patch that kernel version anymore.
|
||||
|
||||
But at the same time, the developers at Debian/Ubuntu or other distributions work to keep the same old version alive by bringing the relevant changes from the newer kernel versions (being maintained by the core kernel team) to your distribution’s old kernel.
|
||||
|
||||
Bottom line is that even if it seems like your distribution is using an outdated Linux kernel, it is actually being well maintained and not really outdated.
|
||||
|
||||
### Should you use the latest stable kernel version?
|
||||
|
||||
![][7]
|
||||
|
||||
A new stable Linux kernel version is released every 2-3 months. And this makes many users wonder who they can get their hands on that new shiny thing.
|
||||
|
||||
To be frank, you should not do that unless you have a pretty good reason for it. Your distribution doesn’t provide it to you. You cannot just use ‘_sudo apt give-me-the-latest-stable-kernel_‘.
|
||||
|
||||
Now, manually [installing the mainline Linux kernel version][8] could be a challenge in itself. Even if you manage to install it, it is now up to you to make sure that this kernel is updated every time there is a bug fix. And when this new kernel reaches end of life, it becomes your responsibility to upgrade to the newer kernel version. It won’t be handled with apt upgrade like regular [Ubuntu updates][9].
|
||||
|
||||
You should also keep in mind that your distribution also has drivers and patches which you may not be able to use if you switch to the mainline kernel.
|
||||
|
||||
As [Greg Kroah-Hartman][10] puts it, “_**the best kernel you can use is one that someone else supports**_“. And who can be better at this job then your Linux distribution!
|
||||
|
||||
I hope you have a better understanding on this topic and you won’t panic the next time you find out that the kernel version your system is using has reached end of life.
|
||||
|
||||
I welcome your questions and suggestions. Please feel free to use the comment section.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://itsfoss.com/why-distros-use-old-kernel/
|
||||
|
||||
作者:[Abhishek Prakash][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://itsfoss.com/author/abhishek/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://itsfoss.com/find-which-kernel-version-is-running-in-ubuntu/
|
||||
[2]: https://www.kernel.org/
|
||||
[3]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2019/12/linux-kernel-status.jpg?ssl=1
|
||||
[4]: http://lkml.iu.edu/hypermail/linux/kernel/1906.0/02354.html
|
||||
[5]: https://www.kernel.org/category/releases.html
|
||||
[6]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2019/12/Keep_Calm_and_Trust_Your_Distribution.png?ssl=1
|
||||
[7]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2019/12/using_latest_kernel.png?ssl=1
|
||||
[8]: https://wiki.ubuntu.com/Kernel/MainlineBuilds
|
||||
[9]: https://itsfoss.com/update-ubuntu/
|
||||
[10]: https://en.wikipedia.org/wiki/Greg_Kroah-Hartman
|
@ -1,272 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (runningwater)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Using the Java Persistence API)
|
||||
[#]: via: (https://opensource.com/article/19/10/using-java-persistence-api)
|
||||
[#]: author: (Stephon Brown https://opensource.com/users/stephb)
|
||||
|
||||
使用 Java 持久化 API
|
||||
======
|
||||
我们通过为自行车商店构建示例应用程序来学习如何使用 JPA。
|
||||
![Coffee beans][1]
|
||||
|
||||
对应用开发者来说,Java 持久化 API (JPA)是一项重要的技能,需要透彻理解。它为 Java 开发人员定义了如何将对象的方法调用转换为访问、持久化及管理 NoSQL 和关系型数据库数据存储的问题的方案。
|
||||
|
||||
本文通过构建自行车借贷服务的教程示例来详细研究 JPA。此示例会使用 Spring Boot 框架、MongoDB 数据库([已经不开源][2])和 Maven 包管理来构建一个大型应用程序,并且创建一个创建、读取、更新和删除(CRUD) 层。这儿我选择 NetBeans 11 作为我的 IDE。
|
||||
|
||||
此教程仅从开源的角度来介绍 Java 持久化的工作原理,不涉及其作为工具的使用说明。这全是关于编写应用程序模式的学习,但对于理解具体的软件实现也很益处。可以从我的[ GitHub 源仓库][3] 来获取相关代码。
|
||||
|
||||
### Java: 不仅仅是 'beans'
|
||||
|
||||
Java 是一门面向对象的编程语言,自 1996 年发布第一版 Java 开发工具(JDK)起,已经变化了很多很多。要了解其特性和其虚拟机本身就是个很长的历史。简而言之,和 Linux 内核很相似,自发布以来,该语言已经向多个方向分支发展。有对社区免费的标准版本、有针对企业的企业版本及由多家供应商提供的开源替代品。主要版本每六个月发布一次,其功能往往差异很大,所以确认选用版本前得先做些研究。
|
||||
|
||||
总而言之,Java 的历史很悠久。本教程重点介绍 Java 11 的开源实现 [JDK 11][4]。因其是扔然有效的长期支持版本之一。
|
||||
|
||||
* **Spring Boot: ** Spring Boot 是由 Pivotal 公司开发大型 Spring 框架的一个模块。Spring 是 Java 开发中一个非常流行的框架。它支持各种框架和配置,也支持 WEB 应用程序及提供了应用安全保障功能。Spring Boot 为快速启动项目集成了基本的配置。本教程使用 Spring Boot 来快速编写控制台应用程序并针对数据库编写测试用例。
|
||||
* **Maven:** Maven 是由 Apache 开发的项目/包管理工具。Maven 通过 `POM.xml` 文件来管理包及其依赖项。如果你们使用过 NPM 的话,可能会非常熟悉包管理器的功能。此外 Maven 也用来进行项目构建及生成功能报告。
|
||||
* **Lombok:** Lombok 是一个库,它通过在对象文件里面添加注解来自动创建 getters/setters 方法。像 C# 这些语言已经实现了此功能,Lombok 只是把此功能引入 Java 语言而已。
|
||||
* **NetBeans: ** NetBeans 是一款很流行的开源 IDE,专门用于 Java 开发。它的许多工具都随着 Java SE 和 EE 的版本更新而更新。
|
||||
|
||||
|
||||
|
||||
我们会这组工具来创建一个简单的虚构自行车商店应用程序。会实现对 "Customer" 和 "Bike" 对象集合的的插入操作。
|
||||
|
||||
### 酿造完美
|
||||
|
||||
导航到 [Spring Initializr][5] 页面。该网站可以生成基于 Spring Boot 和其依赖项的基本项目。选择以下选项:
|
||||
|
||||
1. **项目:** Maven 工程
|
||||
2. **语言:** Java
|
||||
3. **Spring Boot:** 2.1.8 (或最稳定版本)
|
||||
4. **项目元数据:** 无论你使用什么名字,其命名约定都是像 **com.stephb** 这样的。
|
||||
* 你可以保留 Artifact 名字为 “Demo”。
|
||||
5. **依赖项:** 添加:
|
||||
* Spring Data MongoDB
|
||||
* Lombok
|
||||
|
||||
|
||||
|
||||
点击 **下载**,然后用你的 IDE(例如 NetBeans) 打开此新项目。
|
||||
|
||||
#### 模型层(Model)概要
|
||||
|
||||
项目里面,模型代表从数据库里取出的信息的具体对象。我们关注两个对象:**Customer** 和 **Bike**。首先,在 **src** 目录创建 **dto** 目录;然后,创建两个名为 **Customer.java** 和 **Bike.java** 的 Java 类对象文件。其结构如下示:
|
||||
|
||||
**Customer.Java**
|
||||
|
||||
|
||||
```
|
||||
1 package com.stephb.JavaMongo.dto;
|
||||
2
|
||||
3 import lombok.Getter;
|
||||
4 import lombok.Setter;
|
||||
5 import org.springframework.data.annotation.Id;
|
||||
6
|
||||
7 /**
|
||||
8 *
|
||||
9 * @author stephon
|
||||
10 */
|
||||
11 @Getter @Setter
|
||||
12 public class Customer {
|
||||
13
|
||||
14 private @Id String id;
|
||||
15 private String emailAddress;
|
||||
16 private String firstName;
|
||||
17 private String lastName;
|
||||
18 private String address;
|
||||
19
|
||||
20 }
|
||||
```
|
||||
|
||||
**Bike.java**
|
||||
|
||||
|
||||
```Java
|
||||
1 package com.stephb.JavaMongo.dto;
|
||||
2
|
||||
3 import lombok.Getter;
|
||||
4 import lombok.Setter;
|
||||
5 import org.springframework.data.annotation.Id;
|
||||
6
|
||||
7 /**
|
||||
8 *
|
||||
9 * @author stephon
|
||||
10 */
|
||||
11 @Getter @Setter
|
||||
12 public class Bike {
|
||||
13 private @Id String id;
|
||||
14 private String modelNumber;
|
||||
15 private String color;
|
||||
16 private String description;
|
||||
17
|
||||
18 @Override
|
||||
19 public String toString() {
|
||||
20 return "This bike model is " + this.modelNumber + " is the color " + this.color + " and is " + description;
|
||||
21 }
|
||||
22 }
|
||||
```
|
||||
|
||||
如你所见,对象中使用 Lombok 注解来为定义的属性(特性)生成 getters/setters 方法。如果你不想对所有属于都生成 getters/setters 方法,可以在属性上定义这些注解。这两个类会变成容器,里面携带有数据,无论在何处想显示信息都可以使用。
|
||||
|
||||
#### 配置数据库
|
||||
|
||||
我使用 [Mongo Docker][7] 容器来进行此次测试。如果你的系统上已经安装了 MongoDB,则不必运行 Docker 实例。你也可以登陆其官网,然后按照安装说明,选择系统信息来安装 MongoDB。
|
||||
|
||||
安装后,就可以使用命令行、GUI(例如 MongoDB Compass)或用于连接数据源的 IDE 驱动程序来与新的 MongoDB 服务进行交互。到目前为止,可以开始定义数据层了,用来拉取、转换和持久化数据。需要设置数据库访问属性,请导航到程序中的 **applications.properties** 文件,然后添加如下内容:
|
||||
|
||||
|
||||
```
|
||||
1 spring.data.mongodb.host=localhost
|
||||
2 spring.data.mongodb.port=27017
|
||||
3 spring.data.mongodb.database=BikeStore
|
||||
```
|
||||
|
||||
#### 定义数据访问对象(数据)层
|
||||
|
||||
数据访问层(DAL)中的数据访问对象(DAO)定义了与数据库中的数据的交互过程。令人惊叹的就是在使用 **spring-boot-starter** 后,查询数据库的大部分工作已经完成。
|
||||
|
||||
让我们从 **Customer** DAO 开始。在 **src** 下的新目录 **dao** 中创建一个接口文件,然后再创建一个名为 **CustomerRepository.java** 的 Java 类文件,其内容如下示:
|
||||
|
||||
|
||||
```
|
||||
1 package com.stephb.JavaMongo.dao;
|
||||
2
|
||||
3 import com.stephb.JavaMongo.dto.Customer;
|
||||
4 import java.util.List;
|
||||
5 import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
6
|
||||
7 /**
|
||||
8 *
|
||||
9 * @author stephon
|
||||
10 */
|
||||
11 public interface CustomerRepository extends MongoRepository<Customer, String>{
|
||||
12 @Override
|
||||
13 public ListStringCustomer> findAll();
|
||||
14 public List<Customer> findByFirstName(String firstName);
|
||||
15 public List<Customer> findByLastName(String lastName);
|
||||
16 }
|
||||
```
|
||||
|
||||
这个类是一个接口,扩展或继承于 **MongoRepository** 类,而 MongoRepository 类依赖于 DTO (**Customer.java**)和一个字符串,它们用来实现自定义函数查询功能。因为您已继承自此类,所以您可以访问许多方法函数,这些函数允许持久化和查询对象,而无需实现或引用自己定义的方法函数。例如,在实例化 **CustomerRepository** 对象后,你就可以直接使用 **Save** 功能。如果你需要扩展更多的功能,也可以重写这些方法。我创建了一些自定义查询来搜索我的集合,这些集合对象是我自定义的元素。
|
||||
|
||||
**Bike** 对象也有一个存储源负责与数据库交互。与 **CustomerRepository** 的实现非常类似。其实现如下所示:
|
||||
|
||||
```
|
||||
1 package com.stephb.JavaMongo.dao;
|
||||
2
|
||||
3 import com.stephb.JavaMongo.dto.Bike;
|
||||
4 import java.util.List;
|
||||
5 import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
6
|
||||
7 /**
|
||||
8 *
|
||||
9 * @author stephon
|
||||
10 */
|
||||
11 public interface BikeRepository extends MongoRepository<Bike,String>{
|
||||
12 public Bike findByModelNumber(String modelNumber);
|
||||
13 @Override
|
||||
14 public List<Bike> findAll();
|
||||
15 public List<Bike> findByColor(String color);
|
||||
16 }
|
||||
```
|
||||
|
||||
#### 运行程序
|
||||
|
||||
现在,你已经有了一种结构化数据的方式,可以对数据进行提取、转换和持久化,然后运行这个程序。
|
||||
|
||||
找到 **Application.java** 文件(有可能不是此名称,具体取决于你的应用程序名称,但都会包含有 “application” )。在定义此类的地方,在后面加上 **CommandLineRunner 的实现**。这将允许你实现 **run** 方法来创建命令行应用程序。重写 **CommandLineRunner** 接口提供的 **run** 方法,并包含如下内容用来测试 **BikeRepository** :
|
||||
|
||||
|
||||
```
|
||||
1 package com.stephb.JavaMongo;
|
||||
2
|
||||
3 import com.stephb.JavaMongo.dao.BikeRepository;
|
||||
4 import com.stephb.JavaMongo.dao.CustomerRepository;
|
||||
5 import com.stephb.JavaMongo.dto.Bike;
|
||||
6 import java.util.Scanner;
|
||||
7 import org.springframework.beans.factory.annotation.Autowired;
|
||||
8 import org.springframework.boot.CommandLineRunner;
|
||||
9 import org.springframework.boot.SpringApplication;
|
||||
10 import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
11
|
||||
12
|
||||
13 @SpringBootApplication
|
||||
14 public class JavaMongoApplication implements CommandLineRunner {
|
||||
15 @Autowired
|
||||
16 private BikeRepository bikeRepo;
|
||||
17 private CustomerRepository custRepo;
|
||||
18
|
||||
19 public static void main(String[] args) {
|
||||
20 SpringApplication.run(JavaMongoApplication.class, args);
|
||||
21 }
|
||||
22 @Override
|
||||
23 public void run(String... args) throws [Exception] {
|
||||
24 Scanner scan = new Scanner(System.in);
|
||||
25 String response = "";
|
||||
26 boolean running = true;
|
||||
27 while(running){
|
||||
28 System.out.println("What would you like to create? \n C: The Customer \n B: Bike? \n X:Close");
|
||||
29 response = scan.nextLine();
|
||||
30 if ("B".equals(response.toUpperCase())) {
|
||||
31 String[] bikeInformation = new String;
|
||||
32 System.out.println("Enter the information for the Bike");
|
||||
33 System.out.println("Model Number");
|
||||
34 bikeInformation[0] = scan.nextLine();
|
||||
35 System.out.println("Color");
|
||||
36 bikeInformation[1] = scan.nextLine();
|
||||
37 System.out.println("Description");
|
||||
38 bikeInformation[2] = scan.nextLine();
|
||||
39
|
||||
40 Bike bike = new Bike();
|
||||
41 bike.setModelNumber(bikeInformation[0]);
|
||||
42 bike.setColor(bikeInformation[1]);
|
||||
43 bike.setDescription(bikeInformation[2]);
|
||||
44
|
||||
45 bike = bikeRepo.save(bike);
|
||||
46 System.out.println(bike.toString());
|
||||
47
|
||||
48
|
||||
49 } else if ("X".equals(response.toUpperCase())) {
|
||||
50 System.out.println("Bye");
|
||||
51 running = false;
|
||||
52 } else {
|
||||
53 System.out.println("Sorry nothing else works right now!");
|
||||
54 }
|
||||
55 }
|
||||
56
|
||||
57 }
|
||||
58 }
|
||||
```
|
||||
|
||||
其中的 **@Autowired** 注解会自动依赖注入 **BikeRepository** 和 **CustomerRepository** Bean。我们将使用这些类来从数据库持久化和采集数据。
|
||||
|
||||
已经好了。你已经创建了一个命令行应用程序。该应用程序连接到数据库,并且能够以最少的代码执行 CRUD 操作
|
||||
|
||||
### 结论
|
||||
|
||||
从诸如对象和类之类的编程语言概念转换为用于在数据库中存储、检索或更改数据的调用对于构建应用程序至关重要。Java 持久化 API (JPA)正是为 Java 开发人员解决这一难题的重要工具。 你正在使用 Java 操纵哪些数据库呢? 请在评论中分享。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/19/10/using-java-persistence-api
|
||||
|
||||
作者:[Stephon Brown][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[runningwater](https://github.com/runningwater)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/stephb
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/java-coffee-beans.jpg?itok=3hkjX5We (Coffee beans)
|
||||
[2]: https://www.techrepublic.com/article/mongodb-ceo-tells-hard-truths-about-commercial-open-source/
|
||||
[3]: https://github.com/StephonBrown/SpringMongoJava
|
||||
[4]: https://openjdk.java.net/projects/jdk/11/
|
||||
[5]: https://start.spring.io/
|
||||
[6]: http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+string
|
||||
[7]: https://hub.docker.com/_/mongo
|
||||
[8]: http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+exception
|
||||
[9]: http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+system
|
@ -0,0 +1,104 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Why Vim fans love the Herbstluftwm Linux window manager)
|
||||
[#]: via: (https://opensource.com/article/19/12/herbstluftwm-linux-desktop)
|
||||
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
|
||||
|
||||
为什么 Vim 爱好者喜欢 Herbstluftwm Linux 窗口管理器
|
||||
======
|
||||
本文是 24 天 Linux 桌面特别系列的一部分。如果你是Vim爱好者,请试试 Herbstluftwm,这是一个受到 ”Vim 方式“启发的基于平铺的 Linux 窗口管理器。
|
||||
![OpenStack source code \(Python\) in VIM][1]
|
||||
|
||||
每个人都喜欢 Vim(除了 Dvorak 和 Emacs 用户)。Vim 非常流行,以至于有整个 Web 浏览器都使用 Vim 键绑定来浏览网页,非常流行的 [Zsh][2] 终端仿真器中的 Vim 模式,甚至是文本编辑器。 还有一个名为 [herbstluftwm][3] 的窗口管理器,它部分模仿了 “Vim 方式”。Herbstluftwm 取消了窗口,在程序载入和使用时使用平铺或者象限代替。 你可以使用键盘(**Alt+h**、**Alt+j**、**Alt+k** 和 **Alt+l**)从一个平铺块移动到另一个平铺块。
|
||||
|
||||
![Herbstluftwm][4]
|
||||
|
||||
从发行版软件仓库安装 Herbstluftwm。安装后,请退出当前桌面会话,以便可以登录到新会话。默认情况下,会话管理器(KDM、GDM、LightDM 或 XDM,具体取决于你的设置)将继续登录到以前的桌面,因此在登录之前必须覆盖它。
|
||||
|
||||
在 GDM 中:
|
||||
|
||||
![][5]
|
||||
|
||||
在 SDDM 中:
|
||||
|
||||
![][6]
|
||||
|
||||
### Herbstluftwm 桌面之旅
|
||||
|
||||
第一次登录 Herbstluftwm 时,你只会看到绿屏,边缘有深绿色边框。你所看到的是初始平铺,其中没有加载任何应用程序。要启动默认应用,请按 **Alt+回车**。
|
||||
|
||||
文档中强调使用 **$HOME/.config/herbstluftwm/autostart** 作为登录时启动应用的配置文件。对于不必在每次登录时启动的应用,可以使用 xterm 作为启动守护进程。与往常一样,在命令将 shell 的控制权返回给你后加一个 **amp;** 符号。例如,要启动 Emacs:
|
||||
|
||||
|
||||
|
||||
```
|
||||
`% emacs &`
|
||||
```
|
||||
|
||||
这将在新平铺中启动 Emacs 窗口,然后返回命令行。
|
||||
|
||||
![Emacs running in Herbstluftwm][7]
|
||||
|
||||
#### 切换平铺
|
||||
|
||||
要从一个平铺块移动到另一个平铺块,请使用经典的 **h**、**j**、**k** 或 **l** 与 **Alt** 的 Vim 导航组合键。例如,要从终端切换到其下方平铺中的应用(即屏幕底部),请按 **Alt+j**。要返回,请按 **Alt+k**。左移和右移分别为 **Alt+h** 和 **Alt+l**。
|
||||
|
||||
#### 分割屏幕
|
||||
|
||||
你可以使用 **Alt+o** 垂直分割屏幕,使用 **Alt+u** 水平分割屏幕。
|
||||
|
||||
要删除空白平铺块,请移动到该平铺块,然后按 **Alt+r**。
|
||||
|
||||
### 配置 herbstluftwm
|
||||
|
||||
除了 **Alt** 键绑定外,你还可通过 **herbstclient** 命令与 Herbstluftwm 进行通信。此命令可以在 shell 中交互,或者你可在配置文件中保留你的首选项。
|
||||
|
||||
你可以查看 Herbstluftwm 中的所有可用属性:
|
||||
|
||||
|
||||
```
|
||||
`$ herbstclient attr`
|
||||
```
|
||||
|
||||
Herbstluftwm 的默认行为在默认配置文件中定义,你可以将其复制到家目录并进行修改。启动后,Herbstluftwm 执行配置文件中包含的命令。例如,你觉得使用以 **Alt** 为中心的键绑定很笨拙(它传统上是应用内保留快捷键),那你可以在配置文件中更改触发 Herbstluftwm 操作的键:
|
||||
|
||||
|
||||
```
|
||||
% mkdir ~/.config/herbstluftwm
|
||||
% cp /usr/xdg/herbstluftwm/autostart \
|
||||
~/.config/herbstluftwm
|
||||
% sed -i 's/Mod=Mod1/Mod=Mod4/' ~/.config/herbstluftwm
|
||||
% herbstclient reload
|
||||
```
|
||||
|
||||
这将使 Herbstluftwm 的修饰键更改为 Super 键(”Windows“或 ”Tux“ 键,具体取决于你的键盘)。
|
||||
|
||||
使用自动启动文件,你可以设置自定义键绑定,为特定类型的应用创建标签,以便以一致的方式平铺应用,并执行更多操作。
|
||||
|
||||
### 为何你要尝试 herbstluftwm
|
||||
|
||||
Herbstluftwm 是一个平铺窗口管理器的很好例子。它默认平铺窗口,并允许用户定义全局规则例外。它使用类似 Vim 的导航,但可以快速简单地覆盖它。你可能是你一直在寻找的平铺管理器,所以请尽快尝试一下。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/19/12/herbstluftwm-linux-desktop
|
||||
|
||||
作者:[Seth Kenlon][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/seth
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/openstack_python_vim_2.jpg?itok=4fza48WU (OpenStack source code (Python) in VIM)
|
||||
[2]: https://opensource.com/article/19/9/getting-started-zsh
|
||||
[3]: https://herbstluftwm.org
|
||||
[4]: https://opensource.com/sites/default/files/uploads/advent-herbsluftwm.png (Herbstluftwm)
|
||||
[5]: https://opensource.com/sites/default/files/advent-gdm_1.jpg
|
||||
[6]: https://opensource.com/sites/default/files/advent-kdm_0.jpg
|
||||
[7]: https://opensource.com/sites/default/files/uploads/advent-herbsluftwm-emacs.jpg (Emacs running in Herbstluftwm)
|
Loading…
Reference in New Issue
Block a user