Taogen's Blog

Stay hungry stay foolish.

Content

  • Types, Values, and Variables
  • Expression, Statement, Functions
  • Objects, Classes
  • Arrays
  • Sync
  • JavaScript in Web Browsers
  • Modules

Expression

Invocation Expressions

Conditional Invocation

let result = json?.name;
let result = json != null ? json.name : undefined;

Null Check

if a != null return a, else return default value

// Ternary Operator
let result = a != null ? a : defaultValue;
// Logical Operator ||
// When it's used with non-boolean values, the || operator will return a non-boolean value of one of the specified expression or operands.
let result = a || defaultValue;
// Nullish Coalescing Operator ??
let result = a ?? defaultValue;

2

if a != null and b != null return b; else return null;

let result = a != null && a.name != null ? a.name : null;
let result = a && a.name;

Statement

For Loop

for (let i = 0; i < arr.length; ++i)
arr.forEach((value, index) => { /* ... */ })
for (let index in arr)
for (const value of arr)

console.log(obj1 [, obj2, ..., objN]);

console.log(obj)
console.log(obj1, obj2)
console.log("obj is: ", obj)
console.log("obj is: ", obj, ". And my name is ", name)
console.log("objects are: ", obj1, obj2)
console.log("obj is: " + JSON.parse(JSON.stringify(obj)))

console.log(msg [, subst1, ..., substN]);

  • %o or %O Outputs a JavaScript object.
  • %d or %i Outputs an integer.
  • %s Outputs a string.
  • %f Outputs a floating-point value.
console.log("obj is %o", obj)
console.log("Hello, %s. You've called me %d times.", "Bob", 1);

Object

Merge object fields

let json1 = {"name": "Jack"};
let json2 = {
...json1,
age: 18
};

Deep copy

let json = {"name": "Jack"};
let copy = JSON.parse(JSON.stringify(json));

Get all keys of JSON/JavaScript object

const obj = {a: 'somestring', b: 123, c: false};
Object.keys(obj);

Get all values of JSON/JavaScript object

const obj = {a: 'somestring', b: 123, c: false};
Object.values(obj);
// ES6
const obj = {a: 'somestring', b: 123, c: false};
Object.keys(obj).map(key => obj[key]);

Traversing JavaScript Object

for-in

// iterates over all enumerable properties of an object.
const jsonObj = {name: "Jack", age: 18}
for (const key in jsonObj) {
console.log(`${key}: ${jsonObj[key]}`);
}

Object.entries() or Object.keys()

// to traverse a Javascript object
const jsonObj = {name: "Jack", age: 18}
Object.entries(jsonObj).forEach(([key, value]) => {
console.log(key, value)
});
Object.keys(obj).forEach(function(key) {
var value = obj[key];
// ...
});

to include non-enumerable properties

Object.getOwnPropertyNames(obj).forEach(function(key) {
var value = obj[key];
// ...
});

Array

Traversal

for (let i = 0; i < arr.length; ++i)
arr.forEach((value, index) => { /* ... */ })
for (let index in arr)
for (const value of arr)

forEach: executes a provided function once for each array element. another type of for loop.

NOTE: The forEach loop is another type of for loop in JavaScript. However, forEach() is actually an array method, so it can only be used exclusively with arrays. There is also no way to stop or break a forEach loop. If you need that type of behavior in your loop, you’ll have to use a basic for loop.

const array1 = ['a', 'b', 'c'];
array1.forEach(element => console.log(element));

for in: iterates over all enumerable properties of an object. E.g. key of JSON object, index of array.

const object = { a: 1, b: 2, c: 3 };

for (const property in object) {
console.log(`${property}: ${object[property]}`);
}

for of: The for...of statement creates a loop iterating over iterable objects, including: built-in String, Array, array-like objects (e.g., arguments or NodeList), TypedArray, Map, Set, and user-defined iterables.

const array1 = ['a', 'b', 'c'];

for (const element of array1) {
console.log(element);
}
for (const [idx, el] of arr.entries()) {
console.log( idx + ': ' + el );
}

Summary

If you don’t need break for loop use forEach, else use for of.

Find Max in an array

Find max value in an array

// apply
const array = [{name: "a", value: 1}, {nama: "b", value: 2}]
Math.max.apply(Math, array.map(o => o.value));

const array = [1,2,3,5,1,2]
Math.max.apply(Math, array);
// ES6 spread
const array = [{name: "a", value: 1}, {nama: "b", value: 2}]
Math.max(...array.map(o => o.value));

const array = [1,2,3,5,1,2]
Math.max(...array);
// standard loop
let max = testArray[0];
for (let i = 1; i < testArrayLength; ++i) {
if (testArray[i] > max) {
max = testArray[i];
}
}
// reduce
const array = [{name: "a", value: 1}, {nama: "b", value: 2}]
array.reduce((a, b) => Math.max(a.value, b.value));

const array = [1,2,3,5,1,2]
array.reduce((a, b) => Math.max(a, b));

Time cost: standard loop < ES6 spread and apply < reduce

Finding the max value of an attribute in an array of objects

// reduce
const array = [{name: "a", value: 1}, {nama: "b", value: 2}]
const maxValueObject = array.reduce((prev, current) => (prev.value > current.value) ? prev : current)

Convert type of elements of Array

int array to string array

// This will not mutate array. It will return a new array.
let array = [1, 2, 3];
let newArray = array.map(String);
let array = [1, 2, 3];
let newArray = array.map(item => item.toString())
let array = [1, 2, 3];
let newArray = array.join(",").split(",");

string array to int array

// Note: For older browsers that don't support map
let array = ['1', '2', '3'];
let newArray = array.map(Number); // i=>Number(i)
let array = ['1', '2', '3'];
let newArray = array.map(item => parseInt(item));
let array = ['1', '2', '3'];
let newArray = Array.from(array, Number);

Request

URL Parameters

Getting URL parameters

// https://example.com/?id=1
const queryString = window.location.search;
console.log(queryString); // ?id=1
const urlParams = new URLSearchParams(queryString);
const id = urlParams.get('id');
console.log(id); // 1

Checking for the Presence of a Parameter

console.log(urlParams.has('id')); // true

Getting All of a Parameter’s Values

console.log(urlParams.getAll('size'));
// [ 'm' ]

//Programmatically add a second size parameter.
urlParams.append('size', 'xl');

console.log(urlParams.getAll('size'));
// [ 'm', 'xl' ]

Iterating over Parameters

const
keys = urlParams.keys(),
values = urlParams.values(),
entries = urlParams.entries();

for (const key of keys) console.log(key);
// product, color, newuser, size

for (const value of values) console.log(value);
// shirt, blue, , m

for(const entry of entries) {
console.log(`${entry[0]}: ${entry[1]}`);
}

Document

Remove Elements

remove element by id

document.querySelector("#remove").remove();

remove elements by class name

function removeElementsByClass(className){
const elements = document.getElementsByClassName(className);
while(elements.length > 0){
elements[0].parentNode.removeChild(elements[0]);
}
}
const removeElementsByClass = (className) => document.querySelectorAll(className).forEach(el => el.remove());

Empty Elements

empty element by id

document.querySelector("#remove").innerHTML = "";

empty elements by class name

const emptyElementsByClass = (className) => document.querySelectorAll(className).forEach(el => el.innerHTML = "");

Regular Expression

RegExp

// creating regular expression from a string, you have to double-up your backslashes \\.
const regex = new RegExp('^\\d{10}$');
const regex = new RegExp('^\\d{10}$', 'g');

/regex/mod

// if you use regex syntax, you need eacape / by \/
const regex = /^\d{10}$/;
const regex = /^\d{10}$/g;

API

  • RegExp

    • regexp.exec(str) - Returns the first match info array. It likes [matched string, group 1, group 2, ...]. The flag g has no effect.

      /hello(\d)/.exec("hello1, hello2"); // ['hello1', '1', ...]
      /hello(\d)/g.exec("hello1, hello2"); // ['hello1', '1', ...]
    • regexp.test(str) - Returns whether it matches. The flag g has no effect.

      /hello(\d)/.test("hello1, hello2"); // true
      /hello(\d)/g.test("hello1, hello2"); // true
  • String

    • string.match(regexp) - Returns the first match info array [matched string, group 1, group 2, ...], or return an all matched string array [matched string 1, matched string 2, ...] when it uses the flag g.

      let s = "hello1, hello2";
      s.match(/hello(\d)/); // return the first match object ['hello1', '1', ...]
      s.match(/hello(\d)/g); // return all match strings ['hello1', 'hello2']
    • string.matchAll(regexp) - Returns all match info arrays. The regexp must use the flag g (global search).

      let s = "hello1, hello2";
      s.matchAll(/hello(\d)/); // Uncaught TypeError: String.prototype.matchAll called with a non-global RegExp argument
      for (const match of s.matchAll(/hello(\d)/g)) {
      console.log(match); // the match info array
      console.log(match[0]); // the matched string
      console.log(match[1]); // the group 1 of the matched string
      }
    • string.replace(regexp, replacement) - Returns a string with the first or all matched string replaced.

      let s = "hello1, hello2";
      s.replace(/hello(\d)/, 'hey'); // 'hey, hello2'
      s.replace(/hello(\d)/g, 'hey'); // 'hey, hey'
      // replace with group
      let s = "hello1, hello2";
      s.replace(/hello(\d)/, "hi$1"); // 'hi1, hello2'
      s.replace(/hello(\d)/g, "hi$1"); // 'hi1, hi2'
      // extract group
      s.replace(/hello(\d)/g, "$1"); // '1, 2'
    • string.replaceAll(regexp, replacement) - Returns a string with the all matched string replaced. The regexp must use the flag g (global search).

      let s = "hello1, hello2";
      s.replaceAll(/hello(\d)/, 'hey'); // Uncaught TypeError: String.prototype.replaceAll called with a non-global RegExp argument
      s.replaceAll(/hello(\d)/g, 'hey'); // 'hey, hey'
      // replace with group.
      // replaceAll(/xxx/g, '') results are same with replace(/xxx/g, '')
      s.replaceAll(/hello(\d)/g, "hi$1"); // 'hi1, hi2'
      s.replaceAll(/hello(\d)/g, "$1"); // '1, 2'
    • string.search(regexp)

    • string.split(regexp)

Flags

Flag Description Corresponding property
d Generate indices for substring matches. hasIndices
g Global search. global
i Case-insensitive search. ignoreCase
m Allows ^ and $ to match newline characters. multiline
s Allows . to match newline characters. dotAll
u “Unicode”; treat a pattern as a sequence of Unicode code points. unicode
v An upgrade to the u mode with more Unicode features. unicodeSets
y Perform a “sticky” search that matches starting at the current position in the target string. sticky

Match emoji unicode

const text = "Hello, 😀🚀🌎!";
const emojiRegex = /\p{Emoji}/gu;
const emojis = text.match(emojiRegex);

console.log(emojis); // ['😀', '🚀', '🌎']

Make sure to use the u flag at the end of the regex pattern (/.../u) to enable Unicode mode, which is necessary when working with Unicode property escapes.

References

Single sign-on (SSO) is an authentication method that allows users to sign in using one set of credentials to multiple independent software systems.

Implementations of single sign-on:

  • Cookie-based
  • Session-based
  • Central Authentication Service (CAS)

It works by using web based HTTP cookies to transport user credentials from browser to server without from the user. Credentials on the client machine are gathered and encrypted before it being stored in the cookie.

Once the user enters the username and password in any subsystem, the user credentials will be stored in cookie, which is shared by multiple subsystems and automatically sent to the server.

The domain name of each system using cookie-based SSO should be the same or have the same top-level domain name. So user credentials in cookie can be shared between multiple systems.

Advantages

  • Easy to implement.

Disadvantages

  • Can’t cross domain.

Session-Based SSO

It works by using web based HTTP cookies to transport user authentication token.

The user token is stored in the client browser and sent to the server as session value. session values and user ID are stored in a cache like Redis shared across subsystems. Each subsystem checks the user from the cache by the token in the HTTP request cookie.

Advantages

  • Suitable for distributed system applications.

Disadvantages

  • Can’t cross domain.

Central Authentication Service (CAS)

When the user accesses the application system for the first time, since he has not logged in, he is directed to the authentication system to log in. The authentication system accepts security information such as user name and password, and generates an access token (ticket). The user accesses the application system through the ticket. After receiving the request, the application system will visit the authentication system to check the legitimacy of the ticket. If the check is passed, the user can access the application system resources without logging in again.

Advantages

  • Support cross domain.

Disadvantages

  • Need a an independent authentication system.

Expressions

Divisor is zero

Exception:

java.lang.ArithmeticException

Error code example:

int a = 1;
int b = 0;
int result = a / b;

Suggestion:

You need to check if the divisor is zero.

Statements

Update a collection in for loop (Fail fast)

Exception:

java.lang.UnsupportedOperationException

Error code example:

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6);
for (int i = 0; i < list.size(); i++) {
Integer value = list.get(i);
System.out.println(value);
if (value == 1) {
list.add(100); // or list.remove(0);
}
}

Suggestion:

Do not insert, update, and delete elements of a collection while looping through the collection.

Objects and Classes

Call a null object methods

Exception:

java.lang.NullPointerException

Error code example:

User user = null;
String name = user.getName();

Suggestion:

You need to check if the object is null when calling its methods.

Collections

Add elements to Collections.emptyList()

Exception:

java.lang.UnsupportedOperationException

Error code example:

List<Integer> list = Collections.emptyList();
list.add(1);

Suggestion:

Don’t return Collections.emptyList() in intermediate methods.

Add elements to List built by Arrays.asList()

Exception:

java.lang.UnsupportedOperationException

Error code example:

List<Integer> list = Arrays.asList(1, 2, 3);
list.add(4);

Suggestion:

If you need update the list later, you should use new ArrayList(Arrays.asList()) instead of Arrays.asList().

Requirement drives technology capabilities.

Keep Coding

Solve algorithm problems.

Do some great open-source projects (making wheels or writing common systems). Or contribute to open-source projects.

Keep Learning

Reading technical books and docs.

Writing blogs.

Creating some small toy projects.

Solving other code problems. Answer questions on StackOverflow.

Answer interview questions by yourself.

Browse technical websites and forums.

Deep Dive

Reading classic fundamental CS books, documentation, and specification.

Reading source code.

Naming Convention

Java uses camelCase as the naming convention for variables, methods, and classes. But class names must be capitalized.

Package management

You can use Maven or Gradle for package (dependency) management.

Project Creation

Maven project

You can create a Maven project in the following way.

1. Maven command line

$ mvn archetype:generate \
-DgroupId=com.taogen.demo \
-DartifactId={project-name} \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DarchetypeVersion=1.4 \
-DinteractiveMode=false

2. Create a Maven project in a IDE

3. Spring Initializr

Project structure

Basic Maven Application

java-project/

├── src/main/java/
│ └── com/taogen/javaproject/

├── src/main/resources/

├── src/test/java/
├── src/test/resources/
└── pom.xml
  • src/main/java: Java source code
    • com/taogen/javaproject: Base package path. It represents the domain name of your project website.
  • src/main/resources/: Static resources such as image, JavaScript, and CSS files. And configuration files.
  • src/test/java/: Test code.
  • pom.xml: Maven project configuration file.

Spring boot Web Application

java-project/

├── src/main/java/
│ └── com/taogen/javaproject/
│ ├── modules/
│ │ └── user/
│ │ ├── controller/
│ │ │ └── UserController.java
│ │ ├── entity/ (or domain/)
│ │ │ └── User.java
│ │ ├── service/
│ │ │ ├── UserService.java
│ │ │ └── impl/
│ │ │ └── UserServiceImpl.java
│ │ ├── repository/ (or mapper/)
│ │ │ └── UserRepository.java
│ │ ├── vo/
│ │ └── dto/
│ ├── util/
│ ├── config/
│ ├── aspect/
│ ├── annotation/
│ ├── constant/
│ ├── enums/
│ ├── exception/
│ ├── filter/
│ └── task/

├── src/main/resources/
│ ├── static/
│ ├── templates/
│ ├── log4j2.xml
│ └── application.yml (or application.properties)

├── src/test/java/
├── src/test/resources/

├── .gitignore
├── .gitmessage
├── Dockerfile
├── pom.xml
├── LICENSE
└── README.md
  • src/main/java: Java source code
    • modules/
      • controller/: HTTP request handlers.
      • entity/: JavaBeans, POJOs, or domain models.
      • service/: Business logic processing code.
      • repository/: Data access layer code for various persistence stores.
      • dto/: Data Transfer Objects, encapsulate values to carry data between processes or networks.
      • vo/: Value objects, a special type of objects that can hold values.
    • util/: Utility classes such as StringUtil, DateUtil, IOUtil, etc.
    • config/: Configuration files such as Redis, MySQL data source, Security, etc.
    • aspect/: Spring AOP classes.
    • annotation/: Custom Java annotations.
    • constant/: Static variable classes.
    • enums/: Java Enum type classes.
    • exception/: Custom exception classes that are extended by a java.lang.Exception class or its subclass.
    • filter/: Servlet filter classes that are implemented javax.servlet.Filter.
    • task/: Scheduled tasks.
  • src/main/resources/
    • static/: Static files such as CSS, JavaScript, image, etc.
    • templates/: Template files such as JSP, FreeMarker, Thymeleaf, etc.
    • log4j2.xml: Log configuration file.
    • application.yml: Spring boot configuration file.
  • src/test/java/: Test code.
  • pom.xml: Maven project configuration file.

Solve coding problems

Programming language coding problems

Solve problems on LeetCode

Do small toy projects

To-do list

Calculator

Snake game

Make projects that solve your own problems or requirements

Web page crawlers

Web video downloader

Automation scripts or tools

Contribute to open-source projects

You can search “How to find open source projects to contribute”

Create your own open-source project

Doing open source projects forces you to write better and clean code.

Getting Started

Introduction to the programming language

Programming environment

Print “Hello world”

An example of the combination of variables, functions, expressions, statements, standard Input, and output

Comment. Single line and multiline comments.

Variables and Data Types

Variables

Variable declaration, initialization, assignment.

Variable scope. Local variables and global variables.

Data Types

Data type table by categories

Data type literal (type initialization) examples

Get the type of the variable

Type check

Check null or undefined

Check string type

Check number type

Check object type

Check array type

Check function type

Type conversion

Conversion between bool, integer, float, and string

number to string

string to number

string to float

Type formatting

String formatting. placeholders formatting.

Integer formatting. Add leading zero. Specify length.

Float formatting. fixed-point decimal formatting.

Date formatting. Format date objects by pattern.

String and Array

String

String basic

String declaration and initialization. multiline strings.

Length of string

Lookup

chatAt. the n character of the string

indexOf, lastIndexOf

String check

equals

isEmpty

contains

startsWith, endsWith

matches regex

String conversion

toCharArray

toLowerCase, toUpperCase

String Handling

String concatenation

Substring

replace

trim

split

join

String formatting

Array / List

Array basic

Array declaration and initialization

length of array

Lookup

Access element by index

indexOf

contains

Operations

insert, append

update

remove

Handling

Deep copy

Slice or subarray

Array concatenation

Filter / Map / Reduce (sum, min, max) / Predicate (some, every)

Join

Sorting

Reversion

Deduplication

Conversion

Array to string

Expressions

Arithmetic operators

addition +, subtraction -, multiplication *, division /, remainder/modulo %, exponentiation ^.

Logical operators

and &&, or ||, not !

Comparison operators

equality ==, inequality !=, greater than >, less than <, >=, <=

Bitwise operators

bitwise AND &, bitwise OR |, bitwise NOT ~, bitwise XOR ^, left shift <<, right shift >>

Others operators

Assignment, member access, conditional operator

Operator precedence

Statements

Conditional

if

switch

For loop

while

for

do…while

Exception handling

try…catch…finally

throw

Functions

Function definition

Function call

Argument passing and return value

Modules

Classes and Objects

Object-Oriented Programming

Classes

Class definition

Class members. Constructors. Static and instance members of classes.

Objects

Object construction

Object Deep copy

Inheritance

Standard Library

I/O Streams and Files

Input/Output streams

Input stream types. Bytes input streams, character input streams, buffered input streams.

Output stream types. Bytes output streams, character output streams, buffered output streams.

Read from and write into files.

Read from and write into memory.

Read a text file as string

Non-blocking and asynchronous I/O

Moving data into and out of buffers

Files

File and directory operations. creation, delete, update.

File path.

File information. Mine type of files.

Temporary files and directories.

Container

Container types

List, Set, Map, Queue, Stack

Container Operations

Basic operations

Traversal, print, add elements, remove elements

Conversion

Element type conversion, Container conversion.

Common operations

Merge

Join

Deduplication

Sorting

Reversion

Computation

Reduction

Aggregation and group

Concurrent Containers

Thread and concurrency

Processes and threads

Synchronization.

Liveness. Deadlock and livelock (starvation).

Guarded blocks. wait and notifiy.

Immutable objects. Its state cannot change after it is constructed.

High level concurrency objects. Reentrant lock. Executors and thread pools. Concurrent collections. Atomic variables.

Network programming

Sockets

Web Sockets

Server-sent events

HTTP

HTTP clients.

Download files by HTTP URLs.

Security

Class loaders

User authentication

Digital signatures

Encryption

Advanced Topics

Lambda expressions and Functional Programming

Streams

Stream Creation

The filter, map Methods

Collecting Results

Grouping and Partitioning

Reduction Operations

Parallel Streams

Annotation

Generics

Reflection

Packaging and deployment

Regular expressions

Others

Package manager and create project.

Style Guide

Project structures.

Best practice.

Common libraries

  • String handling
  • DateTime handling

References

[1] What are the basic fundamental concepts of programming?

[2] C++ Primer, Fifth Edition - Table of Contents

[3] Java SE 8 API

[4] The Java Tutorial: A Short Course on the Basics - Table of Contents

[5] Core Java, Volume I: Fundamentals, 12th Edition - Table of Contents

[6] Core Java, Vol. II-Advanced Features, 12th Edition - Table of Contents

Factors in choosing a programming language

  • Purpose
  • Speed to Market / Productivity
  • Popularity: Technology ecology
  • Usability: Readable code, less effort
  • Learning Curve / Simplicity
  • Performance / Speed
  • Security

Programming languages for developing web applications

When you’re talking about web applications at large scale, you have to consider things like security, performance, concurrency, developer productivity, scalability as a language, interoperability

Server-side programming languages for web application development

  • Small scale project: PHP, Python, Ruby
  • Large scale project: Java, Go, C# (.NET)
  • Others: Node.js

Comparison table of web application server-side programming languages

Language Purpose Speed to Market Popularity Usability Performance Security Learning Curve
PHP Produce dynamic web pages 1 2 3 2 3 2
Python High productivity and readable code 1 1 2 3 2 1
Ruby simply and with less effort 1 3 1 1 1 3

PHP disadvantages

  • Website may be difficult for modify if you don’t have a good architecture at the beginning.

Ruby disadvantages

  • There is so many magic here makes you feeling very well at the beginning. But when you need to go deeper into the framework. There would be a lot of pain.

Programming languages for developing mobile applications

Android

The majority of Android apps are written in Java and Kotlin. In some cases, programming languages like C, C++, and Basic are also used.

Java

Kotlin

C/C++ (Android Native Development Kit (NDK))

  • C++ can be used in Android app development in rare cases, especially when you plan to build native-activity Android applications. It is because C++ is less flexible and very difficult to set up, and as a result, it can lead to more bugs. Usually, Java is preferred more over C++.
  • Google offers the native development kit (NDK) that uses native languages like C and C++ for Android development. But you can’t build an entire Android app with C or C++. You need to learn Java.

iOS

Swift

Objective C

Cross-Platform Mobile

C# (Xamarin)

Dart (Flutter)

Lua (Corona, Solar2D)

JavaScript (React Native)

Python (convert Python apps into Android Packages)

Programming languages for developing desktop applications

Windows

C, C++

C#(.Net)

Linux

C/C++

Mac OS X

Swift

Objective C

Cross-Platform Desktop

Java

Python

JavaScript (Electron)

Local Environment

Check if your network status is OK and if you can access other websites.

Try a different browser or clear browser cache.

Check if the HOSTS file maps the domain name to 127.0.0.1 or an incorrect IP address. You can see what IP address the domain name is mapped to with the following command line.

ping {your_domain}

Check if you are using a HTTP proxy or if the proxy can access the website.

DNS Settings

Check that the A record of the DNS configuration points the domain name to the correct IP address.

Cloud Host

Check if the cloud host is on and accessible. You can use the following command line to check if cloud host is on.

ping {IP_address}

Check if the HTTP (80) or HTTPS (443) port is allowed in the firewall.

telnet {IP_address} 80
telnet {IP_address} 443

Check that the reverse proxy server listening on port 80 or 443 is running. You can find out what process is using 80 or 443 with the following command line.

sudo lsof -i -P -n | grep LISTEN

Check if the configuration of the reverse proxy server is correct.

Check if the actual web project is up and running. You can view all processes with the following command line.

ps -ef

Check that the cloud host operating system has sufficient resources such as CPU, memory, disk, and bandwidth. You can view the resource usage of the cloud host in the cloud platform console or view usage by command lines.

功能介绍

阿里云视频点播(VoD)提供了很多视频相关的功能。主要流程是上传视频,处理视频和播放视频。具体介绍可以参考视频点播产品简介。下图为视频点播的架构图。

视频点播架构图

准备工作

本节目标:开通服务,以及获取可以调用视频点播 SDK 和 API 的 accessKeyId 和 accessKeySecret。

开通视频点播服务

访问视频点播控制台页面,点击开通服务即可。第一次进入会提示开通服务,之后会直接进入点播控制台。

启用存储视频的Bucket

视频点播服务的视频存储可以使用点播系统Bucket,也可以使用OSS自有Bucket。

使用点播系统Bucket需要手动启用,操作如下:

视频点播控制台-> 配置管理 -> 存储管理 -> 顶部栏,选择地区:华东2(上海)-> 待分配 点播系统bucket:启用

点播系统Bucket上的视频使用视频点播相关功能会更方便。但如果你之前使用了阿里云的OSS,想要对你OSS上的视频使用视频点播相关功能也是可以的,就是会麻烦一点,需要多调用一个注册媒资信息接口。

这两种 bucket 有很多不同之处。详情可以查看视频点播-开发指南-存储管理视频点播-配置管理-存储管理

创建用户和授权

阿里云控制台 -> 右上角账号名称 -> 访问控制 -> 用户 -> 点击创建用户 -> 输入登录名称和显示名称,勾选 OpenAPI 调用访问 -> 点击确定 -> 保存用户的 accessKeyId 和 accessKeySecret

创建用户成功后,进入用户详情页面,为用户授予权限。

权限管理 -> 新增授权 -> 添加 “AliyunVODFullAccess 管理视频点播服务(VOD)的权限”

开发环境

本节目标:引入视频点播相关的 Java SDK。

Maven依赖

  • aliyun-java-sdk-core为核心库
  • aliyun-java-sdk-vod为VOD库
  • aliyun-java-sdk-kms为密钥管理服务(KMS)的所需依赖,若不涉及则无需添加。
  • com.aliyun.vod:upload 视频点播上传SDK没有开源,需要手动下载jar包引入到项目中。详情参考 SDK简介与下载-上传SDK
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.6.0</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-vod</artifactId>
<version>2.16.10</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-kms</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>com.aliyun.vod</groupId>
<artifactId>upload</artifactId>
<version>1.4.15</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/aliyun-java-vod-upload-1.4.15.jar</systemPath>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20230227</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.25</version>
</dependency>

调用接口

本节目标:了解接口调用流程和基本使用。

SDK的使用流程如下:

上传视频的代码示例

private static String uploadVideoToVOD(String fileName, InputStream inputStream) {
UploadStreamRequest request = new UploadStreamRequest(
accessKeyId, accessKeySecret, fileName, fileName, inputStream);
request.setAccessKeyId(accessKeyId);
request.setAccessKeySecret(accessKeySecret);
UploadVideoImpl uploader = new UploadVideoImpl();
UploadStreamResponse response = uploader.uploadStream(request);
System.out.print("RequestId=" + response.getRequestId() + "\n"); //请求视频点播服务的请求ID
String videoId = response.getVideoId();
if (response.isSuccess()) {
System.out.print("VideoId=" + response.getVideoId() + "\n");
} else { //如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因
System.out.print("VideoId=" + response.getVideoId() + "\n");
System.out.print("ErrorCode=" + response.getCode() + "\n");
System.out.print("ErrorMessage=" + response.getMessage() + "\n");
}
return videoId;
}

/**
* 获取上传结果
*
* 用于检查是否上传成功,视频上传成功后才能进行视频处理
*
* @param videoId
* @return
* @throws ClientException
*/
private static GetUploadDetailsResponse.UploadDetail getUploadDetails(String videoId) throws ClientException {
GetUploadDetailsRequest request = new GetUploadDetailsRequest();
request.setMediaIds(videoId);
GetUploadDetailsResponse acsResponse = initVodClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET).getAcsResponse(request);
return acsResponse.getUploadDetails().get(0);
}

创建 DefaultAcsClient,请求视频AI处理的代码示例

private static String submitAiJob(String videoId) throws ClientException {
SubmitAIJobRequest request = new SubmitAIJobRequest();
request.setMediaId(videoId);
request.setTypes("AIVideoTag");
// Face,ASR,OCR,Category,Annotation
request.setConfig("{\"AIVideoTag\": {\"AnalyseTypes\": \"ASR,OCR,Annotation\"} }");
SubmitAIJobResponse acsResponse = initVodClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET).getAcsResponse(request);
String jobId = acsResponse.getAIJobList().get(0).getJobId();
System.out.println("jobId: " + jobId);
return jobId;
}

private static ListAIJobResponse.AIJob listAiJob(String jobId) throws ClientException {
ListAIJobRequest listAIJobRequest = new ListAIJobRequest();
listAIJobRequest.setJobIds(jobId);
ListAIJobResponse acsResponse = initVodClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET).getAcsResponse(listAIJobRequest);
ListAIJobResponse.AIJob aiJob = acsResponse.getAIJobList().get(0);
return aiJob;
}

public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) {
String regionId = "cn-shanghai"; // 点播服务接入地域
DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}

视频点播的服务端 Java SDK 的使用

  • 创建一个对应视频处理的 Request 对象。SDK 的 request 对象和 API 上写的的基本一致,API 名称加个 Request 后缀即可。API 上有详情的参数说明,可结合使用。
  • 创建一个 VodClient 对象。
  • 获取对应的视频处理 Response 对象。

其他

查看费用

阿里云控制台 -> 顶部栏 “费用” -> 账单详情 -> 明细账单。

账单明细会有延迟,不能看到实时的消费明细。

明细账单数据相对于实际费用消耗延迟24小时更新,其中实例ID相关的信息( 实例配置,实例规格,实例昵称,资源组,公网IP,私网IP,可用区)延迟48小时更新。

关闭服务

视频点播服务没有统一的关闭入口。但可以在视频点播控制台中删除所有付费项。比如:删除掉所有域名,清空所有存储,取消转码和调用。

0%